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 ------------------------------------------------------------------*/
34 #include "common/utils.h"
36 #include "usbd_storage.h"
38 #include "drivers/bus_spi.h"
39 #include "drivers/io.h"
40 #include "drivers/light_led.h"
41 #include "drivers/sdcard.h"
42 #include "drivers/usb_msc.h"
44 #include "pg/sdcard.h"
45 #include "pg/bus_spi.h"
47 /* Private typedef -----------------------------------------------------------*/
48 /* Private define ------------------------------------------------------------*/
49 /* Private macro -------------------------------------------------------------*/
50 /* Private variables ---------------------------------------------------------*/
51 /* Private function prototypes -----------------------------------------------*/
52 /* Extern function prototypes ------------------------------------------------*/
53 /* Private functions ---------------------------------------------------------*/
55 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
56 * otherwise SDIO won't be able to preempt USB.
59 #define STORAGE_LUN_NBR 1
60 #define STORAGE_BLK_NBR 0x10000
61 #define STORAGE_BLK_SIZ 0x200
63 static int8_t STORAGE_Init (uint8_t lun
);
66 static int8_t STORAGE_GetCapacity (uint8_t lun
,
68 uint16_t *block_size
);
70 static int8_t STORAGE_GetCapacity (uint8_t lun
,
72 uint32_t *block_size
);
75 static int8_t STORAGE_IsReady (uint8_t lun
);
77 static int8_t STORAGE_IsWriteProtected (uint8_t lun
);
79 static int8_t STORAGE_Read (uint8_t lun
,
84 static int8_t STORAGE_Write (uint8_t lun
,
89 static int8_t STORAGE_GetMaxLun (void);
91 /* USB Mass storage Standard Inquiry Data */
92 static uint8_t STORAGE_Inquirydata
[] = {//36
100 (STANDARD_INQUIRY_DATA_LEN
- 5),
102 (USBD_STD_INQUIRY_LENGTH
- 5),
107 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
108 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
109 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
110 '0', '.', '0' ,'1', /* Version : 4 Bytes */
113 #ifdef USE_HAL_DRIVER
114 USBD_StorageTypeDef USBD_MSC_MICRO_SD_SPI_fops
=
119 STORAGE_IsWriteProtected
,
123 (int8_t*)STORAGE_Inquirydata
,
126 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SD_SPI_fops
=
131 STORAGE_IsWriteProtected
,
135 (int8_t*)STORAGE_Inquirydata
,
139 /*******************************************************************************
140 * Function Name : Read_Memory
141 * Description : Handle the Read operation from the microSD card.
145 *******************************************************************************/
146 static int8_t STORAGE_Init (uint8_t lun
)
150 sdcard_init(sdcardConfig());
151 while (sdcard_poll() == 0);
156 /*******************************************************************************
157 * Function Name : Read_Memory
158 * Description : Handle the Read operation from the STORAGE card.
162 *******************************************************************************/
163 #ifdef USE_HAL_DRIVER
164 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
166 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
170 *block_num
= sdcard_getMetadata()->numBlocks
;
175 /*******************************************************************************
176 * Function Name : Read_Memory
177 * Description : Handle the Read operation from the STORAGE card.
181 *******************************************************************************/
182 static int8_t STORAGE_IsReady (uint8_t lun
)
192 /*******************************************************************************
193 * Function Name : Read_Memory
194 * Description : Handle the Read operation from the STORAGE card.
198 *******************************************************************************/
199 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
205 /*******************************************************************************
206 * Function Name : Read_Memory
207 * Description : Handle the Read operation from the STORAGE card.
211 *******************************************************************************/
212 static int8_t STORAGE_Read (uint8_t lun
,
218 for (int i
= 0; i
< blk_len
; i
++) {
219 while (sdcard_readBlock(blk_addr
+ i
, buf
+ (512 * i
), NULL
, 0) == 0);
220 while (sdcard_poll() == 0);
225 /*******************************************************************************
226 * Function Name : Write_Memory
227 * Description : Handle the Write operation to the STORAGE card.
231 *******************************************************************************/
232 static int8_t STORAGE_Write (uint8_t lun
,
238 for (int i
= 0; i
< blk_len
; i
++) {
239 while (sdcard_writeBlock(blk_addr
+ i
, buf
+ (i
* 512), NULL
, 0) != SDCARD_OPERATION_IN_PROGRESS
) {
242 while (sdcard_poll() == 0);
247 /*******************************************************************************
248 * Function Name : Write_Memory
249 * Description : Handle the Write operation to the STORAGE card.
253 *******************************************************************************/
254 static int8_t STORAGE_GetMaxLun (void)
256 return (STORAGE_LUN_NBR
- 1);
259 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/