printf("Micro SD Card Test...\r\n");
/* 检测SD卡是否正常(处于数据传输模式的传输状态) */
HAL_SD_CardCIDTypeDef sdcard_cid;
uint32_t sdcard_status = HAL_SD_GetCardState(&hsd);
if(sdcard_status == HAL_SD_CARD_TRANSFER)
{
printf("SD card init ok!\r\n\r\n");
HAL_SD_GetCardCID(&hsd,&sdcard_cid);
//打印SD卡基本信息
printf("SD card information!\r\n");
printf("ManufacturerID: %d \r\n",sdcard_cid.ManufacturerID);
printf("CardType: %d \r\n",hsd.SdCard.CardType);
printf("CardCapacity: %llu\r\n",((unsigned long long)hsd.SdCard.BlockSize*hsd.SdCard.BlockNbr));
printf("CardBlockSize: %d \r\n",hsd.SdCard.BlockSize);
printf("BlockNbr: %d \r\n",hsd.SdCard.BlockNbr);
printf("Class: %d \r\n",hsd.SdCard.Class);
//读取并打印SD卡的CID信息
}else
{
printf("SD card init fail!\r\n" );
return 0;
}
printf("------------------- Block Erase -------------------------------\r\n");
sdcard_status = HAL_SD_Erase(&hsd, 10*512, 11*512);
if (sdcard_status == 0)
{
printf("Erase block ok\r\n");
}
else
{
printf("Erase block fail\r\n");
}
HAL_Delay(100);
printf("------------------- Read SD card block data Test ------------------\r\n");
sdcard_status = HAL_SD_ReadBlocks_DMA(&hsd,(uint8_t *)read_buf,10*512,1);
if(sdcard_status == 0)
{
printf("Read block data ok \r\n" );
for(int i = 0; i < 512; i++)
{
printf("0x%02x ", read_buf[i]);
if((i+1)%16 == 0)
{
printf("\r\n");
}
}
}
else
{
printf("Read block data fail!\r\n " );
}
for(int i = 0; i < 512; i++)
{
write_buf[i] = i % 256;
}
printf("------------------- Write SD card block data Test ------------------\r\n");
sdcard_status = HAL_SD_WriteBlocks_DMA(&hsd,(uint8_t *)write_buf,5120,1);
if(sdcard_status == 0)
{
printf("Write block data ok \r\n" );
}
else
{
printf("Write block data fail!\r\n " );
}
HAL_Delay(100);
printf("------------------- Read SD card block data after Write ------------------\r\n");
sdcard_status = HAL_SD_ReadBlocks_DMA(&hsd,(uint8_t *)read_buf,5120,1);
if(sdcard_status == 0)
{
printf("Read block data ok \r\n" );
for(int i = 0; i < 512; i++)
{
printf("0x%02x ", read_buf[i]);
if((i+1)%16 == 0)
{
printf("\r\n");
}
}
}
cubemx :sd卡 读写测试程序
最新推荐文章于 2024-08-08 12:18:37 发布