Home > other >  Novice just use STM32 through SD card SPI, speaking, reading and writing, and routines in the sector
Novice just use STM32 through SD card SPI, speaking, reading and writing, and routines in the sector

Time:10-09

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* the Function Name: SD_WriteSingleBlock
* Description: write a block of SD card
* Input: u32 sector sector address (sector value, not physical address)
* u8 * buffer data memory address (at least 512 byte size)
* the Output: None
* Return: u8 r1
* 0: success
* other: failure
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
U8 SD_WriteSingleBlock (u32 sector, const u8 * data)
{
U8 r1.
Under-16 I;
Under-16 retry.
//set to high speed mode
SPI_SetSpeed (SPI_SPEED_HIGH);

//if not SDHC, given the sector address and convert it into a byte address
//if (SD_Type!=SD_TYPE_V2HC)
//{
//sector=sector<9. The boundary of the sector//512 or physical sector aligned address
//}

R1=SD_SendCommand (CMD24, sector, 0 x01);
If (r1!=0 x00)
{
Return r1.//reply is not correct, direct return
}

//start preparing data transmission
SD_CS_ENABLE ();
//put three empty data first, waiting for the SD card ready
SPI_ReadWriteByte (0 XFF);
SPI_ReadWriteByte (0 XFF);
SPI_ReadWriteByte (0 XFF);
//put the initial token 0 xfe
SPI_ReadWriteByte (0 xfe);

//send a sector data
for(i=0; i<512; I++)
{
Data++ SPI_ReadWriteByte (*);
}
//send the dummy of two Byte CRC
SPI_ReadWriteByte (0 XFF);
SPI_ReadWriteByte (0 XFF);

//wait for SD card reply
R1=SPI_ReadWriteByte (0 XFF);
If ((r1 & amp; 0 x1f)!=0 x05)
{
SD_CS_DISABLE ();
Return r1.
}

//wait for operation is completed
Retry=0;
while(! SPI_ReadWriteByte (0 XFF))//card programming, data line is pulled low
{
Retry++;
If (retry> 65534)//if long time to write not complete, an error to exit the
{
SD_CS_DISABLE ();
return 1;//write timeout return 1
}
}

//write complete, slice to choose 1
SD_CS_DISABLE ();
SPI_ReadWriteByte (0 XFF);

return 0;
}

CodePudding user response:

Use 8 GSD card, how to don't on the file system

CodePudding user response:

beginner, don't quite understand at first, and the file system