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 <drv/timer.h>
46 #include <cpu/power.h>
48 #include <string.h> /* memset */
52 * read directly from the card.
54 typedef struct CardCSD
56 uint16_t block_len; ///< Length of a block
57 uint32_t block_num; ///< Number of block on the card
58 uint16_t capacity; ///< Card capacity in MB
61 #define SD_IN_IDLE 0x01
62 #define SD_STARTTOKEN 0xFE
64 #define TIMEOUT_NAC 256
66 #define SD_DEFAULT_BLOCKLEN 512
69 * SPI communication channel.
76 static bool sd_status;
78 #define SD_BUSY_TIMEOUT ms_to_ticks(200)
80 static bool sd_select(bool state)
84 ticks_t start = timer_clock();
88 if (kfile_getc(fd) == 0xff)
95 while (timer_clock() - start < SD_BUSY_TIMEOUT);
97 LOG_ERR("sd_select timeout\n");
102 kfile_putc(0xff, fd);
109 static int16_t sd_waitR1(void)
113 for (int i = 0; i < TIMEOUT_NAC; i++)
115 datain = kfile_getc(fd);
117 return (int16_t)datain;
119 LOG_ERR("Timeout waiting R1\n");
123 static int16_t sd_sendCommand(uint8_t cmd, uint32_t param, uint8_t crc)
125 /* The 7th bit of command must be a 1 */
126 kfile_putc(cmd | 0x40, fd);
129 kfile_putc((param >> 24) & 0xFF, fd);
130 kfile_putc((param >> 16) & 0xFF, fd);
131 kfile_putc((param >> 8) & 0xFF, fd);
132 kfile_putc((param) & 0xFF, fd);
140 static bool sd_getBlock(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() \
177 if (!sd_select(true)) \
179 LOG_ERR("%s failed, card busy\n", __func__); \
185 #define SD_SETBLOCKLEN 0x50
187 static int16_t sd_setBlockLen(uint32_t newlen)
191 int16_t r1 = sd_sendCommand(SD_SETBLOCKLEN, newlen, 0);
197 #define SD_SEND_CSD 0x49
199 static int16_t sd_getCSD(CardCSD *csd)
203 int16_t r1 = sd_sendCommand(SD_SEND_CSD, 0, 0);
207 LOG_ERR("send_csd failed: %04X\n", r1);
213 bool res = sd_getBlock(buf, sizeof(buf));
218 uint16_t mult = (1L << ((((buf[9] & 0x03) << 1) | ((buf[10] & 0x80) >> 7)) + 2));
219 uint16_t c_size = (((uint16_t)(buf[6] & 0x03)) << 10) | (((uint16_t)buf[7]) << 2) |
220 (((uint16_t)(buf[8] & 0xC0)) >> 6);
222 csd->block_len = (1L << (buf[5] & 0x0F));
223 csd->block_num = (c_size + 1) * mult;
224 csd->capacity = (csd->block_len * csd->block_num) >> 20; // in MB
226 LOG_INFO("block_len %d bytes, block_num %ld, total capacity %dMB\n", csd->block_len, csd->block_num, csd->capacity);
234 #define SD_READ_SINGLEBLOCK 0x51
236 static int16_t sd_readBlock(void *buf, uint32_t addr)
240 int16_t r1 = sd_sendCommand(SD_READ_SINGLEBLOCK, addr, 0);
244 LOG_ERR("read single block failed: %04X\n", r1);
249 bool res = sd_getBlock(buf, SD_DEFAULT_BLOCKLEN);
253 LOG_ERR("read single block failed reading data\n");
260 #define SD_WRITE_SINGLEBLOCK 0x58
261 #define SD_DATA_ACCEPTED 0x05
263 static int16_t sd_writeBlock(const void *buf, uint32_t addr)
267 int16_t r1 = sd_sendCommand(SD_WRITE_SINGLEBLOCK, addr, 0);
271 LOG_ERR("write single block failed: %04X\n", r1);
276 kfile_putc(SD_STARTTOKEN, fd);
277 kfile_write(fd, buf, SD_DEFAULT_BLOCKLEN);
281 uint8_t dataresp = (kfile_getc(fd) & 0x1F);
284 // FIXME: sometimes dataresp is 0, find out why.
285 if (dataresp != SD_DATA_ACCEPTED)
287 LOG_ERR("write single block failed: %02X\n", dataresp);
300 uint8_t buf[SD_DEFAULT_BLOCKLEN];
302 if (sd_readBlock(buf, 0) != 0)
306 for (int i = 0; i < SD_DEFAULT_BLOCKLEN; i++)
308 kprintf("%02X ", buf[i]);
314 if (sd_writeBlock(buf, 0) != 0)
317 memset(buf, 0, sizeof(buf));
318 if (sd_readBlock(buf, 0) != 0)
322 for (int i = 0; i < SD_DEFAULT_BLOCKLEN; i++)
324 kprintf("%02X ", buf[i]);
333 #define SD_GO_IDLE_STATE 0x40
334 #define SD_GO_IDLE_STATE_CRC 0x95
335 #define SD_SEND_OP_COND 0x41
336 #define SD_SEND_OP_COND_CRC 0xF9
338 #define SD_START_DELAY ms_to_ticks(10)
339 #define SD_INIT_TIMEOUT ms_to_ticks(1000)
340 #define SD_IDLE_RETRIES 4
342 bool sd_init(KFile *_fd)
352 /* Wait a few moments for supply voltage to stabilize */
353 timer_delay(SD_START_DELAY);
355 /* Give 80 clk pulses to wake up the card */
356 for (int i = 0; i < 10; i++)
357 kfile_putc(0xff, fd);
360 for (int i = 0; i < SD_IDLE_RETRIES; i++)
363 r1 = sd_sendCommand(SD_GO_IDLE_STATE, 0, SD_GO_IDLE_STATE_CRC);
366 if (r1 == SD_IN_IDLE)
370 if (r1 != SD_IN_IDLE)
372 LOG_ERR("go_idle_state failed: %04X\n", r1);
376 ticks_t start = timer_clock();
378 /* Wait for card to start */
382 r1 = sd_sendCommand(SD_SEND_OP_COND, 0, SD_SEND_OP_COND_CRC);
386 while (r1 != 0 && timer_clock() - start < SD_INIT_TIMEOUT);
390 LOG_ERR("send_op_cond failed: %04X\n", r1);
394 r1 = sd_setBlockLen(SD_DEFAULT_BLOCKLEN);
398 LOG_ERR("setBlockLen failed: %04X\n", r1);
406 DSTATUS sd_disk_initialize(BYTE drv)
408 return sd_disk_status(drv);
411 DSTATUS sd_disk_status(BYTE drv)
421 DRESULT sd_disk_read(BYTE drv, BYTE* buf, DWORD sector, BYTE count)
430 if (sd_readBlock(buf, sector * SD_DEFAULT_BLOCKLEN))
432 buf += SD_DEFAULT_BLOCKLEN;
438 DRESULT sd_disk_write(BYTE drv, const BYTE* buf, DWORD sector, BYTE count)
447 if (sd_writeBlock(buf, sector * SD_DEFAULT_BLOCKLEN))
449 buf += SD_DEFAULT_BLOCKLEN;
455 DRESULT sd_disk_ioctl(BYTE drv, BYTE cmd, void* buf)
467 case GET_SECTOR_SIZE:
468 *(WORD *)buf = SD_DEFAULT_BLOCKLEN;
471 case GET_SECTOR_COUNT:
478 *(DWORD *)buf = csd.block_num;
489 LOG_ERR("unknown command: [%d]\n", cmd);
494 DWORD get_fattime(void)