From 0de7f8213c7eaf65614d5ad0eea97acad63d0ee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 28 Jun 2017 10:27:53 +0200 Subject: [PATCH] sdcard: add reading data block --- global.h | 8 +++- lib/flash_sdcard.c | 107 +++++++++++++++++++++++++++++++++++++++------ 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/global.h b/global.h index 03598e4..49185ab 100644 --- a/global.h +++ b/global.h @@ -15,7 +15,7 @@ /** global definitions and methods (API) * @file global.h * @author King Kévin - * @date 2016 + * @date 2016-2017 */ #pragma once @@ -252,6 +252,12 @@ #define SPI_MISO_PIN(x) CAT3(GPIO_SPI,x,_MISO) /** get SPI pin for MOSI signal based on SPI identifier */ #define SPI_MOSI_PIN(x) CAT3(GPIO_SPI,x,_MOSI) +/** get SPI CRC polynomial register based on SPI identifier */ +#define SPI_CRC_PR(x) CAT3(SPI,x,_CRCPR) +/** get SPI CRC transmit register based on SPI identifier */ +#define SPI_CRC_TXR(x) CAT3(SPI,x,_TXCRCR) +/** get SPI CRC receive register based on SPI identifier */ +#define SPI_CRC_RXR(x) CAT3(SPI,x,_RXCRCR) /** @} */ /** @defgroup board_led board LED GPIO diff --git a/lib/flash_sdcard.c b/lib/flash_sdcard.c index aa16655..8095903 100644 --- a/lib/flash_sdcard.c +++ b/lib/flash_sdcard.c @@ -40,6 +40,11 @@ #define FLASH_SDCARD_SPI 1 /**< SPI peripheral */ /** @} */ +/** maximum N_AC value (time between the response token R1 and data block when reading data (see section 7.5.4) + * @note this is set to N_CR until we can read CSD (see section 7.2.6) + */ +static uint16_t n_ac = 8; + /** table for CRC-7 calculation for the command messages (see section 4.5) * @note faster than calculating the CRC and doesn't cost a lot of space * @note generated using pycrc --width=7 --poly=0x09 --reflect-in=false --reflect-out=false --xor-in=0x00 --xor-out=0x00 --generate=table @@ -77,7 +82,7 @@ static uint16_t flash_sdcard_spi_read(void) { spi_send(SPI(FLASH_SDCARD_SPI), 0xffff); // send not command token (i.e. starting with 1) while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_TXE)); // wait until Tx buffer is empty before clearing the (previous) RXNE flag - SPI_DR(SPI(FLASH_SDCARD_SPI)); // clear RXNE flag (by reading previously received data (not done by spi_read or spi_xref) + (void)SPI_DR(SPI(FLASH_SDCARD_SPI)); // clear RXNE flag (by reading previously received data (not done by spi_read or spi_xref) while (!(SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_RXNE)); // wait for next data to be available return SPI_DR(SPI(FLASH_SDCARD_SPI)); // return received adat } @@ -120,14 +125,15 @@ static void flash_sdcard_send_command(uint8_t index, uint32_t argument) static bool flash_sdcard_command_response(uint8_t index, uint32_t argument, uint8_t* response, size_t size) { // send command token - while (SPI_SR(SPI(FLASH_SDCARD_SPI))&SPI_SR_BSY); // wait until not busy gpio_clear(SPI_NSS_PORT(FLASH_SDCARD_SPI), SPI_NSS_PIN(FLASH_SDCARD_SPI)); // set CS low to select slave and start SPI mode (see section 7.2) - flash_sdcard_spi_wait(); // wait for N_CS before writing command (see section 7.5.1.1) + flash_sdcard_spi_wait(); // wait for N_CS (min. 0, but it works better with 8 clock cycles) before writing command (see section 7.5.1.1) flash_sdcard_send_command(index, argument); // send command token - // verify response token R1 - flash_sdcard_spi_wait(); // wait for N_CR before reading response (see section 7.5.1.1) - uint8_t r1 = flash_sdcard_spi_read(); // get response (see section 7.3.2.1) + // get response token R1 + uint8_t r1 = 0xff; // response token R1 (see section 7.3.2.1) + for (uint8_t i=0; i<8 && r1&0x80; i++) { // wait for N_CR (1 to 8 8 clock cycles) before reading response (see section 7.5.1.1) + r1 = flash_sdcard_spi_read(); // get response (see section 7.3.2.1) + } if (0x00==(r1&0xfe) && 0!=size && NULL!=response) { // we have to read a response for (size_t i=0; i