Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sdcard_spi.c File Reference

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"
+ Include dependency graph for sdcard_spi.c:

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...
 

Detailed Description

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
sd-card-pinout.png
SD Card pinout

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.

sdinit.png

Developed using Test Driven Development. Test code available at: https://github.com/bartslinger/paparazzi-unittest

Todo:
CRC checksums are not implemented. Fake values of 0xFF are used and they are ignored by the card.

Definition in file sdcard_spi.c.

Function Documentation

void sdcard_spi_init ( struct SDCard sdcard,
struct spi_periph spi_p,
const uint8_t  slave_idx 
)
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().

Parameters
sdcardPointer 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_multiwrite_start ( struct SDCard sdcard,
uint32_t  addr 
)

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()).

Parameters
sdcardPointer to the SDCard.
addrBlock 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_multiwrite_stop ( struct SDCard sdcard)

Stop with multiwrite procedure.

Parameters
sdcardPointer 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.

Parameters
sdcardPointer to the SDCard.
addrBlock address to read from. The addresses are consecutive numbers, regardless the SDCardType.
callbackFunction 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_request_bytes ( struct SDCard sdcard,
uint8_t  len 
)

Request one or more bytes from the SDCard.

This function request one or more bytes and parses the output in sdcard_spi_spicallback().

Parameters
sdcardPointer to the SDCard.
lenNumber 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_send_app_cmd ( struct SDCard sdcard,
uint8_t  cmd,
uint32_t  arg 
)

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

Parameters
sdcardPointer to the SDCard.
cmdCommand index, possible values in range from 0 to 63.
argArgument 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_send_cmd ( struct SDCard sdcard,
uint8_t  cmd,
uint32_t  arg 
)

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

Parameters
sdcardPointer to the SDCard.
cmdCommand index, can be a value from 0 to 63.
argArgument 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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.

Parameters
tPointer 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void sdcard_spi_write_block ( struct SDCard sdcard,
uint32_t  addr 
)

Write a single block (512 bytes) to the SDCard at a given address.

Parameters
sdcardPointer to the SDCard.
addrBlock 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().

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

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.

Todo:
Add support for multiple SD cards.

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().