Add RANGEFINDER and OPTICALFLOW MT build option (#14042)
[betaflight.git] / src / main / msc / usbd_storage_sd_spi.c
blob195c6c51f5a9e1a0d70592b8853a0c862aaadeda
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"
33 #include "common/utils.h"
35 #include "usbd_storage.h"
37 #include "drivers/bus_spi.h"
38 #include "drivers/io.h"
39 #include "drivers/light_led.h"
40 #include "drivers/sdcard.h"
41 #include "drivers/usb_msc.h"
43 #include "pg/sdcard.h"
44 #include "pg/bus_spi.h"
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Extern function prototypes ------------------------------------------------*/
52 /* Private functions ---------------------------------------------------------*/
54 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
55 * otherwise SDIO won't be able to preempt USB.
58 #define STORAGE_LUN_NBR 1
59 #define STORAGE_BLK_NBR 0x10000
60 #define STORAGE_BLK_SIZ 0x200
62 static int8_t STORAGE_Init (uint8_t lun);
64 #ifdef USE_HAL_DRIVER
65 static int8_t STORAGE_GetCapacity (uint8_t lun,
66 uint32_t *block_num,
67 uint16_t *block_size);
68 #else
69 static int8_t STORAGE_GetCapacity (uint8_t lun,
70 uint32_t *block_num,
71 uint32_t *block_size);
72 #endif
74 static int8_t STORAGE_IsReady (uint8_t lun);
76 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
78 static int8_t STORAGE_Read (uint8_t lun,
79 uint8_t *buf,
80 uint32_t blk_addr,
81 uint16_t blk_len);
83 static int8_t STORAGE_Write (uint8_t lun,
84 uint8_t *buf,
85 uint32_t blk_addr,
86 uint16_t blk_len);
88 static int8_t STORAGE_GetMaxLun (void);
90 /* USB Mass storage Standard Inquiry Data */
91 static uint8_t STORAGE_Inquirydata[] = {//36
93 /* LUN 0 */
94 0x00,
95 0x80,
96 0x02,
97 0x02,
98 #ifdef USE_HAL_DRIVER
99 (STANDARD_INQUIRY_DATA_LEN - 5),
100 #else
101 (USBD_STD_INQUIRY_LENGTH - 5),
102 #endif
103 0x00,
104 0x00,
105 0x00,
106 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
107 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
108 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
109 '0', '.', '0' ,'1', /* Version : 4 Bytes */
112 #ifdef USE_HAL_DRIVER
113 USBD_StorageTypeDef USBD_MSC_MICRO_SD_SPI_fops =
115 STORAGE_Init,
116 STORAGE_GetCapacity,
117 STORAGE_IsReady,
118 STORAGE_IsWriteProtected,
119 STORAGE_Read,
120 STORAGE_Write,
121 STORAGE_GetMaxLun,
122 (int8_t*)STORAGE_Inquirydata,
124 #else
125 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SD_SPI_fops =
127 STORAGE_Init,
128 STORAGE_GetCapacity,
129 STORAGE_IsReady,
130 STORAGE_IsWriteProtected,
131 STORAGE_Read,
132 STORAGE_Write,
133 STORAGE_GetMaxLun,
134 (int8_t*)STORAGE_Inquirydata,
136 #endif
138 /*******************************************************************************
139 * Function Name : Read_Memory
140 * Description : Handle the Read operation from the microSD card.
141 * Input : None.
142 * Output : None.
143 * Return : None.
144 *******************************************************************************/
145 static int8_t STORAGE_Init (uint8_t lun)
147 UNUSED(lun);
148 LED0_OFF;
149 sdcard_init(sdcardConfig());
150 while (sdcard_poll() == 0);
151 mscSetActive();
152 return 0;
155 /*******************************************************************************
156 * Function Name : Read_Memory
157 * Description : Handle the Read operation from the STORAGE card.
158 * Input : None.
159 * Output : None.
160 * Return : None.
161 *******************************************************************************/
162 #ifdef USE_HAL_DRIVER
163 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
164 #else
165 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
166 #endif
168 UNUSED(lun);
169 *block_num = sdcard_getMetadata()->numBlocks;
170 *block_size = 512;
171 return (0);
174 /*******************************************************************************
175 * Function Name : Read_Memory
176 * Description : Handle the Read operation from the STORAGE card.
177 * Input : None.
178 * Output : None.
179 * Return : None.
180 *******************************************************************************/
181 static int8_t STORAGE_IsReady (uint8_t lun)
183 UNUSED(lun);
184 int8_t ret = -1;
185 if (sdcard_poll()) {
186 ret = 0;
188 return ret;
191 /*******************************************************************************
192 * Function Name : Read_Memory
193 * Description : Handle the Read operation from the STORAGE card.
194 * Input : None.
195 * Output : None.
196 * Return : None.
197 *******************************************************************************/
198 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
200 UNUSED(lun);
201 return 0;
204 /*******************************************************************************
205 * Function Name : Read_Memory
206 * Description : Handle the Read operation from the STORAGE card.
207 * Input : None.
208 * Output : None.
209 * Return : None.
210 *******************************************************************************/
211 static int8_t STORAGE_Read (uint8_t lun,
212 uint8_t *buf,
213 uint32_t blk_addr,
214 uint16_t blk_len)
216 UNUSED(lun);
217 for (int i = 0; i < blk_len; i++) {
218 while (sdcard_readBlock(blk_addr + i, buf + (512 * i), NULL, 0) == 0);
219 while (sdcard_poll() == 0);
221 mscSetActive();
222 return 0;
224 /*******************************************************************************
225 * Function Name : Write_Memory
226 * Description : Handle the Write operation to the STORAGE card.
227 * Input : None.
228 * Output : None.
229 * Return : None.
230 *******************************************************************************/
231 static int8_t STORAGE_Write (uint8_t lun,
232 uint8_t *buf,
233 uint32_t blk_addr,
234 uint16_t blk_len)
236 UNUSED(lun);
237 for (int i = 0; i < blk_len; i++) {
238 while (sdcard_writeBlock(blk_addr + i, buf + (i * 512), NULL, 0) != SDCARD_OPERATION_IN_PROGRESS) {
239 sdcard_poll();
241 while (sdcard_poll() == 0);
243 mscSetActive();
244 return 0;
246 /*******************************************************************************
247 * Function Name : Write_Memory
248 * Description : Handle the Write operation to the STORAGE card.
249 * Input : None.
250 * Output : None.
251 * Return : None.
252 *******************************************************************************/
253 static int8_t STORAGE_GetMaxLun (void)
255 return (STORAGE_LUN_NBR - 1);
258 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/