Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / sdcard / sdcard_standard.h
blobec94db301922641e65ece7b09e66b4812f0a481d
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight 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.
9 * Cleanflight 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.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #pragma once
20 #include <stdint.h>
22 typedef struct sdcardCSD_t {
23 uint8_t data[16];
24 } sdcardCSD_t;
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)
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
33 #define SDCARD_CSD_V1_TAAC_OFFSET 8
34 #define SDCARD_CSD_V1_TAAC_LEN 8
36 #define SDCARD_CSD_V1_NSAC_OFFSET 16
37 #define SDCARD_CSD_V1_NSAC_LEN 8
39 #define SDCARD_CSD_V1_TRAN_SPEED_OFFSET 24
40 #define SDCARD_CSD_V1_TRAN_SPEED_LEN 8
42 #define SDCARD_CSD_V1_CCC_OFFSET 32
43 #define SDCARD_CSD_V1_CCC_LEN 12
45 #define SDCARD_CSD_V1_READ_BLOCK_LEN_OFFSET 44
46 #define SDCARD_CSD_V1_READ_BLOCK_LEN_LEN 4
48 #define SDCARD_CSD_V1_READ_BLOCK_PARTIAL_ALLOWED_OFFSET 48
49 #define SDCARD_CSD_V1_READ_BLOCK_PARTIAL_ALLOWED_LEN 1
51 #define SDCARD_CSD_V1_WRITE_BLOCK_MISALIGN_OFFSET 49
52 #define SDCARD_CSD_V1_WRITE_BLOCK_MISALIGN_LEN 1
54 #define SDCARD_CSD_V1_READ_BLOCK_MISALIGN_OFFSET 50
55 #define SDCARD_CSD_V1_READ_BLOCK_MISALIGN_LEN 1
57 #define SDCARD_CSD_V1_DSR_IMPLEMENTED_OFFSET 51
58 #define SDCARD_CSD_V1_DSR_IMPLEMENTED_LEN 1
60 #define SDCARD_CSD_V1_CSIZE_OFFSET 54
61 #define SDCARD_CSD_V1_CSIZE_LEN 12
63 #define SDCARD_CSD_V1_VDD_READ_CURR_MIN_OFFSET 66
64 #define SDCARD_CSD_V1_VDD_READ_CURR_MIN_LEN 3
66 #define SDCARD_CSD_V1_VDD_READ_CURR_MAX_OFFSET 69
67 #define SDCARD_CSD_V1_VDD_READ_CURR_MAX_LEN 3
69 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MIN_OFFSET 72
70 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MIN_LEN 3
72 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MAX_OFFSET 75
73 #define SDCARD_CSD_V1_VDD_WRITE_CURR_MAX_LEN 3
75 #define SDCARD_CSD_V1_CSIZE_MULT_OFFSET 78
76 #define SDCARD_CSD_V1_CSIZE_MULT_LEN 3
78 #define SDCARD_CSD_V1_ERASE_SINGLE_BLOCK_ALLOWED_OFFSET 81
79 #define SDCARD_CSD_V1_ERASE_SINGLE_BLOCK_ALLOWED_LEN 1
81 #define SDCARD_CSD_V1_SECTOR_SIZE_OFFSET 82
82 #define SDCARD_CSD_V1_SECTOR_SIZE_LEN 7
84 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_SIZE_OFFSET 89
85 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_SIZE_LEN 7
87 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_ENABLE_OFFSET 96
88 #define SDCARD_CSD_V1_WRITE_PROTECT_GROUP_ENABLE_LEN 1
90 #define SDCARD_CSD_V1_R2W_FACTOR_OFFSET 99
91 #define SDCARD_CSD_V1_R2W_FACTOR_LEN 3
93 #define SDCARD_CSD_V1_WRITE_BLOCK_LEN_OFFSET 102
94 #define SDCARD_CSD_V1_WRITE_BLOCK_LEN_LEN 4
96 #define SDCARD_CSD_V1_WRITE_BLOCK_PARTIAL_ALLOWED_OFFSET 106
97 #define SDCARD_CSD_V1_WRITE_BLOCK_PARTIAL_ALLOWED_LEN 1
99 #define SDCARD_CSD_V1_FILE_FORMAT_GROUP_OFFSET 112
100 #define SDCARD_CSD_V1_FILE_FORMAT_GROUP_LEN 1
102 #define SDCARD_CSD_V1_COPY_OFFSET 113
103 #define SDCARD_CSD_V1_COPY_LEN 1
105 #define SDCARD_CSD_V1_PERMANENT_WRITE_PROTECT_OFFSET 114
106 #define SDCARD_CSD_V1_PERMANENT_WRITE_PROTECT_LEN 1
108 #define SDCARD_CSD_V1_TEMPORARY_WRITE_PROTECT_OFFSET 115
109 #define SDCARD_CSD_V1_TEMPORARY_WRITE_PROTECT_LEN 1
111 #define SDCARD_CSD_V1_FILE_FORMAT_OFFSET 116
112 #define SDCARD_CSD_V1_FILE_FORMAT_LEN 2
114 #define SDCARD_CSD_V1_CRC_OFFSET 120
115 #define SDCARD_CSD_V1_CRC_LEN 7
117 #define SDCARD_CSD_V1_TRAILER_OFFSET 127
118 #define SDCARD_CSD_V1_TRAILER_LEN 1
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
124 #define SDCARD_CSD_V2_TAAC_OFFSET 8
125 #define SDCARD_CSD_V2_TAAC_LEN 8
127 #define SDCARD_CSD_V2_NSAC_OFFSET 16
128 #define SDCARD_CSD_V2_NSAC_LEN 8
130 #define SDCARD_CSD_V2_TRAN_SPEED_OFFSET 24
131 #define SDCARD_CSD_V2_TRAN_SPEED_LEN 8
133 #define SDCARD_CSD_V2_CCC_OFFSET 32
134 #define SDCARD_CSD_V2_CCC_LEN 12
136 #define SDCARD_CSD_V2_READ_BLOCK_LEN_OFFSET 44
137 #define SDCARD_CSD_V2_READ_BLOCK_LEN_LEN 4
139 #define SDCARD_CSD_V2_READ_BLOCK_PARTIAL_ALLOWED_OFFSET 48
140 #define SDCARD_CSD_V2_READ_BLOCK_PARTIAL_ALLOWED_LEN 1
142 #define SDCARD_CSD_V2_WRITE_BLOCK_MISALIGN_OFFSET 49
143 #define SDCARD_CSD_V2_WRITE_BLOCK_MISALIGN_LEN 1
145 #define SDCARD_CSD_V2_READ_BLOCK_MISALIGN_OFFSET 50
146 #define SDCARD_CSD_V2_READ_BLOCK_MISALIGN_LEN 1
148 #define SDCARD_CSD_V2_DSR_IMPLEMENTED_OFFSET 51
149 #define SDCARD_CSD_V2_DSR_IMPLEMENTED_LEN 1
151 #define SDCARD_CSD_V2_CSIZE_OFFSET 58
152 #define SDCARD_CSD_V2_CSIZE_LEN 22
154 #define SDCARD_CSD_V2_ERASE_SINGLE_BLOCK_ALLOWED_OFFSET 81
155 #define SDCARD_CSD_V2_ERASE_SINGLE_BLOCK_ALLOWED_LEN 1
157 #define SDCARD_CSD_V2_SECTOR_SIZE_OFFSET 82
158 #define SDCARD_CSD_V2_SECTOR_SIZE_LEN 7
160 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_SIZE_OFFSET 89
161 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_SIZE_LEN 7
163 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_ENABLE_OFFSET 96
164 #define SDCARD_CSD_V2_WRITE_PROTECT_GROUP_ENABLE_LEN 1
166 #define SDCARD_CSD_V2_R2W_FACTOR_OFFSET 99
167 #define SDCARD_CSD_V2_R2W_FACTOR_LEN 3
169 #define SDCARD_CSD_V2_WRITE_BLOCK_LEN_OFFSET 102
170 #define SDCARD_CSD_V2_WRITE_BLOCK_LEN_LEN 4
172 #define SDCARD_CSD_V2_WRITE_BLOCK_PARTIAL_ALLOWED_OFFSET 106
173 #define SDCARD_CSD_V2_WRITE_BLOCK_PARTIAL_ALLOWED_LEN 1
175 #define SDCARD_CSD_V2_FILE_FORMAT_GROUP_OFFSET 112
176 #define SDCARD_CSD_V2_FILE_FORMAT_GROUP_LEN 1
178 #define SDCARD_CSD_V2_COPY_OFFSET 113
179 #define SDCARD_CSD_V2_COPY_LEN 1
181 #define SDCARD_CSD_V2_PERMANENT_WRITE_PROTECT_OFFSET 114
182 #define SDCARD_CSD_V2_PERMANENT_WRITE_PROTECT_LEN 1
184 #define SDCARD_CSD_V2_TEMPORARY_WRITE_PROTECT_OFFSET 115
185 #define SDCARD_CSD_V2_TEMPORARY_WRITE_PROTECT_LEN 1
187 #define SDCARD_CSD_V2_FILE_FORMAT_OFFSET 116
188 #define SDCARD_CSD_V2_FILE_FORMAT_LEN 2
190 #define SDCARD_CSD_V2_CRC_OFFSET 120
191 #define SDCARD_CSD_V2_CRC_LEN 7
193 #define SDCARD_CSD_V2_TRAILER_OFFSET 127
194 #define SDCARD_CSD_V2_TRAILER_LEN 1
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
201 #define SDCARD_BLOCK_SIZE 512
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
212 #define SDCARD_CSD_STRUCTURE_VERSION_1 0
213 #define SDCARD_CSD_STRUCTURE_VERSION_2 1
215 #define SDCARD_VOLTAGE_ACCEPTED_2_7_to_3_6 0x01
216 #define SDCARD_VOLTAGE_ACCEPTED_LVR 0x02
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
233 #define SDCARD_ACOMMAND_SEND_OP_COND 41
234 #define SDCARD_ACOMMAND_SET_WR_BLOCK_ERASE_COUNT 23
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
240 uint32_t readBitfield(uint8_t *buffer, unsigned bitIndex, unsigned bitLen);