STM32存储左右互搏 SDIO总线读写SD/MicroSD/TF卡
SD/MicroSD/TF卡是基于FLASH的一种常见非易失存储单元,由接口协议电路和FLASH构成。市面上由不同尺寸和不同容量的卡,手机领域用的TF卡实际就是MicroSD卡,尺寸比SD卡小,而电路和协议操作则是一样。这里介绍STM32CUBEIDE开发平台HAL库SDIO总线操作SD/MicroSD/TF卡的例程。
SD/MicroSD/TF卡访问接口
SD/MicroSD/TF卡可以通过访问更快的SDIO专用协议接口或是访问慢一些的普通SPI接口进行操作,两种协议接口复用管脚。通过SDIO访问的接口连接方式如下:
其中CMD连接用于指示发送的是命令还是数据。CLK提供访问同步时钟,4根数据线(DATA0 ~ DATA3 )则实现信息双向传输。
SDIO可以操作在1bit数据线和4bit数据线模式,因为4bit数据线明显效率高于1bit数据线模式,所以1bit数据线模式很少用,只有在某种极限节省连接资源的情况下可以用1bit数据线模式,在1bit模式下,数据线DATA0用来传输数据,DATA1用作中断。在4bit数据线模式下,数据线DATA0~DATA3用于传输数据,其中DATA1复用作中断线。
例程采用STM32F103VET6芯片对4GB的TF卡进行操作,TF卡也可以插入转换卡套插入SD卡接口。
STM32工程配置
首先建立基本工程并设置时钟:
配置SDIO接口:
配置使用DMA, 优先级可以根据需要调整:
配置UART1作为控制和打印输出接口:
保存并生成初始工程代码:
STM32工程代码
UART串口printf打印输出实现参考:STM32 UART串口printf函数应用及浮点打印代码空间节省 (HAL)
对SD/MicroSD/TF卡的SDIO接口操作可以调用HAL库函数进行,代码实现在main.c文件里,实现如下功能:
- 串口收到0x01指令,查询SD/MicroSD/TF卡容量等信息
- 串口收到0x02指令,执行特定区域(块0)的擦除
- 串口收到0x03指令,阻塞模式执行写操作
- 串口收到0x04指令,阻塞模式执行读操作
- 串口收到0x05指令,中断模式执行写操作
- 串口收到0x06指令,中断模式执行读操作
- 串口收到0x07指令,DMA模式执行写操作
- 串口收到0x08指令,DMA模式执行读操作
完整的main.c代码如下:
/* USER CODE BEGIN Header */
/********************************************************************************* @file : main.c* @brief : Main program body******************************************************************************* @attention** Copyright (c) 2022 STMicroelectronics.* All rights reserved.** This software is licensed under terms that can be found in the LICENSE file* in the root directory of this software component.* If no LICENSE file comes with this software, it is provided AS-IS.********************************************************************************/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "string.h"
#include "usart.h"
/* USER CODE END Includes *//* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
__IO float usDelayBase;
void PY_usDelayTest(void)
{__IO uint32_t firstms, secondms;__IO uint32_t counter = 0;firstms = HAL_GetTick()+1;secondms = firstms+1;while(uwTick!=firstms) ;while(uwTick!=secondms) counter++;usDelayBase = ((float)counter)/1000;
}void PY_Delay_us_t(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t usNum = (uint32_t)(Delay*usDelayBase);delayReg = 0;while(delayReg!=usNum) delayReg++;
}void PY_usDelayOptimize(void)
{__IO uint32_t firstms, secondms;__IO float coe = 1.0;firstms = HAL_GetTick();PY_Delay_us_t(1000000) ;secondms = HAL_GetTick();coe = ((float)1000)/(secondms-firstms);usDelayBase = coe*usDelayBase;
}void PY_Delay_us(uint32_t Delay)
{__IO uint32_t delayReg;__IO uint32_t msNum = Delay/1000;__IO uint32_t usNum = (uint32_t)((Delay%1000)*usDelayBase);if(msNum>0) HAL_Delay(msNum);delayReg = 0;while(delayReg!=usNum) delayReg++;
}
/* USER CODE END PTD *//* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
//#define BLOCKSIZE 512U /*!< Block size is 512 bytes */
#define BLOCK_START_ADDR 0 /* Block start address */
#define NUM_OF_BLOCKS 1 /* Total number of blocks */
#define BUFFER_WORDS_SIZE ((BLOCKSIZE * NUM_OF_BLOCKS) >> 2) /* Total data size in bytes *//* USER CODE END PD *//* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM *//* Private variables ---------------------------------------------------------*/
SD_HandleTypeDef hsd;
DMA_HandleTypeDef hdma_sdio;UART_HandleTypeDef huart1;/* USER CODE BEGIN PV */
uint8_t uart1_rxd[256];
uint8_t uart1_txd[256];
uint8_t cmd;/* USER CODE END PV *//* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_SDIO_SD_Init(void);
static void MX_USART1_UART_Init(void);
/* USER CODE BEGIN PFP *//* USER CODE END PFP *//* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t SD_Buffer_Tx[512] = {0} ;
uint8_t SD_Buffer_Rx[512] = {0};uint32_t SD_Status = 0;
uint32_t SD_Rx_Int = 0;
uint32_t SD_Tx_Int = 0;void SD_DMA_INIT_M2P(SD_HandleTypeDef* hsd) //DMA init: memory --> peripheral
{HAL_DMA_DeInit(&hdma_sdio);/* SDIO DMA Init *//* SDIO Init */hdma_sdio.Instance = DMA2_Channel4;hdma_sdio.Init.Direction = DMA_MEMORY_TO_PERIPH;hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;hdma_sdio.Init.Mode = DMA_NORMAL;hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;if (HAL_DMA_Init(&hdma_sdio) != HAL_OK){Error_Handler();}/* Several peripheral DMA handle pointers point to the same DMA handle.Be aware that there is only one channel to perform all the requested DMAs. *//* Be sure to change transfer direction before callingHAL_SD_ReadBlocks_DMA or HAL_SD_WriteBlocks_DMA. */__HAL_LINKDMA(hsd,hdmarx,hdma_sdio);__HAL_LINKDMA(hsd,hdmatx,hdma_sdio);
}void SD_DMA_INIT_P2M(SD_HandleTypeDef* hsd) //DMA init: memory <-- peripheral
{HAL_DMA_DeInit(&hdma_sdio);/* SDIO DMA Init *//* SDIO Init */hdma_sdio.Instance = DMA2_Channel4;hdma_sdio.Init.Direction = DMA_PERIPH_TO_MEMORY;hdma_sdio.Init.PeriphInc = DMA_PINC_DISABLE;hdma_sdio.Init.MemInc = DMA_MINC_ENABLE;hdma_sdio.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;hdma_sdio.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;hdma_sdio.Init.Mode = DMA_NORMAL;hdma_sdio.Init.Priority = DMA_PRIORITY_LOW;if (HAL_DMA_Init(&hdma_sdio) != HAL_OK){Error_Handler();}/* Several peripheral DMA handle pointers point to the same DMA handle.Be aware that there is only one channel to perform all the requested DMAs. *//* Be sure to change transfer direction before callingHAL_SD_ReadBlocks_DMA or HAL_SD_WriteBlocks_DMA. */__HAL_LINKDMA(hsd,hdmarx,hdma_sdio);__HAL_LINKDMA(hsd,hdmatx,hdma_sdio);
}
/* USER CODE END 0 *//*** @brief The application entry point.* @retval int*/
int main(void)
{/* USER CODE BEGIN 1 *//* USER CODE END 1 *//* MCU Configuration--------------------------------------------------------*//* Reset of all peripherals, Initializes the Flash interface and the Systick. */HAL_Init();/* USER CODE BEGIN Init *//* USER CODE END Init *//* Configure the system clock */SystemClock_Config();/* USER CODE BEGIN SysInit *//* USER CODE END SysInit *//* Initialize all configured peripherals */MX_GPIO_Init();MX_DMA_Init();MX_SDIO_SD_Init();MX_USART1_UART_Init();/* USER CODE BEGIN 2 */PY_usDelayTest();PY_usDelayOptimize();HAL_UART_Receive_IT(&huart1, uart1_rxd, 1);/* USER CODE END 2 *//* Infinite loop *//* USER CODE BEGIN WHILE */while (1){/*HAL_SD_CARD_TRANSFER is the operate correct and complete status for SD card operation*/if(cmd==0x01) //Get SD card information{cmd = 0;printf("\r\n SD card test...\r\n");if(HAL_SD_GetCardState(&hsd) == HAL_SD_CARD_TRANSFER) //Get SD card resource info{printf("\r\n Initialize SD card successful!\r\n");printf(" SD card information↓ \r\n");printf(" Card Capacity : %llu \r\n", (unsigned long long)hsd.SdCard.BlockSize * hsd.SdCard.BlockNbr);printf(" One block size in bytes : %d \r\n", (int)hsd.SdCard.BlockSize);printf(" Logical Capacity in blocks : %d \r\n", (int)hsd.SdCard.LogBlockNbr);printf(" Logical block size in bytes : %d \r\n", (int)hsd.SdCard.LogBlockSize);printf(" Relative Card Address : %d \r\n", (int)hsd.SdCard.RelCardAdd);printf(" Card Type : %d \r\n", (int)hsd.SdCard.CardType);HAL_SD_CardCIDTypeDef sdcard_cid;HAL_SD_GetCardCID(&hsd,&sdcard_cid); //Get SD card vendor infoprintf(" Manufacturer ID: %d \r\n", (int)sdcard_cid.ManufacturerID);}else{printf("\r\n SD card initiation failed!\r\n" );}}else if(cmd==0x02) //Erase SD card block{cmd = 0;printf("\r\n------------------- Block Erase -------------------------------\r\n");if(HAL_SD_Erase(&hsd, BLOCK_START_ADDR, NUM_OF_BLOCKS) == HAL_OK) //Erase block operation{while(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) PY_Delay_us_t(10);printf("\r\n Erase Block Successful!\r\n");}else{printf("\r\n Erase Block Failed!\r\n");}}else if(cmd==0x03) //SD card write in block mode{cmd = 0;memset(SD_Buffer_Tx, 0xAA, sizeof(SD_Buffer_Tx)); //0xAA written into buffer for this testprintf("\r\n------------------- Write SD card block data in block mode ------------------\r\n");__disable_irq();SD_Status = HAL_SD_WriteBlocks(&hsd, SD_Buffer_Tx, BLOCK_START_ADDR, NUM_OF_BLOCKS, 0xFFFFFFFF); //Write block operation in block modeif(SD_Status==HAL_OK){while(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) PY_Delay_us_t(10); //Wait for write endprintf("\r\n Write block data in block mode successful!\r\n");}else{printf("\r\n Write block data in block mode failed!\r\n");}__enable_irq();}else if(cmd==0x04) //SD card read in block mode{cmd = 0;printf("\r\n------------------- Read SD card block data in block mode ------------------\r\n");__disable_irq();SD_Status = HAL_SD_ReadBlocks(&hsd, SD_Buffer_Rx, BLOCK_START_ADDR, NUM_OF_BLOCKS, 0xFFFFFFFF); //read block operation in block modeif( SD_Status== HAL_OK){while(HAL_SD_GetCardState(&hsd) != HAL_SD_CARD_TRANSFER) PY_Delay_us_t(10); //Wait for read endprintf("\r\n Read block data in block mode successful!\r\n");for(uint32_t i = 0; i < sizeof(SD_Buffer_Rx); i++){printf("0x%02x:%02x ", (unsigned int)i, (unsigned int)SD_Buffer_Rx[i]);}printf("\r\n");}else{printf("\r\n Read block data in block mode failed!\r\n");}__enable_irq();}else if(cmd==0x05) //SD card write in INT mode{cmd = 0;memset(SD_Buffer_Tx, 0x55, sizeof(SD_Buffer_Tx)); //0x55 written into buffer for this testprintf("\r\n------------------- Write SD card block data in INT mode ------------------\r\n");SD_Tx_Int = 1;SD_Status = HAL_SD_WriteBlocks_IT(&hsd, SD_Buffer_Tx, BLOCK_START_ADDR, NUM_OF_BLOCKS); //write block operation in INT modeif(SD_Status== HAL_OK){while(SD_Tx_Int==1) PY_Delay_us_t(1); //Wait for write endprintf("\r\n Write block data in INT mode successful!\r\n");}else{printf("\r\n Write block data in INT mode failed!\r\n");}}else if(cmd==0x06) //SD card read in INT mode{cmd = 0;printf("\r\n------------------- Read SD card block data in INT mode ------------------\r\n");SD_Rx_Int = 1;SD_Status = HAL_SD_ReadBlocks_IT(&hsd, SD_Buffer_Rx, BLOCK_START_ADDR, NUM_OF_BLOCKS); //read block operation in INT modeif( SD_Status== HAL_OK){while(SD_Rx_Int==1) PY_Delay_us_t(1); //Wait for read endprintf("\r\n Read block data in INT mode successful!\r\n");for(uint32_t i = 0; i < sizeof(SD_Buffer_Rx); i++){printf("0x%02x:%02x ", (unsigned int)i, (unsigned int)SD_Buffer_Rx[i]);}printf("\r\n");}else{printf("\r\n Read block data in INT mode failed!\r\n");}}else if(cmd==0x07) //SD card write in DMA mode{cmd = 0;SD_DMA_INIT_M2P(&hsd); //Switch DMA mode directionmemset(SD_Buffer_Tx, 0x5A, sizeof(SD_Buffer_Tx)); //0x5A written into buffer for this testprintf("\r\n------------------- Write SD card block data in DMA mode ------------------\r\n");SD_Tx_Int = 1;SD_Status = HAL_SD_WriteBlocks_DMA(&hsd, SD_Buffer_Tx, BLOCK_START_ADDR, NUM_OF_BLOCKS); //write block operation in DMA modeif(SD_Status== HAL_OK){while(SD_Tx_Int==1) PY_Delay_us_t(1); //Wait for write endprintf("\r\n Write block data in DMA mode successful!\r\n");}else{printf("\r\n Write block data in DMA mode failed!\r\n");}}else if(cmd==0x08) //SD card read in DMA mode{cmd = 0;SD_DMA_INIT_P2M(&hsd); //Switch DMA mode directionprintf("\r\n------------------- Read SD card block data in DMA mode ------------------\r\n");SD_Rx_Int = 1;SD_Status = HAL_SD_ReadBlocks_DMA(&hsd, SD_Buffer_Rx, BLOCK_START_ADDR, NUM_OF_BLOCKS); //read block operation in DMA modeif( SD_Status== HAL_OK){while(SD_Rx_Int==1) PY_Delay_us_t(1); //Wait for read endprintf("\r\n Read block data in DMA mode successful!\r\n");for(uint32_t i = 0; i < sizeof(SD_Buffer_Rx); i++){printf("0x%02x:%02x ", (unsigned int)i, (unsigned int)SD_Buffer_Rx[i]);}printf("\r\n");}else{printf("\r\n Read block data in DMA mode failed!\r\n");}}else;/* USER CODE END WHILE *//* USER CODE BEGIN 3 */}/* USER CODE END 3 */
}/*** @brief System Clock Configuration* @retval None*/
void SystemClock_Config(void)
{RCC_OscInitTypeDef RCC_OscInitStruct = {0};RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};/** Initializes the RCC Oscillators according to the specified parameters* in the RCC_OscInitTypeDef structure.*/RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;RCC_OscInitStruct.HSEState = RCC_HSE_ON;RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;RCC_OscInitStruct.HSIState = RCC_HSI_ON;RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK){Error_Handler();}/** Initializes the CPU, AHB and APB buses clocks*/RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK){Error_Handler();}
}/*** @brief SDIO Initialization Function* @param None* @retval None*/
static void MX_SDIO_SD_Init(void)
{/* USER CODE BEGIN SDIO_Init 0 *//* USER CODE END SDIO_Init 0 *//* USER CODE BEGIN SDIO_Init 1 *//* USER CODE END SDIO_Init 1 */hsd.Instance = SDIO;hsd.Init.ClockEdge = SDIO_CLOCK_EDGE_RISING;hsd.Init.ClockBypass = SDIO_CLOCK_BYPASS_DISABLE;hsd.Init.ClockPowerSave = SDIO_CLOCK_POWER_SAVE_DISABLE;hsd.Init.BusWide = SDIO_BUS_WIDE_1B;hsd.Init.HardwareFlowControl = SDIO_HARDWARE_FLOW_CONTROL_ENABLE;hsd.Init.ClockDiv = 6;if (HAL_SD_Init(&hsd) != HAL_OK){Error_Handler();}if (HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B) != HAL_OK){Error_Handler();}/* USER CODE BEGIN SDIO_Init 2 *//* USER CODE END SDIO_Init 2 */}/*** @brief USART1 Initialization Function* @param None* @retval None*/
static void MX_USART1_UART_Init(void)
{/* USER CODE BEGIN USART1_Init 0 *//* USER CODE END USART1_Init 0 *//* USER CODE BEGIN USART1_Init 1 *//* USER CODE END USART1_Init 1 */huart1.Instance = USART1;huart1.Init.BaudRate = 115200;huart1.Init.WordLength = UART_WORDLENGTH_8B;huart1.Init.StopBits = UART_STOPBITS_1;huart1.Init.Parity = UART_PARITY_NONE;huart1.Init.Mode = UART_MODE_TX_RX;huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;huart1.Init.OverSampling = UART_OVERSAMPLING_16;if (HAL_UART_Init(&huart1) != HAL_OK){Error_Handler();}/* USER CODE BEGIN USART1_Init 2 *//* USER CODE END USART1_Init 2 */}/*** Enable DMA controller clock*/
static void MX_DMA_Init(void)
{/* DMA controller clock enable */__HAL_RCC_DMA2_CLK_ENABLE();/* DMA interrupt init *//* DMA2_Channel4_5_IRQn interrupt configuration */HAL_NVIC_SetPriority(DMA2_Channel4_5_IRQn, 0, 0);HAL_NVIC_EnableIRQ(DMA2_Channel4_5_IRQn);}/*** @brief GPIO Initialization Function* @param None* @retval None*/
static void MX_GPIO_Init(void)
{/* GPIO Ports Clock Enable */__HAL_RCC_GPIOC_CLK_ENABLE();__HAL_RCC_GPIOA_CLK_ENABLE();__HAL_RCC_GPIOD_CLK_ENABLE();}/* USER CODE BEGIN 4 */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{if(huart==&huart1){cmd = uart1_rxd[0];HAL_UART_Receive_IT(&huart1, uart1_rxd, 1);}
}void HAL_SD_TxCpltCallback(SD_HandleTypeDef *hsd)
{SD_Tx_Int = 0;
}void HAL_SD_RxCpltCallback(SD_HandleTypeDef *hsd)
{SD_Rx_Int = 0;
}
/* USER CODE END 4 *//*** @brief This function is executed in case of error occurrence.* @retval None*/
void Error_Handler(void)
{/* USER CODE BEGIN Error_Handler_Debug *//* User can add his own implementation to report the HAL error return state */__disable_irq();while (1){}/* USER CODE END Error_Handler_Debug */
}#ifdef USE_FULL_ASSERT
/*** @brief Reports the name of the source file and the source line number* where the assert_param error has occurred.* @param file: pointer to the source file name* @param line: assert_param error line source number* @retval None*/
void assert_failed(uint8_t *file, uint32_t line)
{/* USER CODE BEGIN 6 *//* User can add his own implementation to report the file name and line number,ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) *//* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
STM32例程测试
串口指令0x01测试效果如下:
串口指令0x02测试效果如下:
串口指令0x03测试效果如下:
串口指令0x04测试效果如下:
串口指令0x05测试效果如下:
串口指令0x06测试效果如下:
串口指令0x07测试效果如下:
串口指令0x08测试效果如下:
STM32例程下载
STM32F103VET6 SDIO总线读写SD/MicroSD/TF卡例程下载
–End–