Merge pull request #10479 from iNavFlight/mmosca-fix-arming-flags-display
[inav.git] / src / main / msc / usbd_storage_sdio.c
blob4a262419abd9714f5767b52d08359a8806a155e4
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 #include "drivers/sdmmc_sdio.h"
36 #include "drivers/light_led.h"
37 #include "drivers/io.h"
38 #include "common/utils.h"
40 #ifdef USE_HAL_DRIVER
41 #include "usbd_msc.h"
42 #else
43 #include "usbd_msc_mem.h"
44 #include "usbd_msc_core.h"
45 #endif
47 #include "usbd_storage.h"
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /* Private macro -------------------------------------------------------------*/
52 /* Private variables ---------------------------------------------------------*/
53 /* Private function prototypes -----------------------------------------------*/
54 /* Extern function prototypes ------------------------------------------------*/
55 /* Private functions ---------------------------------------------------------*/
57 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
58 * otherwise SDIO won't be able to preempt USB.
61 #define STORAGE_LUN_NBR 1
62 #define STORAGE_BLK_NBR 0x10000
63 #define STORAGE_BLK_SIZ 0x200
65 static int8_t STORAGE_Init (uint8_t lun);
67 #ifdef USE_HAL_DRIVER
68 static int8_t STORAGE_GetCapacity (uint8_t lun,
69 uint32_t *block_num,
70 uint16_t *block_size);
71 #else
72 static int8_t STORAGE_GetCapacity (uint8_t lun,
73 uint32_t *block_num,
74 uint32_t *block_size);
75 #endif
77 static int8_t STORAGE_IsReady (uint8_t lun);
79 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
81 static int8_t STORAGE_Read (uint8_t lun,
82 uint8_t *buf,
83 uint32_t blk_addr,
84 uint16_t blk_len);
86 static int8_t STORAGE_Write (uint8_t lun,
87 uint8_t *buf,
88 uint32_t blk_addr,
89 uint16_t blk_len);
91 static int8_t STORAGE_GetMaxLun (void);
93 /* USB Mass storage Standard Inquiry Data */
94 static uint8_t STORAGE_Inquirydata[] = {//36
96 /* LUN 0 */
97 0x00,
98 0x80,
99 0x02,
100 0x02,
101 #ifdef USE_HAL_DRIVER
102 (STANDARD_INQUIRY_DATA_LEN - 5),
103 #else
104 (USBD_STD_INQUIRY_LENGTH - 5),
105 #endif
106 0x00,
107 0x00,
108 0x00,
109 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
110 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
111 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
112 '0', '.', '0' ,'1', /* Version : 4 Bytes */
115 #ifdef USE_HAL_DRIVER
116 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops =
118 STORAGE_Init,
119 STORAGE_GetCapacity,
120 STORAGE_IsReady,
121 STORAGE_IsWriteProtected,
122 STORAGE_Read,
123 STORAGE_Write,
124 STORAGE_GetMaxLun,
125 (int8_t*)STORAGE_Inquirydata,
127 #else
128 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops =
130 STORAGE_Init,
131 STORAGE_GetCapacity,
132 STORAGE_IsReady,
133 STORAGE_IsWriteProtected,
134 STORAGE_Read,
135 STORAGE_Write,
136 STORAGE_GetMaxLun,
137 (int8_t*)STORAGE_Inquirydata,
139 #endif
141 /*******************************************************************************
142 * Function Name : Read_Memory
143 * Description : Handle the Read operation from the microSD card.
144 * Input : None.
145 * Output : None.
146 * Return : None.
147 *******************************************************************************/
148 static int8_t STORAGE_Init (uint8_t lun)
150 //Initialize SD_DET
151 #ifdef SDCARD_DETECT_PIN
152 const IO_t sd_det = IOGetByTag(IO_TAG(SDCARD_DETECT_PIN));
153 IOInit(sd_det, OWNER_SDCARD_DETECT, 0);
154 IOConfigGPIO(sd_det, IOCFG_IPU);
155 #endif
157 UNUSED(lun);
158 LED0_OFF;
159 #if defined(STM32H7) // H7 uses IDMA
160 SD_Initialize_LL(0);
161 #else
162 SD_Initialize_LL(SDIO_DMA);
163 #endif
164 if (SD_Init() != 0) return 1;
165 LED0_ON;
166 return 0;
169 /*******************************************************************************
170 * Function Name : Read_Memory
171 * Description : Handle the Read operation from the STORAGE card.
172 * Input : None.
173 * Output : None.
174 * Return : None.
175 *******************************************************************************/
176 #ifdef USE_HAL_DRIVER
177 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
178 #else
179 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
180 #endif
182 UNUSED(lun);
183 if (SD_IsDetected() == 0) {
184 return -1;
186 SD_GetCardInfo();
188 *block_num = SD_CardInfo.CardCapacity;
189 *block_size = 512;
190 return (0);
193 /*******************************************************************************
194 * Function Name : Read_Memory
195 * Description : Handle the Read operation from the STORAGE card.
196 * Input : None.
197 * Output : None.
198 * Return : None.
199 *******************************************************************************/
200 static int8_t STORAGE_IsReady (uint8_t lun)
202 UNUSED(lun);
203 int8_t ret = -1;
204 if (SD_GetState() == true && SD_IsDetected() == SD_PRESENT) {
205 ret = 0;
207 return ret;
210 /*******************************************************************************
211 * Function Name : Read_Memory
212 * Description : Handle the Read operation from the STORAGE card.
213 * Input : None.
214 * Output : None.
215 * Return : None.
216 *******************************************************************************/
217 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
219 UNUSED(lun);
220 return 0;
223 /*******************************************************************************
224 * Function Name : Read_Memory
225 * Description : Handle the Read operation from the STORAGE card.
226 * Input : None.
227 * Output : None.
228 * Return : None.
229 *******************************************************************************/
230 static int8_t STORAGE_Read (uint8_t lun,
231 uint8_t *buf,
232 uint32_t blk_addr,
233 uint16_t blk_len)
235 UNUSED(lun);
236 if (SD_IsDetected() == 0) {
237 return -1;
239 LED1_ON;
240 //buf should be 32bit aligned, but usually is so we don't do byte alignment
241 if (SD_ReadBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
242 while (SD_CheckRead());
243 while(SD_GetState() == false);
244 LED1_OFF;
245 return 0;
247 LED1_OFF;
248 return -1;
250 /*******************************************************************************
251 * Function Name : Write_Memory
252 * Description : Handle the Write operation to the STORAGE card.
253 * Input : None.
254 * Output : None.
255 * Return : None.
256 *******************************************************************************/
257 static int8_t STORAGE_Write (uint8_t lun,
258 uint8_t *buf,
259 uint32_t blk_addr,
260 uint16_t blk_len)
262 UNUSED(lun);
263 if (SD_IsDetected() == 0) {
264 return -1;
266 LED1_ON;
267 //buf should be 32bit aligned, but usually is so we don't do byte alignment
268 if (SD_WriteBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
269 while (SD_CheckWrite());
270 while(SD_GetState() == false);
271 LED1_OFF;
272 return 0;
274 LED1_OFF;
275 return -1;
277 /*******************************************************************************
278 * Function Name : Write_Memory
279 * Description : Handle the Write operation to the STORAGE card.
280 * Input : None.
281 * Output : None.
282 * Return : None.
283 *******************************************************************************/
284 static int8_t STORAGE_GetMaxLun (void)
286 return (STORAGE_LUN_NBR - 1);
289 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/