-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdcard.h
212 lines (183 loc) · 5.85 KB
/
sdcard.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
/******************************************************************************
* Copyright (C) 2017-2018 Marco Giammarini
*
* Authors:
* Marco Giammarini <m.giammarini@warcomeb.it>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
******************************************************************************/
/******************************************************************************
* @mainpage SDCard/SPI library based on @a libohiboard
*
* @section changelog ChangeLog
*
* @li v1.0 of 2017/07/07 - First release
*
* @section library External Library
*
* The library use the following external library
* @li libohiboard https://github.com/ohilab/libohiboard a C framework for
* NXP Kinetis microcontroller
* @li timer https://github.com/warcomeb/timer small programmable library for
* generate an internal tick
*
* @section thanksto Thanks to...
*
* The library is based on the great description of MMC/SD protocol written
* by Elm Chan in this page http://elm-chan.org/docs/mmc/mmc_e.html
*
******************************************************************************/
#ifndef __WARCOMEB_SDCARD_H
#define __WARCOMEB_SDCARD_H
#define WARCOMEB_SDCARD_LIBRARY_VERSION "1.0"
#define WARCOMEB_SDCARD_LIBRARY_VERSION_M 1
#define WARCOMEB_SDCARD_LIBRARY_VERSION_m 0
#define WARCOMEB_SDCARD_LIBRARY_VERSION_bug 0
#define WARCOMEB_SDCARD_LIBRARY_TIME 1499427261
#include "libohiboard.h"
#ifndef __NO_BOARD_H
#include "board.h"
#endif
#ifdef WARCOMEB_SDCARD_DEBUG
#include "../cli/cli.h"
#endif
typedef enum _SDCard_Errors
{
SDCARD_ERRORS_OK,
SDCARD_ERRORS_CARD_NOT_PRESENT,
SDCARD_ERRORS_CARD_NOT_DETECTED,
SDCARD_ERRORS_COMMAND_TIMEOUT,
SDCARD_ERRORS_COMMAND_FAILED,
SDCARD_ERRORS_TIMEOUT, /**< Generic timeout errors */
SDCARD_ERRORS_INIT_FAILED,
SDCARD_ERRORS_WRITE_BLOCK_FAILED,
SDCARD_ERRORS_READ_BLOCK_FAILED,
SDCARD_ERRORS_WRITE_BLOCKS_FAILED,
SDCARD_ERRORS_READ_BLOCKS_FAILED,
SDCARD_ERRORS_ERASE_BLOCKS_FAILED
} SDCard_Errors;
typedef enum _SDCard_PresentType
{
SDCARD_PRESENTTYPE_LOW = 0,
SDCARD_PRESENTTYPE_HIGH = 1,
} SDCard_PresentType;
typedef struct _SDCard_Device
{
Spi_DeviceHandle device;
Gpio_Pins csPin;
Gpio_Pins cpPin; /**< Card Present pin */
SDCard_PresentType cpType;
bool isSDHC;
uint8_t cardVersion;
uint8_t cardType;
void (*delayTime)(uint32_t delay); /**< Function for blocking delay */
uint32_t (*currentTime)(void); /**< Function for current time */
bool isInit;
} SDCard_Device;
/**
* @brief
*
* @param[in] dev
* @return
*/
SDCard_Errors SDCard_init (SDCard_Device* dev);
/**
* This function control if there is some pending write operations.
*
* @param[in] dev
* @return TRUE if the SDC is busy, FALSE otherwise.
*/
bool SDCard_isBusy(SDCard_Device* dev);
/**
* This function control if the SDC is present in the socket.
*
* @param[in] dev
* @return TRUE if the SDC is present in the socket, FALSE otherwise.
*/
bool SDCard_isPresent(SDCard_Device* dev);
/**
* @brief
*
* @param[in] dev
* @param[in] blockAddress
* @param[in] data
* @return
*/
SDCard_Errors SDCard_writeBlock (SDCard_Device* dev,
uint32_t blockAddress,
const uint8_t* data);
/**
* @brief
*
* @param[in] dev
* @param[in] blockAddress
* @param[in] data
* @param[in] count Number of sectors to write (1 to 128)
* @return
*/
SDCard_Errors SDCard_writeBlocks (SDCard_Device* dev,
uint32_t blockAddress,
const uint8_t* data,
uint8_t count);
/**
* @brief
*
* @param[in] dev
* @param[in] blockAddress
* @param[out] data
* @return
*/
SDCard_Errors SDCard_readBlock (SDCard_Device* dev,
uint32_t blockAddress,
uint8_t* data);
/**
* @brief
*
* @param[in] dev
* @param[in] blockAddress
* @param[out] data
* @param[in] count Number of sectors to read (1 to 128)
* @return
*/
SDCard_Errors SDCard_readBlocks (SDCard_Device* dev,
uint32_t blockAddress,
uint8_t* data,
uint8_t count);
/**
* @brief
*
* @param[in] dev
* @param[in] blockAddress
* @param[in] count
* @return
*/
SDCard_Errors SDCard_eraseBlocks (SDCard_Device* dev,
uint32_t blockAddress,
uint32_t count);
/**
* @brief
*
* @param[in] dev
* @param[out] size The number of sectors of the card
* @return
*/
SDCard_Errors SDCard_getSectorCount (SDCard_Device* dev,
uint32_t* size);
#endif /* __WARCOMEB_SDCARD_H */