我正在使用基於微控制器STM32F401RET6的Nucleo F401RE電路板。我連接到一個Micro SD插槽,並有興趣將數據寫入SD卡並從中讀取數據。我使用STM32CubeX軟件生成代碼,特別是內置函數的SD庫。我試圖編寫一個簡單的代碼,將數組寫入特定的數組,並嘗試讀取相同的數據後綴。代碼如下:從微控制器向微型SD卡寫入數據STM32F401RET6
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校驗失敗)」 。我錯過了什麼?我多次檢查硬件配置,並將微型SD卡正確連接到微控制器。如果需要,我可以添加HAL內置函數的實現。謝謝。
不管是哪個扇區寫的,你會得到這個錯誤嗎?嘗試寫入另一個行業。嘗試與其他SD卡? – staringlizard
仍然一樣。我沒有嘗試更換SD卡,但是我可以在電腦上閱讀,所以我猜這沒關係。 –
您的電腦不使用HAL庫與SD卡通信。我會嘗試與另一張SD卡,看看你是否有更好的運氣。 – staringlizard