Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / sdmmc_sdio.h
blob647cb2d732002fc663fb2c917dd6b251746c6e28
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/>.
22 * Original author: Alain (https://github.com/aroyer-qc)
23 * Modified for F4 and BF source: Chris Hockuba (https://github.com/conkerkh)
27 #ifndef __fatfs_sd_sdio_H__
28 #define __fatfs_sd_sdio_H__
30 #include <stdint.h>
32 #include "platform.h"
33 #ifdef STM32F4
34 #include "stm32f4xx.h"
35 #endif
37 #ifdef STM32F7
38 #include "stm32f7xx.h"
39 #endif
41 /* SDCARD pinouts
43 * SD CARD PINS
44 _________________
45 / 1 2 3 4 5 6 7 8 | NR |SDIO INTERFACE
46 / | |NAME STM32F746 DESCRIPTION
47 / 9 | | 4-BIT 1-BIT
48 | | |
49 | | 1 |CD/DAT3 PC11 - Connector data line 3
50 | | 2 |CMD PD2 PD2 Command/Response line
51 | | 3 |VSS1 GND GND GND
52 | SD CARD Pinout | 4 |VDD 3.3V 3.3V 3.3V Power supply
53 | | 5 |CLK PC12 PC12 Clock
54 | | 6 |VSS2 GND GND GND
55 | | 7 |DAT0 PC8 PC8 Connector data line 0
56 | | 8 |DAT1 PC9 - Connector data line 1
57 |___________________| 9 |DAT2 PC10 - Connector data line 2
61 /* Define(s) --------------------------------------------------------------------------------------------------------*/
63 //#define MSD_OK ((uint8_t)0x00)
64 #define MSD_ERROR ((uint8_t)0x01)
65 #define MSD_ERROR_SD_NOT_PRESENT ((uint8_t)0x02)
67 #define SD_DATATIMEOUT ((uint32_t)100000000)
69 #define SD_DETECT_GPIO_PORT GPIOC
70 #define SD_DETECT_PIN GPIO_PIN_13
72 /* Structure(s) -----------------------------------------------------------------------------------------------------*/
74 typedef enum
76 // SD specific error defines
77 SD_CMD_CRC_FAIL = (1), // Command response received (but CRC check failed)
78 SD_DATA_CRC_FAIL = (2), // Data block sent/received (CRC check failed)
79 SD_CMD_RSP_TIMEOUT = (3), // Command response TimeOut
80 SD_DATA_TIMEOUT = (4), // Data TimeOut
81 SD_TX_UNDERRUN = (5), // Transmit FIFO underrun
82 SD_RX_OVERRUN = (6), // Receive FIFO overrun
83 SD_START_BIT_ERR = (7), // Start bit not detected on all data signals in wide bus mode
84 SD_CMD_OUT_OF_RANGE = (8), // Command's argument was out of range.
85 SD_ADDR_MISALIGNED = (9), // Misaligned address
86 SD_BLOCK_LEN_ERR = (10), // Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length
87 SD_ERASE_SEQ_ERR = (11), // An error in the sequence of erase command occurs.
88 SD_BAD_ERASE_PARAM = (12), // An invalid selection for erase groups
89 SD_WRITE_PROT_VIOLATION = (13), // Attempt to program a write protect block
90 SD_LOCK_UNLOCK_FAILED = (14), // Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card
91 SD_COM_CRC_FAILED = (15), // CRC check of the previous command failed
92 SD_ILLEGAL_CMD = (16), // Command is not legal for the card state
93 SD_CARD_ECC_FAILED = (17), // Card internal ECC was applied but failed to correct the data
94 SD_CC_ERROR = (18), // Internal card controller error
95 SD_GENERAL_UNKNOWN_ERROR = (19), // General or unknown error
96 SD_STREAM_READ_UNDERRUN = (20), // The card could not sustain data transfer in stream read operation.
97 SD_STREAM_WRITE_OVERRUN = (21), // The card could not sustain data programming in stream mode
98 SD_CID_CSD_OVERWRITE = (22), // CID/CSD overwrite error
99 SD_WP_ERASE_SKIP = (23), // Only partial address space was erased
100 SD_CARD_ECC_DISABLED = (24), // Command has been executed without using internal ECC
101 SD_ERASE_RESET = (25), // Erase sequence was cleared before executing because an out of erase sequence command was received
102 SD_AKE_SEQ_ERROR = (26), // Error in sequence of authentication.
103 SD_INVALID_VOLTRANGE = (27),
104 SD_ADDR_OUT_OF_RANGE = (28),
105 SD_SWITCH_ERROR = (29),
106 SD_SDMMC_DISABLED = (30),
107 SD_SDMMC_FUNCTION_BUSY = (31),
108 SD_SDMMC_FUNCTION_FAILED = (32),
109 SD_SDMMC_UNKNOWN_FUNCTION = (33),
110 SD_OUT_OF_BOUND = (34),
113 // Standard error defines
114 SD_INTERNAL_ERROR = (35),
115 SD_NOT_CONFIGURED = (36),
116 SD_REQUEST_PENDING = (37),
117 SD_REQUEST_NOT_APPLICABLE = (38),
118 SD_INVALID_PARAMETER = (39),
119 SD_UNSUPPORTED_FEATURE = (40),
120 SD_UNSUPPORTED_HW = (41),
121 SD_ERROR = (42),
122 SD_BUSY = (43),
123 SD_OK = (0)
124 } SD_Error_t;
127 typedef struct
129 uint8_t DAT_BUS_WIDTH; // Shows the currently defined data bus width
130 uint8_t SECURED_MODE; // Card is in secured mode of operation
131 uint16_t SD_CARD_TYPE; // Carries information about card type
132 uint32_t SIZE_OF_PROTECTED_AREA; // Carries information about the capacity of protected area
133 uint8_t SPEED_CLASS; // Carries information about the speed class of the card
134 uint8_t PERFORMANCE_MOVE; // Carries information about the card's performance move
135 uint8_t AU_SIZE; // Carries information about the card's allocation unit size
136 uint16_t ERASE_SIZE; // Determines the number of AUs to be erased in one operation
137 uint8_t ERASE_TIMEOUT; // Determines the TimeOut for any number of AU erase
138 uint8_t ERASE_OFFSET; // Carries information about the erase offset
139 } SD_CardStatus_t;
141 typedef struct
143 uint8_t CSDStruct; // CSD structure
144 uint8_t SysSpecVersion; // System specification version
145 uint8_t Reserved1; // Reserved
146 uint8_t TAAC; // Data read access time 1
147 uint8_t NSAC; // Data read access time 2 in CLK cycles
148 uint8_t MaxBusClkFrec; // Max. bus clock frequency
149 uint16_t CardComdClasses; // Card command classes
150 uint8_t RdBlockLen; // Max. read data block length
151 uint8_t PartBlockRead; // Partial blocks for read allowed
152 uint8_t WrBlockMisalign; // Write block misalignment
153 uint8_t RdBlockMisalign; // Read block misalignment
154 uint8_t DSRImpl; // DSR implemented
155 uint8_t Reserved2; // Reserved
156 uint32_t DeviceSize; // Device Size
157 uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
158 uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
159 uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
160 uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
161 uint8_t DeviceSizeMul; // Device size multiplier
162 uint8_t EraseGrSize; // Erase group size
163 uint8_t EraseGrMul; // Erase group size multiplier
164 uint8_t WrProtectGrSize; // Write protect group size
165 uint8_t WrProtectGrEnable; // Write protect group enable
166 uint8_t ManDeflECC; // Manufacturer default ECC
167 uint8_t WrSpeedFact; // Write speed factor
168 uint8_t MaxWrBlockLen; // Max. write data block length
169 uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
170 uint8_t Reserved3; // Reserved
171 uint8_t ContentProtectAppli; // Content protection application
172 uint8_t FileFormatGrouop; // File format group
173 uint8_t CopyFlag; // Copy flag (OTP)
174 uint8_t PermWrProtect; // Permanent write protection
175 uint8_t TempWrProtect; // Temporary write protection
176 uint8_t FileFormat; // File format
177 uint8_t ECC; // ECC code
178 uint8_t CSD_CRC; // CSD CRC
179 uint8_t Reserved4; // Always 1
180 } SD_CSD_t;
182 typedef struct
184 uint8_t ManufacturerID; // Manufacturer ID
185 uint16_t OEM_AppliID; // OEM/Application ID
186 uint32_t ProdName1; // Product Name part1
187 uint8_t ProdName2; // Product Name part2
188 uint8_t ProdRev; // Product Revision
189 uint32_t ProdSN; // Product Serial Number
190 uint8_t Reserved1; // Reserved1
191 uint16_t ManufactDate; // Manufacturing Date
192 uint8_t CID_CRC; // CID CRC
193 uint8_t Reserved2; // Always 1
195 } SD_CID_t;
197 typedef enum
199 SD_STD_CAPACITY_V1_1 = 0,
200 SD_STD_CAPACITY_V2_0 = 1,
201 SD_HIGH_CAPACITY = 2,
202 SD_MULTIMEDIA = 3,
203 SD_SECURE_DIGITAL_IO = 4,
204 SD_HIGH_SPEED_MULTIMEDIA = 5,
205 SD_SECURE_DIGITAL_IO_COMBO = 6,
206 SD_HIGH_CAPACITY_MMC = 7,
207 } SD_CardType_t;
209 typedef struct
211 volatile SD_CSD_t SD_csd; // SD card specific data register
212 volatile SD_CID_t SD_cid; // SD card identification number register
213 uint64_t CardCapacity; // Card capacity
214 uint32_t CardBlockSize; // Card block size
215 } SD_CardInfo_t;
217 /* Prototype(s) -----------------------------------------------------------------------------------------------------*/
219 extern SD_CardInfo_t SD_CardInfo;
220 extern SD_CardType_t SD_CardType;
222 bool SD_Initialize_LL (DMA_Stream_TypeDef *dma);
223 SD_Error_t SD_Init (void);
224 bool SD_IsDetected (void);
225 bool SD_GetState (void);
226 SD_Error_t SD_GetCardInfo (void);
228 SD_Error_t SD_ReadBlocks_DMA (uint64_t ReadAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
229 SD_Error_t SD_CheckRead (void);
230 SD_Error_t SD_WriteBlocks_DMA (uint64_t WriteAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
231 SD_Error_t SD_CheckWrite (void);
233 SD_Error_t SD_Erase (uint64_t StartAddress, uint64_t EndAddress);
234 SD_Error_t SD_GetCardStatus (SD_CardStatus_t* pCardStatus);
236 /* ------------------------------------------------------------------------------------------------------------------*/
238 #endif // __fatfs_sd_sdio_H__