mirror of
https://github.com/betaflight/betaflight.git
synced 2025-07-21 15:25:36 +03:00
DMA support for SD-card write
This commit is contained in:
parent
97ee6142a9
commit
a35ccd28de
4 changed files with 167 additions and 856 deletions
|
@ -23,817 +23,8 @@
|
|||
|
||||
#include "platform.h"
|
||||
|
||||
#include "drivers/gpio.h"
|
||||
#include "drivers/sdcard.h"
|
||||
#include "nvic.h"
|
||||
|
||||
void SD_Detect_LowLevel_DeInit(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
/* Configure SD_SPI_INSTANCE_DETECT_PIN pin: SD Card detect pin */
|
||||
GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
|
||||
GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
|
||||
}
|
||||
|
||||
void SD_Detect_LowLevel_Init(void)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
|
||||
/* Configure SD_SPI_INSTANCE_DETECT_PIN pin: SD Card detect pin */
|
||||
GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN;
|
||||
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
|
||||
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
|
||||
GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief DeInitializes the SD/SD communication.
|
||||
* @param None
|
||||
* @retval None
|
||||
*/
|
||||
void SD_DeInit(void)
|
||||
{
|
||||
//SD_LowLevel_DeInit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes the SD/SD communication.
|
||||
* @param None
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_Init(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
|
||||
/*!< Initialize SD_SPI_INSTANCE */
|
||||
//SD_LowLevel_Init();
|
||||
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
|
||||
/*!< Send dummy byte 0xFF, 10 times with CS high */
|
||||
/*!< Rise CS and MOSI for 80 clocks cycles */
|
||||
for (i = 0; i <= 9; i++)
|
||||
{
|
||||
/*!< Send dummy byte 0xFF */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
}
|
||||
|
||||
/*------------Put SD in SPI mode--------------*/
|
||||
/*!< SD initialized and set to SPI mode properly */
|
||||
return (SD_GoIdleState());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Detect if SD card is correctly plugged in the memory slot.
|
||||
* @param None
|
||||
* @retval Return if SD is detected or not
|
||||
*/
|
||||
uint8_t SD_Detect(void)
|
||||
{
|
||||
/*!< Check GPIO to detect SD */
|
||||
if (GPIO_ReadInputData(SD_DETECT_GPIO_PORT) & SD_DETECT_PIN)
|
||||
{
|
||||
#ifdef SD_DETECT_INVERTED
|
||||
return SD_NOT_PRESENT;
|
||||
#else
|
||||
return SD_PRESENT;
|
||||
#endif
|
||||
} else {
|
||||
#ifdef SD_DETECT_INVERTED
|
||||
return SD_PRESENT;
|
||||
#else
|
||||
return SD_NOT_PRESENT;
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns information about specific card.
|
||||
* @param cardinfo: pointer to a SD_CardInfo structure that contains all SD
|
||||
* card information.
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
|
||||
{
|
||||
SD_Error status = SD_RESPONSE_FAILURE;
|
||||
|
||||
SD_GetCSDRegister(&(cardinfo->SD_csd));
|
||||
status = SD_GetCIDRegister(&(cardinfo->SD_cid));
|
||||
cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
|
||||
cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
|
||||
cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
|
||||
cardinfo->CardCapacity *= cardinfo->CardBlockSize;
|
||||
|
||||
/*!< Returns the reponse */
|
||||
return status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads a block of data from the SD.
|
||||
* @param pBuffer: pointer to the buffer that receives the data read from the
|
||||
* SD.
|
||||
* @param ReadAddr: SD's internal address to read from.
|
||||
* @param BlockSize: the SD card Data block size.
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
|
||||
SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, ReadAddr, 0xFF);
|
||||
|
||||
/*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
|
||||
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
/*!< Now look for the data token to signify the start of the data */
|
||||
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
|
||||
{
|
||||
/*!< Read the SD block data : read NumByteToRead data */
|
||||
for (i = 0; i < BlockSize; i++)
|
||||
{
|
||||
/*!< Save the received data */
|
||||
*pBuffer = SD_ReadByte();
|
||||
|
||||
/*!< Point to the next location where the byte read will be saved */
|
||||
pBuffer++;
|
||||
}
|
||||
/*!< Get CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_ReadByte();
|
||||
SD_ReadByte();
|
||||
/*!< Set response value to success */
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Returns the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reads multiple block of data from the SD.
|
||||
* @param pBuffer: pointer to the buffer that receives the data read from the
|
||||
* SD.
|
||||
* @param ReadAddr: SD's internal address to read from.
|
||||
* @param BlockSize: the SD card Data block size.
|
||||
* @param NumberOfBlocks: number of blocks to be read.
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
|
||||
{
|
||||
uint32_t i = 0, Offset = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
/*!< Data transfer */
|
||||
while (NumberOfBlocks--)
|
||||
{
|
||||
/*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
|
||||
SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF);
|
||||
/*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
|
||||
if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
return SD_RESPONSE_FAILURE;
|
||||
}
|
||||
/*!< Now look for the data token to signify the start of the data */
|
||||
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
|
||||
{
|
||||
/*!< Read the SD block data : read NumByteToRead data */
|
||||
for (i = 0; i < BlockSize; i++)
|
||||
{
|
||||
/*!< Read the pointed data */
|
||||
*pBuffer = SD_ReadByte();
|
||||
/*!< Point to the next location where the byte read will be saved */
|
||||
pBuffer++;
|
||||
}
|
||||
/*!< Set next read address*/
|
||||
Offset += 512;
|
||||
/*!< get CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_ReadByte();
|
||||
SD_ReadByte();
|
||||
/*!< Set response value to success */
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*!< Set response value to failure */
|
||||
rvalue = SD_RESPONSE_FAILURE;
|
||||
}
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
/*!< Returns the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes a block on the SD
|
||||
* @param pBuffer: pointer to the buffer containing the data to be written on
|
||||
* the SD.
|
||||
* @param WriteAddr: address to write on.
|
||||
* @param BlockSize: the SD card Data block size.
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block */
|
||||
SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF);
|
||||
|
||||
/*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
|
||||
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
/*!< Send a dummy byte */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Send the data token to signify the start of the data */
|
||||
SD_WriteByte(0xFE);
|
||||
|
||||
/*!< Write the block data to SD : write count data by block */
|
||||
for (i = 0; i < BlockSize; i++)
|
||||
{
|
||||
/*!< Send the pointed byte */
|
||||
SD_WriteByte(*pBuffer);
|
||||
/*!< Point to the next location where the byte read will be saved */
|
||||
pBuffer++;
|
||||
}
|
||||
/*!< Put CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_ReadByte();
|
||||
SD_ReadByte();
|
||||
/*!< Read data response */
|
||||
if (SD_GetDataResponse() == SD_DATA_OK)
|
||||
{
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Returns the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Writes many blocks on the SD
|
||||
* @param pBuffer: pointer to the buffer containing the data to be written on
|
||||
* the SD.
|
||||
* @param WriteAddr: address to write on.
|
||||
* @param BlockSize: the SD card Data block size.
|
||||
* @param NumberOfBlocks: number of blocks to be written.
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
|
||||
{
|
||||
uint32_t i = 0, Offset = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
/*!< Data transfer */
|
||||
while (NumberOfBlocks--)
|
||||
{
|
||||
/*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks */
|
||||
SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr + Offset, 0xFF);
|
||||
/*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
|
||||
if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
return SD_RESPONSE_FAILURE;
|
||||
}
|
||||
/*!< Send dummy byte */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
/*!< Send the data token to signify the start of the data */
|
||||
SD_WriteByte(SD_START_DATA_SINGLE_BLOCK_WRITE);
|
||||
/*!< Write the block data to SD : write count data by block */
|
||||
for (i = 0; i < BlockSize; i++)
|
||||
{
|
||||
/*!< Send the pointed byte */
|
||||
SD_WriteByte(*pBuffer);
|
||||
/*!< Point to the next location where the byte read will be saved */
|
||||
pBuffer++;
|
||||
}
|
||||
/*!< Set next write address */
|
||||
Offset += 512;
|
||||
/*!< Put CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_ReadByte();
|
||||
SD_ReadByte();
|
||||
/*!< Read data response */
|
||||
if (SD_GetDataResponse() == SD_DATA_OK)
|
||||
{
|
||||
/*!< Set response value to success */
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
/*!< Set response value to failure */
|
||||
rvalue = SD_RESPONSE_FAILURE;
|
||||
}
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
/*!< Returns the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the CSD card register.
|
||||
* Reading the contents of the CSD register in SPI mode is a simple
|
||||
* read-block transaction.
|
||||
* @param SD_csd: pointer on an SCD register structure
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_GetCSDRegister(SD_CSD* SD_csd)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
uint8_t CSD_Tab[16];
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
/*!< Send CMD9 (CSD register) or CMD10(CSD register) */
|
||||
SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF);
|
||||
/*!< Wait for response in the R1 format (0x00 is no errors) */
|
||||
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
|
||||
{
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
/*!< Store CSD register value on CSD_Tab */
|
||||
CSD_Tab[i] = SD_ReadByte();
|
||||
}
|
||||
}
|
||||
/*!< Get CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
/*!< Set response value to success */
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Byte 0 */
|
||||
SD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
|
||||
SD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
|
||||
SD_csd->Reserved1 = CSD_Tab[0] & 0x03;
|
||||
|
||||
/*!< Byte 1 */
|
||||
SD_csd->TAAC = CSD_Tab[1];
|
||||
|
||||
/*!< Byte 2 */
|
||||
SD_csd->NSAC = CSD_Tab[2];
|
||||
|
||||
/*!< Byte 3 */
|
||||
SD_csd->MaxBusClkFrec = CSD_Tab[3];
|
||||
|
||||
/*!< Byte 4 */
|
||||
SD_csd->CardComdClasses = CSD_Tab[4] << 4;
|
||||
|
||||
/*!< Byte 5 */
|
||||
SD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
|
||||
SD_csd->RdBlockLen = CSD_Tab[5] & 0x0F;
|
||||
|
||||
/*!< Byte 6 */
|
||||
SD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
|
||||
SD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
|
||||
SD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
|
||||
SD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
|
||||
SD_csd->Reserved2 = 0; /*!< Reserved */
|
||||
|
||||
SD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10;
|
||||
|
||||
/*!< Byte 7 */
|
||||
SD_csd->DeviceSize |= (CSD_Tab[7]) << 2;
|
||||
|
||||
/*!< Byte 8 */
|
||||
SD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
|
||||
|
||||
SD_csd->MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
|
||||
SD_csd->MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
|
||||
|
||||
/*!< Byte 9 */
|
||||
SD_csd->MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
|
||||
SD_csd->MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
|
||||
SD_csd->DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
|
||||
/*!< Byte 10 */
|
||||
SD_csd->DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
|
||||
|
||||
SD_csd->EraseGrSize = (CSD_Tab[10] & 0x40) >> 6;
|
||||
SD_csd->EraseGrMul = (CSD_Tab[10] & 0x3F) << 1;
|
||||
|
||||
/*!< Byte 11 */
|
||||
SD_csd->EraseGrMul |= (CSD_Tab[11] & 0x80) >> 7;
|
||||
SD_csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
|
||||
|
||||
/*!< Byte 12 */
|
||||
SD_csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
|
||||
SD_csd->ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
|
||||
SD_csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
|
||||
SD_csd->MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
|
||||
|
||||
/*!< Byte 13 */
|
||||
SD_csd->MaxWrBlockLen |= (CSD_Tab[13] & 0xC0) >> 6;
|
||||
SD_csd->WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
|
||||
SD_csd->Reserved3 = 0;
|
||||
SD_csd->ContentProtectAppli = (CSD_Tab[13] & 0x01);
|
||||
|
||||
/*!< Byte 14 */
|
||||
SD_csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
|
||||
SD_csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
|
||||
SD_csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
|
||||
SD_csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
|
||||
SD_csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
|
||||
SD_csd->ECC = (CSD_Tab[14] & 0x03);
|
||||
|
||||
/*!< Byte 15 */
|
||||
SD_csd->CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1;
|
||||
SD_csd->Reserved4 = 1;
|
||||
|
||||
/*!< Return the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read the CID card register.
|
||||
* Reading the contents of the CID register in SPI mode is a simple
|
||||
* read-block transaction.
|
||||
* @param SD_cid: pointer on an CID register structure
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_GetCIDRegister(SD_CID* SD_cid)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
SD_Error rvalue = SD_RESPONSE_FAILURE;
|
||||
uint8_t CID_Tab[16];
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD10 (CID register) */
|
||||
SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF);
|
||||
|
||||
/*!< Wait for response in the R1 format (0x00 is no errors) */
|
||||
if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
|
||||
{
|
||||
if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
|
||||
{
|
||||
/*!< Store CID register value on CID_Tab */
|
||||
for (i = 0; i < 16; i++)
|
||||
{
|
||||
CID_Tab[i] = SD_ReadByte();
|
||||
}
|
||||
}
|
||||
/*!< Get CRC bytes (not really needed by us, but required by SD) */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
/*!< Set response value to success */
|
||||
rvalue = SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
/*!< Send dummy byte: 8 Clock pulses of delay */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Byte 0 */
|
||||
SD_cid->ManufacturerID = CID_Tab[0];
|
||||
|
||||
/*!< Byte 1 */
|
||||
SD_cid->OEM_AppliID = CID_Tab[1] << 8;
|
||||
|
||||
/*!< Byte 2 */
|
||||
SD_cid->OEM_AppliID |= CID_Tab[2];
|
||||
|
||||
/*!< Byte 3 */
|
||||
SD_cid->ProdName1 = CID_Tab[3] << 24;
|
||||
|
||||
/*!< Byte 4 */
|
||||
SD_cid->ProdName1 |= CID_Tab[4] << 16;
|
||||
|
||||
/*!< Byte 5 */
|
||||
SD_cid->ProdName1 |= CID_Tab[5] << 8;
|
||||
|
||||
/*!< Byte 6 */
|
||||
SD_cid->ProdName1 |= CID_Tab[6];
|
||||
|
||||
/*!< Byte 7 */
|
||||
SD_cid->ProdName2 = CID_Tab[7];
|
||||
|
||||
/*!< Byte 8 */
|
||||
SD_cid->ProdRev = CID_Tab[8];
|
||||
|
||||
/*!< Byte 9 */
|
||||
SD_cid->ProdSN = CID_Tab[9] << 24;
|
||||
|
||||
/*!< Byte 10 */
|
||||
SD_cid->ProdSN |= CID_Tab[10] << 16;
|
||||
|
||||
/*!< Byte 11 */
|
||||
SD_cid->ProdSN |= CID_Tab[11] << 8;
|
||||
|
||||
/*!< Byte 12 */
|
||||
SD_cid->ProdSN |= CID_Tab[12];
|
||||
|
||||
/*!< Byte 13 */
|
||||
SD_cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
|
||||
SD_cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
|
||||
|
||||
/*!< Byte 14 */
|
||||
SD_cid->ManufactDate |= CID_Tab[14];
|
||||
|
||||
/*!< Byte 15 */
|
||||
SD_cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
|
||||
SD_cid->Reserved2 = 1;
|
||||
|
||||
/*!< Return the reponse */
|
||||
return rvalue;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Send 5 bytes command to the SD card.
|
||||
* @param Cmd: The user expected command to send to SD card.
|
||||
* @param Arg: The command argument.
|
||||
* @param Crc: The CRC.
|
||||
* @retval None
|
||||
*/
|
||||
void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc)
|
||||
{
|
||||
uint32_t i = 0x00;
|
||||
|
||||
uint8_t Frame[6];
|
||||
|
||||
Frame[0] = (Cmd | 0x40); /*!< Construct byte 1 */
|
||||
|
||||
Frame[1] = (uint8_t)(Arg >> 24); /*!< Construct byte 2 */
|
||||
|
||||
Frame[2] = (uint8_t)(Arg >> 16); /*!< Construct byte 3 */
|
||||
|
||||
Frame[3] = (uint8_t)(Arg >> 8); /*!< Construct byte 4 */
|
||||
|
||||
Frame[4] = (uint8_t)(Arg); /*!< Construct byte 5 */
|
||||
|
||||
Frame[5] = (Crc); /*!< Construct CRC: byte 6 */
|
||||
|
||||
for (i = 0; i < 6; i++)
|
||||
{
|
||||
SD_WriteByte(Frame[i]); /*!< Send the Cmd bytes */
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get SD card data response.
|
||||
* @param None
|
||||
* @retval The SD status: Read data response xxx0<status>1
|
||||
* - status 010: Data accepted
|
||||
* - status 101: Data rejected due to a crc error
|
||||
* - status 110: Data rejected due to a Write error.
|
||||
* - status 111: Data rejected due to other error.
|
||||
*/
|
||||
uint8_t SD_GetDataResponse(void)
|
||||
{
|
||||
uint32_t i = 0;
|
||||
uint8_t response, rvalue;
|
||||
|
||||
while (i <= 64)
|
||||
{
|
||||
/*!< Read response */
|
||||
response = SD_ReadByte();
|
||||
/*!< Mask unused bits */
|
||||
response &= 0x1F;
|
||||
switch (response)
|
||||
{
|
||||
case SD_DATA_OK:
|
||||
{
|
||||
rvalue = SD_DATA_OK;
|
||||
break;
|
||||
}
|
||||
case SD_DATA_CRC_ERROR:
|
||||
return SD_DATA_CRC_ERROR;
|
||||
case SD_DATA_WRITE_ERROR:
|
||||
return SD_DATA_WRITE_ERROR;
|
||||
default:
|
||||
{
|
||||
rvalue = SD_DATA_OTHER_ERROR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
/*!< Exit loop in case of data ok */
|
||||
if (rvalue == SD_DATA_OK)
|
||||
break;
|
||||
/*!< Increment loop counter */
|
||||
i++;
|
||||
}
|
||||
|
||||
/*!< Wait null data */
|
||||
while (SD_ReadByte() == 0);
|
||||
|
||||
/*!< Return response */
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the SD response.
|
||||
* @param None
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_GetResponse(uint8_t Response)
|
||||
{
|
||||
uint32_t Count = 0xFFF;
|
||||
|
||||
/* Check if response is got or a timeout is happen */
|
||||
while ((SD_ReadByte() != Response) && Count)
|
||||
{
|
||||
Count--;
|
||||
}
|
||||
|
||||
if (Count == 0)
|
||||
{
|
||||
/* After time out */
|
||||
return SD_RESPONSE_FAILURE;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Right response got */
|
||||
return SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the SD status.
|
||||
* @param None
|
||||
* @retval The SD status.
|
||||
*/
|
||||
uint16_t SD_GetStatus(void)
|
||||
{
|
||||
uint16_t Status = 0;
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD13 (SD_SEND_STATUS) to get SD status */
|
||||
SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF);
|
||||
|
||||
Status = SD_ReadByte();
|
||||
Status |= (uint16_t)(SD_ReadByte() << 8);
|
||||
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
|
||||
/*!< Send dummy byte 0xFF */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
return Status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Put SD in Idle state.
|
||||
* @param None
|
||||
* @retval The SD Response:
|
||||
* - SD_RESPONSE_FAILURE: Sequence failed
|
||||
* - SD_RESPONSE_NO_ERROR: Sequence succeed
|
||||
*/
|
||||
SD_Error SD_GoIdleState(void)
|
||||
{
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode */
|
||||
SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95);
|
||||
|
||||
/*!< Wait for In Idle State Response (R1 Format) equal to 0x01 */
|
||||
if (SD_GetResponse(SD_IN_IDLE_STATE))
|
||||
{
|
||||
/*!< No Idle State Response: return response failue */
|
||||
return SD_RESPONSE_FAILURE;
|
||||
}
|
||||
|
||||
// SD_SendCmd(SD_CMD_SEND_IF_COND, 0, 0x65);
|
||||
// /*!< Wait for In Idle State Response (R1 Format) equal to 0x01 */
|
||||
// if (SD_GetResponse(SD_IN_IDLE_STATE))
|
||||
// {
|
||||
// /*!< No Idle State Response: return response failue */
|
||||
// return SD_RESPONSE_FAILURE;
|
||||
// }
|
||||
|
||||
|
||||
/*----------Activates the card initialization process-----------*/
|
||||
do
|
||||
{
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
|
||||
/*!< Send Dummy byte 0xFF */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
/*!< SD chip select low */
|
||||
SD_CS_LOW();
|
||||
|
||||
/*!< Send CMD1 (Activates the card process) until response equal to 0x0 */
|
||||
SD_SendCmd(SD_CMD_SEND_OP_COND, 0, 0xFF);
|
||||
/*!< Wait for no error Response (R1 Format) equal to 0x00 */
|
||||
}
|
||||
while (SD_GetResponse(SD_RESPONSE_NO_ERROR));
|
||||
|
||||
/*!< SD chip select high */
|
||||
SD_CS_HIGH();
|
||||
|
||||
/*!< Send dummy byte 0xFF */
|
||||
SD_WriteByte(SD_DUMMY_BYTE);
|
||||
|
||||
return SD_RESPONSE_NO_ERROR;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Write a byte on the SD.
|
||||
* @param Data: byte to send.
|
||||
* @retval None
|
||||
*/
|
||||
uint8_t SD_WriteByte(uint8_t Data)
|
||||
{
|
||||
/*!< Wait until the transmit buffer is empty */
|
||||
while(SPI_I2S_GetFlagStatus(SD_SPI_INSTANCE, SPI_I2S_FLAG_TXE) == RESET)
|
||||
{
|
||||
}
|
||||
|
||||
/*!< Send the byte */
|
||||
SPI_SendData8(SD_SPI_INSTANCE, Data);
|
||||
|
||||
/*!< Wait to receive a byte*/
|
||||
while(SPI_I2S_GetFlagStatus(SD_SPI_INSTANCE, SPI_I2S_FLAG_RXNE) == RESET)
|
||||
{
|
||||
}
|
||||
|
||||
/*!< Return the byte read from the SPI bus */
|
||||
return SPI_ReceiveData8(SD_SPI_INSTANCE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Read a byte from the SD.
|
||||
* @param None
|
||||
* @retval The received byte.
|
||||
*/
|
||||
uint8_t SD_ReadByte(void)
|
||||
{
|
||||
uint8_t Data = 0;
|
||||
|
||||
/*!< Wait until the transmit buffer is empty */
|
||||
while (SPI_I2S_GetFlagStatus(SD_SPI_INSTANCE, SPI_I2S_FLAG_TXE) == RESET)
|
||||
{
|
||||
}
|
||||
/*!< Send the byte */
|
||||
SPI_SendData8(SD_SPI_INSTANCE, SD_DUMMY_BYTE);
|
||||
|
||||
/*!< Wait until a data is received */
|
||||
while (SPI_I2S_GetFlagStatus(SD_SPI_INSTANCE, SPI_I2S_FLAG_RXNE) == RESET)
|
||||
{
|
||||
}
|
||||
/*!< Get the received data */
|
||||
Data = SPI_ReceiveData8(SD_SPI_INSTANCE);
|
||||
|
||||
/*!< Return the shifted data */
|
||||
return Data;
|
||||
}
|
||||
#include "drivers/bus_spi.h"
|
||||
#include "drivers/system.h"
|
||||
|
||||
|
@ -852,12 +43,15 @@ uint8_t SD_ReadByte(void)
|
|||
#define STATIC_ASSERT(condition, name ) \
|
||||
typedef char assert_failed_ ## name [(condition) ? 1 : -1 ]
|
||||
|
||||
#define SDCARD_USE_DMA_FOR_TX
|
||||
|
||||
typedef enum {
|
||||
SDCARD_STATE_NOT_PRESENT = 0,
|
||||
SDCARD_STATE_INITIALIZATION,
|
||||
SDCARD_STATE_INITIALIZATION_RECEIVE_CID,
|
||||
SDCARD_STATE_READY,
|
||||
SDCARD_STATE_READING,
|
||||
SDCARD_STATE_SENDING_WRITE,
|
||||
SDCARD_STATE_WRITING,
|
||||
} sdcardState_e;
|
||||
|
||||
|
@ -1068,19 +262,8 @@ static sdcardReceiveBlockStatus_e sdcard_receiveDataBlock(uint8_t *buffer, int c
|
|||
return SDCARD_RECEIVE_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the buffer of `count` bytes to the SD card.
|
||||
*
|
||||
* Returns true if the card accepted the write (card will enter a busy state).
|
||||
*/
|
||||
static bool sdcard_sendDataBlock(uint8_t *buffer, int count)
|
||||
static bool sdcard_sendDataBlockFinish()
|
||||
{
|
||||
// Card wants 8 dummy clock cycles after the command response to become ready
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, 0xFF);
|
||||
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, SDCARD_SINGLE_BLOCK_WRITE_START_TOKEN);
|
||||
spiTransfer(SDCARD_SPI_INSTANCE, NULL, buffer, count);
|
||||
|
||||
// Send a dummy CRC
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, 0x00);
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, 0x00);
|
||||
|
@ -1102,6 +285,51 @@ static bool sdcard_sendDataBlock(uint8_t *buffer, int count)
|
|||
return (dataResponseToken & 0x1F) == 0x05;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the buffer of `count` bytes to the SD card.
|
||||
*
|
||||
* Returns true if the write was begun (card will enter a busy state).
|
||||
*/
|
||||
static bool sdcard_sendDataBlock(uint8_t *buffer, int count)
|
||||
{
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, SDCARD_SINGLE_BLOCK_WRITE_START_TOKEN);
|
||||
|
||||
#ifdef SDCARD_USE_DMA_FOR_TX
|
||||
// Queue the transmission of the sector payload
|
||||
DMA_InitTypeDef DMA_InitStructure;
|
||||
|
||||
DMA_StructInit(&DMA_InitStructure);
|
||||
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t) &SDCARD_SPI_INSTANCE->DR;
|
||||
DMA_InitStructure.DMA_Priority = DMA_Priority_Low;
|
||||
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
|
||||
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
|
||||
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
|
||||
|
||||
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
|
||||
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t) buffer;
|
||||
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
|
||||
|
||||
DMA_InitStructure.DMA_BufferSize = count;
|
||||
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;
|
||||
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
|
||||
|
||||
DMA_DeInit(SDCARD_DMA_CHANNEL_TX);
|
||||
DMA_Init(SDCARD_DMA_CHANNEL_TX, &DMA_InitStructure);
|
||||
|
||||
DMA_Cmd(SDCARD_DMA_CHANNEL_TX, ENABLE);
|
||||
|
||||
SPI_I2S_DMACmd(SDCARD_SPI_INSTANCE, SPI_I2S_DMAReq_Tx, ENABLE);
|
||||
|
||||
return true;
|
||||
#else
|
||||
// Send the sector payload now
|
||||
spiTransfer(SDCARD_SPI_INSTANCE, NULL, buffer, count);
|
||||
|
||||
// And check the SD card's acknowledgement
|
||||
return sdcard_sendDataBlockFinish();
|
||||
#endif
|
||||
}
|
||||
|
||||
static bool sdcard_receiveCID()
|
||||
{
|
||||
uint8_t cid[16];
|
||||
|
@ -1297,12 +525,47 @@ void sdcard_poll()
|
|||
}
|
||||
}
|
||||
break;
|
||||
case SDCARD_STATE_SENDING_WRITE:
|
||||
// Has the DMA write finished yet?
|
||||
if (DMA_GetFlagStatus(SDCARD_DMA_CHANNEL_TX_COMPLETE_FLAG) == SET) {
|
||||
DMA_ClearFlag(SDCARD_DMA_CHANNEL_TX_COMPLETE_FLAG);
|
||||
|
||||
DMA_Cmd(SDCARD_DMA_CHANNEL_TX, DISABLE);
|
||||
|
||||
// Drain anything left in the Rx FIFO (we didn't read it during the write)
|
||||
while (SPI_I2S_GetFlagStatus(SDCARD_SPI_INSTANCE, SPI_I2S_FLAG_RXNE) == SET) {
|
||||
SDCARD_SPI_INSTANCE->DR;
|
||||
}
|
||||
|
||||
// Wait for the final bit to be transmitted
|
||||
while (spiIsBusBusy(SDCARD_SPI_INSTANCE)) {
|
||||
}
|
||||
|
||||
SPI_I2S_DMACmd(SDCARD_SPI_INSTANCE, SPI_I2S_DMAReq_Tx, DISABLE);
|
||||
|
||||
// Finish up by sending the CRC and checking the SD-card's acceptance/rejectance
|
||||
if (sdcard_sendDataBlockFinish()) {
|
||||
// The SD card is now busy committing that write to the card
|
||||
sdcard.state = SDCARD_STATE_WRITING;
|
||||
|
||||
// Since we've transmitted the buffer we may as well go ahead and tell the caller their operation is complete
|
||||
if (sdcard.pendingOperation.callback) {
|
||||
sdcard.pendingOperation.callback(SDCARD_BLOCK_OPERATION_WRITE, sdcard.pendingOperation.blockIndex, sdcard.pendingOperation.buffer, sdcard.pendingOperation.callbackData);
|
||||
}
|
||||
} else {
|
||||
// Our write was rejected! Bad CRC/address?
|
||||
sdcard.state = SDCARD_STATE_READY;
|
||||
if (sdcard.pendingOperation.callback) {
|
||||
sdcard.pendingOperation.callback(SDCARD_BLOCK_OPERATION_WRITE, sdcard.pendingOperation.blockIndex, NULL, sdcard.pendingOperation.callbackData);
|
||||
}
|
||||
}
|
||||
}
|
||||
break;
|
||||
case SDCARD_STATE_WRITING:
|
||||
if (sdcard_waitForIdle(SDCARD_MAXIMUM_BYTE_DELAY_FOR_CMD_REPLY)) {
|
||||
sdcard_deselect();
|
||||
sdcard.state = SDCARD_STATE_READY;
|
||||
}
|
||||
|
||||
break;
|
||||
case SDCARD_STATE_READING:
|
||||
switch (sdcard_receiveDataBlock(sdcard.pendingOperation.buffer, SDCARD_BLOCK_SIZE)) {
|
||||
|
@ -1347,12 +610,12 @@ void sdcard_poll()
|
|||
/**
|
||||
* Write the 512-byte block from the given buffer into the block with the given index.
|
||||
*
|
||||
* Returns true if the write was successfully sent to the card, or false if the operation could
|
||||
* Returns true if the write was successfully sent to the card for later commit, or false if the operation could
|
||||
* not be started due to the card being busy (try again later), or because the write was invalid (bad address).
|
||||
*
|
||||
* The buffer is not copied anywhere, you must keep the pointer to the buffer valid until the operation completes!
|
||||
*/
|
||||
bool sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer)
|
||||
bool sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData)
|
||||
{
|
||||
if (sdcard.state != SDCARD_STATE_READY)
|
||||
return false;
|
||||
|
@ -1362,8 +625,29 @@ bool sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer)
|
|||
// Standard size cards use byte addressing, high capacity cards use block addressing
|
||||
uint8_t status = sdcard_sendCommand(SDCARD_COMMAND_WRITE_BLOCK, sdcard.highCapacity ? blockIndex : blockIndex * SDCARD_BLOCK_SIZE);
|
||||
|
||||
// Card wants 8 dummy clock cycles after the command response to become ready
|
||||
spiTransferByte(SDCARD_SPI_INSTANCE, 0xFF);
|
||||
|
||||
#ifdef SDCARD_USE_DMA_FOR_TX
|
||||
sdcard.pendingOperation.buffer = buffer;
|
||||
sdcard.pendingOperation.blockIndex = blockIndex;
|
||||
sdcard.pendingOperation.callback = callback;
|
||||
sdcard.pendingOperation.callbackData = callbackData;
|
||||
#endif
|
||||
|
||||
if (status == 0 && sdcard_sendDataBlock(buffer, SDCARD_BLOCK_SIZE)) {
|
||||
|
||||
#ifdef SDCARD_USE_DMA_FOR_TX
|
||||
sdcard.state = SDCARD_STATE_SENDING_WRITE;
|
||||
#else
|
||||
/* The data has already been received by the SD card (buffer has been transmitted), we only have to wait for the
|
||||
* card to commit it. Let the caller know it can free its buffer.
|
||||
*/
|
||||
sdcard.state = SDCARD_STATE_WRITING;
|
||||
if (callback) {
|
||||
callback(SDCARD_BLOCK_OPERATION_WRITE, blockIndex, buffer, callbackData);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Leave the card selected while the write is in progress
|
||||
return true;
|
||||
|
@ -1413,4 +697,3 @@ bool sdcard_isReady() {
|
|||
}
|
||||
|
||||
#endif
|
||||
>>>>>>> 98133e5... Basic SDCard block read / write (minimal timeout/error handling)
|
||||
|
|
|
@ -243,13 +243,6 @@ typedef struct sdcard_metadata_t {
|
|||
uint32_t numBlocks; /* Card capacity in 512-byte blocks*/
|
||||
} sdcardMetadata_t;
|
||||
|
||||
typedef enum {
|
||||
SDCARD_NO_OPERATION,
|
||||
SDCARD_OPERATION_IN_PROGRESS,
|
||||
SDCARD_OPERATION_SUCCESS,
|
||||
SDCARD_OPERATION_ERROR,
|
||||
} sdcardOperationStatus_e;
|
||||
|
||||
typedef enum {
|
||||
SDCARD_BLOCK_OPERATION_READ,
|
||||
SDCARD_BLOCK_OPERATION_WRITE,
|
||||
|
@ -260,8 +253,8 @@ typedef void(*sdcard_operationCompleteCallback_c)(sdcardBlockOperation_e operati
|
|||
|
||||
bool sdcard_init();
|
||||
|
||||
bool sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer);
|
||||
bool sdcard_readBlock(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
|
||||
bool sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
|
||||
|
||||
void sdcard_poll();
|
||||
bool sdcard_isReady();
|
||||
|
|
|
@ -80,8 +80,9 @@
|
|||
|
||||
typedef enum {
|
||||
AFATFS_CACHE_STATE_EMPTY,
|
||||
AFATFS_CACHE_STATE_READING,
|
||||
AFATFS_CACHE_STATE_IN_SYNC,
|
||||
AFATFS_CACHE_STATE_READING,
|
||||
AFATFS_CACHE_STATE_WRITING,
|
||||
AFATFS_CACHE_STATE_DIRTY
|
||||
} afatfsCacheBlockState_e;
|
||||
|
||||
|
@ -354,7 +355,7 @@ typedef struct afatfs_t {
|
|||
uint8_t cache[AFATFS_SECTOR_SIZE * AFATFS_NUM_CACHE_SECTORS];
|
||||
afatfsCacheBlockDescriptor_t cacheDescriptor[AFATFS_NUM_CACHE_SECTORS];
|
||||
uint32_t cacheTimer;
|
||||
int cacheDirtyEntries; // The number of cache entries in the AFATFS_CACHE_STATE_DIRTY state
|
||||
int cacheUnflushedEntries; // The number of cache entries in the AFATFS_CACHE_STATE_DIRTY or AFATFS_CACHE_STATE_WRITING states
|
||||
|
||||
afatfsFile_t openFiles[AFATFS_MAX_OPEN_FILES];
|
||||
|
||||
|
@ -508,7 +509,7 @@ static void afatfs_cacheSectorMarkDirty(uint8_t *memory)
|
|||
|
||||
if (descriptor && descriptor->state != AFATFS_CACHE_STATE_DIRTY) {
|
||||
descriptor->state = AFATFS_CACHE_STATE_DIRTY;
|
||||
afatfs.cacheDirtyEntries++;
|
||||
afatfs.cacheUnflushedEntries++;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -521,23 +522,6 @@ static void afatfs_cacheSectorInit(afatfsCacheBlockDescriptor_t *descriptor, uin
|
|||
descriptor->state = AFATFS_CACHE_STATE_EMPTY;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to flush the dirty cache entry with the given index to the SDcard. Returns true if the SDcard accepted
|
||||
* the write, false if the card was busy.
|
||||
*/
|
||||
static bool afatfs_cacheFlushSector(int cacheIndex)
|
||||
{
|
||||
if (afatfs.cacheDescriptor[cacheIndex].state != AFATFS_CACHE_STATE_DIRTY)
|
||||
return true; // Already flushed
|
||||
|
||||
if (sdcard_writeBlock(afatfs.cacheDescriptor[cacheIndex].sectorIndex, afatfs_cacheSectorGetMemory(cacheIndex))) {
|
||||
afatfs.cacheDescriptor[cacheIndex].state = AFATFS_CACHE_STATE_IN_SYNC;
|
||||
afatfs.cacheDirtyEntries--;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the SD card driver when one of our read operations completes.
|
||||
*/
|
||||
|
@ -564,6 +548,52 @@ static void afatfs_sdcardReadComplete(sdcardBlockOperation_e operation, uint32_t
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Called by the SD card driver when one of our write operations completes.
|
||||
*/
|
||||
static void afatfs_sdcardWriteComplete(sdcardBlockOperation_e operation, uint32_t sectorIndex, uint8_t *buffer, uint32_t callbackData)
|
||||
{
|
||||
(void) operation;
|
||||
(void) callbackData;
|
||||
|
||||
for (int i = 0; i < AFATFS_NUM_CACHE_SECTORS; i++) {
|
||||
/* Keep in mind that someone may have marked the sector as dirty after writing had already begun. In this case we must leave
|
||||
* it marked as dirty because those modifications may have been made too late to make it to the disk!
|
||||
*/
|
||||
if (afatfs.cacheDescriptor[i].sectorIndex == sectorIndex
|
||||
&& afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_WRITING
|
||||
) {
|
||||
if (buffer == NULL) {
|
||||
// Write failed, remark the sector as dirty
|
||||
afatfs.cacheDescriptor[i].state = AFATFS_CACHE_STATE_DIRTY;
|
||||
} else {
|
||||
afatfs_assert(afatfs_cacheSectorGetMemory(i) == buffer);
|
||||
|
||||
afatfs.cacheUnflushedEntries--;
|
||||
|
||||
afatfs.cacheDescriptor[i].state = AFATFS_CACHE_STATE_IN_SYNC;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to flush the dirty cache entry with the given index to the SDcard. Returns true if the SDcard accepted
|
||||
* the write, false if the card was busy.
|
||||
*/
|
||||
static bool afatfs_cacheFlushSector(int cacheIndex)
|
||||
{
|
||||
if (afatfs.cacheDescriptor[cacheIndex].state != AFATFS_CACHE_STATE_DIRTY)
|
||||
return true; // Already flushed
|
||||
|
||||
if (sdcard_writeBlock(afatfs.cacheDescriptor[cacheIndex].sectorIndex, afatfs_cacheSectorGetMemory(cacheIndex), afatfs_sdcardWriteComplete, 0)) {
|
||||
afatfs.cacheDescriptor[cacheIndex].state = AFATFS_CACHE_STATE_WRITING;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Find a sector in the cache which corresponds to the given physical sector index, or NULL if the sector isn't
|
||||
* cached. Note that the cached sector could be in any state including completely empty.
|
||||
|
@ -665,7 +695,7 @@ static int afatfs_allocateCacheSector(uint32_t sectorIndex)
|
|||
*/
|
||||
bool afatfs_flush()
|
||||
{
|
||||
if (afatfs.cacheDirtyEntries > 0) {
|
||||
if (afatfs.cacheUnflushedEntries > 0) {
|
||||
for (int i = 0; i < AFATFS_NUM_CACHE_SECTORS; i++) {
|
||||
if (afatfs.cacheDescriptor[i].state == AFATFS_CACHE_STATE_DIRTY && !afatfs.cacheDescriptor[i].locked) {
|
||||
afatfs_cacheFlushSector(i);
|
||||
|
@ -762,10 +792,11 @@ static void afatfs_fileGetCursorClusterAndSector(afatfsFilePtr_t file, uint32_t
|
|||
|
||||
// Fall through
|
||||
|
||||
case AFATFS_CACHE_STATE_WRITING:
|
||||
case AFATFS_CACHE_STATE_IN_SYNC:
|
||||
if ((sectorFlags & AFATFS_CACHE_WRITE) != 0) {
|
||||
afatfs.cacheDescriptor[cacheSectorIndex].state = AFATFS_CACHE_STATE_DIRTY;
|
||||
afatfs.cacheDirtyEntries++;
|
||||
afatfs.cacheUnflushedEntries++;
|
||||
}
|
||||
// Fall through
|
||||
|
||||
|
|
|
@ -105,6 +105,10 @@
|
|||
// Divide to under 25MHz for normal operation:
|
||||
#define SDCARD_SPI_FULL_SPEED_CLOCK_DIVIDER 2
|
||||
|
||||
// Note, this is the same DMA channel as USART1_RX. Luckily we don't use DMA for USART Rx.
|
||||
#define SDCARD_DMA_CHANNEL_TX DMA1_Channel5
|
||||
#define SDCARD_DMA_CHANNEL_TX_COMPLETE_FLAG DMA1_FLAG_TC5
|
||||
|
||||
#define ACC
|
||||
#define USE_ACC_LSM303DLHC
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue