Merge pull request #17361 from fjmolinas/pr_sdcard_ztimer

drivers/sdcard_spi: convert to ztimer_usec
This commit is contained in:
Koen Zandberg 2021-12-09 17:44:48 +01:00 committed by GitHub
commit d0eb8e2f7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 361 additions and 321 deletions

View File

@ -13,5 +13,5 @@ config MODULE_SDCARD_SPI
select MODULE_PERIPH_GPIO select MODULE_PERIPH_GPIO
select MODULE_PERIPH_SPI select MODULE_PERIPH_SPI
select MODULE_PERIPH_SPI_RECONFIGURE if HAS_PERIPH_SPI_RECONFIGURE select MODULE_PERIPH_SPI_RECONFIGURE if HAS_PERIPH_SPI_RECONFIGURE
select MODULE_XTIMER
select MODULE_CHECKSUM select MODULE_CHECKSUM
select ZTIMER_USEC

View File

@ -2,4 +2,5 @@ FEATURES_REQUIRED += periph_gpio
FEATURES_REQUIRED += periph_spi FEATURES_REQUIRED += periph_spi
FEATURES_OPTIONAL += periph_spi_reconfigure FEATURES_OPTIONAL += periph_spi_reconfigure
USEMODULE += checksum USEMODULE += checksum
USEMODULE += xtimer USEMODULE += ztimer
USEMODULE += ztimer_usec

View File

@ -32,6 +32,7 @@ extern "C" {
#include "periph/gpio.h" #include "periph/gpio.h"
#include "stdbool.h" #include "stdbool.h"
#include "sdcard_spi.h" #include "sdcard_spi.h"
#include "timex.h"
/* number of clocks that should be applied to the card on init /* number of clocks that should be applied to the card on init
before taking further actions (see sd spec. 6.4.1.1 Power Up Time of Card) */ before taking further actions (see sd spec. 6.4.1.1 Power Up Time of Card) */

View File

@ -25,7 +25,7 @@
#include "periph/spi.h" #include "periph/spi.h"
#include "periph/gpio.h" #include "periph/gpio.h"
#include "checksum/ucrc16.h" #include "checksum/ucrc16.h"
#include "xtimer.h" #include "ztimer.h"
#include <stdio.h> #include <stdio.h>
#include <string.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_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_cid(sdcard_spi_t *card);
static sd_rw_response_t _read_csd(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 _read_data_packet(sdcard_spi_t *card, uint8_t token, uint8_t *data,
static sd_rw_response_t _write_data_packet(sdcard_spi_t *card, uint8_t token, const uint8_t *data, int size); 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 */ /* number of used sd cards */
#define SDCARD_SPI_NUM ARRAY_SIZE(sdcard_spi_params) #define SDCARD_SPI_NUM ARRAY_SIZE(sdcard_spi_params)
@ -53,10 +55,11 @@ sdcard_spi_t sdcard_spi_devs[SDCARD_SPI_NUM];
static uint8_t _crc_7(const uint8_t *data, int n); 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 */ /* 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 /* 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 */ greater card compatibility on platforms that don't have a hw pull up installed */
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);
/* wrapper for default spi_transfer_byte function */ /* wrapper for default spi_transfer_byte function */
@ -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 */ /* 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 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) int sdcard_spi_init(sdcard_spi_t *card, const sdcard_spi_params_t *params)
{ {
sd_init_fsm_state_t state = SD_INIT_START; sd_init_fsm_state_t state = SD_INIT_START;
card->params = *params; card->params = *params;
card->spi_clk = SD_CARD_SPI_SPEED_PREINIT; card->spi_clk = SD_CARD_SPI_SPEED_PREINIT;
@ -97,8 +116,8 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
(gpio_init(card->params.clk, GPIO_OUT) == 0) && (gpio_init(card->params.clk, GPIO_OUT) == 0) &&
(gpio_init(card->params.cs, GPIO_OUT) == 0) && (gpio_init(card->params.cs, GPIO_OUT) == 0) &&
(gpio_init(card->params.miso, GPIO_IN_PU) == 0) && (gpio_init(card->params.miso, GPIO_IN_PU) == 0) &&
( (!gpio_is_valid(card->params.power)) || ((!gpio_is_valid(card->params.power)) ||
(gpio_init(card->params.power, GPIO_OUT) == 0)) ) { (gpio_init(card->params.power, GPIO_OUT) == 0))) {
DEBUG("gpio_init(): [OK]\n"); DEBUG("gpio_init(): [OK]\n");
return SD_INIT_SPI_POWER_SEQ; return SD_INIT_SPI_POWER_SEQ;
@ -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)) { if (gpio_is_valid(card->params.power)) {
gpio_write(card->params.power, card->params.power_act_high); 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); 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) */ * (same as sending dummy bytes with 0xFF) */
for (int i = 0; i < SD_POWERSEQUENCE_CLOCK_COUNT; i += 1) { for (int i = 0; i < SD_POWERSEQUENCE_CLOCK_COUNT; i += 1) {
gpio_set(card->params.clk); 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); 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; 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"); DEBUG("CMD8: [R7 MISMATCH]\n");
_unselect_card_spi(card); _unselect_card_spi(card);
return SD_INIT_CARD_UNKNOWN;; return SD_INIT_CARD_UNKNOWN;
} }
DEBUG("CMD8: _transfer_bytes (R7): [ERROR]\n"); 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: case SD_INIT_SEND_ACMD41_HCS:
DEBUG("SD_INIT_SEND_ACMD41_HCS\n"); 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 { do {
uint8_t acmd41hcs_r1 = sdcard_spi_send_acmd(card, SD_CMD_41, SD_ACMD_41_ARG_HC, 0); 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) && 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"); DEBUG("ACMD41: [OK]\n");
return SD_INIT_SEND_CMD58; 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); _unselect_card_spi(card);
return SD_INIT_CARD_UNKNOWN; return SD_INIT_CARD_UNKNOWN;
case SD_INIT_SEND_ACMD41: case SD_INIT_SEND_ACMD41:
DEBUG("SD_INIT_SEND_ACMD41\n"); 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 { do {
uint8_t acmd41_r1 = sdcard_spi_send_acmd(card, SD_CMD_41, SD_CMD_NO_ARG, 0); 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)) { 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; card->card_type = SD_V1;
return SD_INIT_SEND_CMD16; 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"); DEBUG("ACMD41: [ERROR]\n");
return SD_INIT_SEND_CMD1; return SD_INIT_SEND_CMD1;
@ -251,7 +270,7 @@ static sd_init_fsm_state_t _init_sd_fsm_step(sdcard_spi_t *card, sd_init_fsm_sta
uint32_t ocr = ((uint32_t)r3[0] << (3 * 8)) | uint32_t ocr = ((uint32_t)r3[0] << (3 * 8)) |
((uint32_t)r3[1] << (2 * 8)) | (r3[2] << 8) | r3[3]; ((uint32_t)r3[1] << (2 * 8)) | (r3[2] << 8) | r3[3];
DEBUG("R3 RESPONSE: 0x%02x 0x%02x 0x%02x 0x%02x\n", r3[0], r3[1], r3[2], r3[3]); DEBUG("R3 RESPONSE: 0x%02x 0x%02x 0x%02x 0x%02x\n", r3[0], r3[1], r3[2], r3[3]);
DEBUG("OCR: 0x%"PRIx32"\n", ocr); DEBUG("OCR: 0x%" PRIx32 "\n", ocr);
if ((ocr & SYSTEM_VOLTAGE) != 0) { if ((ocr & SYSTEM_VOLTAGE) != 0) {
DEBUG("OCR: SYS VOLTAGE SUPPORTED\n"); DEBUG("OCR: SYS VOLTAGE SUPPORTED\n");
@ -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) 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 { do {
uint8_t read_byte = 0; 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); 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; 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) 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 { do {
uint8_t read_byte; 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; 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"); DEBUG("_wait_for_not_busy: [FAILED]\n");
return false; return false;
@ -415,9 +434,10 @@ static uint8_t _crc_7(const uint8_t *data, int n)
return (crc << 1) | 1; 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 r1_resu;
uint8_t cmd_data[6]; 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)]; uint8_t echo[sizeof(cmd_data)];
do { do {
DEBUG("sdcard_spi_send_cmd: CMD%02d (0x%08" PRIx32 ") (remaining retry time %"PRIu32" usec)\n", sd_cmd_idx, argument, DEBUG(
(retry_timeout > xtimer_now_usec()) ? (retry_timeout - xtimer_now_usec()) : 0); "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)) { 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"); 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; 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; 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; uint8_t r1_resu;
do { do {
DEBUG("sdcard_spi_send_acmd: CMD%02d (0x%08" PRIx32 ") (remaining retry time %"PRIu32" usec)\n", sd_cmd_idx, argument, 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); r1_resu = sdcard_spi_send_cmd(card, SD_CMD_55, SD_CMD_NO_ARG, 0);
if (R1_VALID(r1_resu) && !R1_ERROR(r1_resu)) { if (R1_VALID(r1_resu) && !R1_ERROR(r1_resu)) {
r1_resu = sdcard_spi_send_cmd(card, sd_cmd_idx, argument, 0); 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"); 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"); DEBUG("sdcard_spi_send_acmd: [TIMEOUT]\n");
return r1_resu; 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) 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; uint8_t r1;
@ -523,7 +545,7 @@ static inline uint8_t _wait_for_r1(sdcard_spi_t *card, uint32_t retry_us)
return r1; return r1;
} }
} while ((retry_us > 0) && (xtimer_now_usec() < retry_timeout)); } while (retry_us && _deadline_left(retry_timeout));
DEBUG("_wait_for_r1: [TIMEOUT]\n"); DEBUG("_wait_for_r1: [TIMEOUT]\n");
return r1; return r1;
@ -542,31 +564,37 @@ void _unselect_card_spi(sdcard_spi_t *card)
spi_release(card->params.spi_dev); 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; uint8_t rx = 0;
int i = 7; int i = 7;
for(; i >= 0; i--){
if( ((out >> (i)) & 0x01) == 1){ for (; i >= 0; i--) {
if (((out >> (i)) & 0x01) == 1) {
gpio_set(card->params.mosi); gpio_set(card->params.mosi);
}else{ }
else {
gpio_clear(card->params.mosi); 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); gpio_set(card->params.clk);
rx = (rx | ((gpio_read(card->params.miso) > 0) << i)); 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); gpio_clear(card->params.clk);
} }
*in = rx; *in = rx;
return 1; 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); *in = spi_transfer_byte(card->params.spi_dev, GPIO_UNDEF, true, out);
return 1; 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; int trans_ret;
unsigned trans_bytes = 0; unsigned trans_bytes = 0;
uint8_t in_temp; 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; 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); DEBUG("_read_data_packet: size: %d\n", size);
if (_wait_for_token(card, token, SD_DATA_TOKEN_RETRY_US) == true) { 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); 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) int nbl, sd_rw_response_t *state)
{ {
_select_card_spi(card); _select_card_spi(card);
@ -840,6 +871,7 @@ sd_rw_response_t _read_cid(sdcard_spi_t *card)
DEBUG("\n"); DEBUG("\n");
uint8_t crc7 = _crc_7(&(cid_raw_data[0]), SD_SIZE_OF_CID_AND_CSD_REG - 1); 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 (nbl == SD_BLOCKS_FOR_REG_READ) {
if (crc7 == cid_raw_data[SD_SIZE_OF_CID_AND_CSD_REG - 1]) { if (crc7 == cid_raw_data[SD_SIZE_OF_CID_AND_CSD_REG - 1]) {
card->cid.MID = cid_raw_data[0]; card->cid.MID = cid_raw_data[0];
@ -847,7 +879,7 @@ sd_rw_response_t _read_cid(sdcard_spi_t *card)
memcpy(&card->cid.PNM[0], &cid_raw_data[2], SD_SIZE_OF_PNM); memcpy(&card->cid.PNM[0], &cid_raw_data[2], SD_SIZE_OF_PNM);
card->cid.PRV = cid_raw_data[8]; card->cid.PRV = cid_raw_data[8];
memcpy((uint8_t *)&card->cid.PSN, &cid_raw_data[9], 4); memcpy((uint8_t *)&card->cid.PSN, &cid_raw_data[9], 4);
card->cid.MDT = (cid_raw_data[13]<<4) | cid_raw_data[14]; card->cid.MDT = (cid_raw_data[13] << 4) | cid_raw_data[14];
card->cid.CID_CRC = cid_raw_data[15]; card->cid.CID_CRC = cid_raw_data[15];
DEBUG("_read_cid: [OK]\n"); DEBUG("_read_cid: [OK]\n");
return SD_RW_OK; return SD_RW_OK;
@ -878,63 +910,63 @@ sd_rw_response_t _read_csd(sdcard_spi_t *card)
if (read_resu == SD_BLOCKS_FOR_REG_READ) { if (read_resu == SD_BLOCKS_FOR_REG_READ) {
if (_crc_7(c, SD_SIZE_OF_CID_AND_CSD_REG - 1) == c[SD_SIZE_OF_CID_AND_CSD_REG - 1]) { if (_crc_7(c, SD_SIZE_OF_CID_AND_CSD_REG - 1) == c[SD_SIZE_OF_CID_AND_CSD_REG - 1]) {
if (SD_GET_CSD_STRUCTURE(c) == SD_CSD_V1) { if (SD_GET_CSD_STRUCTURE(c) == SD_CSD_V1) {
card->csd.v1.CSD_STRUCTURE = c[0]>>6; card->csd.v1.CSD_STRUCTURE = c[0] >> 6;
card->csd.v1.TAAC = c[1]; card->csd.v1.TAAC = c[1];
card->csd.v1.NSAC = c[2]; card->csd.v1.NSAC = c[2];
card->csd.v1.TRAN_SPEED = c[3]; card->csd.v1.TRAN_SPEED = c[3];
card->csd.v1.CCC = (c[4]<<4) | ((c[5] & 0xF0)>>4); card->csd.v1.CCC = (c[4] << 4) | ((c[5] & 0xF0) >> 4);
card->csd.v1.READ_BL_LEN = (c[5] & 0x0F); card->csd.v1.READ_BL_LEN = (c[5] & 0x0F);
card->csd.v1.READ_BL_PARTIAL = (c[6] & (1<<7))>>7; card->csd.v1.READ_BL_PARTIAL = (c[6] & (1 << 7)) >> 7;
card->csd.v1.WRITE_BLK_MISALIGN = (c[6] & (1<<6))>>6; card->csd.v1.WRITE_BLK_MISALIGN = (c[6] & (1 << 6)) >> 6;
card->csd.v1.READ_BLK_MISALIGN = (c[6] & (1<<5))>>5; card->csd.v1.READ_BLK_MISALIGN = (c[6] & (1 << 5)) >> 5;
card->csd.v1.DSR_IMP = (c[6] & (1<<4))>>4; card->csd.v1.DSR_IMP = (c[6] & (1 << 4)) >> 4;
card->csd.v1.C_SIZE = ((c[6] & 0x03)<<10) | (c[7]<<2) | (c[8]>>6); card->csd.v1.C_SIZE = ((c[6] & 0x03) << 10) | (c[7] << 2) | (c[8] >> 6);
card->csd.v1.VDD_R_CURR_MIN = (c[8] & 0x38)>>3; card->csd.v1.VDD_R_CURR_MIN = (c[8] & 0x38) >> 3;
card->csd.v1.VDD_R_CURR_MAX = (c[8] & 0x07); card->csd.v1.VDD_R_CURR_MAX = (c[8] & 0x07);
card->csd.v1.VDD_W_CURR_MIN = (c[9] & 0xE0)>>5; card->csd.v1.VDD_W_CURR_MIN = (c[9] & 0xE0) >> 5;
card->csd.v1.VDD_W_CURR_MAX = (c[9] & 0x1C)>>2; card->csd.v1.VDD_W_CURR_MAX = (c[9] & 0x1C) >> 2;
card->csd.v1.C_SIZE_MULT = ((c[9] & 0x03)<<1) | (c[10]>>7); card->csd.v1.C_SIZE_MULT = ((c[9] & 0x03) << 1) | (c[10] >> 7);
card->csd.v1.ERASE_BLK_EN = (c[10] & (1<<6))>>6; card->csd.v1.ERASE_BLK_EN = (c[10] & (1 << 6)) >> 6;
card->csd.v1.SECTOR_SIZE = ((c[10] & 0x3F)<<1) | (c[11]>>7); card->csd.v1.SECTOR_SIZE = ((c[10] & 0x3F) << 1) | (c[11] >> 7);
card->csd.v1.WP_GRP_SIZE = (c[11] & 0x7F); card->csd.v1.WP_GRP_SIZE = (c[11] & 0x7F);
card->csd.v1.WP_GRP_ENABLE = c[12]>>7; card->csd.v1.WP_GRP_ENABLE = c[12] >> 7;
card->csd.v1.R2W_FACTOR = (c[12] & 0x1C)>>2; card->csd.v1.R2W_FACTOR = (c[12] & 0x1C) >> 2;
card->csd.v1.WRITE_BL_LEN = (c[12] & 0x03)<<2 | (c[13]>>6); card->csd.v1.WRITE_BL_LEN = (c[12] & 0x03) << 2 | (c[13] >> 6);
card->csd.v1.WRITE_BL_PARTIAL = (c[13] & (1<<5))>>5; card->csd.v1.WRITE_BL_PARTIAL = (c[13] & (1 << 5)) >> 5;
card->csd.v1.FILE_FORMAT_GRP = (c[14] & (1<<7))>>7; card->csd.v1.FILE_FORMAT_GRP = (c[14] & (1 << 7)) >> 7;
card->csd.v1.COPY = (c[14] & (1<<6))>>6; card->csd.v1.COPY = (c[14] & (1 << 6)) >> 6;
card->csd.v1.PERM_WRITE_PROTECT = (c[14] & (1<<5))>>5; card->csd.v1.PERM_WRITE_PROTECT = (c[14] & (1 << 5)) >> 5;
card->csd.v1.TMP_WRITE_PROTECT = (c[14] & (1<<4))>>4; card->csd.v1.TMP_WRITE_PROTECT = (c[14] & (1 << 4)) >> 4;
card->csd.v1.FILE_FORMAT = (c[14] & 0x0C)>>2; card->csd.v1.FILE_FORMAT = (c[14] & 0x0C) >> 2;
card->csd.v1.CSD_CRC = c[15]; card->csd.v1.CSD_CRC = c[15];
card->csd_structure = SD_CSD_V1; card->csd_structure = SD_CSD_V1;
return SD_RW_OK; return SD_RW_OK;
} }
else if (SD_GET_CSD_STRUCTURE(c) == SD_CSD_V2) { else if (SD_GET_CSD_STRUCTURE(c) == SD_CSD_V2) {
card->csd.v2.CSD_STRUCTURE = c[0]>>6; card->csd.v2.CSD_STRUCTURE = c[0] >> 6;
card->csd.v2.TAAC = c[1]; card->csd.v2.TAAC = c[1];
card->csd.v2.NSAC = c[2]; card->csd.v2.NSAC = c[2];
card->csd.v2.TRAN_SPEED = c[3]; card->csd.v2.TRAN_SPEED = c[3];
card->csd.v2.CCC = (c[4]<<4) | ((c[5] & 0xF0)>>4); card->csd.v2.CCC = (c[4] << 4) | ((c[5] & 0xF0) >> 4);
card->csd.v2.READ_BL_LEN = (c[5] & 0x0F); card->csd.v2.READ_BL_LEN = (c[5] & 0x0F);
card->csd.v2.READ_BL_PARTIAL = (c[6] & (1<<7))>>7; card->csd.v2.READ_BL_PARTIAL = (c[6] & (1 << 7)) >> 7;
card->csd.v2.WRITE_BLK_MISALIGN = (c[6] & (1<<6))>>6; card->csd.v2.WRITE_BLK_MISALIGN = (c[6] & (1 << 6)) >> 6;
card->csd.v2.READ_BLK_MISALIGN = (c[6] & (1<<5))>>5; card->csd.v2.READ_BLK_MISALIGN = (c[6] & (1 << 5)) >> 5;
card->csd.v2.DSR_IMP = (c[6] & (1<<4))>>4; card->csd.v2.DSR_IMP = (c[6] & (1 << 4)) >> 4;
card->csd.v2.C_SIZE = (((uint32_t)c[7] & 0x3F)<<16) card->csd.v2.C_SIZE = (((uint32_t)c[7] & 0x3F) << 16)
| (c[8]<<8) | c[9]; | (c[8] << 8) | c[9];
card->csd.v2.ERASE_BLK_EN = (c[10] & (1<<6))>>6; card->csd.v2.ERASE_BLK_EN = (c[10] & (1 << 6)) >> 6;
card->csd.v2.SECTOR_SIZE = (c[10] & 0x3F)<<1 | (c[11]>>7); card->csd.v2.SECTOR_SIZE = (c[10] & 0x3F) << 1 | (c[11] >> 7);
card->csd.v2.WP_GRP_SIZE = (c[11] & 0x7F); card->csd.v2.WP_GRP_SIZE = (c[11] & 0x7F);
card->csd.v2.WP_GRP_ENABLE = (c[12] & (1<<7))>> 7; card->csd.v2.WP_GRP_ENABLE = (c[12] & (1 << 7)) >> 7;
card->csd.v2.R2W_FACTOR = (c[12] & 0x1C)>> 2; card->csd.v2.R2W_FACTOR = (c[12] & 0x1C) >> 2;
card->csd.v2.WRITE_BL_LEN = ((c[12] & 0x03)<<2) | (c[13]>>6); card->csd.v2.WRITE_BL_LEN = ((c[12] & 0x03) << 2) | (c[13] >> 6);
card->csd.v2.WRITE_BL_PARTIAL = (c[13] & (1<<5))>>5; card->csd.v2.WRITE_BL_PARTIAL = (c[13] & (1 << 5)) >> 5;
card->csd.v2.FILE_FORMAT_GRP = (c[14] & (1<<7))>>7; card->csd.v2.FILE_FORMAT_GRP = (c[14] & (1 << 7)) >> 7;
card->csd.v2.COPY = (c[14] & (1<<6))>>6; card->csd.v2.COPY = (c[14] & (1 << 6)) >> 6;
card->csd.v2.PERM_WRITE_PROTECT = (c[14] & (1<<5))>>5; card->csd.v2.PERM_WRITE_PROTECT = (c[14] & (1 << 5)) >> 5;
card->csd.v2.TMP_WRITE_PROTECT = (c[14] & (1<<4))>>4; card->csd.v2.TMP_WRITE_PROTECT = (c[14] & (1 << 4)) >> 4;
card->csd.v2.FILE_FORMAT = (c[14] & 0x0C)>>2; card->csd.v2.FILE_FORMAT = (c[14] & 0x0C) >> 2;
card->csd.v2.CSD_CRC = c[15]; card->csd.v2.CSD_CRC = c[15];
card->csd_structure = SD_CSD_V2; card->csd_structure = SD_CSD_V2;
return SD_RW_OK; return SD_RW_OK;
@ -951,13 +983,15 @@ sd_rw_response_t _read_csd(sdcard_spi_t *card)
return state; 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); _select_card_spi(card);
uint8_t sds_raw_data[SD_SIZE_OF_SD_STATUS]; 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); uint8_t r1_resu = sdcard_spi_send_cmd(card, SD_CMD_55, SD_CMD_NO_ARG, 0);
_unselect_card_spi(card); _unselect_card_spi(card);
if (R1_VALID(r1_resu)) { if (R1_VALID(r1_resu)) {
if(!R1_ERROR(r1_resu)){ if (!R1_ERROR(r1_resu)) {
sd_rw_response_t state; sd_rw_response_t state;
int nbl = _read_blocks(card, SD_CMD_13, 0, sds_raw_data, SD_SIZE_OF_SD_STATUS, int nbl = _read_blocks(card, SD_CMD_13, 0, sds_raw_data, SD_SIZE_OF_SD_STATUS,
@ -972,11 +1006,11 @@ sd_rw_response_t sdcard_spi_read_sds(sdcard_spi_t *card, sd_status_t *sd_status)
if (nbl == SD_BLOCKS_FOR_REG_READ) { if (nbl == SD_BLOCKS_FOR_REG_READ) {
sd_status->DAT_BUS_WIDTH = sds_raw_data[0] >> 6; sd_status->DAT_BUS_WIDTH = sds_raw_data[0] >> 6;
sd_status->SECURED_MODE = (sds_raw_data[0] & (1<<5)) >> 5; sd_status->SECURED_MODE = (sds_raw_data[0] & (1 << 5)) >> 5;
sd_status->SD_CARD_TYPE = (sds_raw_data[2] << 8) | sds_raw_data[3]; sd_status->SD_CARD_TYPE = (sds_raw_data[2] << 8) | sds_raw_data[3];
sd_status->SIZE_OF_PROTECTED_AREA = ((uint32_t)sds_raw_data[4] << (3*8)) | sd_status->SIZE_OF_PROTECTED_AREA = ((uint32_t)sds_raw_data[4] << (3 * 8)) |
((uint32_t)sds_raw_data[5] << (2*8)) | ((uint32_t)sds_raw_data[5] << (2 * 8)) |
(sds_raw_data[6] << 8 ) | (sds_raw_data[6] << 8) |
sds_raw_data[7]; sds_raw_data[7];
sd_status->SPEED_CLASS = sds_raw_data[8]; sd_status->SPEED_CLASS = sds_raw_data[8];
sd_status->PERFORMANCE_MOVE = sds_raw_data[9]; sd_status->PERFORMANCE_MOVE = sds_raw_data[9];
@ -990,8 +1024,8 @@ sd_rw_response_t sdcard_spi_read_sds(sdcard_spi_t *card, sd_status_t *sd_status)
sd_status->VSC_AU_SIZE = ((sds_raw_data[16] & 0x03) << 8) sd_status->VSC_AU_SIZE = ((sds_raw_data[16] & 0x03) << 8)
| sds_raw_data[17]; | sds_raw_data[17];
sd_status->SUS_ADDR = (sds_raw_data[18] << 14) | sd_status->SUS_ADDR = (sds_raw_data[18] << 14) |
(sds_raw_data[19] << 6 ) | (sds_raw_data[19] << 6) |
(sds_raw_data[20] >> 2 ); (sds_raw_data[20] >> 2);
DEBUG("sdcard_spi_read_sds: [OK]\n"); DEBUG("sdcard_spi_read_sds: [OK]\n");
return SD_RW_OK; return SD_RW_OK;
} }
@ -1024,7 +1058,8 @@ uint32_t sdcard_spi_get_sector_count(sdcard_spi_t *card)
uint32_t sdcard_spi_get_au_size(sdcard_spi_t *card) uint32_t sdcard_spi_get_au_size(sdcard_spi_t *card)
{ {
sd_status_t sds; sd_status_t sds;
if(sdcard_spi_read_sds(card, &sds) == SD_RW_OK) {
if (sdcard_spi_read_sds(card, &sds) == SD_RW_OK) {
if (sds.AU_SIZE < 0xB) { if (sds.AU_SIZE < 0xB) {
return 1 << (13 + sds.AU_SIZE); /* sds->AU_SIZE = 1 maps to 16KB; 2 to 32KB etc.*/ return 1 << (13 + sds.AU_SIZE); /* sds->AU_SIZE = 1 maps to 16KB; 2 to 32KB etc.*/
} }

View File

@ -1,6 +1,6 @@
include ../Makefile.tests_common include ../Makefile.tests_common
USEMODULE += io1_xplained USEMODULE += io1_xplained
USEMODULE += xtimer USEMODULE += ztimer_msec
include $(RIOTBASE)/Makefile.include include $(RIOTBASE)/Makefile.include

View File

@ -3,3 +3,5 @@
CONFIG_MODULE_IO1_XPLAINED=y CONFIG_MODULE_IO1_XPLAINED=y
CONFIG_MODULE_AT30TSE75X=y CONFIG_MODULE_AT30TSE75X=y
CONFIG_MODULE_SDCARD_SPI=y CONFIG_MODULE_SDCARD_SPI=y
CONFIG_MODULE_ZTIMER=y
CONFIG_MODULE_ZTIMER_MSEC=y

View File

@ -21,7 +21,8 @@
#include <stdio.h> #include <stdio.h>
#include <inttypes.h> #include <inttypes.h>
#include "xtimer.h" #include "timex.h"
#include "ztimer.h"
#include "board.h" #include "board.h"
#include "periph/gpio.h" #include "periph/gpio.h"
@ -31,7 +32,7 @@
#include "io1_xplained.h" #include "io1_xplained.h"
#include "io1_xplained_params.h" #include "io1_xplained_params.h"
#define DELAY_1S (1U) /* 1 seconds delay between each test */ #define DELAY_1S (1U * MS_PER_SEC) /* 1 seconds delay between each test */
static io1_xplained_t dev; static io1_xplained_t dev;
@ -71,12 +72,12 @@ int main(void)
"+-------------------------------------+\n", "+-------------------------------------+\n",
(int)temperature, (int)temperature,
(unsigned)((temperature - (int)temperature) * 1000)); (unsigned)((temperature - (int)temperature) * 1000));
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
/* Card detect pin is inverted */ /* Card detect pin is inverted */
if (!gpio_read(IO1_SDCARD_SPI_PARAM_DETECT)) { if (!gpio_read(IO1_SDCARD_SPI_PARAM_DETECT)) {
_sd_card_cid(); _sd_card_cid();
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
} }
uint16_t light; uint16_t light;
@ -84,23 +85,23 @@ int main(void)
printf("Light level: %i\n" printf("Light level: %i\n"
"+-------------------------------------+\n", "+-------------------------------------+\n",
light); light);
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
/* set led */ /* set led */
gpio_set(IO1_LED_PIN); gpio_set(IO1_LED_PIN);
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
/* clear led */ /* clear led */
gpio_clear(IO1_LED_PIN); gpio_clear(IO1_LED_PIN);
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
/* toggle led */ /* toggle led */
gpio_toggle(IO1_LED_PIN); gpio_toggle(IO1_LED_PIN);
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
/* toggle led again */ /* toggle led again */
gpio_toggle(IO1_LED_PIN); gpio_toggle(IO1_LED_PIN);
xtimer_sleep(DELAY_1S); ztimer_sleep(ZTIMER_MSEC, DELAY_1S);
} }
return 0; return 0;