SD card v7 BSP (#361)

* Outdated apps: add api-light-usage

* Gpio: update SD card CS pin settings

* API-power: added fns to disable/enable external 3v3 dc-dc

* API-gpio: separated SD card detect routines

* Resources: removed sd cs pin

* SD card: low level init now resets card power supply

* App SD-filesystem: use new card detect fns

* SD card: fix low level init headers

* SD card: more realilable low level init, power reset, exit from command read cycle conditionally

* App SD-filesystem: led notifiers, init cycling

* SD card: backport to F4

* SD card: handle eject in init sequence

* SD card: api to set level on detect gpio

* SPI: api to set state on bus pins

* SD card: set low state on bus pins while power reset

Co-authored-by: coreglitch <mail@s3f.ru>
This commit is contained in:
SG
2021-03-09 14:53:33 +03:00
committed by GitHub
parent 3f10ce47f0
commit fef16a8e7a
20 changed files with 463 additions and 214 deletions

View File

@@ -92,7 +92,10 @@
#include "string.h"
#include "stdio.h"
#include "spi.h"
#include "api-hal-spi.h"
#include <api-hal-spi.h>
#include <api-hal-power.h>
#include <api-hal-delay.h>
#include <api-hal-sd.h>
/** @addtogroup BSP
* @{
@@ -290,19 +293,40 @@ static uint8_t SD_ReadData(void);
* - MSD_ERROR: Sequence failed
* - MSD_OK: Sequence succeed
*/
uint8_t BSP_SD_Init(void) {
/* Init to maximum slow speed */
// TODO: SPI manager
uint8_t BSP_SD_Init(bool reset_card) {
/* Slow speed init */
/* TODO: SPI manager */
api_hal_spi_lock_device(&sd_slow_spi);
/* We must reset card in spi_lock context */
if(reset_card) {
/* disable power and set low on all bus pins */
api_hal_power_disable_external_3_3v();
SD_SPI_Bus_To_Down_State();
hal_sd_detect_set_low();
delay(250);
/* reinit bus and enable power */
SD_SPI_Bus_To_Normal_State();
hal_sd_detect_init();
api_hal_power_enable_external_3_3v();
delay(100);
}
/* Configure IO functionalities for SD pin */
SD_IO_Init();
/* SD detection pin is not physically mapped on the Adafruit shield */
SdStatus = SD_PRESENT;
uint8_t res = SD_GoIdleState();
uint8_t res = BSP_SD_ERROR;
// TODO: SPI manager
for(uint8_t i = 0; i < 128; i++) {
res = SD_GoIdleState();
if(res == BSP_SD_OK) break;
}
/* TODO: SPI manager */
api_hal_spi_unlock_device(&sd_slow_spi);
/* SD initialized and set to SPI mode properly */
@@ -872,9 +896,10 @@ uint8_t SD_GetDataResponse(void) {
*/
uint8_t SD_GoIdleState(void) {
SD_CmdAnswer_typedef response;
__IO uint8_t counter = 0;
__IO uint8_t counter;
/* Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode and
wait for In Idle State Response (R1 Format) equal to 0x01 */
counter = 0;
do {
counter++;
response = SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95, SD_ANSWER_R1_EXPECTED);
@@ -892,7 +917,9 @@ uint8_t SD_GoIdleState(void) {
SD_IO_WriteByte(SD_DUMMY_BYTE);
if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) {
/* initialise card V1 */
counter = 0;
do {
counter++;
/* initialise card V1 */
/* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
response = SD_SendCmd(SD_CMD_APP_CMD, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
@@ -903,11 +930,16 @@ uint8_t SD_GoIdleState(void) {
response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(counter >= SD_MAX_TRY) {
return BSP_SD_ERROR;
}
} while(response.r1 == SD_R1_IN_IDLE_STATE);
flag_SDHC = 0;
} else if(response.r1 == SD_R1_IN_IDLE_STATE) {
/* initialise card V2 */
counter = 0;
do {
counter++;
/* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
@@ -917,10 +949,15 @@ uint8_t SD_GoIdleState(void) {
response = SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x40000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(counter >= SD_MAX_TRY) {
return BSP_SD_ERROR;
}
} while(response.r1 == SD_R1_IN_IDLE_STATE);
if((response.r1 & SD_R1_ILLEGAL_COMMAND) == SD_R1_ILLEGAL_COMMAND) {
counter = 0;
do {
counter++;
/* Send CMD55 (SD_CMD_APP_CMD) before any ACMD command: R1 response (0x00: no errors) */
response = SD_SendCmd(SD_CMD_APP_CMD, 0, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
@@ -933,6 +970,9 @@ uint8_t SD_GoIdleState(void) {
SD_SendCmd(SD_CMD_SD_APP_OP_COND, 0x00000000, 0xFF, SD_ANSWER_R1_EXPECTED);
SD_IO_CSState(1);
SD_IO_WriteByte(SD_DUMMY_BYTE);
if(counter >= SD_MAX_TRY) {
return BSP_SD_ERROR;
}
} while(response.r1 == SD_R1_IN_IDLE_STATE);
}

View File

@@ -46,6 +46,7 @@
/* Includes ------------------------------------------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/** @addtogroup BSP
* @{
@@ -208,7 +209,7 @@ typedef struct
/** @defgroup STM32_ADAFRUIT_SD_Exported_Functions
* @{
*/
uint8_t BSP_SD_Init(void);
uint8_t BSP_SD_Init(bool reset_card);
uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint32_t NumOfBlocks, uint32_t Timeout);
uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint32_t NumOfBlocks, uint32_t Timeout);
uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr);

View File

@@ -163,10 +163,9 @@ void MX_GPIO_Init(void)
/*Configure GPIO pin : PtPin */
GPIO_InitStruct.Pin = SD_CS_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF6_LSCO;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
HAL_GPIO_Init(SD_CS_GPIO_Port, &GPIO_InitStruct);
/*Configure GPIO pin : PtPin */

View File

@@ -339,6 +339,36 @@ void Enable_SPI(SPI_HandleTypeDef* spi_instance){
__HAL_SPI_ENABLE(spi_instance);
}
}
void SD_SPI_Bus_To_Down_State(){
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Pin = GPIO_PIN_2;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI_D_MOSI_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SPI_D_MOSI_GPIO_Port, &GPIO_InitStruct);
GPIO_InitStruct.Pin = SPI_D_SCK_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
HAL_GPIO_Init(SPI_D_SCK_GPIO_Port, &GPIO_InitStruct);
HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_2, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SPI_D_MOSI_GPIO_Port, SPI_D_MOSI_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SPI_D_SCK_GPIO_Port, SPI_D_SCK_Pin, GPIO_PIN_RESET);
}
void SD_SPI_Bus_To_Normal_State(){
HAL_GPIO_WritePin(SD_CS_GPIO_Port, SD_CS_Pin, GPIO_PIN_SET);
HAL_SPI_MspInit(&SPI_SD_HANDLE);
}
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/