SD卡存储数据程序
时间: 2023-07-01 21:12:50 浏览: 173
好的,以下是一个简单的SD卡存储数据的程序示例,您可以根据自己的需求进行修改:
```c
#include "stm32f4xx_hal.h"
#include <string.h>
/* SD卡句柄定义 */
SD_HandleTypeDef hsd;
/* SD卡初始化函数 */
void MX_SDIO_SD_Init(void)
{
/* 初始化SD卡时钟 */
__HAL_RCC_SDIO_CLK_ENABLE();
/* 初始化DMA时钟 */
__HAL_RCC_DMA2_CLK_ENABLE();
/* 初始化SDIO GPIO引脚 */
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF12_SDIO;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* 初始化DMA配置 */
hdma_sdio_rx.Instance = DMA2_Stream3;
hdma_sdio_rx.Init.Channel = DMA_CHANNEL_4;
hdma_sdio_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_sdio_rx.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_sdio_rx.Init.MemInc = DMA_MINC_ENABLE;
hdma_sdio_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_sdio_rx.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_sdio_rx.Init.Mode = DMA_PFCTRL;
hdma_sdio_rx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
hdma_sdio_rx.Init.FIFOMode = DMA_FIFOMODE_ENABLE;
hdma_sdio_rx.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_sdio_rx.Init.MemBurst = DMA_MBURST_INC4;
hdma_sdio_rx.Init.PeriphBurst = DMA_PBURST_INC4;
HAL_DMA_Init(&hdma_sdio_rx);
/* 初始化SDIO配置 */
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_DISABLE;
hsd.Init.ClockDiv = 0;
HAL_SD_Init(&hsd, &SDCardInfo);
/* 初始化SD卡信息 */
HAL_SD_ConfigWideBusOperation(&hsd, SDIO_BUS_WIDE_4B);
HAL_SD_GetCardCID(&hsd, &SDCardInfo.CID);
HAL_SD_GetCardCSD(&hsd, &SDCardInfo.CSD);
HAL_SD_GetCardStatus(&hsd, &SDCardInfo.CardStatus);
HAL_SD_GetCardInfo(&hsd, &SDCardInfo);
}
/* SD卡读取函数 */
uint8_t SD_ReadBlocks(uint32_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
{
if(HAL_SD_ReadBlocks_DMA(&hsd, (uint8_t *)pData, BlockAdd, NumberOfBlocks) != HAL_OK)
{
return 1;
}
if(HAL_SD_CheckReadOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != HAL_OK)
{
return 2;
}
return 0;
}
/* SD卡写入函数 */
uint8_t SD_WriteBlocks(uint32_t *pData, uint32_t BlockAdd, uint32_t NumberOfBlocks)
{
if(HAL_SD_WriteBlocks_DMA(&hsd, (uint8_t *)pData, BlockAdd, NumberOfBlocks) != HAL_OK)
{
return 1;
}
if(HAL_SD_CheckWriteOperation(&hsd, (uint32_t)SD_DATATIMEOUT) != HAL_OK)
{
return 2;
}
return 0;
}
/* 存储数据到SD卡 */
void store_data_to_sd(uint32_t *data, uint32_t block_addr, uint32_t block_num)
{
/* 写入数据到SD卡 */
SD_WriteBlocks(data, block_addr, block_num);
}
int main(void)
{
/* 初始化SD卡 */
MX_SDIO_SD_Init();
/* 存储数据到SD卡 */
uint32_t data[512] = {0}; // 数据缓冲区
uint32_t block_addr = 0; // SD卡起始块地址
uint32_t block_num = 1; // SD卡块数
memcpy(data, "Hello World!", sizeof("Hello World!")); // 将数据拷贝到缓冲区
store_data_to_sd(data, block_addr, block_num); // 存储数据到SD卡
while (1)
{
// 主循环
}
}
```
示例程序中,我们使用了一个名为`store_data_to_sd`的函数来将数据存储到SD卡中。该函数接受三个参数:数据缓冲区、SD卡起始块地址以及SD卡块数。在该函数内部,我们调用了`SD_WriteBlocks`函数来将数据写入到SD卡中。
需要注意的是,我们在程序开头调用了`MX_SDIO_SD_Init`函数来初始化SD卡。在实际使用中,您需要将`block_addr`和`block_num`参数设置为您要存储数据的SD卡起始块地址和块数。此外,您还需要根据自己的需求修改数据缓冲区的大小和内容。
希望这个示例程序能够帮助您开始存储数据到SD卡中。
阅读全文