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 ------------------------------------------------------------------*/
33 #include "common/utils.h"
35 #include "usbd_storage.h"
37 #include "drivers/bus_spi.h"
38 #include "drivers/io.h"
39 #include "drivers/light_led.h"
40 #include "drivers/sdcard.h"
41 #include "drivers/usb_msc.h"
43 #include "pg/sdcard.h"
44 #include "pg/bus_spi.h"
46 /* Private typedef -----------------------------------------------------------*/
47 /* Private define ------------------------------------------------------------*/
48 /* Private macro -------------------------------------------------------------*/
49 /* Private variables ---------------------------------------------------------*/
50 /* Private function prototypes -----------------------------------------------*/
51 /* Extern function prototypes ------------------------------------------------*/
52 /* Private functions ---------------------------------------------------------*/
54 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
55 * otherwise SDIO won't be able to preempt USB.
58 #define STORAGE_LUN_NBR 1
59 #define STORAGE_BLK_NBR 0x10000
60 #define STORAGE_BLK_SIZ 0x200
62 static int8_t STORAGE_Init (uint8_t lun
);
65 static int8_t STORAGE_GetCapacity (uint8_t lun
,
67 uint16_t *block_size
);
69 static int8_t STORAGE_GetCapacity (uint8_t lun
,
71 uint32_t *block_size
);
74 static int8_t STORAGE_IsReady (uint8_t lun
);
76 static int8_t STORAGE_IsWriteProtected (uint8_t lun
);
78 static int8_t STORAGE_Read (uint8_t lun
,
83 static int8_t STORAGE_Write (uint8_t lun
,
88 static int8_t STORAGE_GetMaxLun (void);
90 /* USB Mass storage Standard Inquiry Data */
91 static uint8_t STORAGE_Inquirydata
[] = {//36
99 (STANDARD_INQUIRY_DATA_LEN
- 5),
101 (USBD_STD_INQUIRY_LENGTH
- 5),
106 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
107 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
108 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
109 '0', '.', '0' ,'1', /* Version : 4 Bytes */
112 #ifdef USE_HAL_DRIVER
113 USBD_StorageTypeDef USBD_MSC_MICRO_SD_SPI_fops
=
118 STORAGE_IsWriteProtected
,
122 (int8_t*)STORAGE_Inquirydata
,
125 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SD_SPI_fops
=
130 STORAGE_IsWriteProtected
,
134 (int8_t*)STORAGE_Inquirydata
,
138 /*******************************************************************************
139 * Function Name : Read_Memory
140 * Description : Handle the Read operation from the microSD card.
144 *******************************************************************************/
145 static int8_t STORAGE_Init (uint8_t lun
)
149 sdcard_init(sdcardConfig());
150 while (sdcard_poll() == 0);
155 /*******************************************************************************
156 * Function Name : Read_Memory
157 * Description : Handle the Read operation from the STORAGE card.
161 *******************************************************************************/
162 #ifdef USE_HAL_DRIVER
163 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
165 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
169 *block_num
= sdcard_getMetadata()->numBlocks
;
174 /*******************************************************************************
175 * Function Name : Read_Memory
176 * Description : Handle the Read operation from the STORAGE card.
180 *******************************************************************************/
181 static int8_t STORAGE_IsReady (uint8_t lun
)
191 /*******************************************************************************
192 * Function Name : Read_Memory
193 * Description : Handle the Read operation from the STORAGE card.
197 *******************************************************************************/
198 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
204 /*******************************************************************************
205 * Function Name : Read_Memory
206 * Description : Handle the Read operation from the STORAGE card.
210 *******************************************************************************/
211 static int8_t STORAGE_Read (uint8_t lun
,
217 for (int i
= 0; i
< blk_len
; i
++) {
218 while (sdcard_readBlock(blk_addr
+ i
, buf
+ (512 * i
), NULL
, 0) == 0);
219 while (sdcard_poll() == 0);
224 /*******************************************************************************
225 * Function Name : Write_Memory
226 * Description : Handle the Write operation to the STORAGE card.
230 *******************************************************************************/
231 static int8_t STORAGE_Write (uint8_t lun
,
237 for (int i
= 0; i
< blk_len
; i
++) {
238 while (sdcard_writeBlock(blk_addr
+ i
, buf
+ (i
* 512), NULL
, 0) != SDCARD_OPERATION_IN_PROGRESS
) {
241 while (sdcard_poll() == 0);
246 /*******************************************************************************
247 * Function Name : Write_Memory
248 * Description : Handle the Write operation to the STORAGE card.
252 *******************************************************************************/
253 static int8_t STORAGE_GetMaxLun (void)
255 return (STORAGE_LUN_NBR
- 1);
258 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/