Some tricks for old compatibility.
[bertos.git] / bertos / drv / sd.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
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.
10  *
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.
15  *
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
19  *
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.
28  *
29  * Copyright 2007 Develer S.r.l. (http://www.develer.com/)
30  * -->
31  *
32  * \brief Function library for secure digital memory.
33  *
34  *
35  * \author Francesco Sacchi <batt@develer.com>
36  *
37  * $WIZ$ module_name = "sd"
38  * $WIZ$ module_depends = "kfile", "timer", "kblock", "sd_spi"
39  * $WIZ$ module_hw = "bertos/hw/hw_sd.h"
40  * $WIZ$ module_configuration = "bertos/cfg/cfg_sd.h"
41  */
42
43
44 #ifndef DRV_SD_H
45 #define DRV_SD_H
46
47 #include "cfg/cfg_sd.h"
48
49 #include <io/kfile.h>
50 #include <io/kblock.h>
51
52 #include <fs/fatfs/diskio.h>
53
54 #if CPU_CM3_SAM3X8
55
56 typedef struct SDcid
57 {
58     uint8_t        manfid;
59     uint8_t        prod_name[8];
60     uint32_t       serial;
61     uint16_t       oemid;
62     uint32_t       year_off;
63     uint8_t        m_rev;
64     uint8_t        l_rev;
65 }SDcid;
66
67 typedef struct SDcsd
68 {
69         uint8_t     structure;
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    max_data_rate; ///< Step rate, usec
74     uint32_t    blk_len;      ///< Block data size len in byte
75         uint32_t    blk_num;      ///< Number of block in card
76         uint32_t        write_blk_bits; ///< Max write block length in bits
77         uint32_t        read_blk_bits;  ///< Max read block length in bits
78     uint8_t     read_partial:1,
79                 read_misalign:1,
80                 write_partial:1,
81                 write_misalign:1;
82 } SDcsd;
83
84 typedef struct SDssr
85 {
86         uint8_t    bus_width;
87         uint8_t    card_type;
88         uint8_t    speed_class;
89         uint8_t    au_size;
90         uint8_t    erase_size;
91 } SDssr;
92
93
94 #define SD_READY_FOR_DATA         BV(8)
95 #define SD_CARD_IS_LOCKED         BV(25)
96
97 #define SD_SEND_CID_RCA     0
98 #define SD_SEND_ALL_CID   BV(0)
99
100 #endif
101
102 #define SD_START_DELAY  10
103 #define SD_INIT_TIMEOUT ms_to_ticks(2000)
104 #define SD_IDLE_RETRIES 4
105 #define SD_DEFAULT_BLOCKLEN 512
106
107 /**
108  * $WIZ$ sd_mode = "SD_SDMMC_MODE", "SD_SPI_MODE"
109  * \{
110  */
111 #define SD_SDMMC_MODE  0
112 #define SD_SPI_MODE    1
113 /** \} */
114
115 #define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
116
117 /**
118  * SD Card context structure.
119  */
120 typedef struct Sd
121 {
122
123         KBlock b;   ///< KBlock base class
124         KFile *ch;  ///< SPI communication channel
125         uint16_t r1;  ///< Last status data received from SD
126         uint16_t tranfer_len; ///< Lenght for the read/write commands, cached in order to increase speed.
127
128         #if CPU_CM3_SAM3X8
129         SDcid cid;
130         SDcsd csd;
131         SDssr ssr;
132         uint32_t addr;
133         uint32_t status;
134         #endif
135
136 } Sd;
137
138 bool sd_hw_initUnbuf(Sd *sd, KFile *ch);
139 bool sd_hw_initBuf(Sd *sd, KFile *ch);
140
141 bool sd_spi_initUnbuf(Sd *sd, KFile *ch);
142 bool sd_spi_initBuf(Sd *sd, KFile *ch);
143
144 #if CPU_CM3_SAM3X8
145
146 void sd_dumpCsd(Sd *sd);
147 void sd_dumpCid(Sd *sd);
148 void sd_dumpSsr(Sd *sd);
149
150 void sd_sendInit(void);
151 void sd_goIdle(void);
152 int sd_sendIfCond(Sd *sd);
153 int sd_sendAppOpCond(Sd *sd);
154
155 int sd_getCid(Sd *sd, uint32_t addr, uint8_t flag);
156 int sd_getCsd(Sd *sd);
157 int sd_getSrc(Sd *sd);
158
159 int sd_appStatus(Sd *sd);
160 int sd_getRelativeAddr(Sd *sd);
161
162 int sd_getStatus(Sd *sd, uint32_t *buf, size_t words);
163
164 int sd_selectCard(Sd *sd);
165 int sd_deSelectCard(Sd *sd);
166 int sd_setBusWidth(Sd *sd, size_t len);
167 int sd_set_BlockLen(Sd *sd, size_t len);
168 void sd_setHightSpeed(Sd *sd);
169 int sd_readSingleBlock(Sd *sd, size_t index, uint32_t *buf, size_t words);
170 int sd_writeSingleBlock(Sd *sd, size_t index, uint32_t *buf, size_t words);
171
172
173 INLINE int sd_setBus4bit(Sd *sd)
174 {
175         return sd_setBusWidth(sd, 4);
176 }
177
178 INLINE int sd_setBus1bit(Sd *sd)
179 {
180         return sd_setBusWidth(sd, 1);
181 }
182
183 #endif
184
185 // For old compatibility.
186 #ifndef CONFIG_SD_MODE
187         #define CONFIG_SD_MODE  SD_SPI_MODE
188         #define SD_INCLUDE_SPI_SOURCE
189 #endif
190
191 #if CONFIG_SD_OLD_INIT
192
193         #if !(ARCH & ARCH_NIGHTTEST)
194                 #warning "Deprecated: this API will be removed in the next major release,"
195                 #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
196         #endif
197
198         /**
199          * Initializes the SD driver.
200          *
201          * \param ch A pointer to a SPI channel where the SD will read/write to.
202          *
203          * \return true if initialization succeds, false otherwise.
204          *
205          * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
206          *       use the new one instead.
207          *
208          * \see CONFIG_SD_OLD_INIT.
209          */
210         #if CONFIG_SD_MODE == SD_SPI_MODE
211                 #define sd_init(ch) {static struct Sd sd; sd_spi_initUnbuf(&sd, (ch));}
212         #else
213                 #define sd_init(ch) {static struct Sd sd; sd_hw_initUnbuf(&sd, (ch));}
214         #endif
215
216 #else
217
218         /**
219          * Initializes the SD driver.
220          *
221          * \param sd The SD KBlock context.
222          * \param ch A pointer to a SPI channel where the SD will read/write to.
223          * \param buffered Set to true if you want the KBlock to be buffered,
224          *        to false otherwise. The FatFs module does not require the device
225          *        to be buffered because it has an internal cache. This will save
226          *        512 bytes of RAM in this case.
227          *
228          * \return true if initialization succeds, false otherwise.
229          */
230         #if CONFIG_SD_MODE == SD_SPI_MODE
231                 #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_spi_initUnbuf((sd), (ch)) : sd_spi_initBuf((sd), (ch)))
232         #else
233                 #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_hw_initUnbuf((sd), (ch)) : sd_hw_initBuf((sd), (ch)))
234         #endif
235
236 #endif
237
238
239 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
240
241 bool sd_test(Sd *sd);
242 void sd_writeTest(Sd *sd);
243
244 INLINE Sd *SD_CAST(KBlock *b)
245 {
246         ASSERT(b->priv.type == KBT_SD);
247         return (Sd *)b;
248 }
249
250 #endif /* DRV_SD_H */
251