4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
32 * \brief Function library for secure digital memory.
34 * \author Francesco Sacchi <batt@develer.com>
41 #include <io/kblock.h>
42 #include <drv/timer.h>
46 #include "cfg/cfg_sd.h"
48 #define LOG_LEVEL SD_LOG_LEVEL
49 #define LOG_FORMAT SD_LOG_FORMAT
51 #include <cpu/power.h>
53 #include <string.h> /* memset */
57 * read directly from the card.
59 typedef struct CardCSD
61 uint16_t block_len; ///< Length of a block
62 uint32_t block_num; ///< Number of block on the card
63 uint16_t capacity; ///< Card capacity in MB
66 #define SD_IN_IDLE 0x01
67 #define SD_STARTTOKEN 0xFE
69 #define TIMEOUT_NAC 256
71 #define SD_DEFAULT_BLOCKLEN 512
73 #define SD_BUSY_TIMEOUT ms_to_ticks(200)
75 static bool sd_select(Sd *sd, bool state)
81 ticks_t start = timer_clock();
85 if (kfile_getc(fd) == 0xff)
92 while (timer_clock() - start < SD_BUSY_TIMEOUT);
94 LOG_ERR("sd_select timeout\n");
106 static int16_t sd_waitR1(Sd *sd)
110 for (int i = 0; i < TIMEOUT_NAC; i++)
112 datain = kfile_getc(sd->ch);
114 return (int16_t)datain;
116 LOG_ERR("Timeout waiting R1\n");
120 static int16_t sd_sendCommand(Sd *sd, uint8_t cmd, uint32_t param, uint8_t crc)
123 /* The 7th bit of command must be a 1 */
124 kfile_putc(cmd | 0x40, fd);
127 kfile_putc((param >> 24) & 0xFF, fd);
128 kfile_putc((param >> 16) & 0xFF, fd);
129 kfile_putc((param >> 8) & 0xFF, fd);
130 kfile_putc((param) & 0xFF, fd);
134 return sd_waitR1(sd);
138 static bool sd_getBlock(Sd *sd, void *buf, size_t len)
145 for (int i = 0; i < TIMEOUT_NAC; i++)
147 token = kfile_getc(fd);
150 if (token == SD_STARTTOKEN)
152 if (kfile_read(fd, buf, len) == len)
154 if (kfile_read(fd, &crc, sizeof(crc)) == sizeof(crc))
155 /* check CRC here if needed */
158 LOG_ERR("get_block error getting crc\n");
161 LOG_ERR("get_block len error: %d\n", (int)len);
164 LOG_ERR("get_block token error: %02X\n", token);
170 LOG_ERR("get_block timeout waiting token\n");
174 #define SD_SELECT(sd) \
177 if (!sd_select((sd), true)) \
179 LOG_ERR("%s failed, card busy\n", __func__); \
185 #define SD_SETBLOCKLEN 0x50
187 static int16_t sd_setBlockLen(Sd *sd, uint32_t newlen)
191 sd->r1 = sd_sendCommand(sd, SD_SETBLOCKLEN, newlen, 0);
193 sd_select(sd, false);
197 #define SD_SEND_CSD 0x49
199 static int16_t sd_getCSD(Sd *sd, CardCSD *csd)
203 sd->r1 = sd_sendCommand(sd, SD_SEND_CSD, 0, 0);
207 LOG_ERR("send_csd failed: %04X\n", sd->r1);
208 sd_select(sd, false);
213 bool res = sd_getBlock(sd, buf, sizeof(buf));
214 sd_select(sd, false);
218 #if LOG_LEVEL >= LOG_LVL_INFO
220 for (int i = 0; i < 16; i++)
221 kprintf("%02X ", buf[i]);
225 uint16_t mult = (1L << ((((buf[9] & 0x03) << 1) | ((buf[10] & 0x80) >> 7)) + 2));
226 uint16_t c_size = (((uint16_t)(buf[6] & 0x03)) << 10) | (((uint16_t)buf[7]) << 2) |
227 (((uint16_t)(buf[8] & 0xC0)) >> 6);
229 csd->block_len = (1L << (buf[5] & 0x0F));
230 csd->block_num = (c_size + 1) * mult;
231 csd->capacity = (csd->block_len * csd->block_num) >> 20; // in MB
233 LOG_INFO("block_len %d bytes, block_num %ld, total capacity %dMB\n", csd->block_len, csd->block_num, csd->capacity);
241 #define SD_READ_SINGLEBLOCK 0x51
243 static size_t sd_readDirect(struct KBlock *b, block_idx_t idx, void *buf, size_t offset, size_t size)
246 LOG_INFO("reading from block %ld, offset %d, size %d\n", idx, offset, size);
248 if ((sd->r1 = sd_setBlockLen(sd, size)))
250 LOG_ERR("setBlockLen failed: %04X\n", sd->r1);
255 sd->r1 = sd_sendCommand(sd, SD_READ_SINGLEBLOCK, idx * SD_DEFAULT_BLOCKLEN + offset, 0);
259 LOG_ERR("read single block failed: %04X\n", sd->r1);
260 sd_select(sd, false);
264 bool res = sd_getBlock(sd, buf, size);
265 sd_select(sd, false);
268 LOG_ERR("read single block failed reading data\n");
275 #define SD_WRITE_SINGLEBLOCK 0x58
276 #define SD_DATA_ACCEPTED 0x05
278 static int sd_writeBlock(KBlock *b, block_idx_t idx, const void *buf)
283 LOG_INFO("writing block %ld\n", idx);
284 if ((sd->r1 = sd_setBlockLen(sd, SD_DEFAULT_BLOCKLEN)))
286 LOG_ERR("setBlockLen failed: %04X\n", sd->r1);
292 sd->r1 = sd_sendCommand(sd, SD_WRITE_SINGLEBLOCK, idx * SD_DEFAULT_BLOCKLEN, 0);
296 LOG_ERR("write single block failed: %04X\n", sd->r1);
297 sd_select(sd, false);
301 kfile_putc(SD_STARTTOKEN, fd);
302 kfile_write(fd, buf, SD_DEFAULT_BLOCKLEN);
306 uint8_t dataresp = (kfile_getc(fd) & 0x1F);
307 sd_select(sd, false);
309 // FIXME: sometimes dataresp is 0, find out why.
310 if (dataresp != SD_DATA_ACCEPTED)
312 LOG_ERR("write single block failed: %02X\n", dataresp);
322 uint8_t buf[SD_DEFAULT_BLOCKLEN];
324 if (sd_readDirect(&sd->b, 0, buf, 0, sd->b.blk_size) != sd->b.blk_size)
328 for (int i = 0; i < SD_DEFAULT_BLOCKLEN; i++)
330 kprintf("%02X ", buf[i]);
336 if (sd_writeBlock(&sd->b, 0, buf) != 0)
339 memset(buf, 0, sizeof(buf));
340 if (sd_readDirect(&sd->b, 0, buf, 0, sd->b.blk_size) != sd->b.blk_size)
344 for (block_idx_t i = 0; i < sd->b.blk_size; i++)
346 kprintf("%02X ", buf[i]);
355 static int sd_error(KBlock *b)
361 static int sd_clearerr(KBlock *b)
368 static const KBlockVTable sd_unbuffered_vt =
370 .readDirect = sd_readDirect,
371 .writeBlock = sd_writeBlock,
374 .clearerr = sd_clearerr,
377 static const KBlockVTable sd_buffered_vt =
379 .readDirect = sd_readDirect,
380 .writeBlock = sd_writeBlock,
382 .readBuf = kblock_swReadBuf,
383 .writeBuf = kblock_swWriteBuf,
384 .load = kblock_swLoad,
385 .store = kblock_swStore,
388 .clearerr = sd_clearerr,
391 #define SD_GO_IDLE_STATE 0x40
392 #define SD_GO_IDLE_STATE_CRC 0x95
393 #define SD_SEND_OP_COND 0x41
394 #define SD_SEND_OP_COND_CRC 0xF9
396 #define SD_START_DELAY ms_to_ticks(10)
397 #define SD_INIT_TIMEOUT ms_to_ticks(1000)
398 #define SD_IDLE_RETRIES 4
400 static bool sd_blockInit(Sd *sd, KFile *ch)
404 memset(sd, 0, sizeof(*sd));
405 DB(sd->b.priv.type = KBT_SD);
411 /* Wait a few moments for supply voltage to stabilize */
412 timer_delay(SD_START_DELAY);
414 /* Give 80 clk pulses to wake up the card */
415 for (int i = 0; i < 10; i++)
416 kfile_putc(0xff, ch);
419 for (int i = 0; i < SD_IDLE_RETRIES; i++)
422 sd->r1 = sd_sendCommand(sd, SD_GO_IDLE_STATE, 0, SD_GO_IDLE_STATE_CRC);
423 sd_select(sd, false);
425 if (sd->r1 == SD_IN_IDLE)
429 if (sd->r1 != SD_IN_IDLE)
431 LOG_ERR("go_idle_state failed: %04X\n", sd->r1);
435 ticks_t start = timer_clock();
437 /* Wait for card to start */
441 sd->r1 = sd_sendCommand(sd, SD_SEND_OP_COND, 0, SD_SEND_OP_COND_CRC);
442 sd_select(sd, false);
445 while (sd->r1 != 0 && timer_clock() - start < SD_INIT_TIMEOUT);
449 LOG_ERR("send_op_cond failed: %04X\n", sd->r1);
453 sd->r1 = sd_setBlockLen(sd, SD_DEFAULT_BLOCKLEN);
457 LOG_ERR("setBlockLen failed: %04X\n", sd->r1);
462 sd->r1 = sd_getCSD(sd, &csd);
465 LOG_ERR("getCSD failed: %04X\n", sd->r1);
469 sd->b.blk_size = SD_DEFAULT_BLOCKLEN;
470 sd->b.blk_cnt = csd.block_num * (csd.block_len / SD_DEFAULT_BLOCKLEN);
471 LOG_INFO("blk_size %d, blk_cnt %ld\n", sd->b.blk_size, sd->b.blk_cnt);
474 #if CONFIG_SD_AUTOASSIGN_FAT
475 disk_assignDrive(&sd->b, 0);
481 bool sd_initUnbuf(Sd *sd, KFile *ch)
483 if (sd_blockInit(sd, ch))
485 sd->b.priv.vt = &sd_unbuffered_vt;
492 static uint8_t sd_buf[SD_DEFAULT_BLOCKLEN];
494 bool sd_initBuf(Sd *sd, KFile *ch)
496 if (sd_blockInit(sd, ch))
498 sd->b.priv.buf = sd_buf;
499 sd->b.priv.flags |= KB_BUFFERED;
500 sd->b.priv.vt = &sd_buffered_vt;
501 sd->b.priv.vt->load(&sd->b, 0);