Move unpack lwip ip address macro to macros module.
[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 typedef struct SdCID
55 {
56         uint8_t        manfid;
57         uint8_t        prod_name[8];
58         uint32_t       serial;
59         uint16_t       oemid;
60         uint32_t       year_off;
61         uint8_t        m_rev;
62         uint8_t        l_rev;
63 }SdCID;
64
65 typedef struct SdCSD
66 {
67         uint8_t     structure;
68         uint8_t     ccc;          ///< Card command classes
69         uint32_t    erase_size;  ///< The size of an erasable sector, in write block len
70         uint32_t    capacity;     ///< Card size in byte
71         uint32_t    max_data_rate; ///< Step rate, usec
72         uint32_t    block_len;      ///< Block data size len in byte
73         uint32_t    block_num;      ///< Number of block in card
74         uint32_t        write_blk_bits; ///< Max write block length in bits
75         uint32_t        read_blk_bits;  ///< Max read block length in bits
76         uint8_t     read_partial:1,
77                                 read_misalign:1,
78                                 write_partial:1,
79                                 write_misalign:1;
80 } SdCSD;
81
82 typedef struct SdSSR
83 {
84         uint8_t    bus_width;
85         uint8_t    card_type;
86         uint8_t    speed_class;
87         uint8_t    au_size;
88         uint8_t    erase_size;
89 } SdSSR;
90
91 #define SD_START_DELAY  10
92 #define SD_INIT_TIMEOUT ms_to_ticks(2000)
93 #define SD_IDLE_RETRIES 4
94 #define SD_DEFAULT_BLOCKLEN 512
95
96 /**
97  * $WIZ$ sd_mode = "SD_SDMMC_MODE", "SD_SPI_MODE"
98  * \{
99  */
100 #define SD_SDMMC_MODE  0
101 #define SD_SPI_MODE    1
102 /** \} */
103
104 #define SD_UNBUFFERED     BV(0) ///< Open SD memory disabling page caching, no modification and partial write are allowed.
105
106 struct SdHardware* hw;
107 /**
108  * SD Card context structure.
109  */
110 typedef struct Sd
111 {
112         KBlock b;   ///< KBlock base class
113         KFile *ch;  ///< SPI communication channel
114         struct SdHardware* hw;
115         uint32_t addr;
116         uint32_t status;
117 } Sd;
118
119 bool sd_hw_initUnbuf(Sd *sd, KFile *ch);
120 bool sd_hw_initBuf(Sd *sd, KFile *ch);
121
122 bool sd_spi_initUnbuf(Sd *sd, KFile *ch);
123 bool sd_spi_initBuf(Sd *sd, KFile *ch);
124
125 // For old compatibility.
126 #ifndef CONFIG_SD_MODE
127         #define CONFIG_SD_MODE  SD_SPI_MODE
128         #define SD_INCLUDE_SPI_SOURCE
129 #endif
130
131 #if CONFIG_SD_OLD_INIT
132
133         #if !(ARCH & ARCH_NIGHTTEST)
134                 #warning "Deprecated: this API will be removed in the next major release,"
135                 #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
136         #endif
137
138         /**
139          * Initializes the SD driver.
140          *
141          * \param ch A pointer to a SPI channel where the SD will read/write to.
142          *
143          * \return true if initialization succeds, false otherwise.
144          *
145          * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
146          *       use the new one instead.
147          *
148          * \see CONFIG_SD_OLD_INIT.
149          */
150         #if CONFIG_SD_MODE == SD_SPI_MODE
151                 #define sd_init(ch) {static struct Sd sd; sd_spi_initUnbuf(&sd, (ch));}
152         #else
153                 #define sd_init(ch) {static struct Sd sd; sd_hw_initUnbuf(&sd, (ch));}
154         #endif
155
156 #else
157
158         /**
159          * Initializes the SD driver.
160          *
161          * \param sd The SD KBlock context.
162          * \param ch A pointer to a SPI channel where the SD will read/write to.
163          * \param buffered Set to true if you want the KBlock to be buffered,
164          *        to false otherwise. The FatFs module does not require the device
165          *        to be buffered because it has an internal cache. This will save
166          *        512 bytes of RAM in this case.
167          *
168          * \return true if initialization succeds, false otherwise.
169          */
170         #if CONFIG_SD_MODE == SD_SPI_MODE
171                 #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_spi_initUnbuf((sd), (ch)) : sd_spi_initBuf((sd), (ch)))
172         #else
173                 #define sd_init(sd, ch, buffered) ((buffered & SD_UNBUFFERED) ? sd_hw_initUnbuf((sd), (ch)) : sd_hw_initBuf((sd), (ch)))
174         #endif
175
176 #endif
177
178
179 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
180
181 bool sd_test(Sd *sd);
182 void sd_writeTest(Sd *sd);
183
184 INLINE Sd *SD_CAST(KBlock *b)
185 {
186         ASSERT(b->priv.type == KBT_SD);
187         return (Sd *)b;
188 }
189
190 #endif /* DRV_SD_H */
191