From 9ed8bebba1b52dd3ac4c0c1cf97ce54f07c495c5 Mon Sep 17 00:00:00 2001 From: DrZlo13 Date: Wed, 13 Jan 2021 03:33:36 +1000 Subject: [PATCH] SPI Device API (#304) * spi device config * fix api_interrupt_call warning * added configs for spi2 bus devices * simplified spi bus api * use new spi device api --- applications/gui/u8g2_periphery.c | 4 +- core/api-hal/api-interrupt-mgr.h | 3 +- .../targets/f4/Src/fatfs/stm32_adafruit_sd.c | 11 +-- firmware/targets/f4/Src/fatfs/user_diskio.c | 16 ++--- firmware/targets/f4/Src/spi.c | 2 +- firmware/targets/f4/api-hal/api-hal-gpio.c | 4 +- .../targets/f4/api-hal/api-hal-spi-config.c | 68 +++++++++++++++++++ .../targets/f4/api-hal/api-hal-spi-config.h | 18 +++++ firmware/targets/f4/api-hal/api-hal-spi.c | 37 ++++++++++ firmware/targets/f4/api-hal/api-hal-spi.h | 33 ++++++++- 10 files changed, 172 insertions(+), 24 deletions(-) create mode 100644 firmware/targets/f4/api-hal/api-hal-spi-config.c create mode 100644 firmware/targets/f4/api-hal/api-hal-spi-config.h diff --git a/applications/gui/u8g2_periphery.c b/applications/gui/u8g2_periphery.c index 9ef488ea..df6c9b2c 100644 --- a/applications/gui/u8g2_periphery.c +++ b/applications/gui/u8g2_periphery.c @@ -89,7 +89,7 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ #endif // TODO: SPI manager - api_hal_spi_lock(&SPI_D); + api_hal_spi_lock_device(&display_spi); // TODO change it to FuriRecord pin HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET); @@ -106,7 +106,7 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_ HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET); // TODO: SPI manager - api_hal_spi_unlock(&SPI_D); + api_hal_spi_unlock_device(&display_spi); break; diff --git a/core/api-hal/api-interrupt-mgr.h b/core/api-hal/api-interrupt-mgr.h index a7f8f98f..02a75e4e 100644 --- a/core/api-hal/api-interrupt-mgr.h +++ b/core/api-hal/api-interrupt-mgr.h @@ -17,4 +17,5 @@ typedef struct { bool api_interrupt_init(); void api_interrupt_add(InterruptCallback callback, InterruptType type, void* context); -void api_interrupt_remove(InterruptCallback callback); \ No newline at end of file +void api_interrupt_remove(InterruptCallback callback); +void api_interrupt_call(InterruptType type, void* hw); \ No newline at end of file diff --git a/firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c b/firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c index 9e55a39f..fbd5b3d6 100644 --- a/firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c +++ b/firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c @@ -291,11 +291,9 @@ static uint8_t SD_ReadData(void); * - MSD_OK: Sequence succeed */ uint8_t BSP_SD_Init(void) { - // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); - /* Init to maximum slow speed */ - SD_SPI_Reconfigure_Slow(); + // TODO: SPI manager + api_hal_spi_lock_device(&sd_slow_spi); /* Configure IO functionalities for SD pin */ SD_IO_Init(); @@ -304,11 +302,8 @@ uint8_t BSP_SD_Init(void) { SdStatus = SD_PRESENT; uint8_t res = SD_GoIdleState(); - /* Init to maximum fastest speed */ - SD_SPI_Reconfigure_Fast(); - // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock_device(&sd_slow_spi); /* SD initialized and set to SPI mode properly */ return res; diff --git a/firmware/targets/f4/Src/fatfs/user_diskio.c b/firmware/targets/f4/Src/fatfs/user_diskio.c index 01a34d91..7fb51e06 100644 --- a/firmware/targets/f4/Src/fatfs/user_diskio.c +++ b/firmware/targets/f4/Src/fatfs/user_diskio.c @@ -87,12 +87,12 @@ Diskio_drvTypeDef USER_Driver = { DSTATUS USER_initialize(BYTE pdrv) { /* USER CODE BEGIN INIT */ // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); + api_hal_spi_lock_device(&sd_fast_spi); DSTATUS status = User_CheckStatus(pdrv); // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock_device(&sd_fast_spi); return status; /* USER CODE END INIT */ @@ -122,7 +122,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) { DRESULT res = RES_ERROR; // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); + api_hal_spi_lock_device(&sd_fast_spi); if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) { /* wait until the read operation is finished */ @@ -132,7 +132,7 @@ DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) { } // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock_device(&sd_fast_spi); return res; /* USER CODE END READ */ @@ -153,7 +153,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) { DRESULT res = RES_ERROR; // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); + api_hal_spi_lock_device(&sd_fast_spi); if(BSP_SD_WriteBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) { /* wait until the Write operation is finished */ @@ -163,7 +163,7 @@ DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) { } // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock_device(&sd_fast_spi); return res; /* USER CODE END WRITE */ @@ -186,7 +186,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) { if(Stat & STA_NOINIT) return RES_NOTRDY; // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); + api_hal_spi_lock_device(&sd_fast_spi); switch(cmd) { /* Make sure that no pending write process */ @@ -220,7 +220,7 @@ DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) { } // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock_device(&sd_fast_spi); return res; /* USER CODE END IOCTL */ diff --git a/firmware/targets/f4/Src/spi.c b/firmware/targets/f4/Src/spi.c index 41373dee..fa8f5ef7 100644 --- a/firmware/targets/f4/Src/spi.c +++ b/firmware/targets/f4/Src/spi.c @@ -290,7 +290,7 @@ void SD_SPI_Reconfigure_Fast(void) { SPI_SD_HANDLE.Init.CLKPolarity = SPI_POLARITY_LOW; SPI_SD_HANDLE.Init.CLKPhase = SPI_PHASE_1EDGE; SPI_SD_HANDLE.Init.NSS = SPI_NSS_SOFT; - SPI_SD_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64; + SPI_SD_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2; SPI_SD_HANDLE.Init.FirstBit = SPI_FIRSTBIT_MSB; SPI_SD_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE; SPI_SD_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE; diff --git a/firmware/targets/f4/api-hal/api-hal-gpio.c b/firmware/targets/f4/api-hal/api-hal-gpio.c index 1827a88a..4be09fbc 100644 --- a/firmware/targets/f4/api-hal/api-hal-gpio.c +++ b/firmware/targets/f4/api-hal/api-hal-gpio.c @@ -26,7 +26,7 @@ bool hal_gpio_read_sd_detect(void) { const GpioPin* sd_cs_record = &sd_cs_gpio; // TODO: SPI manager - api_hal_spi_lock(&SPI_SD_HANDLE); + api_hal_spi_lock(sd_fast_spi.spi); // configure pin as input gpio_init_ex(sd_cs_record, GpioModeInput, GpioPullUp, GpioSpeedVeryHigh); @@ -41,7 +41,7 @@ bool hal_gpio_read_sd_detect(void) { delay(1); // TODO: SPI manager - api_hal_spi_unlock(&SPI_SD_HANDLE); + api_hal_spi_unlock(sd_fast_spi.spi); return result; } diff --git a/firmware/targets/f4/api-hal/api-hal-spi-config.c b/firmware/targets/f4/api-hal/api-hal-spi-config.c new file mode 100644 index 00000000..792df528 --- /dev/null +++ b/firmware/targets/f4/api-hal/api-hal-spi-config.c @@ -0,0 +1,68 @@ +#include "main.h" +#include "api-hal-spi-config.h" + +extern SPI_HandleTypeDef SPI_R; +extern SPI_HandleTypeDef SPI_D; + +/** + * SD Card in fast mode (after init) + */ +const SPIDevice sd_fast_spi = { + .spi = &SPI_D, + .config = { + .Mode = SPI_MODE_MASTER, + .Direction = SPI_DIRECTION_2LINES, + .DataSize = SPI_DATASIZE_8BIT, + .CLKPolarity = SPI_POLARITY_LOW, + .CLKPhase = SPI_PHASE_1EDGE, + .NSS = SPI_NSS_SOFT, + .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2, + .FirstBit = SPI_FIRSTBIT_MSB, + .TIMode = SPI_TIMODE_DISABLE, + .CRCCalculation = SPI_CRCCALCULATION_DISABLE, + .CRCPolynomial = 7, + .CRCLength = SPI_CRC_LENGTH_DATASIZE, + .NSSPMode = SPI_NSS_PULSE_ENABLE, + }}; + +/** + * SD Card in slow mode (before init) + */ +const SPIDevice sd_slow_spi = { + .spi = &SPI_D, + .config = { + .Mode = SPI_MODE_MASTER, + .Direction = SPI_DIRECTION_2LINES, + .DataSize = SPI_DATASIZE_8BIT, + .CLKPolarity = SPI_POLARITY_LOW, + .CLKPhase = SPI_PHASE_1EDGE, + .NSS = SPI_NSS_SOFT, + .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256, + .FirstBit = SPI_FIRSTBIT_MSB, + .TIMode = SPI_TIMODE_DISABLE, + .CRCCalculation = SPI_CRCCALCULATION_DISABLE, + .CRCPolynomial = 7, + .CRCLength = SPI_CRC_LENGTH_DATASIZE, + .NSSPMode = SPI_NSS_PULSE_ENABLE, + }}; + +/** + * Display + */ +const SPIDevice display_spi = { + .spi = &SPI_D, + .config = { + .Mode = SPI_MODE_MASTER, + .Direction = SPI_DIRECTION_2LINES, + .DataSize = SPI_DATASIZE_8BIT, + .CLKPolarity = SPI_POLARITY_LOW, + .CLKPhase = SPI_PHASE_1EDGE, + .NSS = SPI_NSS_SOFT, + .BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16, + .FirstBit = SPI_FIRSTBIT_MSB, + .TIMode = SPI_TIMODE_DISABLE, + .CRCCalculation = SPI_CRCCALCULATION_DISABLE, + .CRCPolynomial = 7, + .CRCLength = SPI_CRC_LENGTH_DATASIZE, + .NSSPMode = SPI_NSS_PULSE_ENABLE, + }}; diff --git a/firmware/targets/f4/api-hal/api-hal-spi-config.h b/firmware/targets/f4/api-hal/api-hal-spi-config.h new file mode 100644 index 00000000..01e9be9a --- /dev/null +++ b/firmware/targets/f4/api-hal/api-hal-spi-config.h @@ -0,0 +1,18 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + SPI_HandleTypeDef* spi; + const SPI_InitTypeDef config; +} SPIDevice; + +extern const SPIDevice sd_fast_spi; +extern const SPIDevice sd_slow_spi; +extern const SPIDevice display_spi; + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/firmware/targets/f4/api-hal/api-hal-spi.c b/firmware/targets/f4/api-hal/api-hal-spi.c index 04fbf8a3..23aacc8f 100644 --- a/firmware/targets/f4/api-hal/api-hal-spi.c +++ b/firmware/targets/f4/api-hal/api-hal-spi.c @@ -1,16 +1,44 @@ #include "api-hal-spi.h" +#include +#include +#include osMutexId_t spi_mutex_r; osMutexId_t spi_mutex_d; extern SPI_HandleTypeDef SPI_R; extern SPI_HandleTypeDef SPI_D; +extern void Enable_SPI(SPI_HandleTypeDef* spi); void api_hal_spi_init() { spi_mutex_r = osMutexNew(NULL); spi_mutex_d = osMutexNew(NULL); } +void api_hal_spi_apply_config(const SPIDevice* device) { + osKernelLock(); + + memcpy(&device->spi->Init, &device->config, sizeof(SPI_InitTypeDef)); + + if(HAL_SPI_Init(device->spi) != HAL_OK) { + Error_Handler(); + } + + Enable_SPI(device->spi); + + osKernelUnlock(); +} + +bool api_hal_spi_config_are_actual(const SPIDevice* device) { + return (memcmp(&device->config, &device->spi->Init, sizeof(SPI_InitTypeDef)) == 0); +} + +void api_hal_spi_config_device(const SPIDevice* device) { + if(!api_hal_spi_config_are_actual(device)) { + api_hal_spi_apply_config(device); + } +} + void api_hal_spi_lock(SPI_HandleTypeDef* spi) { if(spi == &SPI_D) { osMutexAcquire(spi_mutex_d, osWaitForever); @@ -29,4 +57,13 @@ void api_hal_spi_unlock(SPI_HandleTypeDef* spi) { } else { Error_Handler(); } +} + +void api_hal_spi_lock_device(const SPIDevice* device) { + api_hal_spi_lock(device->spi); + api_hal_spi_config_device(device); +} + +void api_hal_spi_unlock_device(const SPIDevice* device) { + api_hal_spi_unlock(device->spi); } \ No newline at end of file diff --git a/firmware/targets/f4/api-hal/api-hal-spi.h b/firmware/targets/f4/api-hal/api-hal-spi.h index 02e0378c..5c437feb 100644 --- a/firmware/targets/f4/api-hal/api-hal-spi.h +++ b/firmware/targets/f4/api-hal/api-hal-spi.h @@ -1,7 +1,36 @@ #pragma once #include "main.h" -#include +#include "api-hal-spi-config.h" +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Init SPI API + */ void api_hal_spi_init(); + +/** + * Lock SPI bus + */ void api_hal_spi_lock(SPI_HandleTypeDef* spi); -void api_hal_spi_unlock(SPI_HandleTypeDef* spi); \ No newline at end of file + +/** + * Unlock SPI bus + */ +void api_hal_spi_unlock(SPI_HandleTypeDef* spi); + +/** + * Lock SPI device bus and apply config if needed + */ +void api_hal_spi_lock_device(const SPIDevice* device); + +/** + * Unlock SPI device bus + */ +void api_hal_spi_unlock_device(const SPIDevice* device); + +#ifdef __cplusplus +} +#endif \ No newline at end of file