cubemx :sd卡 读写测试程序

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");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值