|
|
|
|
@ -25,7 +25,7 @@
|
|
|
|
|
#include "periph/spi.h"
|
|
|
|
|
#include "periph/gpio.h"
|
|
|
|
|
#include "checksum/ucrc16.h"
|
|
|
|
|
#include "xtimer.h"
|
|
|
|
|
#include "ztimer.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
@ -40,8 +40,10 @@ static inline bool _wait_for_token(sdcard_spi_t *card, uint8_t token, uint32_t r
|
|
|
|
|
static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_state_t state);
|
|
|
|
|
static sd_rw_response_t _read_cid(sdcard_spi_t *card);
|
|
|
|
|
static sd_rw_response_t _read_csd(sdcard_spi_t *card);
|
|
|
|
|
static sd_rw_response_t _read_data_packet(sdcard_spi_t *card, uint8_t token, uint8_t *data, int size);
|
|
|
|
|
static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, const uint8_t *data, int size);
|
|
|
|
|
static sd_rw_response_t _read_data_packet(sdcard_spi_t *card, uint8_t token, uint8_t *data,
|
|
|
|
|
int size);
|
|
|
|
|
static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, const uint8_t *data,
|
|
|
|
|
int size);
|
|
|
|
|
|
|
|
|
|
/* number of used sd cards */
|
|
|
|
|
#define SDCARD_SPI_NUM ARRAY_SIZE(sdcard_spi_params)
|
|
|
|
|
@ -53,7 +55,8 @@ sdcard_spi_t sdcard_spi_devs[SDCARD_SPI_NUM];
|
|
|
|
|
static uint8_t _crc_7(const uint8_t *data, int n);
|
|
|
|
|
|
|
|
|
|
/* use this transfer method instead of _transfer_bytes to force the use of 0xFF as dummy bytes */
|
|
|
|
|
static inline int _transfer_bytes(sdcard_spi_t *card, const uint8_t *out, uint8_t *in, unsigned int length);
|
|
|
|
|
static inline int _transfer_bytes(sdcard_spi_t *card, const uint8_t *out, uint8_t *in,
|
|
|
|
|
unsigned int length);
|
|
|
|
|
|
|
|
|
|
/* uses bitbanging for spi communication which allows to enable pull-up on the miso pin for
|
|
|
|
|
greater card compatibility on platforms that don't have a hw pull up installed */
|
|
|
|
|
@ -65,9 +68,25 @@ static inline int _hw_spi_rxtx_byte(sdcard_spi_t *card, uint8_t out, uint8_t *in
|
|
|
|
|
/* function pointer to switch to hw spi mode after init sequence */
|
|
|
|
|
static int (*_dyn_spi_rxtx_byte)(sdcard_spi_t *card, uint8_t out, uint8_t *in);
|
|
|
|
|
|
|
|
|
|
static inline uint32_t _deadline_from_interval(uint32_t interval)
|
|
|
|
|
{
|
|
|
|
|
return ztimer_now(ZTIMER_USEC) + interval;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline uint32_t _deadline_left(uint32_t deadline)
|
|
|
|
|
{
|
|
|
|
|
int32_t left = (int32_t)(deadline - ztimer_now(ZTIMER_USEC));
|
|
|
|
|
|
|
|
|
|
if (left < 0) {
|
|
|
|
|
left = 0;
|
|
|
|
|
}
|
|
|
|
|
return left;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int sdcard_spi_init(sdcard_spi_t *card, const sdcard_spi_params_t *params)
|
|
|
|
|
{
|
|
|
|
|
sd_init_fsm_state_t state = SD_INIT_START;
|
|
|
|
|
|
|
|
|
|
card->params = *params;
|
|
|
|
|
card->spi_clk = SD_CARD_SPI_SPEED_PREINIT;
|
|
|
|
|
|
|
|
|
|
@ -112,7 +131,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
|
|
|
|
|
if (gpio_is_valid(card->params.power)) {
|
|
|
|
|
gpio_write(card->params.power, card->params.power_act_high);
|
|
|
|
|
xtimer_usleep(SD_CARD_WAIT_AFTER_POWER_UP_US);
|
|
|
|
|
ztimer_sleep(ZTIMER_USEC, SD_CARD_WAIT_AFTER_POWER_UP_US);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
gpio_set(card->params.mosi);
|
|
|
|
|
@ -122,9 +141,9 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
* (same as sending dummy bytes with 0xFF) */
|
|
|
|
|
for (int i = 0; i < SD_POWERSEQUENCE_CLOCK_COUNT; i += 1) {
|
|
|
|
|
gpio_set(card->params.clk);
|
|
|
|
|
xtimer_usleep(SD_CARD_PREINIT_CLOCK_PERIOD_US/2);
|
|
|
|
|
ztimer_sleep(ZTIMER_USEC, SD_CARD_PREINIT_CLOCK_PERIOD_US / 2);
|
|
|
|
|
gpio_clear(card->params.clk);
|
|
|
|
|
xtimer_usleep(SD_CARD_PREINIT_CLOCK_PERIOD_US/2);
|
|
|
|
|
ztimer_sleep(ZTIMER_USEC, SD_CARD_PREINIT_CLOCK_PERIOD_US / 2);
|
|
|
|
|
}
|
|
|
|
|
return SD_INIT_SEND_CMD0;
|
|
|
|
|
|
|
|
|
|
@ -188,7 +207,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
|
|
|
|
|
DEBUG("CMD8: [R7 MISMATCH]\n");
|
|
|
|
|
_unselect_card_spi(card);
|
|
|
|
|
return SD_INIT_CARD_UNKNOWN;;
|
|
|
|
|
return SD_INIT_CARD_UNKNOWN;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
DEBUG("CMD8: _transfer_bytes (R7): [ERROR]\n");
|
|
|
|
|
@ -205,7 +224,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
|
|
|
|
|
case SD_INIT_SEND_ACMD41_HCS:
|
|
|
|
|
DEBUG("SD_INIT_SEND_ACMD41_HCS\n");
|
|
|
|
|
const uint32_t acmd41_hcs_retry_timeout = xtimer_now_usec() + INIT_CMD_RETRY_US;
|
|
|
|
|
uint32_t acmd41_hcs_retry_timeout = _deadline_from_interval(INIT_CMD_RETRY_US);
|
|
|
|
|
do {
|
|
|
|
|
uint8_t acmd41hcs_r1 = sdcard_spi_send_acmd(card, SD_CMD_41, SD_ACMD_41_ARG_HC, 0);
|
|
|
|
|
if (R1_VALID(acmd41hcs_r1) && !R1_ERROR(acmd41hcs_r1) &&
|
|
|
|
|
@ -213,13 +232,13 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
DEBUG("ACMD41: [OK]\n");
|
|
|
|
|
return SD_INIT_SEND_CMD58;
|
|
|
|
|
}
|
|
|
|
|
} while (((uint32_t)INIT_CMD_RETRY_US > 0) && (xtimer_now_usec() < acmd41_hcs_retry_timeout));
|
|
|
|
|
} while (INIT_CMD_RETRY_US && _deadline_left(acmd41_hcs_retry_timeout));
|
|
|
|
|
_unselect_card_spi(card);
|
|
|
|
|
return SD_INIT_CARD_UNKNOWN;
|
|
|
|
|
|
|
|
|
|
case SD_INIT_SEND_ACMD41:
|
|
|
|
|
DEBUG("SD_INIT_SEND_ACMD41\n");
|
|
|
|
|
const uint32_t acmd41_retry_timeout = xtimer_now_usec() + INIT_CMD_RETRY_US;
|
|
|
|
|
int32_t acmd41_retry_timeout = _deadline_from_interval(INIT_CMD_RETRY_US);
|
|
|
|
|
do {
|
|
|
|
|
uint8_t acmd41_r1 = sdcard_spi_send_acmd(card, SD_CMD_41, SD_CMD_NO_ARG, 0);
|
|
|
|
|
if (R1_VALID(acmd41_r1) && !R1_ERROR(acmd41_r1) && !R1_IDLE_BIT_SET(acmd41_r1)) {
|
|
|
|
|
@ -228,7 +247,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
card->card_type = SD_V1;
|
|
|
|
|
return SD_INIT_SEND_CMD16;
|
|
|
|
|
}
|
|
|
|
|
} while (((uint32_t)INIT_CMD_RETRY_US > 0) && (xtimer_now_usec() < acmd41_retry_timeout));
|
|
|
|
|
} while (INIT_CMD_RETRY_US && _deadline_left(acmd41_retry_timeout));
|
|
|
|
|
|
|
|
|
|
DEBUG("ACMD41: [ERROR]\n");
|
|
|
|
|
return SD_INIT_SEND_CMD1;
|
|
|
|
|
@ -341,7 +360,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
|
|
|
|
|
|
|
|
|
|
static inline bool _wait_for_token(sdcard_spi_t *card, uint8_t token, uint32_t retry_us)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t retry_timeout = xtimer_now_usec() + retry_us;
|
|
|
|
|
uint32_t retry_timeout = _deadline_from_interval(retry_us);
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
uint8_t read_byte = 0;
|
|
|
|
|
@ -355,7 +374,7 @@ static inline bool _wait_for_token(sdcard_spi_t *card, uint8_t token, uint32_t r
|
|
|
|
|
DEBUG("_wait_for_token: [NO MATCH] (0x%02x)\n", read_byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout));
|
|
|
|
|
} while (retry_us && _deadline_left(retry_timeout));
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
@ -374,7 +393,7 @@ static inline void _send_dummy_byte(sdcard_spi_t *card)
|
|
|
|
|
|
|
|
|
|
static inline bool _wait_for_not_busy(sdcard_spi_t *card, uint32_t retry_us)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t retry_timeout = xtimer_now_usec() + retry_us;
|
|
|
|
|
uint32_t retry_timeout = _deadline_from_interval(retry_us);
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
uint8_t read_byte;
|
|
|
|
|
@ -392,7 +411,7 @@ static inline bool _wait_for_not_busy(sdcard_spi_t *card, uint32_t retry_us)
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout));
|
|
|
|
|
} while (retry_us && _deadline_left(retry_timeout));
|
|
|
|
|
|
|
|
|
|
DEBUG("_wait_for_not_busy: [FAILED]\n");
|
|
|
|
|
return false;
|
|
|
|
|
@ -415,9 +434,10 @@ static uint8_t _crc_7(const uint8_t *data, int n)
|
|
|
|
|
return (crc << 1) | 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t sdcard_spi_send_cmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument, uint32_t retry_us)
|
|
|
|
|
uint8_t sdcard_spi_send_cmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument,
|
|
|
|
|
uint32_t retry_us)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t retry_timeout = xtimer_now_usec() + retry_us;
|
|
|
|
|
uint32_t retry_timeout = _deadline_from_interval(retry_us);
|
|
|
|
|
|
|
|
|
|
uint8_t r1_resu;
|
|
|
|
|
uint8_t cmd_data[6];
|
|
|
|
|
@ -432,8 +452,9 @@ uint8_t sdcard_spi_send_cmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t arg
|
|
|
|
|
uint8_t echo[sizeof(cmd_data)];
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
DEBUG("sdcard_spi_send_cmd: CMD%02d (0x%08" PRIx32 ") (remaining retry time %"PRIu32" usec)\n", sd_cmd_idx, argument,
|
|
|
|
|
(retry_timeout > xtimer_now_usec()) ? (retry_timeout - xtimer_now_usec()) : 0);
|
|
|
|
|
DEBUG(
|
|
|
|
|
"sdcard_spi_send_cmd: CMD%02d (0x%08" PRIx32 ") (remaining retry time %" PRIu32 " usec)\n", sd_cmd_idx, argument,
|
|
|
|
|
(retry_timeout > ztimer_now(ZTIMER_USEC)) ? (uint32_t)(retry_timeout - ztimer_now(ZTIMER_USEC)) : 0);
|
|
|
|
|
|
|
|
|
|
if (!_wait_for_not_busy(card, SD_WAIT_FOR_NOT_BUSY_US)) {
|
|
|
|
|
DEBUG("sdcard_spi_send_cmd: timeout while waiting for bus to be not busy!\n");
|
|
|
|
|
@ -468,20 +489,21 @@ uint8_t sdcard_spi_send_cmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t arg
|
|
|
|
|
r1_resu = SD_INVALID_R1_RESPONSE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout));
|
|
|
|
|
} while (retry_us && _deadline_left(retry_timeout));
|
|
|
|
|
|
|
|
|
|
return r1_resu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uint8_t sdcard_spi_send_acmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument, uint32_t retry_us)
|
|
|
|
|
uint8_t sdcard_spi_send_acmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t argument,
|
|
|
|
|
uint32_t retry_us)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t retry_timeout = xtimer_now_usec() + retry_us;
|
|
|
|
|
uint32_t retry_timeout = _deadline_from_interval(retry_us);
|
|
|
|
|
|
|
|
|
|
uint8_t r1_resu;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
DEBUG("sdcard_spi_send_acmd: CMD%02d (0x%08" PRIx32 ") (remaining retry time %" PRIu32 " usec)\n", sd_cmd_idx, argument,
|
|
|
|
|
(retry_timeout > xtimer_now_usec()) ? (retry_timeout - xtimer_now_usec()) : 0);
|
|
|
|
|
(retry_timeout > ztimer_now(ZTIMER_USEC)) ? (uint32_t)(retry_timeout - ztimer_now(ZTIMER_USEC)) : 0);
|
|
|
|
|
r1_resu = sdcard_spi_send_cmd(card, SD_CMD_55, SD_CMD_NO_ARG, 0);
|
|
|
|
|
if (R1_VALID(r1_resu) && !R1_ERROR(r1_resu)) {
|
|
|
|
|
r1_resu = sdcard_spi_send_cmd(card, sd_cmd_idx, argument, 0);
|
|
|
|
|
@ -497,7 +519,7 @@ uint8_t sdcard_spi_send_acmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t ar
|
|
|
|
|
DEBUG("CMD55: [ERROR / NO RESPONSE]\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout));
|
|
|
|
|
} while (retry_us && _deadline_left(retry_timeout));
|
|
|
|
|
|
|
|
|
|
DEBUG("sdcard_spi_send_acmd: [TIMEOUT]\n");
|
|
|
|
|
return r1_resu;
|
|
|
|
|
@ -505,7 +527,7 @@ uint8_t sdcard_spi_send_acmd(sdcard_spi_t *card, uint8_t sd_cmd_idx, uint32_t ar
|
|
|
|
|
|
|
|
|
|
static inline uint8_t _wait_for_r1(sdcard_spi_t *card, uint32_t retry_us)
|
|
|
|
|
{
|
|
|
|
|
const uint32_t retry_timeout = xtimer_now_usec() + retry_us;
|
|
|
|
|
uint32_t retry_timeout = _deadline_from_interval(retry_us);
|
|
|
|
|
|
|
|
|
|
uint8_t r1;
|
|
|
|
|
|
|
|
|
|
@ -523,7 +545,7 @@ static inline uint8_t _wait_for_r1(sdcard_spi_t *card, uint32_t retry_us)
|
|
|
|
|
return r1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout));
|
|
|
|
|
} while (retry_us && _deadline_left(retry_timeout));
|
|
|
|
|
|
|
|
|
|
DEBUG("_wait_for_r1: [TIMEOUT]\n");
|
|
|
|
|
return r1;
|
|
|
|
|
@ -542,31 +564,37 @@ void _unselect_card_spi(sdcard_spi_t *card)
|
|
|
|
|
spi_release(card->params.spi_dev);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int _sw_spi_rxtx_byte(sdcard_spi_t *card, uint8_t out, uint8_t *in){
|
|
|
|
|
static inline int _sw_spi_rxtx_byte(sdcard_spi_t *card, uint8_t out, uint8_t *in)
|
|
|
|
|
{
|
|
|
|
|
uint8_t rx = 0;
|
|
|
|
|
int i = 7;
|
|
|
|
|
|
|
|
|
|
for (; i >= 0; i--) {
|
|
|
|
|
if (((out >> (i)) & 0x01) == 1) {
|
|
|
|
|
gpio_set(card->params.mosi);
|
|
|
|
|
}else{
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
gpio_clear(card->params.mosi);
|
|
|
|
|
}
|
|
|
|
|
xtimer_usleep(SD_CARD_PREINIT_CLOCK_PERIOD_US/2);
|
|
|
|
|
ztimer_sleep(ZTIMER_USEC, SD_CARD_PREINIT_CLOCK_PERIOD_US / 2);
|
|
|
|
|
gpio_set(card->params.clk);
|
|
|
|
|
rx = (rx | ((gpio_read(card->params.miso) > 0) << i));
|
|
|
|
|
xtimer_usleep(SD_CARD_PREINIT_CLOCK_PERIOD_US/2);
|
|
|
|
|
ztimer_sleep(ZTIMER_USEC, SD_CARD_PREINIT_CLOCK_PERIOD_US / 2);
|
|
|
|
|
gpio_clear(card->params.clk);
|
|
|
|
|
}
|
|
|
|
|
*in = rx;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int _hw_spi_rxtx_byte(sdcard_spi_t *card, uint8_t out, uint8_t *in){
|
|
|
|
|
static inline int _hw_spi_rxtx_byte(sdcard_spi_t *card, uint8_t out, uint8_t *in)
|
|
|
|
|
{
|
|
|
|
|
*in = spi_transfer_byte(card->params.spi_dev, GPIO_UNDEF, true, out);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int _transfer_bytes(sdcard_spi_t *card, const uint8_t *out, uint8_t *in, unsigned int length){
|
|
|
|
|
static inline int _transfer_bytes(sdcard_spi_t *card, const uint8_t *out, uint8_t *in,
|
|
|
|
|
unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
int trans_ret;
|
|
|
|
|
unsigned trans_bytes = 0;
|
|
|
|
|
uint8_t in_temp;
|
|
|
|
|
@ -589,7 +617,8 @@ static inline int _transfer_bytes(sdcard_spi_t *card, const uint8_t *out, uint8_
|
|
|
|
|
return trans_bytes;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sd_rw_response_t _read_data_packet(sdcard_spi_t *card, uint8_t token, uint8_t *data, int size)
|
|
|
|
|
static sd_rw_response_t _read_data_packet(sdcard_spi_t *card, uint8_t token, uint8_t *data,
|
|
|
|
|
int size)
|
|
|
|
|
{
|
|
|
|
|
DEBUG("_read_data_packet: size: %d\n", size);
|
|
|
|
|
if (_wait_for_token(card, token, SD_DATA_TOKEN_RETRY_US) == true) {
|
|
|
|
|
@ -693,7 +722,8 @@ int sdcard_spi_read_blocks(sdcard_spi_t *card, int blockaddr, uint8_t *data, int
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, const uint8_t *data, int size)
|
|
|
|
|
static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, const uint8_t *data,
|
|
|
|
|
int size)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
spi_transfer_byte(card->params.spi_dev, GPIO_UNDEF, true, token);
|
|
|
|
|
@ -746,7 +776,8 @@ static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, co
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static inline int _write_blocks(sdcard_spi_t *card, uint8_t cmd_idx, int bladdr, const uint8_t *data, int blsz,
|
|
|
|
|
static inline int _write_blocks(sdcard_spi_t *card, uint8_t cmd_idx, int bladdr,
|
|
|
|
|
const uint8_t *data, int blsz,
|
|
|
|
|
int nbl, sd_rw_response_t *state)
|
|
|
|
|
{
|
|
|
|
|
_select_card_spi(card);
|
|
|
|
|
@ -840,6 +871,7 @@ sd_rw_response_t _read_cid(sdcard_spi_t *card)
|
|
|
|
|
DEBUG("\n");
|
|
|
|
|
|
|
|
|
|
uint8_t crc7 = _crc_7(&(cid_raw_data[0]), SD_SIZE_OF_CID_AND_CSD_REG - 1);
|
|
|
|
|
|
|
|
|
|
if (nbl == SD_BLOCKS_FOR_REG_READ) {
|
|
|
|
|
if (crc7 == cid_raw_data[SD_SIZE_OF_CID_AND_CSD_REG - 1]) {
|
|
|
|
|
card->cid.MID = cid_raw_data[0];
|
|
|
|
|
@ -951,10 +983,12 @@ sd_rw_response_t _read_csd(sdcard_spi_t *card)
|
|
|
|
|
return state;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sd_rw_response_t sdcard_spi_read_sds(sdcard_spi_t *card, sd_status_t *sd_status){
|
|
|
|
|
sd_rw_response_t sdcard_spi_read_sds(sdcard_spi_t *card, sd_status_t *sd_status)
|
|
|
|
|
{
|
|
|
|
|
_select_card_spi(card);
|
|
|
|
|
uint8_t sds_raw_data[SD_SIZE_OF_SD_STATUS];
|
|
|
|
|
uint8_t r1_resu = sdcard_spi_send_cmd(card, SD_CMD_55, SD_CMD_NO_ARG, 0);
|
|
|
|
|
|
|
|
|
|
_unselect_card_spi(card);
|
|
|
|
|
if (R1_VALID(r1_resu)) {
|
|
|
|
|
if (!R1_ERROR(r1_resu)) {
|
|
|
|
|
@ -1024,6 +1058,7 @@ uint32_t sdcard_spi_get_sector_count(sdcard_spi_t *card)
|
|
|
|
|
uint32_t sdcard_spi_get_au_size(sdcard_spi_t *card)
|
|
|
|
|
{
|
|
|
|
|
sd_status_t sds;
|
|
|
|
|
|
|
|
|
|
if (sdcard_spi_read_sds(card, &sds) == SD_RW_OK) {
|
|
|
|
|
if (sds.AU_SIZE < 0xB) {
|
|
|
|
|
return 1 << (13 + sds.AU_SIZE); /* sds->AU_SIZE = 1 maps to 16KB; 2 to 32KB etc.*/
|
|
|
|
|
|