Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
sdcard_spi.h File Reference

Interface for reading and writing blocks of data to and from an SD card directly over SPI. More...

#include "std.h"
#include "mcu_periph/spi.h"
+ Include dependency graph for sdcard_spi.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  SDCard
 

Macros

#define SD_BLOCK_SIZE   512
 

Typedefs

typedef void(* SDCardCallback) (void)
 SDCard Callback function. More...
 

Enumerations

enum  SdResponseType { SdResponseNone , SdResponseR1 , SdResponseR3 , SdResponseR7 }
 
enum  SDCardType {
  SDCardType_Unknown , SDCardType_MmcV3 , SDCardType_SdV1 , SDCardType_SdV2byte ,
  SDCardType_SdV2block
}
 
enum  SDCardStatus {
  SDCard_UnInit , SDCard_Error , SDCard_Idle , SDCard_Busy ,
  SDCard_BeforeDummyClock , SDCard_SendingDummyClock , SDCard_SendingCMD0 , SDCard_ReadingCMD0Resp ,
  SDCard_SendingCMD8 , SDCard_ReadingCMD8Resp , SDCard_ReadingCMD8Parameter , SDCard_SendingACMD41v2 ,
  SDCard_ReadingACMD41v2Resp , SDCard_SendingCMD58 , SDCard_ReadingCMD58Resp , SDCard_ReadingCMD58Parameter ,
  SDCard_SendingCMD16 , SDCard_ReadingCMD16Resp , SDCard_SendingCMD24 , SDCard_ReadingCMD24Resp ,
  SDCard_BeforeSendingDataBlock , SDCard_SendingDataBlock , SDCard_SendingCMD17 , SDCard_ReadingCMD17Resp ,
  SDCard_WaitingForDataToken , SDCard_ReadingDataBlock , SDCard_SendingCMD25 , SDCard_ReadingCMD25Resp ,
  SDCard_MultiWriteIdle , SDCard_MultiWriteWriting , SDCard_MultiWriteBusy , SDCard_MultiWriteStopping
}
 
enum  SDCardErrorStatus {
  SDCardError_None , SDCardError_InitializationNoResponse , SDCardError_CardInfoNoResponse , SDCardError_InvalidCardInfo ,
  SDCardError_ACMD41Timeout , SDCardError_ACMD41NoResponse , SDCardError_CMD58NoResponse , SDCardError_CCSBitInvalid ,
  SDCardError_SetBlockSizeNoResponse , SDCardError_WriteBlockNoResponse , SDCardError_SpiDriverError , SDCardError_BlockWriteError ,
  SDCardError_ReadBlockNoResponse , SDCardError_ReadBlockTimeout , SDCardError_MultiWriteNoResponse , SDCardError_MultiWriteError
}
 

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

Variables

struct SDCard sdcard1
 This is the definition of the SD card. More...
 

Detailed Description

Interface for reading and writing blocks of data to and from an SD card directly over SPI.

Definition in file sdcard_spi.h.


Data Structure Documentation

◆ SDCard

struct SDCard

Definition at line 109 of file sdcard_spi.h.

+ Collaboration diagram for SDCard:
Data Fields
enum SDCardType card_type Type of SDCard.
enum SDCardErrorStatus error_status Contains information on where the error has occured.
SDCardCallback external_callback Callback to call when external operation finishes.
uint8_t input_buf[SD_BLOCK_SIZE+10] The input buffer for the SPI transaction.
uint8_t output_buf[SD_BLOCK_SIZE+10] The output buffer for the SPI transaction.
uint8_t response_counter Response counter used at various locations.
struct spi_periph * spi_p The SPI peripheral for the connection.
struct spi_transaction spi_t The SPI transaction used for the writing and reading of registers.
enum SDCardStatus status The status of the SD card.
uint32_t timeout_counter Timeout counter used for initialization checks with ACMD41.

Macro Definition Documentation

◆ SD_BLOCK_SIZE

#define SD_BLOCK_SIZE   512

Definition at line 38 of file sdcard_spi.h.

Typedef Documentation

◆ SDCardCallback

typedef void(* SDCardCallback) (void)

SDCard Callback function.

If not NULL (or 0), call function

Definition at line 36 of file sdcard_spi.h.

Enumeration Type Documentation

◆ SDCardErrorStatus

Enumerator
SDCardError_None 
SDCardError_InitializationNoResponse 
SDCardError_CardInfoNoResponse 
SDCardError_InvalidCardInfo 
SDCardError_ACMD41Timeout 
SDCardError_ACMD41NoResponse 
SDCardError_CMD58NoResponse 
SDCardError_CCSBitInvalid 
SDCardError_SetBlockSizeNoResponse 
SDCardError_WriteBlockNoResponse 
SDCardError_SpiDriverError 
SDCardError_BlockWriteError 
SDCardError_ReadBlockNoResponse 
SDCardError_ReadBlockTimeout 
SDCardError_MultiWriteNoResponse 
SDCardError_MultiWriteError 

Definition at line 90 of file sdcard_spi.h.

◆ SDCardStatus

Enumerator
SDCard_UnInit 

SDCard is not initialized.

SDCard_Error 

An error has occured, sending debug message.

SDCard_Idle 

Initialization sequence succesful.

SDCard_Busy 

SDCard is busy with internal process.

SDCard_BeforeDummyClock 

About to send dummy clock cycles to initialize spi mode.

SDCard_SendingDummyClock 

Busy sending dummy clock cycles.

SDCard_SendingCMD0 

Busy sending CMD0.

SDCard_ReadingCMD0Resp 

Reading R1 response to CMD0 byte by byte.

SDCard_SendingCMD8 

Busy sending CMD8.

SDCard_ReadingCMD8Resp 

Reading R7 response to CMD8 byte by byte.

SDCard_ReadingCMD8Parameter 

Reading the 32-bit parameter after receiving 0x01.

SDCard_SendingACMD41v2 

Busy sending ACMD41.

SDCard_ReadingACMD41v2Resp 

Reading R1 response to ACMD41 byte by byte.

SDCard_SendingCMD58 

Busy sending CMD58.

SDCard_ReadingCMD58Resp 

Reading R3 response to CMD58 byte by byte.

SDCard_ReadingCMD58Parameter 

Reading the 32-bit parameter after receiving 0x00 from CMD58.

SDCard_SendingCMD16 

Busy sending CMD16.

SDCard_ReadingCMD16Resp 

Reading R1 response to CMD16 byte by byte.

SDCard_SendingCMD24 

Busy sending CMD24.

SDCard_ReadingCMD24Resp 

Reading R1 response to CMD24 byte by byte.

SDCard_BeforeSendingDataBlock 

Start data block transfer.

SDCard_SendingDataBlock 

Busy sending data block.

SDCard_SendingCMD17 

Busy sending CMD17 (block read request)

SDCard_ReadingCMD17Resp 

Reading R1 response to CMD17 byte by byte.

SDCard_WaitingForDataToken 

Reading a byte each period until there is a data token or error.

SDCard_ReadingDataBlock 

Busy reading data block.

SDCard_SendingCMD25 

Busy sending CMD25 (multiwrite start command)

SDCard_ReadingCMD25Resp 

Reading R1 response to CMD25 byte by byte.

SDCard_MultiWriteIdle 

CMD25 complete, ready to sent blocks.

SDCard_MultiWriteWriting 

Busy with the SPI transfer in multiwrite.

SDCard_MultiWriteBusy 

Busy flag after sending data block in multiwrite.

SDCard_MultiWriteStopping 

Busy sending the stop token.

Definition at line 55 of file sdcard_spi.h.

◆ SDCardType

enum SDCardType
Enumerator
SDCardType_Unknown 
SDCardType_MmcV3 
SDCardType_SdV1 
SDCardType_SdV2byte 
SDCardType_SdV2block 

Definition at line 47 of file sdcard_spi.h.

◆ SdResponseType

Enumerator
SdResponseNone 
SdResponseR1 
SdResponseR3 
SdResponseR7 

Definition at line 40 of file sdcard_spi.h.

Function Documentation

◆ sdcard_spi_init()

void sdcard_spi_init ( struct SDCard sdcard,
struct spi_periph spi_p,
const uint8_t  slave_idx 
)

◆ sdcard_spi_multiwrite_next()

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:

◆ sdcard_spi_multiwrite_start()

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:

◆ sdcard_spi_multiwrite_stop()

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:

◆ sdcard_spi_periodic()

◆ sdcard_spi_read_block()

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:

◆ sdcard_spi_write_block()

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

◆ sdcard1

struct SDCard sdcard1
extern

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