Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / msc / usbd_storage_sd_spi.c
blob03f53eed72a58aae352bfa0c920c41f8cf8cd367
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"
34 #include "common/utils.h"
36 #include "usbd_storage.h"
38 #include "drivers/sdcard/sdcard.h"
39 #include "drivers/light_led.h"
40 #include "drivers/io.h"
41 #include "drivers/bus_spi.h"
43 /* Private typedef -----------------------------------------------------------*/
44 /* Private define ------------------------------------------------------------*/
45 /* Private macro -------------------------------------------------------------*/
46 /* Private variables ---------------------------------------------------------*/
47 /* Private function prototypes -----------------------------------------------*/
48 /* Extern function prototypes ------------------------------------------------*/
49 /* Private functions ---------------------------------------------------------*/
51 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
52 * otherwise SDIO won't be able to preempt USB.
55 #define STORAGE_LUN_NBR 1
56 #define STORAGE_BLK_NBR 0x10000
57 #define STORAGE_BLK_SIZ 0x200
59 static int8_t STORAGE_Init (uint8_t lun);
61 #ifdef USE_HAL_DRIVER
62 static int8_t STORAGE_GetCapacity (uint8_t lun,
63 uint32_t *block_num,
64 uint16_t *block_size);
65 #else
66 static int8_t STORAGE_GetCapacity (uint8_t lun,
67 uint32_t *block_num,
68 uint32_t *block_size);
69 #endif
71 static int8_t STORAGE_IsReady (uint8_t lun);
73 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
75 static int8_t STORAGE_Read (uint8_t lun,
76 uint8_t *buf,
77 uint32_t blk_addr,
78 uint16_t blk_len);
80 static int8_t STORAGE_Write (uint8_t lun,
81 uint8_t *buf,
82 uint32_t blk_addr,
83 uint16_t blk_len);
85 static int8_t STORAGE_GetMaxLun (void);
87 /* USB Mass storage Standard Inquiry Data */
88 static uint8_t STORAGE_Inquirydata[] = {//36
90 /* LUN 0 */
91 0x00,
92 0x80,
93 0x02,
94 0x02,
95 #ifdef USE_HAL_DRIVER
96 (STANDARD_INQUIRY_DATA_LEN - 5),
97 #else
98 (USBD_STD_INQUIRY_LENGTH - 5),
99 #endif
100 0x00,
101 0x00,
102 0x00,
103 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
104 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
105 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
106 '0', '.', '0' ,'1', /* Version : 4 Bytes */
109 #ifdef USE_HAL_DRIVER
110 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops =
112 STORAGE_Init,
113 STORAGE_GetCapacity,
114 STORAGE_IsReady,
115 STORAGE_IsWriteProtected,
116 STORAGE_Read,
117 STORAGE_Write,
118 STORAGE_GetMaxLun,
119 (int8_t*)STORAGE_Inquirydata,
121 #else
122 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops =
124 STORAGE_Init,
125 STORAGE_GetCapacity,
126 STORAGE_IsReady,
127 STORAGE_IsWriteProtected,
128 STORAGE_Read,
129 STORAGE_Write,
130 STORAGE_GetMaxLun,
131 (int8_t*)STORAGE_Inquirydata,
133 #endif
135 /*******************************************************************************
136 * Function Name : Read_Memory
137 * Description : Handle the Read operation from the microSD card.
138 * Input : None.
139 * Output : None.
140 * Return : None.
141 *******************************************************************************/
142 static int8_t STORAGE_Init (uint8_t lun)
144 UNUSED(lun);
145 LED0_OFF;
146 sdcard_init();
147 while (sdcard_poll() == 0);
148 LED0_ON;
149 return 0;
152 /*******************************************************************************
153 * Function Name : Read_Memory
154 * Description : Handle the Read operation from the STORAGE card.
155 * Input : None.
156 * Output : None.
157 * Return : None.
158 *******************************************************************************/
159 #ifdef USE_HAL_DRIVER
160 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
161 #else
162 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
163 #endif
165 UNUSED(lun);
166 *block_num = sdcard_getMetadata()->numBlocks;
167 *block_size = 512;
168 return (0);
171 /*******************************************************************************
172 * Function Name : Read_Memory
173 * Description : Handle the Read operation from the STORAGE card.
174 * Input : None.
175 * Output : None.
176 * Return : None.
177 *******************************************************************************/
178 static int8_t STORAGE_IsReady (uint8_t lun)
180 UNUSED(lun);
181 int8_t ret = -1;
182 if (sdcard_poll()) {
183 ret = 0;
185 return ret;
188 /*******************************************************************************
189 * Function Name : Read_Memory
190 * Description : Handle the Read operation from the STORAGE card.
191 * Input : None.
192 * Output : None.
193 * Return : None.
194 *******************************************************************************/
195 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
197 UNUSED(lun);
198 return 0;
201 /*******************************************************************************
202 * Function Name : Read_Memory
203 * Description : Handle the Read operation from the STORAGE card.
204 * Input : None.
205 * Output : None.
206 * Return : None.
207 *******************************************************************************/
208 static int8_t STORAGE_Read (uint8_t lun,
209 uint8_t *buf,
210 uint32_t blk_addr,
211 uint16_t blk_len)
213 UNUSED(lun);
214 LED1_ON;
215 for (int i = 0; i < blk_len; i++) {
216 while (sdcard_readBlock(blk_addr + i, buf + (512 * i), NULL, 0) == 0);
217 while (sdcard_poll() == 0);
219 LED1_OFF;
220 return 0;
222 /*******************************************************************************
223 * Function Name : Write_Memory
224 * Description : Handle the Write operation to the STORAGE card.
225 * Input : None.
226 * Output : None.
227 * Return : None.
228 *******************************************************************************/
229 static int8_t STORAGE_Write (uint8_t lun,
230 uint8_t *buf,
231 uint32_t blk_addr,
232 uint16_t blk_len)
234 UNUSED(lun);
235 LED1_ON;
236 for (int i = 0; i < blk_len; i++) {
237 while (sdcard_writeBlock(blk_addr + i, buf + (i * 512), NULL, 0) != SDCARD_OPERATION_IN_PROGRESS) {
238 sdcard_poll();
240 while (sdcard_poll() == 0);
242 LED1_OFF;
243 return 0;
245 /*******************************************************************************
246 * Function Name : Write_Memory
247 * Description : Handle the Write operation to the STORAGE card.
248 * Input : None.
249 * Output : None.
250 * Return : None.
251 *******************************************************************************/
252 static int8_t STORAGE_GetMaxLun (void)
254 return (STORAGE_LUN_NBR - 1);
257 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/