STM32103RBT6使用HAL库SPI模式下读写SD卡出现问题,求助大神!
时间:10-02
整理:3721RD
点击:
源码如下:在写完等待SD卡写完的时候一直不成功;如果我把sector<<9,创建文件可以成功,但是往文件写内容的时候就不成功了,求大神指点,是什么原因?
int MSD0_WriteSingleBlock(uint32_t sector, uint8_t *buffer)
{
uint8_t r1;
register uint16_t i;
uint32_t retry;
/* if ver != SD2.0 HC, sector need <<9 */
if(SD0_CardInfo.CardType != CARDTYPE_SDV2HC)
{
sector = sector<<9;
}
/* Send CMD24 : Write single block command */
r1 = MSD0_send_command(CMD24, sector, 0);
if(r1 != 0x00)
{
return 1;
}
/* Card enable, Prepare to write */
MSD0_card_enable();
//MSD0_spi_read_write(DUMMY_BYTE);
// MSD0_spi_read_write(DUMMY_BYTE);
// MSD0_spi_read_write(DUMMY_BYTE);
/* Start data write token: 0xFE */
MSD0_spi_read_write(0xFE);
/* Start single block write the data buffer */
for(i=0; i<MSD_BLOCKSIZE; i++)
{
MSD0_spi_read_write(*buffer++);
}
/* 2Bytes dummy CRC */
MSD0_spi_read_write(DUMMY_BYTE);
MSD0_spi_read_write(DUMMY_BYTE);
/* MSD card accept the data */
retry = 0;
do{
r1 = MSD0_spi_read_write(DUMMY_BYTE);
if(++retry>200){
MSD0_card_disable();
return 2;
}
}while((r1&0x1F) != 0x05);
/* Wait all the data programm finished */
retry = 0;
do{ //在等待SD卡空闲的时候R1一直返回0;直到超时,导致写不成功;是什么问题呢?
r1 = MSD0_spi_read_write(DUMMY_BYTE);
if(++retry>0x40000){
MSD0_card_disable();
return 3;
}
}while(r1 != 0xff);
/* chip disable and dummy byte */
MSD0_card_disable();
MSD0_spi_read_write(DUMMY_BYTE);
return 0;
}
int MSD0_WriteSingleBlock(uint32_t sector, uint8_t *buffer)
{
uint8_t r1;
register uint16_t i;
uint32_t retry;
/* if ver != SD2.0 HC, sector need <<9 */
if(SD0_CardInfo.CardType != CARDTYPE_SDV2HC)
{
sector = sector<<9;
}
/* Send CMD24 : Write single block command */
r1 = MSD0_send_command(CMD24, sector, 0);
if(r1 != 0x00)
{
return 1;
}
/* Card enable, Prepare to write */
MSD0_card_enable();
//MSD0_spi_read_write(DUMMY_BYTE);
// MSD0_spi_read_write(DUMMY_BYTE);
// MSD0_spi_read_write(DUMMY_BYTE);
/* Start data write token: 0xFE */
MSD0_spi_read_write(0xFE);
/* Start single block write the data buffer */
for(i=0; i<MSD_BLOCKSIZE; i++)
{
MSD0_spi_read_write(*buffer++);
}
/* 2Bytes dummy CRC */
MSD0_spi_read_write(DUMMY_BYTE);
MSD0_spi_read_write(DUMMY_BYTE);
/* MSD card accept the data */
retry = 0;
do{
r1 = MSD0_spi_read_write(DUMMY_BYTE);
if(++retry>200){
MSD0_card_disable();
return 2;
}
}while((r1&0x1F) != 0x05);
/* Wait all the data programm finished */
retry = 0;
do{ //在等待SD卡空闲的时候R1一直返回0;直到超时,导致写不成功;是什么问题呢?
r1 = MSD0_spi_read_write(DUMMY_BYTE);
if(++retry>0x40000){
MSD0_card_disable();
return 3;
}
}while(r1 != 0xff);
/* chip disable and dummy byte */
MSD0_card_disable();
MSD0_spi_read_write(DUMMY_BYTE);
return 0;
}