duplicate emptyline removal (#14027)
[betaflight.git] / src / main / msc / usbd_storage_sdio.c
blob17c2f8f43b70129a8d42eeefd71b142660f2a80c
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 ******************************************************************************
28 /* Includes ------------------------------------------------------------------*/
29 #include <stdio.h>
30 #include <stdbool.h>
32 #include "platform.h"
34 #ifdef USE_SDCARD
36 #include "common/utils.h"
38 #include "drivers/dma.h"
39 #include "drivers/dma_reqmap.h"
40 #include "drivers/io.h"
41 #include "drivers/light_led.h"
42 #include "drivers/sdcard.h"
43 #include "drivers/sdmmc_sdio.h"
44 #include "drivers/usb_msc.h"
46 #include "pg/pg.h"
47 #include "pg/sdcard.h"
48 #include "pg/sdio.h"
50 #ifdef USE_HAL_DRIVER
51 #include "usbd_msc.h"
52 #else
53 #include "usbd_msc_mem.h"
54 #include "usbd_msc_core.h"
55 #endif
57 #include "usbd_storage.h"
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Extern function prototypes ------------------------------------------------*/
65 /* Private functions ---------------------------------------------------------*/
67 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
68 * otherwise SDIO won't be able to preempt USB.
71 #define STORAGE_LUN_NBR 1
72 #define STORAGE_BLK_NBR 0x10000
73 #define STORAGE_BLK_SIZ 0x200
75 static int8_t STORAGE_Init (uint8_t lun);
77 #ifdef USE_HAL_DRIVER
78 static int8_t STORAGE_GetCapacity (uint8_t lun,
79 uint32_t *block_num,
80 uint16_t *block_size);
81 #else
82 static int8_t STORAGE_GetCapacity (uint8_t lun,
83 uint32_t *block_num,
84 uint32_t *block_size);
85 #endif
87 static int8_t STORAGE_IsReady (uint8_t lun);
89 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
91 static int8_t STORAGE_Read (uint8_t lun,
92 uint8_t *buf,
93 uint32_t blk_addr,
94 uint16_t blk_len);
96 static int8_t STORAGE_Write (uint8_t lun,
97 uint8_t *buf,
98 uint32_t blk_addr,
99 uint16_t blk_len);
101 static int8_t STORAGE_GetMaxLun (void);
103 /* USB Mass storage Standard Inquiry Data */
104 static uint8_t STORAGE_Inquirydata[] = {//36
106 /* LUN 0 */
107 0x00,
108 0x80,
109 0x02,
110 0x02,
111 #ifdef USE_HAL_DRIVER
112 (STANDARD_INQUIRY_DATA_LEN - 5),
113 #else
114 (USBD_STD_INQUIRY_LENGTH - 5),
115 #endif
116 0x00,
117 0x00,
118 0x00,
119 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
120 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
121 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
122 '0', '.', '0' ,'1', /* Version : 4 Bytes */
125 #ifdef USE_HAL_DRIVER
126 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops =
128 STORAGE_Init,
129 STORAGE_GetCapacity,
130 STORAGE_IsReady,
131 STORAGE_IsWriteProtected,
132 STORAGE_Read,
133 STORAGE_Write,
134 STORAGE_GetMaxLun,
135 (int8_t*)STORAGE_Inquirydata,
137 #else
138 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops =
140 STORAGE_Init,
141 STORAGE_GetCapacity,
142 STORAGE_IsReady,
143 STORAGE_IsWriteProtected,
144 STORAGE_Read,
145 STORAGE_Write,
146 STORAGE_GetMaxLun,
147 (int8_t*)STORAGE_Inquirydata,
149 #endif
151 /*******************************************************************************
152 * Function Name : Read_Memory
153 * Description : Handle the Read operation from the microSD card.
154 * Input : None.
155 * Output : None.
156 * Return : None.
157 *******************************************************************************/
158 static int8_t STORAGE_Init (uint8_t lun)
160 //Initialize SD_DET
161 const IO_t sd_det = IOGetByTag(sdcardConfig()->cardDetectTag);
162 IOInit(sd_det, OWNER_SDCARD_DETECT, 0);
163 IOConfigGPIO(sd_det, IOCFG_IPU);
165 UNUSED(lun);
166 LED0_OFF;
168 #ifdef USE_DMA_SPEC
169 const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO, 0, sdioConfig()->dmaopt);
171 if (!dmaChannelSpec || !SD_Initialize_LL((DMA_ARCH_TYPE *)dmaChannelSpec->ref)) {
172 #else
173 #if defined(STM32H7) // H7 uses IDMA
174 if (!SD_Initialize_LL(0)) {
175 #else
176 if (!SD_Initialize_LL(SDCARD_SDIO_DMA_OPT)) {
177 #endif
178 #endif // USE_DMA_SPEC
179 return 1;
182 if (!sdcard_isInserted()) {
183 return 1;
185 if (SD_Init() != SD_OK) {
186 return 1;
189 mscSetActive();
191 return 0;
194 /*******************************************************************************
195 * Function Name : Read_Memory
196 * Description : Handle the Read operation from the STORAGE card.
197 * Input : None.
198 * Output : None.
199 * Return : None.
200 *******************************************************************************/
201 #ifdef USE_HAL_DRIVER
202 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
203 #else
204 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
205 #endif
207 UNUSED(lun);
208 if (!sdcard_isInserted()) {
209 return -1;
211 SD_GetCardInfo();
213 *block_num = SD_CardInfo.CardCapacity;
214 *block_size = 512;
215 return (0);
218 /*******************************************************************************
219 * Function Name : Read_Memory
220 * Description : Handle the Read operation from the STORAGE card.
221 * Input : None.
222 * Output : None.
223 * Return : None.
224 *******************************************************************************/
225 static int8_t STORAGE_IsReady (uint8_t lun)
227 UNUSED(lun);
228 int8_t ret = -1;
229 if (SD_GetState() == true && sdcard_isInserted()) {
230 ret = 0;
232 return ret;
235 /*******************************************************************************
236 * Function Name : Read_Memory
237 * Description : Handle the Read operation from the STORAGE card.
238 * Input : None.
239 * Output : None.
240 * Return : None.
241 *******************************************************************************/
242 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
244 UNUSED(lun);
245 return 0;
248 /*******************************************************************************
249 * Function Name : Read_Memory
250 * Description : Handle the Read operation from the STORAGE card.
251 * Input : None.
252 * Output : None.
253 * Return : None.
254 *******************************************************************************/
255 static int8_t STORAGE_Read (uint8_t lun,
256 uint8_t *buf,
257 uint32_t blk_addr,
258 uint16_t blk_len)
260 UNUSED(lun);
261 if (!sdcard_isInserted()) {
262 return -1;
264 //buf should be 32bit aligned, but usually is so we don't do byte alignment
265 if (SD_ReadBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
266 while (SD_CheckRead());
267 while(SD_GetState() == false);
268 mscSetActive();
269 return 0;
271 return -1;
273 /*******************************************************************************
274 * Function Name : Write_Memory
275 * Description : Handle the Write operation to the STORAGE card.
276 * Input : None.
277 * Output : None.
278 * Return : None.
279 *******************************************************************************/
280 static int8_t STORAGE_Write (uint8_t lun,
281 uint8_t *buf,
282 uint32_t blk_addr,
283 uint16_t blk_len)
285 UNUSED(lun);
286 if (!sdcard_isInserted()) {
287 return -1;
289 //buf should be 32bit aligned, but usually is so we don't do byte alignment
290 if (SD_WriteBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
291 while (SD_CheckWrite());
292 while(SD_GetState() == false);
293 mscSetActive();
294 return 0;
296 return -1;
298 /*******************************************************************************
299 * Function Name : Write_Memory
300 * Description : Handle the Write operation to the STORAGE card.
301 * Input : None.
302 * Output : None.
303 * Return : None.
304 *******************************************************************************/
305 static int8_t STORAGE_GetMaxLun (void)
307 return (STORAGE_LUN_NBR - 1);
310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
312 #endif // USE_SDCARD