NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
sdcard_standard.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * Ninjaflight is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stdint.h>
21 
22 typedef struct sdcardCSD_t {
23  uint8_t data[16];
24 } sdcardCSD_t;
25 
26 #define SDCARD_GET_CSD_FIELD(csd, version, fieldname) \
27  readBitfield(csd.data, SDCARD_CSD_V ## version ## _ ## fieldname ## _OFFSET, SDCARD_CSD_V ## version ## _ ## fieldname ## _LEN)
28 
29 // For v1 and Standard Capacity cards
30 #define SDCARD_CSD_V1_CSD_STRUCTURE_VER_OFFSET 0
31 #define SDCARD_CSD_V1_CSD_STRUCTURE_VER_LEN 2
32 
33 #define SDCARD_CSD_V1_TAAC_OFFSET 8
34 #define SDCARD_CSD_V1_TAAC_LEN 8
35 
36 #define SDCARD_CSD_V1_NSAC_OFFSET 16
37 #define SDCARD_CSD_V1_NSAC_LEN 8
38 
39 #define SDCARD_CSD_V1_TRAN_SPEED_OFFSET 24
40 #define SDCARD_CSD_V1_TRAN_SPEED_LEN 8
41 
42 #define SDCARD_CSD_V1_CCC_OFFSET 32
43 #define SDCARD_CSD_V1_CCC_LEN 12
44 
45 #define SDCARD_CSD_V1_READ_BLOCK_LEN_OFFSET 44
46 #define SDCARD_CSD_V1_READ_BLOCK_LEN_LEN 4
47 
48 #define SDCARD_CSD_V1_READ_BLOCK_PARTIAL_ALLOWED_OFFSET 48
49 #define SDCARD_CSD_V1_READ_BLOCK_PARTIAL_ALLOWED_LEN 1
50 
51 #define SDCARD_CSD_V1_WRITE_BLOCK_MISALIGN_OFFSET 49
52 #define SDCARD_CSD_V1_WRITE_BLOCK_MISALIGN_LEN 1
53 
54 #define SDCARD_CSD_V1_READ_BLOCK_MISALIGN_OFFSET 50
55 #define SDCARD_CSD_V1_READ_BLOCK_MISALIGN_LEN 1
56 
57 #define SDCARD_CSD_V1_DSR_IMPLEMENTED_OFFSET 51
58 #define SDCARD_CSD_V1_DSR_IMPLEMENTED_LEN 1
59 
60 #define SDCARD_CSD_V1_CSIZE_OFFSET 54
61 #define SDCARD_CSD_V1_CSIZE_LEN 12
62 
63 #define SDCARD_CSD_V1_VDD_READ_CURR_MIN_OFFSET 66
64 #define SDCARD_CSD_V1_VDD_READ_CURR_MIN_LEN 3
65 
66 #define SDCARD_CSD_V1_VDD_READ_CURR_MAX_OFFSET 69
67 #define SDCARD_CSD_V1_VDD_READ_CURR_MAX_LEN 3
68 
69 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MIN_OFFSET 72
70 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MIN_LEN 3
71 
72 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MAX_OFFSET 75
73 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MAX_LEN 3
74 
75 #define SDCARD_CSD_V1_CSIZE_MULT_OFFSET 78
76 #define SDCARD_CSD_V1_CSIZE_MULT_LEN 3
77 
78 #define SDCARD_CSD_V1_ERASE_SINGLE_BLOCK_ALLOWED_OFFSET 81
79 #define SDCARD_CSD_V1_ERASE_SINGLE_BLOCK_ALLOWED_LEN 1
80 
81 #define SDCARD_CSD_V1_SECTOR_SIZE_OFFSET 82
82 #define SDCARD_CSD_V1_SECTOR_SIZE_LEN 7
83 
84 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_SIZE_OFFSET 89
85 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_SIZE_LEN 7
86 
87 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_ENABLE_OFFSET 96
88 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_ENABLE_LEN 1
89 
90 #define SDCARD_CSD_V1_R2W_FACTOR_OFFSET 99
91 #define SDCARD_CSD_V1_R2W_FACTOR_LEN 3
92 
93 #define SDCARD_CSD_V1_WRITE_BLOCK_LEN_OFFSET 102
94 #define SDCARD_CSD_V1_WRITE_BLOCK_LEN_LEN 4
95 
96 #define SDCARD_CSD_V1_WRITE_BLOCK_PARTIAL_ALLOWED_OFFSET 106
97 #define SDCARD_CSD_V1_WRITE_BLOCK_PARTIAL_ALLOWED_LEN 1
98 
99 #define SDCARD_CSD_V1_FILE_FORMAT_GROUP_OFFSET 112
100 #define SDCARD_CSD_V1_FILE_FORMAT_GROUP_LEN 1
101 
102 #define SDCARD_CSD_V1_COPY_OFFSET 113
103 #define SDCARD_CSD_V1_COPY_LEN 1
104 
105 #define SDCARD_CSD_V1_PERMANENT_WRITE_PROTECT_OFFSET 114
106 #define SDCARD_CSD_V1_PERMANENT_WRITE_PROTECT_LEN 1
107 
108 #define SDCARD_CSD_V1_TEMPORARY_WRITE_PROTECT_OFFSET 115
109 #define SDCARD_CSD_V1_TEMPORARY_WRITE_PROTECT_LEN 1
110 
111 #define SDCARD_CSD_V1_FILE_FORMAT_OFFSET 116
112 #define SDCARD_CSD_V1_FILE_FORMAT_LEN 2
113 
114 #define SDCARD_CSD_V1_CRC_OFFSET 120
115 #define SDCARD_CSD_V1_CRC_LEN 7
116 
117 #define SDCARD_CSD_V1_TRAILER_OFFSET 127
118 #define SDCARD_CSD_V1_TRAILER_LEN 1
119 
120 // For v2 High Capacity cards
121 #define SDCARD_CSD_V2_CSD_STRUCTURE_VER_OFFSET 0
122 #define SDCARD_CSD_V2_CSD_STRUCTURE_VER_LEN 2
123 
124 #define SDCARD_CSD_V2_TAAC_OFFSET 8
125 #define SDCARD_CSD_V2_TAAC_LEN 8
126 
127 #define SDCARD_CSD_V2_NSAC_OFFSET 16
128 #define SDCARD_CSD_V2_NSAC_LEN 8
129 
130 #define SDCARD_CSD_V2_TRAN_SPEED_OFFSET 24
131 #define SDCARD_CSD_V2_TRAN_SPEED_LEN 8
132 
133 #define SDCARD_CSD_V2_CCC_OFFSET 32
134 #define SDCARD_CSD_V2_CCC_LEN 12
135 
136 #define SDCARD_CSD_V2_READ_BLOCK_LEN_OFFSET 44
137 #define SDCARD_CSD_V2_READ_BLOCK_LEN_LEN 4
138 
139 #define SDCARD_CSD_V2_READ_BLOCK_PARTIAL_ALLOWED_OFFSET 48
140 #define SDCARD_CSD_V2_READ_BLOCK_PARTIAL_ALLOWED_LEN 1
141 
142 #define SDCARD_CSD_V2_WRITE_BLOCK_MISALIGN_OFFSET 49
143 #define SDCARD_CSD_V2_WRITE_BLOCK_MISALIGN_LEN 1
144 
145 #define SDCARD_CSD_V2_READ_BLOCK_MISALIGN_OFFSET 50
146 #define SDCARD_CSD_V2_READ_BLOCK_MISALIGN_LEN 1
147 
148 #define SDCARD_CSD_V2_DSR_IMPLEMENTED_OFFSET 51
149 #define SDCARD_CSD_V2_DSR_IMPLEMENTED_LEN 1
150 
151 #define SDCARD_CSD_V2_CSIZE_OFFSET 58
152 #define SDCARD_CSD_V2_CSIZE_LEN 22
153 
154 #define SDCARD_CSD_V2_ERASE_SINGLE_BLOCK_ALLOWED_OFFSET 81
155 #define SDCARD_CSD_V2_ERASE_SINGLE_BLOCK_ALLOWED_LEN 1
156 
157 #define SDCARD_CSD_V2_SECTOR_SIZE_OFFSET 82
158 #define SDCARD_CSD_V2_SECTOR_SIZE_LEN 7
159 
160 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_SIZE_OFFSET 89
161 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_SIZE_LEN 7
162 
163 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_ENABLE_OFFSET 96
164 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_ENABLE_LEN 1
165 
166 #define SDCARD_CSD_V2_R2W_FACTOR_OFFSET 99
167 #define SDCARD_CSD_V2_R2W_FACTOR_LEN 3
168 
169 #define SDCARD_CSD_V2_WRITE_BLOCK_LEN_OFFSET 102
170 #define SDCARD_CSD_V2_WRITE_BLOCK_LEN_LEN 4
171 
172 #define SDCARD_CSD_V2_WRITE_BLOCK_PARTIAL_ALLOWED_OFFSET 106
173 #define SDCARD_CSD_V2_WRITE_BLOCK_PARTIAL_ALLOWED_LEN 1
174 
175 #define SDCARD_CSD_V2_FILE_FORMAT_GROUP_OFFSET 112
176 #define SDCARD_CSD_V2_FILE_FORMAT_GROUP_LEN 1
177 
178 #define SDCARD_CSD_V2_COPY_OFFSET 113
179 #define SDCARD_CSD_V2_COPY_LEN 1
180 
181 #define SDCARD_CSD_V2_PERMANENT_WRITE_PROTECT_OFFSET 114
182 #define SDCARD_CSD_V2_PERMANENT_WRITE_PROTECT_LEN 1
183 
184 #define SDCARD_CSD_V2_TEMPORARY_WRITE_PROTECT_OFFSET 115
185 #define SDCARD_CSD_V2_TEMPORARY_WRITE_PROTECT_LEN 1
186 
187 #define SDCARD_CSD_V2_FILE_FORMAT_OFFSET 116
188 #define SDCARD_CSD_V2_FILE_FORMAT_LEN 2
189 
190 #define SDCARD_CSD_V2_CRC_OFFSET 120
191 #define SDCARD_CSD_V2_CRC_LEN 7
192 
193 #define SDCARD_CSD_V2_TRAILER_OFFSET 127
194 #define SDCARD_CSD_V2_TRAILER_LEN 1
195 
196 #define SDCARD_SINGLE_BLOCK_READ_START_TOKEN 0xFE
197 #define SDCARD_SINGLE_BLOCK_WRITE_START_TOKEN 0xFE
198 #define SDCARD_MULTIPLE_BLOCK_WRITE_START_TOKEN 0xFC
199 #define SDCARD_MULTIPLE_BLOCK_WRITE_STOP_TOKEN 0xFD
200 
201 #define SDCARD_BLOCK_SIZE 512
202 
203 // Idle bit is set to 1 only when idle during intialization phase:
204 #define SDCARD_R1_STATUS_BIT_IDLE 1
205 #define SDCARD_R1_STATUS_BIT_ERASE_RESET 2
206 #define SDCARD_R1_STATUS_BIT_ILLEGAL_COMMAND 4
207 #define SDCARD_R1_STATUS_BIT_COM_CRC_ERROR 8
208 #define SDCARD_R1_STATUS_BIT_ERASE_SEQUENCE_ERROR 16
209 #define SDCARD_R1_STATUS_BIT_ADDRESS_ERROR 32
210 #define SDCARD_R1_STATUS_BIT_PARAMETER_ERROR 64
211 
212 #define SDCARD_CSD_STRUCTURE_VERSION_1 0
213 #define SDCARD_CSD_STRUCTURE_VERSION_2 1
214 
215 #define SDCARD_VOLTAGE_ACCEPTED_2_7_to_3_6 0x01
216 #define SDCARD_VOLTAGE_ACCEPTED_LVR 0x02
217 
218 #define SDCARD_COMMAND_GO_IDLE_STATE 0
219 #define SDCARD_COMMAND_SEND_OP_COND 1
220 #define SDCARD_COMMAND_SEND_IF_COND 8
221 #define SDCARD_COMMAND_SEND_CSD 9
222 #define SDCARD_COMMAND_SEND_CID 10
223 #define SDCARD_COMMAND_STOP_TRANSMISSION 12
224 #define SDCARD_COMMAND_SEND_STATUS 13
225 #define SDCARD_COMMAND_SET_BLOCKLEN 16
226 #define SDCARD_COMMAND_READ_SINGLE_BLOCK 17
227 #define SDCARD_COMMAND_READ_MULTIPLE_BLOCK 18
228 #define SDCARD_COMMAND_WRITE_BLOCK 24
229 #define SDCARD_COMMAND_WRITE_MULTIPLE_BLOCK 25
230 #define SDCARD_COMMAND_APP_CMD 55
231 #define SDCARD_COMMAND_READ_OCR 58
232 
233 #define SDCARD_ACOMMAND_SEND_OP_COND 41
234 #define SDCARD_ACOMMAND_SET_WR_BLOCK_ERASE_COUNT 23
235 
236 // These are worst-case timeouts defined for High Speed cards
237 #define SDCARD_TIMEOUT_READ_MSEC 100
238 #define SDCARD_TIMEOUT_WRITE_MSEC 250
239 
240 uint32_t readBitfield(uint8_t *buffer, unsigned bitIndex, unsigned bitLen);
uint32_t readBitfield(uint8_t *buffer, unsigned bitIndex, unsigned bitLen)
Definition: sdcard_standard.c:11
uint8_t data[16]
Definition: sdcard_standard.h:23
Definition: sdcard_standard.h:22
struct sdcardCSD_t sdcardCSD_t