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 ------------------------------------------------------------------*/
35 #include "drivers/sdmmc_sdio.h"
36 #include "drivers/light_led.h"
37 #include "drivers/io.h"
38 #include "common/utils.h"
43 #include "usbd_msc_mem.h"
44 #include "usbd_msc_core.h"
47 #include "usbd_storage.h"
49 /* Private typedef -----------------------------------------------------------*/
50 /* Private define ------------------------------------------------------------*/
51 /* Private macro -------------------------------------------------------------*/
52 /* Private variables ---------------------------------------------------------*/
53 /* Private function prototypes -----------------------------------------------*/
54 /* Extern function prototypes ------------------------------------------------*/
55 /* Private functions ---------------------------------------------------------*/
57 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
58 * otherwise SDIO won't be able to preempt USB.
61 #define STORAGE_LUN_NBR 1
62 #define STORAGE_BLK_NBR 0x10000
63 #define STORAGE_BLK_SIZ 0x200
65 static int8_t STORAGE_Init (uint8_t lun
);
68 static int8_t STORAGE_GetCapacity (uint8_t lun
,
70 uint16_t *block_size
);
72 static int8_t STORAGE_GetCapacity (uint8_t lun
,
74 uint32_t *block_size
);
77 static int8_t STORAGE_IsReady (uint8_t lun
);
79 static int8_t STORAGE_IsWriteProtected (uint8_t lun
);
81 static int8_t STORAGE_Read (uint8_t lun
,
86 static int8_t STORAGE_Write (uint8_t lun
,
91 static int8_t STORAGE_GetMaxLun (void);
93 /* USB Mass storage Standard Inquiry Data */
94 static uint8_t STORAGE_Inquirydata
[] = {//36
101 #ifdef USE_HAL_DRIVER
102 (STANDARD_INQUIRY_DATA_LEN
- 5),
104 (USBD_STD_INQUIRY_LENGTH
- 5),
109 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
110 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
111 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
112 '0', '.', '0' ,'1', /* Version : 4 Bytes */
115 #ifdef USE_HAL_DRIVER
116 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops
=
121 STORAGE_IsWriteProtected
,
125 (int8_t*)STORAGE_Inquirydata
,
128 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops
=
133 STORAGE_IsWriteProtected
,
137 (int8_t*)STORAGE_Inquirydata
,
141 /*******************************************************************************
142 * Function Name : Read_Memory
143 * Description : Handle the Read operation from the microSD card.
147 *******************************************************************************/
148 static int8_t STORAGE_Init (uint8_t lun
)
151 #ifdef SDCARD_DETECT_PIN
152 const IO_t sd_det
= IOGetByTag(IO_TAG(SDCARD_DETECT_PIN
));
153 IOInit(sd_det
, OWNER_SDCARD_DETECT
, 0);
154 IOConfigGPIO(sd_det
, IOCFG_IPU
);
159 #if defined(STM32H7) // H7 uses IDMA
162 SD_Initialize_LL(SDIO_DMA
);
164 if (SD_Init() != 0) return 1;
169 /*******************************************************************************
170 * Function Name : Read_Memory
171 * Description : Handle the Read operation from the STORAGE card.
175 *******************************************************************************/
176 #ifdef USE_HAL_DRIVER
177 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
179 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
183 if (SD_IsDetected() == 0) {
188 *block_num
= SD_CardInfo
.CardCapacity
;
193 /*******************************************************************************
194 * Function Name : Read_Memory
195 * Description : Handle the Read operation from the STORAGE card.
199 *******************************************************************************/
200 static int8_t STORAGE_IsReady (uint8_t lun
)
204 if (SD_GetState() == true && SD_IsDetected() == SD_PRESENT
) {
210 /*******************************************************************************
211 * Function Name : Read_Memory
212 * Description : Handle the Read operation from the STORAGE card.
216 *******************************************************************************/
217 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
223 /*******************************************************************************
224 * Function Name : Read_Memory
225 * Description : Handle the Read operation from the STORAGE card.
229 *******************************************************************************/
230 static int8_t STORAGE_Read (uint8_t lun
,
236 if (SD_IsDetected() == 0) {
240 //buf should be 32bit aligned, but usually is so we don't do byte alignment
241 if (SD_ReadBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
242 while (SD_CheckRead());
243 while(SD_GetState() == false);
250 /*******************************************************************************
251 * Function Name : Write_Memory
252 * Description : Handle the Write operation to the STORAGE card.
256 *******************************************************************************/
257 static int8_t STORAGE_Write (uint8_t lun
,
263 if (SD_IsDetected() == 0) {
267 //buf should be 32bit aligned, but usually is so we don't do byte alignment
268 if (SD_WriteBlocks_DMA(blk_addr
, (uint32_t*) buf
, 512, blk_len
) == 0) {
269 while (SD_CheckWrite());
270 while(SD_GetState() == false);
277 /*******************************************************************************
278 * Function Name : Write_Memory
279 * Description : Handle the Write operation to the STORAGE card.
283 *******************************************************************************/
284 static int8_t STORAGE_GetMaxLun (void)
286 return (STORAGE_LUN_NBR
- 1);
289 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/