STM8S/A Standard Peripherals Firmware Library: STM8S_EVAL_SPI_SD_Private_Functions

STM8S/A

STM8S/A Standard Peripherals Firmware Library
STM8S_EVAL_SPI_SD_Private_Functions

Functions

void SD_DeInit (void)
 DeInitializes the SD/SD communication.
uint8_t SD_Detect (void)
 Detect if SD card is correctly plugged in the memory slot.
uint8_t SD_GetCardInfo (SD_CardInfo *cardinfo)
 Returns information about specific card.
uint8_t SD_GetCIDRegister (SD_CID *SD_cid)
 Read the CID card register.
uint8_t SD_GetCSDRegister (SD_CSD *SD_csd)
 Read the CSD card register.
uint8_t SD_GetDataResponse (void)
 Get SD card data response.
uint8_t SD_GetResponse (uint8_t Response)
 Returns the SD response.
uint16_t SD_GetStatus (void)
 Returns the SD status.
uint8_t SD_GoIdleState (void)
 Put SD in Idle state.
uint8_t SD_Init (void)
 Initializes the SD/SD communication.
uint8_t SD_ReadBlock (uint8_t *pBuffer, uint32_t ReadAddr, uint16_t BlockSize)
 Reads a block of data from the SD.
uint8_t SD_ReadBuffer (uint8_t *pBuffer, uint32_t ReadAddr, uint32_t NumByteToRead)
 Read a buffer (many blocks) from the SD card.
uint8_t SD_ReadByte (void)
 Read a byte from the SD.
void SD_SendCmd (uint8_t Cmd, uint32_t Arg, uint8_t Crc)
 Send 5 bytes command to the SD card.
uint8_t SD_WriteBlock (uint8_t *pBuffer, uint32_t WriteAddr, uint16_t BlockSize)
 Writes a block on the SD.
uint8_t SD_WriteBuffer (uint8_t *pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite)
 Write a buffer (many blocks) in the SD card.
uint8_t SD_WriteByte (uint8_t Data)
 Write a byte on the SD.

Function Documentation

void SD_DeInit ( void  )

DeInitializes the SD/SD communication.

Parameters:
None
Return values:
None

Definition at line 78 of file stm8s_eval_spi_sd.c.

References SD_LowLevel_DeInit().

uint8_t SD_Detect ( void  )

Detect if SD card is correctly plugged in the memory slot.

Parameters:
None
Return values:
Returnif SD is detected or not

< Check GPIO to detect SD

Definition at line 117 of file stm8s_eval_spi_sd.c.

References __IO, GPIO_ReadInputData(), SD_DETECT_GPIO_PORT, SD_DETECT_PIN, SD_NOT_PRESENT, and SD_PRESENT.

Referenced by main().

uint8_t SD_GetCardInfo ( SD_CardInfo cardinfo)

Returns information about specific card.

Parameters:
cardinfo,:pointer to a SD_CardInfo structure that contains all SD card information.
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< Returns the response

Definition at line 137 of file stm8s_eval_spi_sd.c.

References SD_CardInfo::CardBlockSize, SD_CardInfo::CardCapacity, SD_CSD::DeviceSize, SD_CSD::DeviceSizeMul, SD_CSD::RdBlockLen, SD_CardInfo::SD_cid, SD_CardInfo::SD_csd, SD_GetCIDRegister(), SD_GetCSDRegister(), and SD_RESPONSE_FAILURE.

uint8_t SD_GetCIDRegister ( SD_CID SD_cid)

Read the CID card register.

Reading the contents of the CID register in SPI mode is a simple read-block transaction.

Parameters:
SD_cid,:pointer on an CID register structure
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< SD chip select low

< Send CMD10 (CID register)

< Wait for response in the R1 format (0x00 is no errors)

< Store CID register value on CID_Tab

< Set response value to success

< Send dummy byte: 8 Clock pulses of delay

< Byte 0

< Byte 1

< Byte 2

< Byte 3

< Byte 4

< Byte 5

< Byte 6

< Byte 7

< Byte 8

< Byte 9

< Byte 10

< Byte 11

< Byte 12

< Byte 13

< Byte 14

< Byte 15

< Return the response

Definition at line 538 of file stm8s_eval_spi_sd.c.

References SD_CID::CID_CRC, i, SD_CID::ManufactDate, SD_CID::ManufacturerID, SD_CID::OEM_AppliID, SD_CID::ProdName1, SD_CID::ProdName2, SD_CID::ProdRev, SD_CID::ProdSN, SD_CID::Reserved1, SD_CID::Reserved2, SD_CMD_SEND_CID, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), SD_START_DATA_SINGLE_BLOCK_READ, and SD_WriteByte().

Referenced by SD_GetCardInfo().

uint8_t SD_GetCSDRegister ( SD_CSD SD_csd)

Read the CSD card register.

Reading the contents of the CSD register in SPI mode is a simple read-block transaction.

Parameters:
SD_csd,:pointer on an SCD register structure
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< SD chip select low

< Send CMD9 (CSD register) or CMD10(CSD register)

< Wait for response in the R1 format (0x00 is no errors)

< Store CSD register value on CSD_Tab

< Set response value to success

< Send dummy byte: 8 Clock pulses of delay

< Byte 0

< Byte 1

< Byte 2

< Byte 3

< Byte 4

< Byte 5

< Byte 6

< Reserved

< Byte 7

< Byte 8

< Byte 9

< Byte 10

< Byte 11

< Byte 12

< Byte 13

< Byte 14

< Byte 15

< Return the response

Definition at line 416 of file stm8s_eval_spi_sd.c.

References SD_CSD::CardComdClasses, SD_CSD::ContentProtectAppli, SD_CSD::CopyFlag, SD_CSD::CSD_CRC, SD_CSD::CSDStruct, SD_CSD::DeviceSize, SD_CSD::DeviceSizeMul, SD_CSD::DSRImpl, SD_CSD::ECC, SD_CSD::EraseGrMul, SD_CSD::EraseGrSize, SD_CSD::FileFormat, SD_CSD::FileFormatGrouop, i, SD_CSD::ManDeflECC, SD_CSD::MaxBusClkFrec, SD_CSD::MaxRdCurrentVDDMax, SD_CSD::MaxRdCurrentVDDMin, SD_CSD::MaxWrBlockLen, SD_CSD::MaxWrCurrentVDDMax, SD_CSD::MaxWrCurrentVDDMin, SD_CSD::NSAC, SD_CSD::PartBlockRead, SD_CSD::PermWrProtect, SD_CSD::RdBlockLen, SD_CSD::RdBlockMisalign, SD_CSD::Reserved1, SD_CSD::Reserved2, SD_CSD::Reserved3, SD_CSD::Reserved4, SD_CMD_SEND_CSD, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), SD_START_DATA_SINGLE_BLOCK_READ, SD_WriteByte(), SD_CSD::SysSpecVersion, SD_CSD::TAAC, SD_CSD::TempWrProtect, SD_CSD::WrBlockMisalign, SD_CSD::WriteBlockPaPartial, SD_CSD::WrProtectGrEnable, SD_CSD::WrProtectGrSize, and SD_CSD::WrSpeedFact.

Referenced by SD_GetCardInfo().

uint8_t SD_GetDataResponse ( void  )

Get SD card data response.

Parameters:
None
Return values:
TheSD 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.

< Read response

< Mask unused bits

< Increment loop counter

< Return response

Definition at line 666 of file stm8s_eval_spi_sd.c.

References i, SD_DATA_CRC_ERROR, SD_DATA_OK, SD_DATA_OTHER_ERROR, SD_DATA_WRITE_ERROR, and SD_ReadByte().

Referenced by SD_WriteBlock(), and SD_WriteBuffer().

uint8_t SD_GetResponse ( uint8_t  Response)

Returns the SD response.

Parameters:
None
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< Check if response is got or a timeout is happen

< After time out

< Right response got

Definition at line 715 of file stm8s_eval_spi_sd.c.

References SD_ReadByte(), SD_RESPONSE_FAILURE, and SD_RESPONSE_NO_ERROR.

Referenced by SD_GetCIDRegister(), SD_GetCSDRegister(), SD_GoIdleState(), SD_ReadBlock(), SD_ReadBuffer(), SD_WriteBlock(), and SD_WriteBuffer().

uint16_t SD_GetStatus ( void  )

Returns the SD status.

Parameters:
None
Return values:
TheSD status.

< SD chip select low

< Send CMD13 (SD_SEND_STATUS) to get SD status

< SD chip select high

< Send dummy byte 0xFF

Definition at line 741 of file stm8s_eval_spi_sd.c.

References SD_CMD_SEND_STATUS, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_ReadByte(), SD_SendCmd(), SD_WriteByte(), and Status.

uint8_t SD_GoIdleState ( void  )

Put SD in Idle state.

Parameters:
None
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< SD chip select low

< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode

< Wait for In Idle State Response (R1 Format) equal to 0x01

< No Idle State Response: return response failure

< SD chip select high

< Send Dummy byte 0xFF

< SD chip select low

< Send CMD1 (Activates the card process) until response equal to 0x0

< Wait for no error Response (R1 Format) equal to 0x00

< SD chip select high

< Send dummy byte 0xFF

Definition at line 770 of file stm8s_eval_spi_sd.c.

References SD_CMD_GO_IDLE_STATE, SD_CMD_SEND_OP_COND, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_GetResponse(), SD_IN_IDLE_STATE, SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), and SD_WriteByte().

Referenced by SD_Init().

uint8_t SD_Init ( void  )

Initializes the SD/SD communication.

Parameters:
None
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< Initialize SD_SPI

< SD chip select high

< Send dummy byte 0xFF, 10 times with CS high

< Rise CS and MOSI for 80 clocks cycles

< Send dummy byte 0xFF

< SD initialized and set to SPI mode properly

Definition at line 90 of file stm8s_eval_spi_sd.c.

References i, SD_CS_LOW, SD_DUMMY_BYTE, SD_GoIdleState(), SD_LowLevel_Init(), and SD_WriteByte().

Referenced by main().

uint8_t SD_ReadBlock ( uint8_t pBuffer,
uint32_t  ReadAddr,
uint16_t  BlockSize 
)

Reads a block of data from the SD.

Parameters:
pBuffer,:pointer to the buffer that receives the data read from the SD.
ReadAddr,:SD's internal address to read from.
BlockSize,:the SD card Data block size.
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< SD chip select low

< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block

< Check if the SD acknowledged the read block command: R1 response (0x00: no errors)

< Now look for the data token to signify the start of the data

< Read the SD block data : read NumByteToRead data

< Save the received data

< Point to the next location where the byte read will be saved

< Set response value to success

< Send dummy byte: 8 Clock pulses of delay

< Returns the response

Definition at line 162 of file stm8s_eval_spi_sd.c.

References i, SD_CMD_READ_SINGLE_BLOCK, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), SD_START_DATA_SINGLE_BLOCK_READ, and SD_WriteByte().

Referenced by main().

uint8_t SD_ReadBuffer ( uint8_t pBuffer,
uint32_t  ReadAddr,
uint32_t  NumByteToRead 
)

Read a buffer (many blocks) from the SD card.

Parameters:
pBuffer: pointer to the buffer that receives the data read from the SD.
ReadAddr: SD's internal address to read from.
NumByteToRead: number of bytes to read from the SD.
Return values:
SDResponse:
  • SD_RESPONSE_FAILURE: Sequence failed.
  • SD_RESPONSE_NO_ERROR: Sequence succeed.

Definition at line 215 of file stm8s_eval_spi_sd.c.

References i, SD_BLOCK_SIZE, SD_CMD_READ_SINGLE_BLOCK, SD_CS_HIGH, SD_CS_LOW, SD_DUMMY_BYTE, SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), SD_START_DATA_SINGLE_BLOCK_READ, and SD_WriteByte().

uint8_t SD_ReadByte ( void  )

Read a byte from the SD.

Parameters:
None
Return values:
Thereceived byte.

< Wait until the transmit buffer is empty

< Wait until a data is received

< Return the shifted data

Definition at line 838 of file stm8s_eval_spi_sd.c.

References RESET, SD_DUMMY_BYTE, SPI_FLAG_RXNE, SPI_FLAG_TXE, SPI_GetFlagStatus(), SPI_ReceiveData(), and SPI_SendData().

Referenced by SD_GetCIDRegister(), SD_GetCSDRegister(), SD_GetDataResponse(), SD_GetResponse(), SD_GetStatus(), SD_ReadBlock(), SD_ReadBuffer(), SD_WriteBlock(), and SD_WriteBuffer().

void SD_SendCmd ( uint8_t  Cmd,
uint32_t  Arg,
uint8_t  Crc 
)

Send 5 bytes command to the SD card.

Parameters:
Cmd,:The user expected command to send to SD card.
Arg,:The command argument.
Crc,:The CRC.
Return values:
None

< Construct byte 1

< Construct byte 2

< Construct byte 3

< Construct byte 4

< Construct byte 5

< Construct CRC: byte 6

< Send the Cmd bytes

Definition at line 633 of file stm8s_eval_spi_sd.c.

References i, and SD_WriteByte().

Referenced by SD_GetCIDRegister(), SD_GetCSDRegister(), SD_GetStatus(), SD_GoIdleState(), SD_ReadBlock(), SD_ReadBuffer(), SD_WriteBlock(), and SD_WriteBuffer().

uint8_t SD_WriteBlock ( uint8_t pBuffer,
uint32_t  WriteAddr,
uint16_t  BlockSize 
)

Writes a block on the SD.

Parameters:
pBuffer,:pointer to the buffer containing the data to be written on the SD.
WriteAddr,:address to write on.
BlockSize,:the SD card Data block size.
Return values:
TheSD Response:
  • SD_RESPONSE_FAILURE: Sequence failed
  • SD_RESPONSE_NO_ERROR: Sequence succeed

< SD chip select low

< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block

< Check if the SD acknowledged the write block command: R1 response (0x00: no errors)

< Send a dummy byte

< Send the data token to signify the start of the data

< Write the block data to SD : write count data by block

< Send the pointed byte

< Point to the next location where the byte read will be saved

< Read data response

< Send dummy byte: 8 Clock pulses of delay

< Returns the response

Definition at line 279 of file stm8s_eval_spi_sd.c.

References i, SD_BLOCK_SIZE, SD_CMD_WRITE_SINGLE_BLOCK, SD_CS_HIGH, SD_CS_LOW, SD_DATA_OK, SD_DUMMY_BYTE, SD_GetDataResponse(), SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), and SD_WriteByte().

Referenced by main().

uint8_t SD_WriteBuffer ( uint8_t pBuffer,
uint32_t  WriteAddr,
uint32_t  NumByteToWrite 
)

Write a buffer (many blocks) in the SD card.

Note:
The amount of data to write should be a multiple of SD card BLOCK size (512 Byte).
Parameters:
pBuffer: pointer to the buffer containing the data to be written on the SD.
WriteAddr: address to write on.
NumByteToWrite: number of data to write.
Return values:
SDResponse:
  • SD_RESPONSE_FAILURE: Sequence failed.
  • SD_RESPONSE_NO_ERROR: Sequence succeed.

Definition at line 348 of file stm8s_eval_spi_sd.c.

References i, SD_BLOCK_SIZE, SD_CMD_WRITE_SINGLE_BLOCK, SD_CS_HIGH, SD_CS_LOW, SD_DATA_OK, SD_DUMMY_BYTE, SD_GetDataResponse(), SD_GetResponse(), SD_ReadByte(), SD_RESPONSE_FAILURE, SD_RESPONSE_NO_ERROR, SD_SendCmd(), SD_START_DATA_SINGLE_BLOCK_WRITE, and SD_WriteByte().

uint8_t SD_WriteByte ( uint8_t  Data)

Write a byte on the SD.

Parameters:
Data,:byte to send.
Return values:
None

< Wait until the transmit buffer is empty

< Wait to receive a byte

Definition at line 816 of file stm8s_eval_spi_sd.c.

References RESET, SPI_FLAG_RXNE, SPI_FLAG_TXE, SPI_GetFlagStatus(), SPI_ReceiveData(), and SPI_SendData().

Referenced by SD_GetCIDRegister(), SD_GetCSDRegister(), SD_GetStatus(), SD_GoIdleState(), SD_Init(), SD_ReadBlock(), SD_ReadBuffer(), SD_SendCmd(), SD_WriteBlock(), and SD_WriteBuffer().

STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com