[SITL] Enable telemetry: LTM and MAVLink (#8940)
[inav.git] / src / main / drivers / sdmmc_sdio.h
blob075768189dd4edb5b53854ceabf96b613e49778d
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_PRESENT ((uint8_t)0x01)
68 #define SD_NOT_PRESENT ((uint8_t)0x00)
70 #define SD_DATATIMEOUT ((uint32_t)100000000)
72 #define SD_DETECT_GPIO_PORT GPIOC
73 #define SD_DETECT_PIN GPIO_PIN_13
75 /* Structure(s) -----------------------------------------------------------------------------------------------------*/
77 typedef enum
79 // SD specific error defines
80 SD_CMD_CRC_FAIL = (1), // Command response received (but CRC check failed)
81 SD_DATA_CRC_FAIL = (2), // Data block sent/received (CRC check failed)
82 SD_CMD_RSP_TIMEOUT = (3), // Command response TimeOut
83 SD_DATA_TIMEOUT = (4), // Data TimeOut
84 SD_TX_UNDERRUN = (5), // Transmit FIFO underrun
85 SD_RX_OVERRUN = (6), // Receive FIFO overrun
86 SD_START_BIT_ERR = (7), // Start bit not detected on all data signals in wide bus mode
87 SD_CMD_OUT_OF_RANGE = (8), // Command's argument was out of range.
88 SD_ADDR_MISALIGNED = (9), // Misaligned address
89 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
90 SD_ERASE_SEQ_ERR = (11), // An error in the sequence of erase command occurs.
91 SD_BAD_ERASE_PARAM = (12), // An invalid selection for erase groups
92 SD_WRITE_PROT_VIOLATION = (13), // Attempt to program a write protect block
93 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
94 SD_COM_CRC_FAILED = (15), // CRC check of the previous command failed
95 SD_ILLEGAL_CMD = (16), // Command is not legal for the card state
96 SD_CARD_ECC_FAILED = (17), // Card internal ECC was applied but failed to correct the data
97 SD_CC_ERROR = (18), // Internal card controller error
98 SD_GENERAL_UNKNOWN_ERROR = (19), // General or unknown error
99 SD_STREAM_READ_UNDERRUN = (20), // The card could not sustain data transfer in stream read operation.
100 SD_STREAM_WRITE_OVERRUN = (21), // The card could not sustain data programming in stream mode
101 SD_CID_CSD_OVERWRITE = (22), // CID/CSD overwrite error
102 SD_WP_ERASE_SKIP = (23), // Only partial address space was erased
103 SD_CARD_ECC_DISABLED = (24), // Command has been executed without using internal ECC
104 SD_ERASE_RESET = (25), // Erase sequence was cleared before executing because an out of erase sequence command was received
105 SD_AKE_SEQ_ERROR = (26), // Error in sequence of authentication.
106 SD_INVALID_VOLTRANGE = (27),
107 SD_ADDR_OUT_OF_RANGE = (28),
108 SD_SWITCH_ERROR = (29),
109 SD_SDMMC_DISABLED = (30),
110 SD_SDMMC_FUNCTION_BUSY = (31),
111 SD_SDMMC_FUNCTION_FAILED = (32),
112 SD_SDMMC_UNKNOWN_FUNCTION = (33),
113 SD_OUT_OF_BOUND = (34),
116 // Standard error defines
117 SD_INTERNAL_ERROR = (35),
118 SD_NOT_CONFIGURED = (36),
119 SD_REQUEST_PENDING = (37),
120 SD_REQUEST_NOT_APPLICABLE = (38),
121 SD_INVALID_PARAMETER = (39),
122 SD_UNSUPPORTED_FEATURE = (40),
123 SD_UNSUPPORTED_HW = (41),
124 SD_ERROR = (42),
125 SD_BUSY = (43),
126 SD_OK = (0)
127 } SD_Error_t;
130 typedef struct
132 uint8_t DAT_BUS_WIDTH; // Shows the currently defined data bus width
133 uint8_t SECURED_MODE; // Card is in secured mode of operation
134 uint16_t SD_CARD_TYPE; // Carries information about card type
135 uint32_t SIZE_OF_PROTECTED_AREA; // Carries information about the capacity of protected area
136 uint8_t SPEED_CLASS; // Carries information about the speed class of the card
137 uint8_t PERFORMANCE_MOVE; // Carries information about the card's performance move
138 uint8_t AU_SIZE; // Carries information about the card's allocation unit size
139 uint16_t ERASE_SIZE; // Determines the number of AUs to be erased in one operation
140 uint8_t ERASE_TIMEOUT; // Determines the TimeOut for any number of AU erase
141 uint8_t ERASE_OFFSET; // Carries information about the erase offset
142 } SD_CardStatus_t;
144 typedef struct
146 uint8_t CSDStruct; // CSD structure
147 uint8_t SysSpecVersion; // System specification version
148 uint8_t Reserved1; // Reserved
149 uint8_t TAAC; // Data read access time 1
150 uint8_t NSAC; // Data read access time 2 in CLK cycles
151 uint8_t MaxBusClkFrec; // Max. bus clock frequency
152 uint16_t CardComdClasses; // Card command classes
153 uint8_t RdBlockLen; // Max. read data block length
154 uint8_t PartBlockRead; // Partial blocks for read allowed
155 uint8_t WrBlockMisalign; // Write block misalignment
156 uint8_t RdBlockMisalign; // Read block misalignment
157 uint8_t DSRImpl; // DSR implemented
158 uint8_t Reserved2; // Reserved
159 uint32_t DeviceSize; // Device Size
160 uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
161 uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
162 uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
163 uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
164 uint8_t DeviceSizeMul; // Device size multiplier
165 uint8_t EraseGrSize; // Erase group size
166 uint8_t EraseGrMul; // Erase group size multiplier
167 uint8_t WrProtectGrSize; // Write protect group size
168 uint8_t WrProtectGrEnable; // Write protect group enable
169 uint8_t ManDeflECC; // Manufacturer default ECC
170 uint8_t WrSpeedFact; // Write speed factor
171 uint8_t MaxWrBlockLen; // Max. write data block length
172 uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
173 uint8_t Reserved3; // Reserved
174 uint8_t ContentProtectAppli; // Content protection application
175 uint8_t FileFormatGrouop; // File format group
176 uint8_t CopyFlag; // Copy flag (OTP)
177 uint8_t PermWrProtect; // Permanent write protection
178 uint8_t TempWrProtect; // Temporary write protection
179 uint8_t FileFormat; // File format
180 uint8_t ECC; // ECC code
181 uint8_t CSD_CRC; // CSD CRC
182 uint8_t Reserved4; // Always 1
183 } SD_CSD_t;
185 typedef struct
187 uint8_t ManufacturerID; // Manufacturer ID
188 uint16_t OEM_AppliID; // OEM/Application ID
189 uint32_t ProdName1; // Product Name part1
190 uint8_t ProdName2; // Product Name part2
191 uint8_t ProdRev; // Product Revision
192 uint32_t ProdSN; // Product Serial Number
193 uint8_t Reserved1; // Reserved1
194 uint16_t ManufactDate; // Manufacturing Date
195 uint8_t CID_CRC; // CID CRC
196 uint8_t Reserved2; // Always 1
198 } SD_CID_t;
200 typedef enum
202 SD_STD_CAPACITY_V1_1 = 0,
203 SD_STD_CAPACITY_V2_0 = 1,
204 SD_HIGH_CAPACITY = 2,
205 SD_MULTIMEDIA = 3,
206 SD_SECURE_DIGITAL_IO = 4,
207 SD_HIGH_SPEED_MULTIMEDIA = 5,
208 SD_SECURE_DIGITAL_IO_COMBO = 6,
209 SD_HIGH_CAPACITY_MMC = 7,
210 } SD_CardType_t;
212 typedef struct
214 volatile SD_CSD_t SD_csd; // SD card specific data register
215 volatile SD_CID_t SD_cid; // SD card identification number register
216 uint64_t CardCapacity; // Card capacity
217 uint32_t CardBlockSize; // Card block size
218 } SD_CardInfo_t;
220 /* Prototype(s) -----------------------------------------------------------------------------------------------------*/
222 extern SD_CardInfo_t SD_CardInfo;
223 extern SD_CardType_t SD_CardType;
225 void SD_Initialize_LL (DMA_Stream_TypeDef *dma);
226 bool SD_Init (void);
227 bool SD_IsDetected (void);
228 bool SD_GetState (void);
229 SD_Error_t SD_GetCardInfo (void);
231 SD_Error_t SD_ReadBlocks_DMA (uint64_t ReadAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
232 SD_Error_t SD_CheckRead (void);
233 SD_Error_t SD_WriteBlocks_DMA (uint64_t WriteAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
234 SD_Error_t SD_CheckWrite (void);
236 SD_Error_t SD_Erase (uint64_t StartAddress, uint64_t EndAddress);
237 SD_Error_t SD_GetCardStatus (SD_CardStatus_t* pCardStatus);
239 /* ------------------------------------------------------------------------------------------------------------------*/
241 #endif // __fatfs_sd_sdio_H__