Paparazzi UAS
v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
|
This is an interface for reading and writing blocks of data to and from an SD card directly over SPI. More...
#include "sdcard_spi.h"
Go to the source code of this file.
Functions | |
void | sdcard_spi_init (struct SDCard *sdcard, struct spi_periph *spi_p, const uint8_t slave_idx) |
Configure initial values for SDCard. More... | |
void | sdcard_spi_periodic (struct SDCard *sdcard) |
Periodic function of the SDCard. More... | |
void | sdcard_spi_write_block (struct SDCard *sdcard, uint32_t addr) |
Write a single block (512 bytes) to the SDCard at a given address. More... | |
void | sdcard_spi_read_block (struct SDCard *sdcard, uint32_t addr, SDCardCallback callback) |
Read a single block (512 bytes) from the SDCard at a given address. More... | |
void | sdcard_spi_multiwrite_start (struct SDCard *sdcard, uint32_t addr) |
Start writing multiple blocks of 512 bytes to the SDCard. More... | |
void | sdcard_spi_multiwrite_next (struct SDCard *sdcard, SDCardCallback callback) |
Write a(nother) data block (512 bytes) to the SDCard. More... | |
void | sdcard_spi_multiwrite_stop (struct SDCard *sdcard) |
Stop with multiwrite procedure. More... | |
Private Functions | |
void | sdcard_spi_spicallback (struct spi_transaction *t) |
Callback function for SPI transactions. More... | |
void | sdcard_spi_send_cmd (struct SDCard *sdcard, uint8_t cmd, uint32_t arg) |
Send a command to the SDCard. More... | |
void | sdcard_spi_send_app_cmd (struct SDCard *sdcard, uint8_t cmd, uint32_t arg) |
Send a app-command to the SDCard. More... | |
void | sdcard_spi_request_bytes (struct SDCard *sdcard, uint8_t len) |
Request one or more bytes from the SDCard. More... | |
Variables | |
struct SDCard | sdcard1 |
This is the definition of the SD card. More... | |
This is an interface for reading and writing blocks of data to and from an SD card directly over SPI.
The pinout of the SD card is given here [sdcard_pinout.] Connect the card according to the following table:
MCU | SD card | Description |
---|---|---|
CS | CS | Chip Select |
MOSI | DI | Master Out Slave In |
MISO | DO | Master In Slave Out |
SCK | SCLK | Clock Signal |
The following resource was used as implementation reference: http://elm-chan.org/docs/mmc/mmc_e.html The initialization procedure is implemented according to the following diagram. Only the branches for SD ver.2 are currently included.
Developed using Test Driven Development. Test code available at: https://github.com/bartslinger/paparazzi-unittest
Definition in file sdcard_spi.c.
void sdcard_spi_init | ( | struct SDCard * | sdcard, |
struct spi_periph * | spi_p, | ||
const uint8_t | slave_idx | ||
) |
Configure initial values for SDCard.
Initialization of the card itself is done later in sdcard_spi_periodic().
sdcard | Pointer to the SDCard. |
spi_p | Pointer to the SPI Peripheral. |
slave_idx | SPI Slave index. |
Definition at line 74 of file sdcard_spi.c.
References spi_transaction::bitorder, SDCard::card_type, spi_transaction::cdiv, spi_transaction::cpha, spi_transaction::cpol, spi_transaction::dss, SDCard::error_status, SDCard::input_buf, spi_transaction::input_buf, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_buf, spi_transaction::output_length, SDCard_BeforeDummyClock, SDCardError_None, SDCardType_Unknown, spi_transaction::select, spi_transaction::slave_idx, SDCard::spi_p, SDCard::spi_t, SPICphaEdge1, SPICpolIdleLow, SPIDiv64, SPIDss8bit, SPIMSBFirst, SPISelectUnselect, SPITransDone, SDCard::status, and spi_transaction::status.
Referenced by sdlogger_spi_direct_init().
void sdcard_spi_multiwrite_next | ( | struct SDCard * | sdcard, |
SDCardCallback | callback | ||
) |
Write a(nother) data block (512 bytes) to the SDCard.
Use only after sdcard_spi_multiwrite_start().
sdcard | Pointer to the SDCard. |
Definition at line 638 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::cdiv, SDCard::external_callback, spi_transaction::input_length, spi_transaction::output_buf, spi_transaction::output_length, SDCard_MultiWriteIdle, SDCard_MultiWriteWriting, sdcard_spi_spicallback(), SDCard::spi_p, spi_submit(), SDCard::spi_t, SPIDiv32, and SDCard::status.
Referenced by sdlogger_spi_direct_periodic(), and sdlogger_spi_direct_put_byte().
Start writing multiple blocks of 512 bytes to the SDCard.
This function notifies the SDCard to expect one or more data packets (which are sent by sdcard_spi_multiwrite_next()).
sdcard | Pointer to the SDCard. |
addr | Block address to start writing at. Addresses are consecutive numbers, regardless the SDCardType. |
Definition at line 616 of file sdcard_spi.c.
References SDCard::card_type, SD_BLOCK_SIZE, SDCard_Idle, SDCard_SendingCMD25, sdcard_spi_send_cmd(), SDCardType_SdV2block, and SDCard::status.
Referenced by sdlogger_spi_direct_periodic().
void sdcard_spi_multiwrite_stop | ( | struct SDCard * | sdcard | ) |
Stop with multiwrite procedure.
sdcard | Pointer to the SDCard. |
Definition at line 666 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::cdiv, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_length, SDCard_MultiWriteIdle, SDCard_MultiWriteStopping, sdcard_spi_spicallback(), SDCard::spi_p, spi_submit(), SDCard::spi_t, SPIDiv32, and SDCard::status.
Referenced by sdlogger_spi_direct_periodic().
void sdcard_spi_periodic | ( | struct SDCard * | sdcard | ) |
Periodic function of the SDCard.
sdcard | Pointer to the SDCard. |
Definition at line 99 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_length, SDCard_BeforeDummyClock, SDCard_Busy, SDCard_MultiWriteBusy, SDCard_SendingACMD41v2, SDCard_SendingDummyClock, sdcard_spi_request_bytes(), sdcard_spi_send_app_cmd(), sdcard_spi_spicallback(), SDCard_WaitingForDataToken, spi_transaction::select, SDCard::spi_p, spi_submit(), SDCard::spi_t, SPINoSelect, SPITransPending, SPITransRunning, SDCard::status, spi_transaction::status, and SDCard::timeout_counter.
Referenced by sdlogger_spi_direct_periodic().
void sdcard_spi_read_block | ( | struct SDCard * | sdcard, |
uint32_t | addr, | ||
SDCardCallback | callback | ||
) |
Read a single block (512 bytes) from the SDCard at a given address.
When the block reading is finished, sdcard_spi_spicallback() is triggered which in turn calls the function from the callback parameter.
sdcard | Pointer to the SDCard. |
addr | Block address to read from. The addresses are consecutive numbers, regardless the SDCardType. |
callback | Function to call when block reading is finished. |
Definition at line 587 of file sdcard_spi.c.
References SDCard::card_type, spi_transaction::cdiv, SDCard::external_callback, SD_BLOCK_SIZE, SDCard_Idle, SDCard_SendingCMD17, sdcard_spi_send_cmd(), SDCardType_SdV2block, SDCard::spi_t, SPIDiv32, and SDCard::status.
Referenced by sdlogger_spi_direct_command(), sdlogger_spi_direct_index_received(), and sdlogger_spi_direct_periodic().
Request one or more bytes from the SDCard.
This function request one or more bytes and parses the output in sdcard_spi_spicallback().
sdcard | Pointer to the SDCard. |
len | Number of bytes to request. |
Definition at line 543 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_length, SDCard::response_counter, sdcard_spi_spicallback(), SDCard::spi_p, spi_submit(), and SDCard::spi_t.
Referenced by sdcard_spi_periodic(), and sdcard_spi_spicallback().
Send a app-command to the SDCard.
The response is parsed by sdcard_spi_spicallback(). This is the same as a normal command, but preceeded by a CMD55. Possible commands and arguments are specified by the SD Association. The following website specifies some common commands: http://elm-chan.org/docs/mmc/mmc_e.html
sdcard | Pointer to the SDCard. |
cmd | Command index, possible values in range from 0 to 63. |
arg | Argument to be passed together with the command. |
Definition at line 502 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_length, sdcard_spi_spicallback(), SDCard::spi_p, spi_submit(), and SDCard::spi_t.
Referenced by sdcard_spi_periodic().
Send a command to the SDCard.
The response is parsed by sdcard_spi_spicallback(). Possible commands and arguments are specified by the SD Association. The following website specifies some common commands: http://elm-chan.org/docs/mmc/mmc_e.html
sdcard | Pointer to the SDCard. |
cmd | Command index, can be a value from 0 to 63. |
arg | Argument to be passed together with the command. |
Definition at line 467 of file sdcard_spi.c.
References spi_transaction::after_cb, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_length, sdcard_spi_spicallback(), SDCard::spi_p, spi_submit(), and SDCard::spi_t.
Referenced by sdcard_spi_multiwrite_start(), sdcard_spi_read_block(), sdcard_spi_spicallback(), and sdcard_spi_write_block().
void sdcard_spi_spicallback | ( | struct spi_transaction * | t | ) |
Callback function for SPI transactions.
This function is called when the SPI transaction is finished and this function was defined as callback.
t | Pointer to the spi_transaction that just finished. |
Definition at line 156 of file sdcard_spi.c.
References spi_transaction::after_cb, SDCard::card_type, spi_transaction::cdiv, SDCard::error_status, SDCard::external_callback, SDCard::input_buf, spi_transaction::input_buf, spi_transaction::input_length, SDCard::output_buf, spi_transaction::output_buf, spi_transaction::output_length, SDCard::response_counter, SD_BLOCK_SIZE, sdcard1, SDCard_BeforeSendingDataBlock, SDCard_Busy, SDCard_Error, SDCard_Idle, SDCard_MultiWriteBusy, SDCard_MultiWriteIdle, SDCard_MultiWriteStopping, SDCard_MultiWriteWriting, SDCard_ReadingACMD41v2Resp, SDCard_ReadingCMD0Resp, SDCard_ReadingCMD16Resp, SDCard_ReadingCMD17Resp, SDCard_ReadingCMD24Resp, SDCard_ReadingCMD25Resp, SDCard_ReadingCMD58Parameter, SDCard_ReadingCMD58Resp, SDCard_ReadingCMD8Parameter, SDCard_ReadingCMD8Resp, SDCard_ReadingDataBlock, SDCard_SendingACMD41v2, SDCard_SendingCMD0, SDCard_SendingCMD16, SDCard_SendingCMD17, SDCard_SendingCMD24, SDCard_SendingCMD25, SDCard_SendingCMD58, SDCard_SendingCMD8, SDCard_SendingDataBlock, SDCard_SendingDummyClock, sdcard_spi_request_bytes(), sdcard_spi_send_cmd(), SDCard_WaitingForDataToken, SDCardError_ACMD41NoResponse, SDCardError_ACMD41Timeout, SDCardError_BlockWriteError, SDCardError_CardInfoNoResponse, SDCardError_CCSBitInvalid, SDCardError_CMD58NoResponse, SDCardError_InitializationNoResponse, SDCardError_InvalidCardInfo, SDCardError_MultiWriteError, SDCardError_MultiWriteNoResponse, SDCardError_ReadBlockNoResponse, SDCardError_ReadBlockTimeout, SDCardError_SetBlockSizeNoResponse, SDCardError_SpiDriverError, SDCardError_WriteBlockNoResponse, SDCardType_SdV1, SDCardType_SdV2block, spi_transaction::select, SDCard::spi_p, spi_submit(), SDCard::spi_t, SPIDiv8, SPISelectUnselect, SDCard::status, and SDCard::timeout_counter.
Referenced by sdcard_spi_multiwrite_next(), sdcard_spi_multiwrite_stop(), sdcard_spi_periodic(), sdcard_spi_request_bytes(), sdcard_spi_send_app_cmd(), and sdcard_spi_send_cmd().
Write a single block (512 bytes) to the SDCard at a given address.
sdcard | Pointer to the SDCard. |
addr | Block address to write to. The addresses are consecutive numbers, regardless the SDCardType. |
Definition at line 560 of file sdcard_spi.c.
References SDCard::card_type, spi_transaction::cdiv, SD_BLOCK_SIZE, SDCard_Idle, SDCard_SendingCMD24, sdcard_spi_send_cmd(), SDCardType_SdV2block, SDCard::spi_t, SPIDiv64, and SDCard::status.
Referenced by sdlogger_spi_direct_index_received().
struct SDCard sdcard1 |
This is the definition of the SD card.
Currently, only one SD card can be used. Most of the functions are prepared for feature support of multiple SD cards, by taking a reference to the SDCard as parameter. Only the sdcard_spi_callback() function, which is called after a spi transaction is finished, does not provide a reference to the SDCard. Thereby sdcard_spi_callback() cannot know which SDCard is ready. However, the spi_transaction is given as a parameter, which is part of the SDCard. So thereby the SDCard might be identified.
Definition at line 56 of file sdcard_spi.c.
Referenced by sdcard_spi_spicallback(), sdlogger_spi_direct_command(), sdlogger_spi_direct_index_received(), sdlogger_spi_direct_init(), sdlogger_spi_direct_multiwrite_written(), sdlogger_spi_direct_periodic(), and sdlogger_spi_direct_put_byte().