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 ******************************************************************************
29 /* Includes ------------------------------------------------------------------*/
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"
48 #include "pg/sdcard.h"
54 #include "usbd_msc_mem.h"
55 #include "usbd_msc_core.h"
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
);
79 static int8_t STORAGE_GetCapacity (uint8_t lun
,
81 uint16_t *block_size
);
83 static int8_t STORAGE_GetCapacity (uint8_t lun
,
85 uint32_t *block_size
);
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
,
97 static int8_t STORAGE_Write (uint8_t lun
,
102 static int8_t STORAGE_GetMaxLun (void);
104 /* USB Mass storage Standard Inquiry Data */
105 static uint8_t STORAGE_Inquirydata
[] = {//36
112 #ifdef USE_HAL_DRIVER
113 (STANDARD_INQUIRY_DATA_LEN
- 5),
115 (USBD_STD_INQUIRY_LENGTH
- 5),
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
=
132 STORAGE_IsWriteProtected
,
136 (int8_t*)STORAGE_Inquirydata
,
139 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops
=
144 STORAGE_IsWriteProtected
,
148 (int8_t*)STORAGE_Inquirydata
,
152 /*******************************************************************************
153 * Function Name : Read_Memory
154 * Description : Handle the Read operation from the microSD card.
158 *******************************************************************************/
159 static int8_t STORAGE_Init (uint8_t lun
)
162 const IO_t sd_det
= IOGetByTag(sdcardConfig()->cardDetectTag
);
163 IOInit(sd_det
, OWNER_SDCARD_DETECT
, 0);
164 IOConfigGPIO(sd_det
, IOCFG_IPU
);
170 const dmaChannelSpec_t
*dmaChannelSpec
= dmaGetChannelSpecByPeripheral(DMA_PERIPH_SDIO
, 0, sdioConfig()->dmaopt
);
172 if (!dmaChannelSpec
|| !SD_Initialize_LL((DMA_ARCH_TYPE
*)dmaChannelSpec
->ref
)) {
174 #if defined(STM32H7) // H7 uses IDMA
175 if (!SD_Initialize_LL(0)) {
177 if (!SD_Initialize_LL(SDCARD_SDIO_DMA_OPT
)) {
179 #endif // USE_DMA_SPEC
183 if (!sdcard_isInserted()) {
186 if (SD_Init() != SD_OK
) {
195 /*******************************************************************************
196 * Function Name : Read_Memory
197 * Description : Handle the Read operation from the STORAGE card.
201 *******************************************************************************/
202 #ifdef USE_HAL_DRIVER
203 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
205 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
209 if (!sdcard_isInserted()) {
214 *block_num
= SD_CardInfo
.CardCapacity
;
219 /*******************************************************************************
220 * Function Name : Read_Memory
221 * Description : Handle the Read operation from the STORAGE card.
225 *******************************************************************************/
226 static int8_t STORAGE_IsReady (uint8_t lun
)
230 if (SD_GetState() == true && sdcard_isInserted()) {
236 /*******************************************************************************
237 * Function Name : Read_Memory
238 * Description : Handle the Read operation from the STORAGE card.
242 *******************************************************************************/
243 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
249 /*******************************************************************************
250 * Function Name : Read_Memory
251 * Description : Handle the Read operation from the STORAGE card.
255 *******************************************************************************/
256 static int8_t STORAGE_Read (uint8_t lun
,
262 if (!sdcard_isInserted()) {
265 //buf should be 32bit aligned, but usually is so we don't do byte alignment
266 if (SD_ReadBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
267 while (SD_CheckRead());
268 while(SD_GetState() == false);
274 /*******************************************************************************
275 * Function Name : Write_Memory
276 * Description : Handle the Write operation to the STORAGE card.
280 *******************************************************************************/
281 static int8_t STORAGE_Write (uint8_t lun
,
287 if (!sdcard_isInserted()) {
290 //buf should be 32bit aligned, but usually is so we don't do byte alignment
291 if (SD_WriteBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
292 while (SD_CheckWrite());
293 while(SD_GetState() == false);
299 /*******************************************************************************
300 * Function Name : Write_Memory
301 * Description : Handle the Write operation to the STORAGE card.
305 *******************************************************************************/
306 static int8_t STORAGE_GetMaxLun (void)
308 return (STORAGE_LUN_NBR
- 1);
311 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/