Add first implementation of new sd api.
[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"
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 #ifdef 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    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,
78                 read_misalign:1,
79                 write_partial:1,
80                 write_misalign:1;
81 } SDcsd;
82
83 int sd_decode_csd(SDcsd *csd, uint32_t *resp, size_t len);
84 void sd_dump_csd(SDcsd *csd);
85 void sd_decode_cid(SDcid *cid, uint32_t *resp, size_t len);
86 void sd_dump_cid(SDcid *cid);
87 void sd_send_init(void);
88 void sd_go_idle(void);
89 int sd_send_if_cond(void);
90 int sd_send_app_op_cond(void);
91 int sd_get_cid(uint32_t *resp, size_t len);
92 int sd_get_csd(uint32_t *resp, size_t len);
93 int sd_app_status(void);
94 #endif
95
96
97 #define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
98
99 /**
100  * SD Card context structure.
101  */
102 typedef struct Sd
103 {
104         KBlock b;   ///< KBlock base class
105         KFile *ch;  ///< SPI communication channel
106         uint16_t r1;  ///< Last status data received from SD
107         uint16_t tranfer_len; ///< Lenght for the read/write commands, cached in order to increase speed.
108 } Sd;
109
110 bool sd_initUnbuf(Sd *sd, KFile *ch);
111 bool sd_initBuf(Sd *sd, KFile *ch);
112
113 #if CONFIG_SD_OLD_INIT
114         #if !(ARCH & ARCH_NIGHTTEST)
115                 #warning "Deprecated: this API will be removed in the next major release,"
116                 #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
117         #endif
118
119         /**
120          * Initializes the SD driver.
121          *
122          * \param ch A pointer to a SPI channel where the SD will read/write to.
123          *
124          * \return true if initialization succeds, false otherwise.
125          *
126          * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
127          *       use the new one instead.
128          *
129          * \see CONFIG_SD_OLD_INIT.
130          */
131         #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));}
132
133 #else
134
135         /**
136          * Initializes the SD driver.
137          *
138          * \param sd The SD KBlock context.
139          * \param ch A pointer to a SPI channel where the SD will read/write to.
140          * \param buffered Set to true if you want the KBlock to be buffered,
141          *        to false otherwise. The FatFs module does not require the device
142          *        to be buffered because it has an internal cache. This will save
143          *        512 bytes of RAM in this case.
144          *
145          * \return true if initialization succeds, false otherwise.
146          */
147         #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_initUnbuf((sd), (ch)) : sd_initBuf((sd), (ch)))
148
149 #endif
150
151
152 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
153
154 bool sd_test(Sd *sd);
155 void sd_writeTest(Sd *sd);
156
157 INLINE Sd *SD_CAST(KBlock *b)
158 {
159         ASSERT(b->priv.type == KBT_SD);
160         return (Sd *)b;
161 }
162
163 #endif /* DRV_SD_H */