STM32H7 - SDMMC Fix the short busy loop being compiled out by newer
[betaflight.git] / src / main / msc / usbd_storage_sdio.c
blob8fd371b83efc3eb68302bdbd4e4e8d4c60c2ecc1
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 #if defined(STM32H7) // H7 uses IDMA
171 SD_Initialize_LL(0);
172 #else
173 const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO, 0, sdioConfig()->dmaopt);
175 if (!dmaChannelSpec) {
176 return 1;
179 SD_Initialize_LL((DMA_ARCH_TYPE *)dmaChannelSpec->ref);
180 #endif
181 #else
182 SD_Initialize_LL(SDCARD_SDIO_DMA_OPT);
183 #endif
185 if (!sdcard_isInserted()) {
186 return 1;
188 if (SD_Init() != SD_OK) {
189 return 1;
192 mscSetActive();
194 return 0;
197 /*******************************************************************************
198 * Function Name : Read_Memory
199 * Description : Handle the Read operation from the STORAGE card.
200 * Input : None.
201 * Output : None.
202 * Return : None.
203 *******************************************************************************/
204 #ifdef USE_HAL_DRIVER
205 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
206 #else
207 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
208 #endif
210 UNUSED(lun);
211 if (!sdcard_isInserted()) {
212 return -1;
214 SD_GetCardInfo();
216 *block_num = SD_CardInfo.CardCapacity;
217 *block_size = 512;
218 return (0);
221 /*******************************************************************************
222 * Function Name : Read_Memory
223 * Description : Handle the Read operation from the STORAGE card.
224 * Input : None.
225 * Output : None.
226 * Return : None.
227 *******************************************************************************/
228 static int8_t STORAGE_IsReady (uint8_t lun)
230 UNUSED(lun);
231 int8_t ret = -1;
232 if (SD_GetState() == true && sdcard_isInserted()) {
233 ret = 0;
235 return ret;
238 /*******************************************************************************
239 * Function Name : Read_Memory
240 * Description : Handle the Read operation from the STORAGE card.
241 * Input : None.
242 * Output : None.
243 * Return : None.
244 *******************************************************************************/
245 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
247 UNUSED(lun);
248 return 0;
251 /*******************************************************************************
252 * Function Name : Read_Memory
253 * Description : Handle the Read operation from the STORAGE card.
254 * Input : None.
255 * Output : None.
256 * Return : None.
257 *******************************************************************************/
258 static int8_t STORAGE_Read (uint8_t lun,
259 uint8_t *buf,
260 uint32_t blk_addr,
261 uint16_t blk_len)
263 UNUSED(lun);
264 if (!sdcard_isInserted()) {
265 return -1;
267 //buf should be 32bit aligned, but usually is so we don't do byte alignment
268 if (SD_ReadBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
269 while (SD_CheckRead());
270 while(SD_GetState() == false);
271 mscSetActive();
272 return 0;
274 return -1;
276 /*******************************************************************************
277 * Function Name : Write_Memory
278 * Description : Handle the Write operation to the STORAGE card.
279 * Input : None.
280 * Output : None.
281 * Return : None.
282 *******************************************************************************/
283 static int8_t STORAGE_Write (uint8_t lun,
284 uint8_t *buf,
285 uint32_t blk_addr,
286 uint16_t blk_len)
288 UNUSED(lun);
289 if (!sdcard_isInserted()) {
290 return -1;
292 //buf should be 32bit aligned, but usually is so we don't do byte alignment
293 if (SD_WriteBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
294 while (SD_CheckWrite());
295 while(SD_GetState() == false);
296 mscSetActive();
297 return 0;
299 return -1;
301 /*******************************************************************************
302 * Function Name : Write_Memory
303 * Description : Handle the Write operation to the STORAGE card.
304 * Input : None.
305 * Output : None.
306 * Return : None.
307 *******************************************************************************/
308 static int8_t STORAGE_GetMaxLun (void)
310 return (STORAGE_LUN_NBR - 1);
313 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
315 #endif // USE_SDCARD