85e7b11cca4b56d409ddf85ddca56afb27a33a8d
[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  * Right now, the interface for these function is the one defined in diskio.h from
35  * the FatFS module.
36  *
37  * \author Francesco Sacchi <batt@develer.com>
38  *
39  * $WIZ$ module_name = "sd"
40  * $WIZ$ module_depends = "kfile", "timer"
41  * $WIZ$ module_hw = "bertos/hw/hw_sd.h"
42  * $WIZ$ module_configuration = "bertos/cfg/cfg_sd.h"
43  */
44
45
46 #ifndef DRV_SD_H
47 #define DRV_SD_H
48
49 #include <io/kfile.h>
50 #include <io/kblock.h>
51
52 #include <fs/fatfs/diskio.h>
53
54 #include "cfg/cfg_sd.h"
55
56
57 typedef struct Sd
58 {
59         KBlock b;
60         KFile *ch;  ///< SPI communication channel
61         uint16_t r1;
62 } Sd;
63
64 bool sd_initUnbuf(Sd *sd, KFile *ch);
65 bool sd_initBuf(Sd *sd, KFile *ch);
66
67 #if CONFIG_SD_OLD_INIT
68         #warning "Deprecated: this API will be removed in the next major release,"
69         #warning "please disable CONFIG_SD_OLD_INIT and pass explicitly the SD context to sd_init()."
70
71         /**
72          * Initializes the SD driver.
73          *
74          * \param ch A pointer to a SPI channel where the SD will read/write to.
75          *
76          * \return true if initialization succeds, false otherwise.
77          *
78          * \note This API is deprecated, disable CONFIG_SD_OLD_INIT and
79          *       use the new one instead.
80          */
81         #define sd_init(ch) {static struct Sd sd; sd_initUnbuf(&sd, (ch));}
82
83 #else
84
85         /**
86          * Initializes the SD driver.
87          *
88          * \param sd The SD KBlock context.
89          * \param ch A pointer to a SPI channel where the SD will read/write to.
90          * \param buffered Set to true if you want the KBlock to be buffered,
91          *        to false otherwise. The FatFs module does not require the device
92          *        to be buffered because it has an internal cache. This will save
93          *        512 bytes of RAM in this case.
94          *
95          * \return true if initialization succeds, false otherwise.
96          */
97         #define sd_init(sd, ch, buffered) ((buffered) ? sd_initBuf((sd), (ch)) : sd_initUnbuf((sd), (ch)))
98
99 #endif
100
101
102 #define KBT_SD MAKE_ID('S', 'D', 'B', 'K')
103
104 bool sd_test(Sd *sd);
105
106 INLINE Sd *SD_CAST(KBlock *b)
107 {
108         ASSERT(b->priv.type == KBT_SD);
109         return (Sd *)b;
110 }
111
112
113 #endif /* DRV_SD_H */