diff --git a/.clang-tidy b/.clang-tidy new file mode 100644 index 0000000..07e9f8c --- /dev/null +++ b/.clang-tidy @@ -0,0 +1,42 @@ +Checks: > + -*, + bugprone-*, + performance-*, + portability-*, + readability-*, + modernize-*, + cppcoreguidelines-*, + clang-analyzer-*, + cert-* + +WarningsAsErrors: '*' +HeaderFilterRegex: '.*' +FormatStyle: 'file' + +CheckOptions: + - key: modernize-use-auto + value: '0' + - key: modernize-use-using + value: '0' + - key: modernize-avoid-bind + value: '0' + - key: modernize-use-nullptr + value: '0' + - key: modernize-use-override + value: '0' + - key: cppcoreguidelines-avoid-magic-numbers + value: '0' + - key: cppcoreguidelines-pro-bounds-array-to-pointer-decay + value: '0' + - key: cppcoreguidelines-pro-type-vararg + value: '0' + - key: readability-magic-numbers + value: '1' + - key: bugprone-exception-escape + value: '0' + - key: cert-msc30-c + value: '0' + - key: cert-msc50-cpp + value: '0' + - key: readability-function-cognitive-complexity-threshold + value: '15' \ No newline at end of file diff --git a/csrc/board/inc/board.h b/csrc/board/inc/board.h index e4bb3fe..91dbc47 100644 --- a/csrc/board/inc/board.h +++ b/csrc/board/inc/board.h @@ -365,10 +365,10 @@ extern sl_led_t led_orange; extern sl_led_t led_yellow; /* SPI DRV Handles */ -extern SPIDRV_HandleData_t sd_card_usart; -extern SPIDRV_HandleData_t cc3100_usart; +//extern SPIDRV_HandleData_t sd_card_usart; +//extern SPIDRV_HandleData_t cc3100_usart; -extern UARTDRV_HandleData_t em9301_uart; +//extern UARTDRV_HandleData_t em9301_uart; extern uint8_t usb_rx_buf[64]; extern uint8_t usb_tx_buf[64]; diff --git a/csrc/board/src/board_CC3100.c b/csrc/board/src/board_CC3100.c index cb65100..711cd2a 100644 --- a/csrc/board/src/board_CC3100.c +++ b/csrc/board/src/board_CC3100.c @@ -19,7 +19,7 @@ static SemaphoreHandle_t rx_queue = NULL; #define CC3100_SPI_RX_TIMEOUT_MS (1000) #define CC3100_SPI_TX_TIMEOUT_MS (1000) -SPIDRV_HandleData_t cc3100_usart; +static SPIDRV_HandleData_t cc3100_usart; const SPIDRV_Init_t cc3100_usart_init_data = { .port = WIFI_SERIAL_PORT, .portLocation = _USART_ROUTE_LOCATION_LOC0, diff --git a/csrc/board/src/board_sd_card.c b/csrc/board/src/board_sd_card.c index 3cf5e59..5b9c75e 100644 --- a/csrc/board/src/board_sd_card.c +++ b/csrc/board/src/board_sd_card.c @@ -15,7 +15,7 @@ #include "semphr.h" /* SPI DRV Handle */ -SPIDRV_HandleData_t sd_card_usart; +static SPIDRV_HandleData_t sd_card_usart; /* Detect SD Card */ static void card_detect_callback(uint8_t intNo); @@ -84,6 +84,22 @@ void BOARD_SD_Card_Enable(void) true); (void)BOARD_SD_CARD_IsInserted(); + + /* SPI DRV Init */ + SPIDRV_Init_t sd_card_init_data = { + .port = BOARD_SD_CARD_USART, + .portLocation = _USART_ROUTE_LOCATION_LOC1, + .bitRate = BOARD_SD_CARD_WAKEUP_BITRATE, + .frameLength = 8, + .dummyTxValue = 0xFF, + .type = spidrvMaster, + .bitOrder = spidrvBitOrderMsbFirst, + .clockMode = spidrvClockMode0, + .csControl = spidrvCsControlApplication, + .slaveStartMode = spidrvSlaveStartImmediate, + }; + + (void)SPIDRV_Init(&sd_card_usart, &sd_card_init_data); } void BOARD_SD_Card_Disable(void) @@ -130,7 +146,7 @@ void BOARD_SD_CARD_SetSlowBaudrate(void) /* recieve callback for async reception */ -static void _recieve_callback(struct SPIDRV_HandleData *handle, +static void recieve_callback(struct SPIDRV_HandleData *handle, Ecode_t transferStatus, int itemsTransferred) { @@ -145,7 +161,7 @@ static void _recieve_callback(struct SPIDRV_HandleData *handle, } /* tx callback for async tx */ -static void _send_callback(struct SPIDRV_HandleData *handle, +static void send_callback(struct SPIDRV_HandleData *handle, Ecode_t transferStatus, int itemsTransferred) { @@ -165,7 +181,7 @@ uint32_t BOARD_SD_CARD_Recieve(void * buffer, int count) Ecode_t ecode = ECODE_EMDRV_SPIDRV_PARAM_ERROR; if(rx_semaphore != NULL) // Change to check if scheduler is active { - ecode = SPIDRV_MReceive(&sd_card_usart, buffer, count, _recieve_callback); + ecode = SPIDRV_MReceive(&sd_card_usart, buffer, count, recieve_callback); if(ECODE_EMDRV_SPIDRV_OK == ecode) { (void)xQueueReceive( rx_semaphore, &ecode, portMAX_DELAY); @@ -186,7 +202,7 @@ uint32_t BOARD_SD_CARD_Send(const void * buffer, int count) Ecode_t ecode = ECODE_EMDRV_SPIDRV_PARAM_ERROR; if(tx_semaphore != NULL) // Change to check if scheduler is active { - ecode = SPIDRV_MTransmit(&sd_card_usart, buffer, count, _send_callback); + ecode = SPIDRV_MTransmit(&sd_card_usart, buffer, count, send_callback); if(ECODE_EMDRV_SPIDRV_OK == ecode) { (void)xQueueReceive( tx_semaphore, &ecode, portMAX_DELAY); diff --git a/csrc/board/src/sdmm.c b/csrc/board/src/sdmm.c index 6bcff94..c56518a 100644 --- a/csrc/board/src/sdmm.c +++ b/csrc/board/src/sdmm.c @@ -13,7 +13,7 @@ MODIFICATIONS: - UISO, 2022-2023 + MISO, 2022-2024 Modifications to add simplified logic to interface with the board @@ -319,22 +319,6 @@ DSTATUS disk_initialize ( BOARD_SD_Card_Enable(); - /* SPI DRV Init */ - SPIDRV_Init_t sd_card_init_data = { - .port = BOARD_SD_CARD_USART, - .portLocation = _USART_ROUTE_LOCATION_LOC1, - .bitRate = BOARD_SD_CARD_WAKEUP_BITRATE, - .frameLength = 8, - .dummyTxValue = 0xFF, - .type = spidrvMaster, - .bitOrder = spidrvBitOrderMsbFirst, - .clockMode = spidrvClockMode0, - .csControl = spidrvCsControlApplication, - .slaveStartMode = spidrvSlaveStartImmediate, - }; - - SPIDRV_Init(&sd_card_usart, &sd_card_init_data); - rcvr_mmc(buf, 10); ty = 0; diff --git a/src/spi.zig b/src/spi.zig new file mode 100644 index 0000000..2337365 --- /dev/null +++ b/src/spi.zig @@ -0,0 +1,84 @@ +//! SPI object + +const c = @cImport({ + @cInclude("board.h"); + @cInclude("miso.h"); + @cInclude("miso_config.h"); +}); + +const freertos = @import("freertos.zig"); + +const ecode = c.Ecode_t; +//const spiDrvHandle +const txferElement = struct { + status: ecode, // status + items: isize, // number of items transfered +}; + +pub fn Spi() type { + return struct { + spidrvHandle: c.SPIDRV_HandleData_t, + + txQueue: freertos.StaticQueue(txferElement, 1), + rxQueue: freertos.StaticQueue(txferElement, 1), + //spiDrvHandle: c.SPIDRV_HandleData_t, + + fn recieve_callback(handle: c.SPIDRV_HandleData_t, buffer: []u8, n: usize) callconv(.C) void { + var xHigherPriorityTaskWoken: freertos.BaseType_t = freertos.pdFALSE; + const transfer_status_information = txferElement{ .status = c.ECODE_EMDRV_SPIDRV_OK, .items = n }; + + // self.xQueueSendFromISR(&transfer_status_information, &xHigherPriorityTaskWoken); + + freertos.portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + fn send_callback(handle: c.SPIDRV_HandleData_t, buffer: []u8, n: usize) callconv(.C) void { + var xHigherPriorityTaskWoken: freertos.BaseType_t = freertos.pdFALSE; + const transfer_status_information = txferElement{ .status = c.ECODE_EMDRV_SPIDRV_OK, .items = n }; + + // self.xQueueSendFromISR(&transfer_status_information, &xHigherPriorityTaskWoken); + + freertos.portYIELD_FROM_ISR(xHigherPriorityTaskWoken); + } + + pub fn init(self: *@This(), spiDrvHandle: c.SPIDRV_HandleData_t) void { + //self.spidrvHandle = spiDrvHandle; + //self.txQueue = freertos.StaticQueue(txferElement, 1); + //self.rxQueue = freertos.StaticQueue(txferElement, 1); + } + pub fn deinit(self: *@This()) void { + //self.txQueue.deinit(); + //self.rxQueue.deinit(); + } + + pub fn send(self: *@This(), data: []const u8, timeout_ticks: ?u32) !usize { + var itemsTransfered: usize = 0; + + const spiEcode = c.SPIDRV_MTransmit(&self.spiDrvHandle, @ptrCast(data.ptr), @intCast(data.len), send_callback); + if (spiEcode == c.ECODE_EMDRV_SPIDRV_OK) { + if (self.txQueue.receive(timeout_ticks)) |val| { + if (val.status == c.ECODE_EMDRV_SPIDRV_OK) { + itemsTransfered = val.items; + } + } else |err| { + return err; + } + } + return itemsTransfered; + } + pub fn receive(self: *@This(), data: []u8, timeout_ticks: ?u32) ![]u8 { + var itemsTransfered: usize = 0; + + const spiEcode = c.SPIDRV_MReceive(&self.spiDrvHandle, @ptrCast(data.ptr), @intCast(data.len), recieve_callback); + if (spiEcode == c.ECODE_EMDRV_SPIDRV_OK) { + if (self.rxQueue.receive(timeout_ticks)) |val| { + if (val.status == c.ECODE_EMDRV_SPIDRV_OK) { + itemsTransfered = val.items; + } + } else |err| { + return err; + } + } + return data[0..itemsTransfered]; + } + }; +}