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