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.
35 * \author Francesco Sacchi <batt@develer.com>
41 #include <kern/kfile.h>
42 #include <drv/timer.h>
47 #include <cpu/power.h>
49 #include <string.h> /* memset */
53 * read directly from the card.
55 typedef struct CardCSD
57 uint16_t block_len; ///< Length of a block
58 uint32_t block_num; ///< Number of block on the card
59 uint16_t capacity; ///< Card capacity in MB
62 #define SD_IN_IDLE 0x01
63 #define SD_STARTTOKEN 0xFE
65 #define TIMEOUT_NAC 256
67 #define SD_DEFAULT_BLOCKLEN 512
70 * SPI communication channel.
77 static bool sd_status;
79 #define SD_BUSY_TIMEOUT ms_to_ticks(200)
81 static bool sd_select(bool state)
85 ticks_t start = timer_clock();
89 if (kfile_getc(fd) == 0xff)
96 while (timer_clock() - start < SD_BUSY_TIMEOUT);
98 LOG_ERR("sd_select timeout\n");
103 kfile_putc(0xff, fd);
110 static int16_t sd_waitR1(void)
114 for (int i = 0; i < TIMEOUT_NAC; i++)
116 datain = kfile_getc(fd);
118 return (int16_t)datain;
120 LOG_ERR("Timeout waiting R1\n");
124 static int16_t sd_sendCommand(uint8_t cmd, uint32_t param, uint8_t crc)
126 /* The 7th bit of command must be a 1 */
127 kfile_putc(cmd | 0x40, fd);
130 kfile_putc((param >> 24) & 0xFF, fd);
131 kfile_putc((param >> 16) & 0xFF, fd);
132 kfile_putc((param >> 8) & 0xFF, fd);
133 kfile_putc((param) & 0xFF, fd);
141 static bool sd_getBlock(void *buf, size_t len)
146 for (int i = 0; i < TIMEOUT_NAC; i++)
148 token = kfile_getc(fd);
151 if (token == SD_STARTTOKEN)
153 if (kfile_read(fd, buf, len) == len)
155 if (kfile_read(fd, &crc, sizeof(crc)) == sizeof(crc))
156 /* check CRC here if needed */
159 LOG_ERR("get_block error getting crc\n");
162 LOG_ERR("get_block len error: %d\n", (int)len);
165 LOG_ERR("get_block token error: %02X\n", token);
171 LOG_ERR("get_block timeout waiting token\n");
175 #define SD_SELECT() \
178 if (!sd_select(true)) \
180 LOG_ERR("%s failed, card busy\n", __func__); \
186 #define SD_SETBLOCKLEN 0x50
188 static int16_t sd_setBlockLen(uint32_t newlen)
192 int16_t r1 = sd_sendCommand(SD_SETBLOCKLEN, newlen, 0);
198 #define SD_SEND_CSD 0x49
200 static int16_t sd_getCSD(CardCSD *csd)
204 int16_t r1 = sd_sendCommand(SD_SEND_CSD, 0, 0);
208 LOG_ERR("send_csd failed: %04X\n", r1);
214 bool res = sd_getBlock(buf, sizeof(buf));
219 uint16_t mult = (1L << ((((buf[9] & 0x03) << 1) | ((buf[10] & 0x80) >> 7)) + 2));
220 uint16_t c_size = (((uint16_t)(buf[6] & 0x03)) << 10) | (((uint16_t)buf[7]) << 2) |
221 (((uint16_t)(buf[8] & 0xC0)) >> 6);
223 csd->block_len = (1L << (buf[5] & 0x0F));
224 csd->block_num = (c_size + 1) * mult;
225 csd->capacity = (csd->block_len * csd->block_num) >> 20; // in MB
227 LOG_INFO("block_len %d bytes, block_num %ld, total capacity %dMB\n", csd->block_len, csd->block_num, csd->capacity);
235 #define SD_READ_SINGLEBLOCK 0x51
237 static int16_t sd_readBlock(void *buf, uint32_t addr)
241 int16_t r1 = sd_sendCommand(SD_READ_SINGLEBLOCK, addr, 0);
245 LOG_ERR("read single block failed: %04X\n", r1);
250 bool res = sd_getBlock(buf, SD_DEFAULT_BLOCKLEN);
254 LOG_ERR("read single block failed reading data\n");
261 #define SD_WRITE_SINGLEBLOCK 0x58
262 #define SD_DATA_ACCEPTED 0x05
264 static int16_t sd_writeBlock(const void *buf, uint32_t addr)
268 int16_t r1 = sd_sendCommand(SD_WRITE_SINGLEBLOCK, addr, 0);
272 LOG_ERR("write single block failed: %04X\n", r1);
277 kfile_putc(SD_STARTTOKEN, fd);
278 kfile_write(fd, buf, SD_DEFAULT_BLOCKLEN);
282 uint8_t dataresp = (kfile_getc(fd) & 0x1F);
285 // FIXME: sometimes dataresp is 0, find out why.
286 if (dataresp != SD_DATA_ACCEPTED)
288 LOG_ERR("write single block failed: %02X\n", dataresp);
301 uint8_t buf[SD_DEFAULT_BLOCKLEN];
303 if (sd_readBlock(buf, 0) != 0)
307 for (int i = 0; i < SD_DEFAULT_BLOCKLEN; i++)
309 kprintf("%02X ", buf[i]);
315 if (sd_writeBlock(buf, 0) != 0)
318 memset(buf, 0, sizeof(buf));
319 if (sd_readBlock(buf, 0) != 0)
323 for (int i = 0; i < SD_DEFAULT_BLOCKLEN; i++)
325 kprintf("%02X ", buf[i]);
334 #define SD_GO_IDLE_STATE 0x40
335 #define SD_GO_IDLE_STATE_CRC 0x95
336 #define SD_SEND_OP_COND 0x41
337 #define SD_SEND_OP_COND_CRC 0xF9
339 #define SD_START_DELAY ms_to_ticks(10)
340 #define SD_INIT_TIMEOUT ms_to_ticks(1000)
341 #define SD_IDLE_RETRIES 4
343 bool sd_init(KFile *_fd)
353 /* Wait a few moments for supply voltage to stabilize */
354 timer_delay(SD_START_DELAY);
356 /* Give 80 clk pulses to wake up the card */
357 for (int i = 0; i < 10; i++)
358 kfile_putc(0xff, fd);
361 for (int i = 0; i < SD_IDLE_RETRIES; i++)
364 r1 = sd_sendCommand(SD_GO_IDLE_STATE, 0, SD_GO_IDLE_STATE_CRC);
367 if (r1 == SD_IN_IDLE)
371 if (r1 != SD_IN_IDLE)
373 LOG_ERR("go_idle_state failed: %04X\n", r1);
377 ticks_t start = timer_clock();
379 /* Wait for card to start */
383 r1 = sd_sendCommand(SD_SEND_OP_COND, 0, SD_SEND_OP_COND_CRC);
387 while (r1 != 0 && timer_clock() - start < SD_INIT_TIMEOUT);
391 LOG_ERR("send_op_cond failed: %04X\n", r1);
395 r1 = sd_setBlockLen(SD_DEFAULT_BLOCKLEN);
399 LOG_ERR("setBlockLen failed: %04X\n", r1);
407 DSTATUS sd_disk_initialize(BYTE drv)
409 return sd_disk_status(drv);
412 DSTATUS sd_disk_status(BYTE drv)
422 DRESULT sd_disk_read(BYTE drv, BYTE* buf, DWORD sector, BYTE count)
431 if (sd_readBlock(buf, sector * SD_DEFAULT_BLOCKLEN))
433 buf += SD_DEFAULT_BLOCKLEN;
439 DRESULT sd_disk_write(BYTE drv, const BYTE* buf, DWORD sector, BYTE count)
448 if (sd_writeBlock(buf, sector * SD_DEFAULT_BLOCKLEN))
450 buf += SD_DEFAULT_BLOCKLEN;
456 DRESULT sd_disk_ioctl(BYTE drv, BYTE cmd, void* buf)
468 case GET_SECTOR_SIZE:
469 *(WORD *)buf = SD_DEFAULT_BLOCKLEN;
472 case GET_SECTOR_COUNT:
479 *(DWORD *)buf = csd.block_num;
490 LOG_ERR("unknown command: [%d]\n", cmd);
495 DWORD get_fattime(void)