[4.4.2] Remove 15 m/s limit on estimated vario (#12788)
[betaflight.git] / src / main / msc / usbd_storage_sd_spi.c
blob694226e392e22ddfcffd48d231938fef2016c939
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/bus_spi.h"
39 #include "drivers/io.h"
40 #include "drivers/light_led.h"
41 #include "drivers/sdcard.h"
42 #include "drivers/usb_msc.h"
44 #include "pg/sdcard.h"
45 #include "pg/bus_spi.h"
47 /* Private typedef -----------------------------------------------------------*/
48 /* Private define ------------------------------------------------------------*/
49 /* Private macro -------------------------------------------------------------*/
50 /* Private variables ---------------------------------------------------------*/
51 /* Private function prototypes -----------------------------------------------*/
52 /* Extern function prototypes ------------------------------------------------*/
53 /* Private functions ---------------------------------------------------------*/
55 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
56 * otherwise SDIO won't be able to preempt USB.
59 #define STORAGE_LUN_NBR 1
60 #define STORAGE_BLK_NBR 0x10000
61 #define STORAGE_BLK_SIZ 0x200
63 static int8_t STORAGE_Init (uint8_t lun);
65 #ifdef USE_HAL_DRIVER
66 static int8_t STORAGE_GetCapacity (uint8_t lun,
67 uint32_t *block_num,
68 uint16_t *block_size);
69 #else
70 static int8_t STORAGE_GetCapacity (uint8_t lun,
71 uint32_t *block_num,
72 uint32_t *block_size);
73 #endif
75 static int8_t STORAGE_IsReady (uint8_t lun);
77 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
79 static int8_t STORAGE_Read (uint8_t lun,
80 uint8_t *buf,
81 uint32_t blk_addr,
82 uint16_t blk_len);
84 static int8_t STORAGE_Write (uint8_t lun,
85 uint8_t *buf,
86 uint32_t blk_addr,
87 uint16_t blk_len);
89 static int8_t STORAGE_GetMaxLun (void);
91 /* USB Mass storage Standard Inquiry Data */
92 static uint8_t STORAGE_Inquirydata[] = {//36
94 /* LUN 0 */
95 0x00,
96 0x80,
97 0x02,
98 0x02,
99 #ifdef USE_HAL_DRIVER
100 (STANDARD_INQUIRY_DATA_LEN - 5),
101 #else
102 (USBD_STD_INQUIRY_LENGTH - 5),
103 #endif
104 0x00,
105 0x00,
106 0x00,
107 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
108 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
109 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
110 '0', '.', '0' ,'1', /* Version : 4 Bytes */
113 #ifdef USE_HAL_DRIVER
114 USBD_StorageTypeDef USBD_MSC_MICRO_SD_SPI_fops =
116 STORAGE_Init,
117 STORAGE_GetCapacity,
118 STORAGE_IsReady,
119 STORAGE_IsWriteProtected,
120 STORAGE_Read,
121 STORAGE_Write,
122 STORAGE_GetMaxLun,
123 (int8_t*)STORAGE_Inquirydata,
125 #else
126 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SD_SPI_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 #endif
139 /*******************************************************************************
140 * Function Name : Read_Memory
141 * Description : Handle the Read operation from the microSD card.
142 * Input : None.
143 * Output : None.
144 * Return : None.
145 *******************************************************************************/
146 static int8_t STORAGE_Init (uint8_t lun)
148 UNUSED(lun);
149 LED0_OFF;
150 sdcard_init(sdcardConfig());
151 while (sdcard_poll() == 0);
152 mscSetActive();
153 return 0;
156 /*******************************************************************************
157 * Function Name : Read_Memory
158 * Description : Handle the Read operation from the STORAGE card.
159 * Input : None.
160 * Output : None.
161 * Return : None.
162 *******************************************************************************/
163 #ifdef USE_HAL_DRIVER
164 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
165 #else
166 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
167 #endif
169 UNUSED(lun);
170 *block_num = sdcard_getMetadata()->numBlocks;
171 *block_size = 512;
172 return (0);
175 /*******************************************************************************
176 * Function Name : Read_Memory
177 * Description : Handle the Read operation from the STORAGE card.
178 * Input : None.
179 * Output : None.
180 * Return : None.
181 *******************************************************************************/
182 static int8_t STORAGE_IsReady (uint8_t lun)
184 UNUSED(lun);
185 int8_t ret = -1;
186 if (sdcard_poll()) {
187 ret = 0;
189 return ret;
192 /*******************************************************************************
193 * Function Name : Read_Memory
194 * Description : Handle the Read operation from the STORAGE card.
195 * Input : None.
196 * Output : None.
197 * Return : None.
198 *******************************************************************************/
199 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
201 UNUSED(lun);
202 return 0;
205 /*******************************************************************************
206 * Function Name : Read_Memory
207 * Description : Handle the Read operation from the STORAGE card.
208 * Input : None.
209 * Output : None.
210 * Return : None.
211 *******************************************************************************/
212 static int8_t STORAGE_Read (uint8_t lun,
213 uint8_t *buf,
214 uint32_t blk_addr,
215 uint16_t blk_len)
217 UNUSED(lun);
218 for (int i = 0; i < blk_len; i++) {
219 while (sdcard_readBlock(blk_addr + i, buf + (512 * i), NULL, 0) == 0);
220 while (sdcard_poll() == 0);
222 mscSetActive();
223 return 0;
225 /*******************************************************************************
226 * Function Name : Write_Memory
227 * Description : Handle the Write operation to the STORAGE card.
228 * Input : None.
229 * Output : None.
230 * Return : None.
231 *******************************************************************************/
232 static int8_t STORAGE_Write (uint8_t lun,
233 uint8_t *buf,
234 uint32_t blk_addr,
235 uint16_t blk_len)
237 UNUSED(lun);
238 for (int i = 0; i < blk_len; i++) {
239 while (sdcard_writeBlock(blk_addr + i, buf + (i * 512), NULL, 0) != SDCARD_OPERATION_IN_PROGRESS) {
240 sdcard_poll();
242 while (sdcard_poll() == 0);
244 mscSetActive();
245 return 0;
247 /*******************************************************************************
248 * Function Name : Write_Memory
249 * Description : Handle the Write operation to the STORAGE card.
250 * Input : None.
251 * Output : None.
252 * Return : None.
253 *******************************************************************************/
254 static int8_t STORAGE_GetMaxLun (void)
256 return (STORAGE_LUN_NBR - 1);
259 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/