-#include <cpu/power.h>
-
-#include <string.h> /* memset */
-
-/**
- * Card Specific Data
- * read directly from the card.
- */
-typedef struct CardCSD
-{
- uint16_t block_len; ///< Length of a block
- uint32_t block_num; ///< Number of block on the card
- uint16_t capacity; ///< Card capacity in MB
-} CardCSD;
-
-#define SD_IN_IDLE 0x01
-#define SD_STARTTOKEN 0xFE
-
-#define TIMEOUT_NAC 16384
-#define SD_DEFAULT_BLOCKLEN 512
-
-#define SD_BUSY_TIMEOUT ms_to_ticks(200)
-
-static bool sd_select(Sd *sd, bool state)
-{
- KFile *fd = sd->ch;
-
- if (state)
- {
- SD_CS_ON();
-
- ticks_t start = timer_clock();
- do
- {
- if (kfile_getc(fd) == 0xff)
- return true;
-
- cpu_relax();
- }
- while (timer_clock() - start < SD_BUSY_TIMEOUT);
-
- SD_CS_OFF();
- LOG_ERR("sd_select timeout\n");
- return false;
- }
- else
- {
- kfile_putc(0xff, fd);
- kfile_flush(fd);
- SD_CS_OFF();
- return true;
- }
-}
-
-static int16_t sd_waitR1(Sd *sd)
-{
- uint8_t datain;
-
- for (int i = 0; i < TIMEOUT_NAC; i++)
- {
- datain = kfile_getc(sd->ch);
- if (datain != 0xff)
- return (int16_t)datain;
- }
- LOG_ERR("Timeout waiting R1\n");
- return EOF;
-}
-
-static int16_t sd_sendCommand(Sd *sd, uint8_t cmd, uint32_t param, uint8_t crc)
-{
- KFile *fd = sd->ch;
- /* The 7th bit of command must be a 1 */
- kfile_putc(cmd | 0x40, fd);
-
- /* send parameter */
- kfile_putc((param >> 24) & 0xFF, fd);
- kfile_putc((param >> 16) & 0xFF, fd);
- kfile_putc((param >> 8) & 0xFF, fd);
- kfile_putc((param) & 0xFF, fd);
-
- kfile_putc(crc, fd);
-
- return sd_waitR1(sd);
-}
-
-static bool sd_getBlock(Sd *sd, void *buf, size_t len)
-{
- uint8_t token;
- uint16_t crc;
-
- KFile *fd = sd->ch;
-
- for (int i = 0; i < TIMEOUT_NAC; i++)
- {
- token = kfile_getc(fd);
- if (token != 0xff)
- {
- if (token == SD_STARTTOKEN)
- {
- if (kfile_read(fd, buf, len) == len)
- {
- if (kfile_read(fd, &crc, sizeof(crc)) == sizeof(crc))
- /* check CRC here if needed */
- return true;
- else
- LOG_ERR("get_block error getting crc\n");
- }
- else
- LOG_ERR("get_block len error: %d\n", (int)len);
- }
- else
- LOG_ERR("get_block token error: %02X\n", token);
-
- return false;
- }
- }
-
- LOG_ERR("get_block timeout waiting token\n");
- return false;
-}
-
-#define SD_SELECT(sd) \
-do \
-{ \
- if (!sd_select((sd), true)) \
- { \
- LOG_ERR("%s failed, card busy\n", __func__); \
- return EOF; \
- } \
-} \
-while (0)
-
-#define SD_SETBLOCKLEN 0x50
-
-static int16_t sd_setBlockLen(Sd *sd, uint32_t newlen)
-{
- SD_SELECT(sd);
-
- sd->r1 = sd_sendCommand(sd, SD_SETBLOCKLEN, newlen, 0);
-
- sd_select(sd, false);
- return sd->r1;
-}
-
-#define SD_SEND_CSD 0x49
-
-static int16_t sd_getCSD(Sd *sd, CardCSD *csd)
-{
- SD_SELECT(sd);
-
- int16_t r1 = sd_sendCommand(sd, SD_SEND_CSD, 0, 0);
-
- if (r1)
- {
- LOG_ERR("send_csd failed: %04X\n", sd->r1);
- sd_select(sd, false);
- return r1;
- }
-
- uint8_t buf[16];
- bool res = sd_getBlock(sd, buf, sizeof(buf));
- sd_select(sd, false);
-
- if (res)
- {
- #if LOG_LEVEL >= LOG_LVL_INFO
- LOG_INFO("CSD: [");
- for (int i = 0; i < 16; i++)
- kprintf("%02X ", buf[i]);
- kprintf("]\n");
- #endif
-
- uint16_t mult = (1L << ((((buf[9] & 0x03) << 1) | ((buf[10] & 0x80) >> 7)) + 2));
- uint16_t c_size = (((uint16_t)(buf[6] & 0x03)) << 10) | (((uint16_t)buf[7]) << 2) |
- (((uint16_t)(buf[8] & 0xC0)) >> 6);
-
- csd->block_len = (1L << (buf[5] & 0x0F));
- csd->block_num = (c_size + 1) * mult;
- csd->capacity = (csd->block_len * csd->block_num) >> 20; // in MB
-
- LOG_INFO("block_len %d bytes, block_num %ld, total capacity %dMB\n", csd->block_len, csd->block_num, csd->capacity);
- return 0;
- }
- else
- return EOF;
-}
-
-
-#define SD_READ_SINGLEBLOCK 0x51
-
-static size_t sd_readDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
-{
- Sd *sd = SD_CAST(b);
- LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
-
- if (sd->tranfer_len != size)
- {
- if ((sd->r1 = sd_setBlockLen(sd, size)))
- {
- LOG_ERR("setBlockLen failed: %04X\n", sd->r1);
- return 0;
- }
- sd->tranfer_len = size;
- }
-
- SD_SELECT(sd);
-
- sd->r1 = sd_sendCommand(sd, SD_READ_SINGLEBLOCK, idx * SD_DEFAULT_BLOCKLEN + offset, 0);
-
- if (sd->r1)
- {
- LOG_ERR("read single block failed: %04X\n", sd->r1);
- sd_select(sd, false);
- return 0;
- }
-
- bool res = sd_getBlock(sd, buf, size);
- sd_select(sd, false);
- if (!res)
- {
- LOG_ERR("read single block failed reading data\n");
- return 0;
- }
- else
- return size;
-}