[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / msc / usbd_storage_sdio.c
blobe424d1944a6173d92eafe291cc1aa6e432c2905d
1 /**
2 ******************************************************************************
3 * @file usbd_storage_template.c
4 * @author MCD Application Team
5 * @version V1.2.0
6 * @date 09-November-2015
7 * @brief Memory management layer
8 ******************************************************************************
9 * @attention
11 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
13 * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
14 * You may not use this file except in compliance with the License.
15 * You may obtain a copy of the License at:
17 * http://www.st.com/software_license_agreement_liberty_v2
19 * Unless required by applicable law or agreed to in writing, software
20 * distributed under the License is distributed on an "AS IS" BASIS,
21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 * See the License for the specific language governing permissions and
23 * limitations under the License.
25 ******************************************************************************
29 /* Includes ------------------------------------------------------------------*/
30 #include <stdio.h>
31 #include <stdbool.h>
33 #include "platform.h"
35 #ifdef USE_SDCARD
37 #include "common/utils.h"
39 #include "drivers/dma.h"
40 #include "drivers/dma_reqmap.h"
41 #include "drivers/io.h"
42 #include "drivers/light_led.h"
43 #include "drivers/sdcard.h"
44 #include "drivers/sdmmc_sdio.h"
45 #include "drivers/usb_msc.h"
47 #include "pg/pg.h"
48 #include "pg/sdcard.h"
49 #include "pg/sdio.h"
51 #ifdef USE_HAL_DRIVER
52 #include "usbd_msc.h"
53 #else
54 #include "usbd_msc_mem.h"
55 #include "usbd_msc_core.h"
56 #endif
58 #include "usbd_storage.h"
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /* Extern function prototypes ------------------------------------------------*/
66 /* Private functions ---------------------------------------------------------*/
68 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
69 * otherwise SDIO won't be able to preempt USB.
72 #define STORAGE_LUN_NBR 1
73 #define STORAGE_BLK_NBR 0x10000
74 #define STORAGE_BLK_SIZ 0x200
76 static int8_t STORAGE_Init (uint8_t lun);
78 #ifdef USE_HAL_DRIVER
79 static int8_t STORAGE_GetCapacity (uint8_t lun,
80 uint32_t *block_num,
81 uint16_t *block_size);
82 #else
83 static int8_t STORAGE_GetCapacity (uint8_t lun,
84 uint32_t *block_num,
85 uint32_t *block_size);
86 #endif
88 static int8_t STORAGE_IsReady (uint8_t lun);
90 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
92 static int8_t STORAGE_Read (uint8_t lun,
93 uint8_t *buf,
94 uint32_t blk_addr,
95 uint16_t blk_len);
97 static int8_t STORAGE_Write (uint8_t lun,
98 uint8_t *buf,
99 uint32_t blk_addr,
100 uint16_t blk_len);
102 static int8_t STORAGE_GetMaxLun (void);
104 /* USB Mass storage Standard Inquiry Data */
105 static uint8_t STORAGE_Inquirydata[] = {//36
107 /* LUN 0 */
108 0x00,
109 0x80,
110 0x02,
111 0x02,
112 #ifdef USE_HAL_DRIVER
113 (STANDARD_INQUIRY_DATA_LEN - 5),
114 #else
115 (USBD_STD_INQUIRY_LENGTH - 5),
116 #endif
117 0x00,
118 0x00,
119 0x00,
120 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
121 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
122 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
123 '0', '.', '0' ,'1', /* Version : 4 Bytes */
126 #ifdef USE_HAL_DRIVER
127 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops =
129 STORAGE_Init,
130 STORAGE_GetCapacity,
131 STORAGE_IsReady,
132 STORAGE_IsWriteProtected,
133 STORAGE_Read,
134 STORAGE_Write,
135 STORAGE_GetMaxLun,
136 (int8_t*)STORAGE_Inquirydata,
138 #else
139 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops =
141 STORAGE_Init,
142 STORAGE_GetCapacity,
143 STORAGE_IsReady,
144 STORAGE_IsWriteProtected,
145 STORAGE_Read,
146 STORAGE_Write,
147 STORAGE_GetMaxLun,
148 (int8_t*)STORAGE_Inquirydata,
150 #endif
152 /*******************************************************************************
153 * Function Name : Read_Memory
154 * Description : Handle the Read operation from the microSD card.
155 * Input : None.
156 * Output : None.
157 * Return : None.
158 *******************************************************************************/
159 static int8_t STORAGE_Init (uint8_t lun)
161 //Initialize SD_DET
162 const IO_t sd_det = IOGetByTag(sdcardConfig()->cardDetectTag);
163 IOInit(sd_det, OWNER_SDCARD_DETECT, 0);
164 IOConfigGPIO(sd_det, IOCFG_IPU);
166 UNUSED(lun);
167 LED0_OFF;
169 #ifdef USE_DMA_SPEC
170 const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO, 0, sdioConfig()->dmaopt);
172 if (!dmaChannelSpec || !SD_Initialize_LL((DMA_ARCH_TYPE *)dmaChannelSpec->ref)) {
173 #else
174 #if defined(STM32H7) // H7 uses IDMA
175 if (!SD_Initialize_LL(0)) {
176 #else
177 if (!SD_Initialize_LL(SDCARD_SDIO_DMA_OPT)) {
178 #endif
179 #endif // USE_DMA_SPEC
180 return 1;
183 if (!sdcard_isInserted()) {
184 return 1;
186 if (SD_Init() != SD_OK) {
187 return 1;
190 mscSetActive();
192 return 0;
195 /*******************************************************************************
196 * Function Name : Read_Memory
197 * Description : Handle the Read operation from the STORAGE card.
198 * Input : None.
199 * Output : None.
200 * Return : None.
201 *******************************************************************************/
202 #ifdef USE_HAL_DRIVER
203 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
204 #else
205 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
206 #endif
208 UNUSED(lun);
209 if (!sdcard_isInserted()) {
210 return -1;
212 SD_GetCardInfo();
214 *block_num = SD_CardInfo.CardCapacity;
215 *block_size = 512;
216 return (0);
219 /*******************************************************************************
220 * Function Name : Read_Memory
221 * Description : Handle the Read operation from the STORAGE card.
222 * Input : None.
223 * Output : None.
224 * Return : None.
225 *******************************************************************************/
226 static int8_t STORAGE_IsReady (uint8_t lun)
228 UNUSED(lun);
229 int8_t ret = -1;
230 if (SD_GetState() == true && sdcard_isInserted()) {
231 ret = 0;
233 return ret;
236 /*******************************************************************************
237 * Function Name : Read_Memory
238 * Description : Handle the Read operation from the STORAGE card.
239 * Input : None.
240 * Output : None.
241 * Return : None.
242 *******************************************************************************/
243 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
245 UNUSED(lun);
246 return 0;
249 /*******************************************************************************
250 * Function Name : Read_Memory
251 * Description : Handle the Read operation from the STORAGE card.
252 * Input : None.
253 * Output : None.
254 * Return : None.
255 *******************************************************************************/
256 static int8_t STORAGE_Read (uint8_t lun,
257 uint8_t *buf,
258 uint32_t blk_addr,
259 uint16_t blk_len)
261 UNUSED(lun);
262 if (!sdcard_isInserted()) {
263 return -1;
265 //buf should be 32bit aligned, but usually is so we don't do byte alignment
266 if (SD_ReadBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
267 while (SD_CheckRead());
268 while(SD_GetState() == false);
269 mscSetActive();
270 return 0;
272 return -1;
274 /*******************************************************************************
275 * Function Name : Write_Memory
276 * Description : Handle the Write operation to the STORAGE card.
277 * Input : None.
278 * Output : None.
279 * Return : None.
280 *******************************************************************************/
281 static int8_t STORAGE_Write (uint8_t lun,
282 uint8_t *buf,
283 uint32_t blk_addr,
284 uint16_t blk_len)
286 UNUSED(lun);
287 if (!sdcard_isInserted()) {
288 return -1;
290 //buf should be 32bit aligned, but usually is so we don't do byte alignment
291 if (SD_WriteBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
292 while (SD_CheckWrite());
293 while(SD_GetState() == false);
294 mscSetActive();
295 return 0;
297 return -1;
299 /*******************************************************************************
300 * Function Name : Write_Memory
301 * Description : Handle the Write operation to the STORAGE card.
302 * Input : None.
303 * Output : None.
304 * Return : None.
305 *******************************************************************************/
306 static int8_t STORAGE_GetMaxLun (void)
308 return (STORAGE_LUN_NBR - 1);
311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
313 #endif // USE_SDCARD