我正在使用基于微控制器 STM32F401RET6 的板 Nucleo F401RE。我将一个 Micro SD 插槽连接到板上,并有兴趣将数据写入 SD 卡并从中读取数据。我使用软件 STM32CubeX 来生成代码,特别是带有内置函数的 SD 库。我试图编写一个简单的代码,它将一个数组写入一个特定的数组并尝试在后面读取相同的数据。代码如下:
uint32_t to_send[512] = {1, 2, 3, 4, 5};
uint32_t to_receive[512];
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_SDIO_SD_Init();
char buffer[14] = "Hello, world\n";
uint64_t address = 0x00;
HAL_SD_ErrorTypedef write_result = HAL_SD_WriteBlocks(&hsd, to_send, address, 512, 1);
HAL_SD_ErrorTypedef read_result = HAL_SD_ReadBlocks(&hsd, to_receive, 0x00, 512, 1);
HAL_UART_Transmit(&huart2, (uint8_t *) &write_result, 1, 1000);
HAL_UART_Transmit(&huart2, (uint8_t *) &read_result, 1, 1000);
while (1)
{
//HAL_UART_Transmit(&huart2, (uint8_t *)buffer, 14, 1000);
HAL_UART_Transmit(&huart2, (uint8_t *)to_receive, 512, 1000);
}
虽然,我没有成功写入数据,但函数 HAL_SD_WriteBlocks() 返回值 SD_CMD_CRC_FAIL,这意味着:“收到命令响应(但 CRC 校验失败)”。我错过了什么?我多次检查硬件配置,micro SD 卡已正确连接到微控制器。如果需要,我可以添加 HAL 内置函数的实现。谢谢你。