Przeglądaj źródła

FL-443 Move sd card to second spi (#271)

* sd card spi 2 fix, add spi mgr
DrZlo13 5 lat temu
rodzic
commit
8b6e7fd4ae

+ 7 - 0
applications/gui/u8g2_periphery.c

@@ -88,6 +88,9 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_
         fuprintf(log, "[u8g2] start\n");
 #endif
 
+        // TODO: SPI manager
+        api_hal_spi_lock(&SPI_D);
+
         // TODO change it to FuriRecord pin
         HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_RESET);
         asm("nop");
@@ -101,6 +104,10 @@ uint8_t u8x8_hw_spi_stm32(u8x8_t* u8x8, uint8_t msg, uint8_t arg_int, void* arg_
         asm("nop");
         // TODO change it to FuriRecord pin
         HAL_GPIO_WritePin(DISPLAY_CS_GPIO_Port, DISPLAY_CS_Pin, GPIO_PIN_SET);
+
+        // TODO: SPI manager
+        api_hal_spi_unlock(&SPI_D);
+
         break;
 
     default:

+ 1 - 0
firmware/targets/api-hal-include/api-hal.h

@@ -14,4 +14,5 @@ template <unsigned int N> struct STOP_EXTERNING_ME {};
 #include "api-hal-vcp.h"
 #include "api-hal-uid.h"
 #include "api-hal-bt.h"
+#include "api-hal-spi.h"
 #include "api-hal-flash.h"

+ 1 - 1
firmware/targets/f4/Inc/main.h

@@ -155,7 +155,7 @@ void Error_Handler(void);
 
 #define SPI_R hspi1
 #define SPI_D hspi2
-#define SPI_SD_HANDLE SPI_R
+#define SPI_SD_HANDLE SPI_D
 
 extern TIM_HandleTypeDef htim1;
 extern TIM_HandleTypeDef htim2;

+ 9 - 2
firmware/targets/f4/Src/fatfs/stm32_adafruit_sd.c

@@ -92,6 +92,7 @@
 #include "string.h"
 #include "stdio.h"
 #include "spi.h"
+#include "api-hal-spi.h"
 
 /** @addtogroup BSP
   * @{
@@ -290,6 +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();
 
@@ -303,6 +307,9 @@ uint8_t BSP_SD_Init(void) {
     /* Init to maximum fastest speed */
     SD_SPI_Reconfigure_Fast();
 
+    // TODO: SPI manager
+    api_hal_spi_unlock(&SPI_SD_HANDLE);
+
     /* SD initialized and set to SPI mode properly */
     return res;
 }
@@ -323,8 +330,8 @@ uint8_t BSP_SD_GetCardInfo(SD_CardInfo* pCardInfo) {
     if(flag_SDHC == 1) {
         pCardInfo->LogBlockSize = 512;
         pCardInfo->CardBlockSize = 512;
-        pCardInfo->CardCapacity =
-            ((uint64_t)pCardInfo->Csd.version.v2.DeviceSize + 1UL) * 1024UL * (uint64_t)pCardInfo->LogBlockSize;
+        pCardInfo->CardCapacity = ((uint64_t)pCardInfo->Csd.version.v2.DeviceSize + 1UL) * 1024UL *
+                                  (uint64_t)pCardInfo->LogBlockSize;
         pCardInfo->LogBlockNbr = (pCardInfo->CardCapacity) / (pCardInfo->LogBlockSize);
     } else {
         pCardInfo->CardCapacity = (pCardInfo->Csd.version.v1.DeviceSize + 1);

+ 35 - 24
firmware/targets/f4/Src/fatfs/user_diskio.c

@@ -35,7 +35,8 @@
 
 /* Includes ------------------------------------------------------------------*/
 #include "user_diskio.h"
-
+#include "spi.h"
+#include "api-hal-spi.h"
 /* Private typedef -----------------------------------------------------------*/
 /* Private define ------------------------------------------------------------*/
 
@@ -45,7 +46,6 @@ static volatile DSTATUS Stat = STA_NOINIT;
 
 static DSTATUS User_CheckStatus(BYTE lun) {
     Stat = STA_NOINIT;
-
     if(BSP_SD_GetCardState() == MSD_OK) {
         Stat &= ~STA_NOINIT;
     }
@@ -84,10 +84,17 @@ Diskio_drvTypeDef USER_Driver = {
   * @param  pdrv: Physical drive number (0..)
   * @retval DSTATUS: Operation status
   */
-DSTATUS USER_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive */
-) {
+DSTATUS USER_initialize(BYTE pdrv) {
     /* USER CODE BEGIN INIT */
-    return User_CheckStatus(pdrv);
+    // TODO: SPI manager
+    api_hal_spi_lock(&SPI_SD_HANDLE);
+
+    DSTATUS status = User_CheckStatus(pdrv);
+
+    // TODO: SPI manager
+    api_hal_spi_unlock(&SPI_SD_HANDLE);
+
+    return status;
     /* USER CODE END INIT */
 }
 
@@ -96,8 +103,7 @@ DSTATUS USER_initialize(BYTE pdrv /* Physical drive nmuber to identify the drive
   * @param  pdrv: Physical drive number (0..)
   * @retval DSTATUS: Operation status
   */
-DSTATUS USER_status(BYTE pdrv /* Physical drive number to identify the drive */
-) {
+DSTATUS USER_status(BYTE pdrv) {
     /* USER CODE BEGIN STATUS */
     return Stat;
     /* USER CODE END STATUS */
@@ -111,14 +117,13 @@ DSTATUS USER_status(BYTE pdrv /* Physical drive number to identify the drive */
   * @param  count: Number of sectors to read (1..128)
   * @retval DRESULT: Operation result
   */
-DRESULT USER_read(
-    BYTE pdrv, /* Physical drive nmuber to identify the drive */
-    BYTE* buff, /* Data buffer to store read data */
-    DWORD sector, /* Sector address in LBA */
-    UINT count /* Number of sectors to read */
-) {
+DRESULT USER_read(BYTE pdrv, BYTE* buff, DWORD sector, UINT count) {
     /* USER CODE BEGIN READ */
     DRESULT res = RES_ERROR;
+
+    // TODO: SPI manager
+    api_hal_spi_lock(&SPI_SD_HANDLE);
+
     if(BSP_SD_ReadBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
         /* wait until the read operation is finished */
         while(BSP_SD_GetCardState() != MSD_OK) {
@@ -126,6 +131,9 @@ DRESULT USER_read(
         res = RES_OK;
     }
 
+    // TODO: SPI manager
+    api_hal_spi_unlock(&SPI_SD_HANDLE);
+
     return res;
     /* USER CODE END READ */
 }
@@ -139,16 +147,14 @@ DRESULT USER_read(
   * @retval DRESULT: Operation result
   */
 #if _USE_WRITE == 1
-DRESULT USER_write(
-    BYTE pdrv, /* Physical drive nmuber to identify the drive */
-    const BYTE* buff, /* Data to be written */
-    DWORD sector, /* Sector address in LBA */
-    UINT count /* Number of sectors to write */
-) {
+DRESULT USER_write(BYTE pdrv, const BYTE* buff, DWORD sector, UINT count) {
     /* USER CODE BEGIN WRITE */
     /* USER CODE HERE */
     DRESULT res = RES_ERROR;
 
+    // TODO: SPI manager
+    api_hal_spi_lock(&SPI_SD_HANDLE);
+
     if(BSP_SD_WriteBlocks((uint32_t*)buff, (uint32_t)(sector), count, SD_DATATIMEOUT) == MSD_OK) {
         /* wait until the Write operation is finished */
         while(BSP_SD_GetCardState() != MSD_OK) {
@@ -156,6 +162,9 @@ DRESULT USER_write(
         res = RES_OK;
     }
 
+    // TODO: SPI manager
+    api_hal_spi_unlock(&SPI_SD_HANDLE);
+
     return res;
     /* USER CODE END WRITE */
 }
@@ -169,17 +178,16 @@ DRESULT USER_write(
   * @retval DRESULT: Operation result
   */
 #if _USE_IOCTL == 1
-DRESULT USER_ioctl(
-    BYTE pdrv, /* Physical drive nmuber (0..) */
-    BYTE cmd, /* Control code */
-    void* buff /* Buffer to send/receive control data */
-) {
+DRESULT USER_ioctl(BYTE pdrv, BYTE cmd, void* buff) {
     /* USER CODE BEGIN IOCTL */
     DRESULT res = RES_ERROR;
     BSP_SD_CardInfo CardInfo;
 
     if(Stat & STA_NOINIT) return RES_NOTRDY;
 
+    // TODO: SPI manager
+    api_hal_spi_lock(&SPI_SD_HANDLE);
+
     switch(cmd) {
     /* Make sure that no pending write process */
     case CTRL_SYNC:
@@ -211,6 +219,9 @@ DRESULT USER_ioctl(
         res = RES_PARERR;
     }
 
+    // TODO: SPI manager
+    api_hal_spi_unlock(&SPI_SD_HANDLE);
+
     return res;
     /* USER CODE END IOCTL */
 }

+ 1 - 0
firmware/targets/f4/Src/main.c

@@ -121,6 +121,7 @@ int main(void)
   MX_FATFS_Init();
   delay_us_init_DWT();
   api_hal_vcp_init();
+  api_hal_spi_init();
   /* USER CODE END 2 */
 
   /* Init scheduler */

+ 23 - 5
firmware/targets/f4/Src/spi.c

@@ -110,10 +110,6 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
     HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /* USER CODE BEGIN SPI1_MspInit 1 */
-  
-  // SD Card need faster spi gpio
-  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
-  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
   /* USER CODE END SPI1_MspInit 1 */
   }
@@ -155,6 +151,28 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* spiHandle)
     HAL_GPIO_Init(SPI_D_SCK_GPIO_Port, &GPIO_InitStruct);
 
   /* USER CODE BEGIN SPI2_MspInit 1 */
+  
+  // SD Card need faster spi gpio
+  GPIO_InitStruct.Pin = GPIO_PIN_2;
+  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
+  HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
+
+  GPIO_InitStruct.Pin = SPI_D_MOSI_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
+  HAL_GPIO_Init(SPI_D_MOSI_GPIO_Port, &GPIO_InitStruct);
+
+  GPIO_InitStruct.Pin = SPI_D_SCK_Pin;
+  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
+  GPIO_InitStruct.Pull = GPIO_NOPULL;
+  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
+  GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
+  HAL_GPIO_Init(SPI_D_SCK_GPIO_Port, &GPIO_InitStruct);
 
   /* USER CODE END SPI2_MspInit 1 */
   }
@@ -272,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_2;
+  SPI_SD_HANDLE.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
   SPI_SD_HANDLE.Init.FirstBit = SPI_FIRSTBIT_MSB;
   SPI_SD_HANDLE.Init.TIMode = SPI_TIMODE_DISABLE;
   SPI_SD_HANDLE.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

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

@@ -0,0 +1,32 @@
+#include "api-hal-spi.h"
+
+osMutexId_t spi_mutex_r;
+osMutexId_t spi_mutex_d;
+
+extern SPI_HandleTypeDef SPI_R;
+extern SPI_HandleTypeDef SPI_D;
+
+void api_hal_spi_init() {
+    spi_mutex_r = osMutexNew(NULL);
+    spi_mutex_d = osMutexNew(NULL);
+}
+
+void api_hal_spi_lock(SPI_HandleTypeDef* spi) {
+    if(spi == &SPI_D) {
+        osMutexAcquire(spi_mutex_d, osWaitForever);
+    } else if(spi == &SPI_R) {
+        osMutexAcquire(spi_mutex_r, osWaitForever);
+    } else {
+        Error_Handler();
+    }
+}
+
+void api_hal_spi_unlock(SPI_HandleTypeDef* spi) {
+    if(spi == &SPI_D) {
+        osMutexRelease(spi_mutex_d);
+    } else if(spi == &SPI_R) {
+        osMutexRelease(spi_mutex_r);
+    } else {
+        Error_Handler();
+    }
+}

+ 7 - 0
firmware/targets/f4/api-hal/api-hal-spi.h

@@ -0,0 +1,7 @@
+#pragma once
+#include "main.h"
+#include <cmsis_os.h>
+
+void api_hal_spi_init();
+void api_hal_spi_lock(SPI_HandleTypeDef* spi);
+void api_hal_spi_unlock(SPI_HandleTypeDef* spi); 

+ 11 - 10
lib/u8g2/u8x8_d_st7565.c

@@ -1023,22 +1023,23 @@ static const uint8_t u8x8_d_st7565_erc12864_alt_init_seq[] = {
   // U8X8_END()             			/* end of sequence */
 
   // flipper zero sequence
-
   U8X8_START_TRANSFER(),
-  U8X8_C(0x0e2), // soft reset
+  U8X8_C(0x0e2),        // soft reset
 
-  U8X8_C(0xA3), // ST7565_st7565_command(CMD_SET_BIAS_7);
+  U8X8_C(0xA3),         // ST7565_st7565_command(CMD_SET_BIAS_7);
 
-  U8X8_C(0xA0), // ST7565_st7565_command(CMD_SET_ADC_NORMAL); 
-  U8X8_C(0xC8), // ST7565_st7565_command(CMD_SET_COM_REVERSE);
-  U8X8_C(0x40), // ST7565_st7565_command(CMD_SET_DISP_START_LINE);
-  U8X8_C(0x28 | 0x4), // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x4);
+  U8X8_C(0xA0),         // ST7565_st7565_command(CMD_SET_ADC_NORMAL); 
+  U8X8_C(0xC8),         // ST7565_st7565_command(CMD_SET_COM_REVERSE);
+  U8X8_C(0x40),         // ST7565_st7565_command(CMD_SET_DISP_START_LINE);
+  U8X8_C(0x28 | 0x4),   // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x4);
   U8X8_DLY(50), 
-  U8X8_C(0x28 | 0x6), // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x6);
+  U8X8_C(0x28 | 0x6),   // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x6);
   U8X8_DLY(50), 
-  U8X8_C(0x28 | 0x7), // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x7);
+  U8X8_C(0x28 | 0x7),   // ST7565_st7565_command(CMD_SET_POWER_CONTROL | 0x7);
   U8X8_DLY(50), 
-  U8X8_C(0x20 | 0x6), // ST7565_st7565_command(CMD_SET_RESISTOR_RATIO | 0x6);
+  U8X8_C(0x20 | 0x6),   // ST7565_st7565_command(CMD_SET_RESISTOR_RATIO | 0x6);
+  U8X8_END_TRANSFER(),  // disable chip
+  U8X8_END()            // end of sequence
 };