2 ******************************************************************************
3 * @file usbd_storage_template.c
4 * @author MCD Application Team
6 * @date 09-November-2015
7 * @brief Memory management layer
8 ******************************************************************************
11 * <h2><center>© 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 ------------------------------------------------------------------*/
36 #include "common/utils.h"
38 #include "drivers/dma.h"
39 #include "drivers/dma_reqmap.h"
40 #include "drivers/io.h"
41 #include "drivers/light_led.h"
42 #include "drivers/sdcard.h"
43 #include "drivers/sdmmc_sdio.h"
44 #include "drivers/usb_msc.h"
47 #include "pg/sdcard.h"
53 #include "usbd_msc_mem.h"
54 #include "usbd_msc_core.h"
57 #include "usbd_storage.h"
59 /* Private typedef -----------------------------------------------------------*/
60 /* Private define ------------------------------------------------------------*/
61 /* Private macro -------------------------------------------------------------*/
62 /* Private variables ---------------------------------------------------------*/
63 /* Private function prototypes -----------------------------------------------*/
64 /* Extern function prototypes ------------------------------------------------*/
65 /* Private functions ---------------------------------------------------------*/
67 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
68 * otherwise SDIO won't be able to preempt USB.
71 #define STORAGE_LUN_NBR 1
72 #define STORAGE_BLK_NBR 0x10000
73 #define STORAGE_BLK_SIZ 0x200
75 static int8_t STORAGE_Init (uint8_t lun
);
78 static int8_t STORAGE_GetCapacity (uint8_t lun
,
80 uint16_t *block_size
);
82 static int8_t STORAGE_GetCapacity (uint8_t lun
,
84 uint32_t *block_size
);
87 static int8_t STORAGE_IsReady (uint8_t lun
);
89 static int8_t STORAGE_IsWriteProtected (uint8_t lun
);
91 static int8_t STORAGE_Read (uint8_t lun
,
96 static int8_t STORAGE_Write (uint8_t lun
,
101 static int8_t STORAGE_GetMaxLun (void);
103 /* USB Mass storage Standard Inquiry Data */
104 static uint8_t STORAGE_Inquirydata
[] = {//36
111 #ifdef USE_HAL_DRIVER
112 (STANDARD_INQUIRY_DATA_LEN
- 5),
114 (USBD_STD_INQUIRY_LENGTH
- 5),
119 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
120 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
121 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
122 '0', '.', '0' ,'1', /* Version : 4 Bytes */
125 #ifdef USE_HAL_DRIVER
126 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops
=
131 STORAGE_IsWriteProtected
,
135 (int8_t*)STORAGE_Inquirydata
,
138 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops
=
143 STORAGE_IsWriteProtected
,
147 (int8_t*)STORAGE_Inquirydata
,
151 /*******************************************************************************
152 * Function Name : Read_Memory
153 * Description : Handle the Read operation from the microSD card.
157 *******************************************************************************/
158 static int8_t STORAGE_Init (uint8_t lun
)
161 const IO_t sd_det
= IOGetByTag(sdcardConfig()->cardDetectTag
);
162 IOInit(sd_det
, OWNER_SDCARD_DETECT
, 0);
163 IOConfigGPIO(sd_det
, IOCFG_IPU
);
169 const dmaChannelSpec_t
*dmaChannelSpec
= dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO
, 0, sdioConfig()->dmaopt
);
171 if (!dmaChannelSpec
|| !SD_Initialize_LL((DMA_ARCH_TYPE
*)dmaChannelSpec
->ref
)) {
173 #if defined(STM32H7) // H7 uses IDMA
174 if (!SD_Initialize_LL(0)) {
176 if (!SD_Initialize_LL(SDCARD_SDIO_DMA_OPT
)) {
178 #endif // USE_DMA_SPEC
182 if (!sdcard_isInserted()) {
185 if (SD_Init() != SD_OK
) {
194 /*******************************************************************************
195 * Function Name : Read_Memory
196 * Description : Handle the Read operation from the STORAGE card.
200 *******************************************************************************/
201 #ifdef USE_HAL_DRIVER
202 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
204 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
208 if (!sdcard_isInserted()) {
213 *block_num
= SD_CardInfo
.CardCapacity
;
218 /*******************************************************************************
219 * Function Name : Read_Memory
220 * Description : Handle the Read operation from the STORAGE card.
224 *******************************************************************************/
225 static int8_t STORAGE_IsReady (uint8_t lun
)
229 if (SD_GetState() == true && sdcard_isInserted()) {
235 /*******************************************************************************
236 * Function Name : Read_Memory
237 * Description : Handle the Read operation from the STORAGE card.
241 *******************************************************************************/
242 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
248 /*******************************************************************************
249 * Function Name : Read_Memory
250 * Description : Handle the Read operation from the STORAGE card.
254 *******************************************************************************/
255 static int8_t STORAGE_Read (uint8_t lun
,
261 if (!sdcard_isInserted()) {
264 //buf should be 32bit aligned, but usually is so we don't do byte alignment
265 if (SD_ReadBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
266 while (SD_CheckRead());
267 while(SD_GetState() == false);
273 /*******************************************************************************
274 * Function Name : Write_Memory
275 * Description : Handle the Write operation to the STORAGE card.
279 *******************************************************************************/
280 static int8_t STORAGE_Write (uint8_t lun
,
286 if (!sdcard_isInserted()) {
289 //buf should be 32bit aligned, but usually is so we don't do byte alignment
290 if (SD_WriteBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
291 while (SD_CheckWrite());
292 while(SD_GetState() == false);
298 /*******************************************************************************
299 * Function Name : Write_Memory
300 * Description : Handle the Write operation to the STORAGE card.
304 *******************************************************************************/
305 static int8_t STORAGE_GetMaxLun (void)
307 return (STORAGE_LUN_NBR
- 1);
310 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/