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>
37 * $WIZ$ module_name = "sd"
38 * $WIZ$ module_depends = "kfile", "timer", "kblock"
39 * $WIZ$ module_hw = "bertos/hw/hw_sd.h"
40 * $WIZ$ module_configuration = "bertos/cfg/cfg_sd.h"
47 #include "cfg/cfg_sd.h"
50 #include <io/kblock.h>
52 #include <fs/fatfs/diskio.h>
70 uint8_t ccc; ///< Card command classes
71 uint32_t erase_size; ///< The size of an erasable sector, in write block len
72 uint32_t capacity; ///< Card size in byte
73 uint32_t blk_len; ///< Block data size len in byte
74 uint32_t blk_num; ///< Number of block in card
75 uint32_t write_blk_bits; ///< Max write block length in bits
76 uint32_t read_blk_bits; ///< Max read block length in bits
77 uint8_t read_partial:1,
84 #define SD_READY_FOR_DATA BV(8)
85 #define SD_CARD_IS_LOCKED BV(25)
87 #define SD_SEND_CID_RCA 0
88 #define SD_SEND_ALL_CID BV(0)
93 #define SD_UNBUFFERED BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
96 * SD Card context structure.
101 KBlock b; ///< KBlock base class
102 KFile *ch; ///< SPI communication channel
103 uint16_t r1; ///< Last status data received from SD
104 uint16_t tranfer_len; ///< Lenght for the read/write commands, cached in order to increase speed.
115 bool sd_initUnbuf(Sd *sd, KFile *ch);
116 bool sd_initBuf(Sd *sd, KFile *ch);
121 void sd_dumpCsd(Sd *sd);
122 void sd_dumpCid(Sd *sd);
124 void sd_sendInit(void);
125 void sd_goIdle(void);
126 int sd_sendIfCond(void);
127 int sd_sendAppOpCond(void);
129 int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag);
130 int sd_getCsd(Sd *sd);
131 int sd_getSrc(Sd *sd);
133 int sd_appStatus(Sd *sd);
134 int sd_getRelativeAddr(Sd *sd);
136 int sd_selectCard(Sd *sd);
137 int sd_deSelectCard(Sd *sd);
138 int sd_setBusWidth(Sd *sd, size_t len);
139 int sd_set_BlockLen(Sd *sd, size_t len);
141 int sd_readSingleBlock(Sd *sd, size_t index, void *_buf, size_t len);
144 INLINE int sd_setBus4bit(Sd *sd)
146 return sd_setBusWidth(sd, 1);
149 INLINE int sd_setBus1bit(Sd *sd)
151 return sd_setBusWidth(sd, 0);
157 #if CONFIG_SD_OLD_INIT
158 #if !(ARCH & ARCH_NIGHTTEST)
159 #warning "Deprecated: this API will be removed in the next major release,"
160 #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
164 * Initializes the SD driver.
166 * \param ch A pointer to a SPI channel where the SD will read/write to.
168 * \return true if initialization succeds, false otherwise.
170 * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
171 * use the new one instead.
173 * \see CONFIG_SD_OLD_INIT.
175 #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));}
180 * Initializes the SD driver.
182 * \param sd The SD KBlock context.
183 * \param ch A pointer to a SPI channel where the SD will read/write to.
184 * \param buffered Set to true if you want the KBlock to be buffered,
185 * to false otherwise. The FatFs module does not require the device
186 * to be buffered because it has an internal cache. This will save
187 * 512 bytes of RAM in this case.
189 * \return true if initialization succeds, false otherwise.
191 #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_initUnbuf((sd), (ch)) : sd_initBuf((sd), (ch)))
196 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
198 bool sd_test(Sd *sd);
199 void sd_writeTest(Sd *sd);
201 INLINE Sd *SD_CAST(KBlock *b)
203 ASSERT(b->priv.type == KBT_SD);
207 #endif /* DRV_SD_H */