Преглед изворни кода

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
DrZlo13 пре 5 година
родитељ
комит
9ed8bebba1

+ 2 - 2
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;
 

+ 2 - 1
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);
+void api_interrupt_remove(InterruptCallback callback);
+void api_interrupt_call(InterruptType type, void* hw);

+ 3 - 8
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;

+ 8 - 8
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 */

+ 1 - 1
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;

+ 2 - 2
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;
 }

+ 68 - 0
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,
+    }};

+ 18 - 0
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

+ 37 - 0
firmware/targets/f4/api-hal/api-hal-spi.c

@@ -1,16 +1,44 @@
 #include "api-hal-spi.h"
+#include <cmsis_os.h>
+#include <stdbool.h>
+#include <string.h>
 
 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);
 }

+ 31 - 2
firmware/targets/f4/api-hal/api-hal-spi.h

@@ -1,7 +1,36 @@
 #pragma once
 #include "main.h"
-#include <cmsis_os.h>
+#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); 
+
+/**
+ * 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