New SPI API supporting DMA
[betaflight.git] / src / main / msc / usbd_storage_sdio.c
blob53c1487a21562409287435e4f21225e182df9ab4
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 #ifdef USE_SDCARD
37 #include "common/utils.h"
39 #include "drivers/dma.h"
40 #include "drivers/dma_reqmap.h"
41 #include "drivers/io.h"
42 #include "drivers/light_led.h"
43 #include "drivers/sdcard.h"
44 #include "drivers/sdmmc_sdio.h"
45 #include "drivers/usb_msc.h"
47 #include "pg/pg.h"
48 #include "pg/sdcard.h"
49 #include "pg/sdio.h"
51 #ifdef USE_HAL_DRIVER
52 #include "usbd_msc.h"
53 #else
54 #include "usbd_msc_mem.h"
55 #include "usbd_msc_core.h"
56 #endif
58 #include "usbd_storage.h"
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /* Extern function prototypes ------------------------------------------------*/
66 /* Private functions ---------------------------------------------------------*/
68 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
69 * otherwise SDIO won't be able to preempt USB.
72 #define STORAGE_LUN_NBR 1
73 #define STORAGE_BLK_NBR 0x10000
74 #define STORAGE_BLK_SIZ 0x200
76 static int8_t STORAGE_Init (uint8_t lun);
78 #ifdef USE_HAL_DRIVER
79 static int8_t STORAGE_GetCapacity (uint8_t lun,
80 uint32_t *block_num,
81 uint16_t *block_size);
82 #else
83 static int8_t STORAGE_GetCapacity (uint8_t lun,
84 uint32_t *block_num,
85 uint32_t *block_size);
86 #endif
88 static int8_t STORAGE_IsReady (uint8_t lun);
90 static int8_t STORAGE_IsWriteProtected (uint8_t lun);
92 static int8_t STORAGE_Read (uint8_t lun,
93 uint8_t *buf,
94 uint32_t blk_addr,
95 uint16_t blk_len);
97 static int8_t STORAGE_Write (uint8_t lun,
98 uint8_t *buf,
99 uint32_t blk_addr,
100 uint16_t blk_len);
102 static int8_t STORAGE_GetMaxLun (void);
104 /* USB Mass storage Standard Inquiry Data */
105 static uint8_t STORAGE_Inquirydata[] = {//36
107 /* LUN 0 */
108 0x00,
109 0x80,
110 0x02,
111 0x02,
112 #ifdef USE_HAL_DRIVER
113 (STANDARD_INQUIRY_DATA_LEN - 5),
114 #else
115 (USBD_STD_INQUIRY_LENGTH - 5),
116 #endif
117 0x00,
118 0x00,
119 0x00,
120 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
121 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
122 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
123 '0', '.', '0' ,'1', /* Version : 4 Bytes */
126 #ifdef USE_HAL_DRIVER
127 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops =
129 STORAGE_Init,
130 STORAGE_GetCapacity,
131 STORAGE_IsReady,
132 STORAGE_IsWriteProtected,
133 STORAGE_Read,
134 STORAGE_Write,
135 STORAGE_GetMaxLun,
136 (int8_t*)STORAGE_Inquirydata,
138 #else
139 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops =
141 STORAGE_Init,
142 STORAGE_GetCapacity,
143 STORAGE_IsReady,
144 STORAGE_IsWriteProtected,
145 STORAGE_Read,
146 STORAGE_Write,
147 STORAGE_GetMaxLun,
148 (int8_t*)STORAGE_Inquirydata,
150 #endif
152 /*******************************************************************************
153 * Function Name : Read_Memory
154 * Description : Handle the Read operation from the microSD card.
155 * Input : None.
156 * Output : None.
157 * Return : None.
158 *******************************************************************************/
159 static int8_t STORAGE_Init (uint8_t lun)
161 //Initialize SD_DET
162 const IO_t sd_det = IOGetByTag(sdcardConfig()->cardDetectTag);
163 IOInit(sd_det, OWNER_SDCARD_DETECT, 0);
164 IOConfigGPIO(sd_det, IOCFG_IPU);
166 UNUSED(lun);
167 LED0_OFF;
169 #ifdef USE_DMA_SPEC
170 const dmaChannelSpec_t *dmaChannelSpec = dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO, 0, sdioConfig()->dmaopt);
172 if (!dmaChannelSpec) {
173 return 1;
176 SD_Initialize_LL((DMA_ARCH_TYPE *)dmaChannelSpec->ref);
177 #else
178 SD_Initialize_LL(SDCARD_SDIO_DMA_OPT);
179 #endif
181 if (!sdcard_isInserted()) {
182 return 1;
184 if (SD_Init() != 0) {
185 return 1;
188 mscSetActive();
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 #ifdef USE_HAL_DRIVER
201 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint16_t *block_size)
202 #else
203 static int8_t STORAGE_GetCapacity (uint8_t lun, uint32_t *block_num, uint32_t *block_size)
204 #endif
206 UNUSED(lun);
207 if (!sdcard_isInserted()) {
208 return -1;
210 SD_GetCardInfo();
212 *block_num = SD_CardInfo.CardCapacity;
213 *block_size = 512;
214 return (0);
217 /*******************************************************************************
218 * Function Name : Read_Memory
219 * Description : Handle the Read operation from the STORAGE card.
220 * Input : None.
221 * Output : None.
222 * Return : None.
223 *******************************************************************************/
224 static int8_t STORAGE_IsReady (uint8_t lun)
226 UNUSED(lun);
227 int8_t ret = -1;
228 if (SD_GetState() == true && sdcard_isInserted()) {
229 ret = 0;
231 return ret;
234 /*******************************************************************************
235 * Function Name : Read_Memory
236 * Description : Handle the Read operation from the STORAGE card.
237 * Input : None.
238 * Output : None.
239 * Return : None.
240 *******************************************************************************/
241 static int8_t STORAGE_IsWriteProtected (uint8_t lun)
243 UNUSED(lun);
244 return 0;
247 /*******************************************************************************
248 * Function Name : Read_Memory
249 * Description : Handle the Read operation from the STORAGE card.
250 * Input : None.
251 * Output : None.
252 * Return : None.
253 *******************************************************************************/
254 static int8_t STORAGE_Read (uint8_t lun,
255 uint8_t *buf,
256 uint32_t blk_addr,
257 uint16_t blk_len)
259 UNUSED(lun);
260 if (!sdcard_isInserted()) {
261 return -1;
263 //buf should be 32bit aligned, but usually is so we don't do byte alignment
264 if (SD_ReadBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
265 while (SD_CheckRead());
266 while(SD_GetState() == false);
267 mscSetActive();
268 return 0;
270 return -1;
272 /*******************************************************************************
273 * Function Name : Write_Memory
274 * Description : Handle the Write operation to the STORAGE card.
275 * Input : None.
276 * Output : None.
277 * Return : None.
278 *******************************************************************************/
279 static int8_t STORAGE_Write (uint8_t lun,
280 uint8_t *buf,
281 uint32_t blk_addr,
282 uint16_t blk_len)
284 UNUSED(lun);
285 if (!sdcard_isInserted()) {
286 return -1;
288 //buf should be 32bit aligned, but usually is so we don't do byte alignment
289 if (SD_WriteBlocks_DMA(blk_addr, (uint32_t*) buf, 512, blk_len) == 0) {
290 while (SD_CheckWrite());
291 while(SD_GetState() == false);
292 mscSetActive();
293 return 0;
295 return -1;
297 /*******************************************************************************
298 * Function Name : Write_Memory
299 * Description : Handle the Write operation to the STORAGE card.
300 * Input : None.
301 * Output : None.
302 * Return : None.
303 *******************************************************************************/
304 static int8_t STORAGE_GetMaxLun (void)
306 return (STORAGE_LUN_NBR - 1);
309 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
311 #endif // USE_SDCARD