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
) {
176 SD_Initialize_LL((DMA_ARCH_TYPE
*)dmaChannelSpec
->ref
);
178 SD_Initialize_LL(SDCARD_SDIO_DMA_OPT
);
181 if (!sdcard_isInserted()) {
184 if (SD_Init() != 0) {
193 /*******************************************************************************
194 * Function Name : Read_Memory
195 * Description : Handle the Read operation from the STORAGE card.
199 *******************************************************************************/
200 #ifdef USE_HAL_DRIVER
201 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
203 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
207 if (!sdcard_isInserted()) {
212 *block_num
= SD_CardInfo
.CardCapacity
;
217 /*******************************************************************************
218 * Function Name : Read_Memory
219 * Description : Handle the Read operation from the STORAGE card.
223 *******************************************************************************/
224 static int8_t STORAGE_IsReady (uint8_t lun
)
228 if (SD_GetState() == true && sdcard_isInserted()) {
234 /*******************************************************************************
235 * Function Name : Read_Memory
236 * Description : Handle the Read operation from the STORAGE card.
240 *******************************************************************************/
241 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
247 /*******************************************************************************
248 * Function Name : Read_Memory
249 * Description : Handle the Read operation from the STORAGE card.
253 *******************************************************************************/
254 static int8_t STORAGE_Read (uint8_t lun
,
260 if (!sdcard_isInserted()) {
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);
272 /*******************************************************************************
273 * Function Name : Write_Memory
274 * Description : Handle the Write operation to the STORAGE card.
278 *******************************************************************************/
279 static int8_t STORAGE_Write (uint8_t lun
,
285 if (!sdcard_isInserted()) {
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);
297 /*******************************************************************************
298 * Function Name : Write_Memory
299 * Description : Handle the Write operation to the STORAGE card.
303 *******************************************************************************/
304 static int8_t STORAGE_GetMaxLun (void)
306 return (STORAGE_LUN_NBR
- 1);
309 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/