77 sdcard->spi_t.slave_idx = slave_idx;
87 sdcard->spi_t.input_length = 0;
88 sdcard->spi_t.output_length = 0;
112 sdcard->spi_t.output_length = 10;
113 sdcard->spi_t.input_length = 0;
114 for (
uint8_t i = 0; i < 10; i++) {
115 sdcard->output_buf[i] = 0xFF;
125 sdcard->timeout_counter++;
136 sdcard->timeout_counter++;
178 if (
t->input_buf[0] == 0x01) {
198 if (
t->input_buf[0] == 0x01) {
230 if (
t->input_buf[0] == 0x01) {
237 }
else if (
t->input_buf[0] == 0x00) {
470 sdcard->spi_t.input_length = 6;
471 sdcard->spi_t.output_length = 6;
472 sdcard->output_buf[0] = 0x40 | cmd;
473 sdcard->output_buf[1] = arg >> 24;
474 sdcard->output_buf[2] = arg >> 16;
475 sdcard->output_buf[3] = arg >> 8;
476 sdcard->output_buf[4] = arg;
479 sdcard->output_buf[5] = 0x95;
482 sdcard->output_buf[5] = 0x87;
485 sdcard->output_buf[5] = 0x01;
504 sdcard->spi_t.output_length = 21;
505 sdcard->spi_t.input_length = 21;
507 sdcard->output_buf[0] = 0x77;
508 sdcard->output_buf[1] = 0x00;
509 sdcard->output_buf[2] = 0x00;
510 sdcard->output_buf[3] = 0x00;
511 sdcard->output_buf[4] = 0x00;
512 sdcard->output_buf[5] = 0x01;
513 sdcard->output_buf[6] = 0xFF;
514 sdcard->output_buf[7] = 0xFF;
515 sdcard->output_buf[8] = 0xFF;
516 sdcard->output_buf[9] = 0xFF;
517 sdcard->output_buf[10] = 0xFF;
518 sdcard->output_buf[11] = 0xFF;
519 sdcard->output_buf[12] = 0xFF;
520 sdcard->output_buf[13] = 0xFF;
521 sdcard->output_buf[14] = 0xFF;
523 sdcard->output_buf[15] = 0x40 + cmd;
524 sdcard->output_buf[16] = arg >> 24;
525 sdcard->output_buf[17] = arg >> 16;
526 sdcard->output_buf[18] = arg >> 8;
527 sdcard->output_buf[19] = arg;
528 sdcard->output_buf[20] = 0x01;
545 sdcard->spi_t.input_length = len;
546 sdcard->spi_t.output_length = len;
547 for (
uint8_t i = 0; i < len; i++) {
548 sdcard->output_buf[i] = 0xFF;
552 sdcard->response_counter++;
603 sdcard->external_callback = callback;
644 sdcard->spi_t.input_length = 516;
645 sdcard->spi_t.output_length = 516;
647 sdcard->spi_t.output_buf[0] = 0xFC;
648 sdcard->spi_t.output_buf[513] = 0xFF;
649 sdcard->spi_t.output_buf[514] = 0xFF;
650 sdcard->spi_t.output_buf[515] = 0xFF;
654 sdcard->external_callback = callback;
672 sdcard->spi_t.input_length = 2;
673 sdcard->spi_t.output_length = 2;
675 sdcard->output_buf[0] = 0xFD;
676 sdcard->output_buf[1] = 0xFF;
enum SPISlaveSelect select
slave selection behavior
enum SPIStatus status
internal state of the peripheral
SPICallback after_cb
NULL or function called after the transaction.
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
uint16_t input_length
number of data words to read
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
uint16_t output_length
number of data words to write
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
@ SPICpolIdleLow
CPOL = 0.
@ SPINoSelect
slave is not selected nor unselected
@ SPISelectUnselect
slave is selected before transaction and unselected after
SPI peripheral structure.
SPI transaction structure.
void sdcard_spi_send_app_cmd(struct SDCard *sdcard, uint8_t cmd, uint32_t arg)
Send a app-command to the SDCard.
void sdcard_spi_multiwrite_next(struct SDCard *sdcard, SDCardCallback callback)
Write a(nother) data block (512 bytes) to the SDCard.
void sdcard_spi_send_cmd(struct SDCard *sdcard, uint8_t cmd, uint32_t arg)
Send a command to the SDCard.
void sdcard_spi_write_block(struct SDCard *sdcard, uint32_t addr)
Write a single block (512 bytes) to the SDCard at a given address.
void sdcard_spi_spicallback(struct spi_transaction *t)
Callback function for SPI transactions.
void sdcard_spi_multiwrite_start(struct SDCard *sdcard, uint32_t addr)
Start writing multiple blocks of 512 bytes to the SDCard.
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.
void sdcard_spi_periodic(struct SDCard *sdcard)
Periodic function of the SDCard.
struct SDCard sdcard1
This is the definition of the SD card.
void sdcard_spi_init(struct SDCard *sdcard, struct spi_periph *spi_p, const uint8_t slave_idx)
Configure initial values for SDCard.
void sdcard_spi_request_bytes(struct SDCard *sdcard, uint8_t len)
Request one or more bytes from the SDCard.
void sdcard_spi_multiwrite_stop(struct SDCard *sdcard)
Stop with multiwrite procedure.
Interface for reading and writing blocks of data to and from an SD card directly over SPI.
@ SDCard_ReadingCMD16Resp
Reading R1 response to CMD16 byte by byte.
@ SDCard_SendingCMD16
Busy sending CMD16.
@ SDCard_Error
An error has occured, sending debug message.
@ SDCard_ReadingCMD58Parameter
Reading the 32-bit parameter after receiving 0x00 from CMD58.
@ SDCard_ReadingCMD24Resp
Reading R1 response to CMD24 byte by byte.
@ SDCard_WaitingForDataToken
Reading a byte each period until there is a data token or error.
@ SDCard_BeforeDummyClock
About to send dummy clock cycles to initialize spi mode.
@ SDCard_ReadingCMD58Resp
Reading R3 response to CMD58 byte by byte.
@ SDCard_ReadingDataBlock
Busy reading data block.
@ SDCard_SendingDummyClock
Busy sending dummy clock cycles.
@ SDCard_MultiWriteIdle
CMD25 complete, ready to sent blocks.
@ SDCard_SendingCMD17
Busy sending CMD17 (block read request)
@ SDCard_ReadingACMD41v2Resp
Reading R1 response to ACMD41 byte by byte.
@ SDCard_SendingCMD24
Busy sending CMD24.
@ SDCard_ReadingCMD8Resp
Reading R7 response to CMD8 byte by byte.
@ SDCard_MultiWriteStopping
Busy sending the stop token.
@ SDCard_SendingCMD0
Busy sending CMD0.
@ SDCard_Idle
Initialization sequence succesful.
@ SDCard_MultiWriteBusy
Busy flag after sending data block in multiwrite.
@ SDCard_SendingDataBlock
Busy sending data block.
@ SDCard_ReadingCMD8Parameter
Reading the 32-bit parameter after receiving 0x01.
@ SDCard_SendingCMD8
Busy sending CMD8.
@ SDCard_SendingCMD58
Busy sending CMD58.
@ SDCard_MultiWriteWriting
Busy with the SPI transfer in multiwrite.
@ SDCard_SendingCMD25
Busy sending CMD25 (multiwrite start command)
@ SDCard_SendingACMD41v2
Busy sending ACMD41.
@ SDCard_Busy
SDCard is busy with internal process.
@ SDCard_ReadingCMD25Resp
Reading R1 response to CMD25 byte by byte.
@ SDCard_BeforeSendingDataBlock
Start data block transfer.
@ SDCard_ReadingCMD17Resp
Reading R1 response to CMD17 byte by byte.
@ SDCard_ReadingCMD0Resp
Reading R1 response to CMD0 byte by byte.
enum SDCardStatus status
The status of the SD card.
uint8_t response_counter
Response counter used at various locations.
struct spi_periph * spi_p
The SPI peripheral for the connection.
uint8_t output_buf[SD_BLOCK_SIZE+10]
The output buffer for the SPI transaction.
enum SDCardType card_type
Type of SDCard.
@ SDCardError_ACMD41NoResponse
@ SDCardError_WriteBlockNoResponse
@ SDCardError_InitializationNoResponse
@ SDCardError_ReadBlockTimeout
@ SDCardError_SetBlockSizeNoResponse
@ SDCardError_SpiDriverError
@ SDCardError_BlockWriteError
@ SDCardError_MultiWriteNoResponse
@ SDCardError_CardInfoNoResponse
@ SDCardError_ACMD41Timeout
@ SDCardError_CMD58NoResponse
@ SDCardError_InvalidCardInfo
@ SDCardError_CCSBitInvalid
@ SDCardError_ReadBlockNoResponse
@ SDCardError_MultiWriteError
uint8_t input_buf[SD_BLOCK_SIZE+10]
The input buffer for the SPI transaction.
enum SDCardErrorStatus error_status
Contains information on where the error has occured.
struct spi_transaction spi_t
The SPI transaction used for the writing and reading of registers.
uint32_t timeout_counter
Timeout counter used for initialization checks with ACMD41.
void(* SDCardCallback)(void)
SDCard Callback function.
SDCardCallback external_callback
Callback to call when external operation finishes.
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.