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/sdcard/sdcard.h"
39 #include "drivers/light_led.h"
40 #include "drivers/io.h"
41 #include "drivers/bus_spi.h"
43 /* Private typedef -----------------------------------------------------------*/
44 /* Private define ------------------------------------------------------------*/
45 /* Private macro -------------------------------------------------------------*/
46 /* Private variables ---------------------------------------------------------*/
47 /* Private function prototypes -----------------------------------------------*/
48 /* Extern function prototypes ------------------------------------------------*/
49 /* Private functions ---------------------------------------------------------*/
51 /* USB NVIC Priority has to be lower than both DMA and SDIO priority,
52 * otherwise SDIO won't be able to preempt USB.
55 #define STORAGE_LUN_NBR 1
56 #define STORAGE_BLK_NBR 0x10000
57 #define STORAGE_BLK_SIZ 0x200
59 static int8_t STORAGE_Init (uint8_t lun
);
62 static int8_t STORAGE_GetCapacity (uint8_t lun
,
64 uint16_t *block_size
);
66 static int8_t STORAGE_GetCapacity (uint8_t lun
,
68 uint32_t *block_size
);
71 static int8_t STORAGE_IsReady (uint8_t lun
);
73 static int8_t STORAGE_IsWriteProtected (uint8_t lun
);
75 static int8_t STORAGE_Read (uint8_t lun
,
80 static int8_t STORAGE_Write (uint8_t lun
,
85 static int8_t STORAGE_GetMaxLun (void);
87 /* USB Mass storage Standard Inquiry Data */
88 static uint8_t STORAGE_Inquirydata
[] = {//36
96 (STANDARD_INQUIRY_DATA_LEN
- 5),
98 (USBD_STD_INQUIRY_LENGTH
- 5),
103 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer : 8 bytes */
104 'P', 'r', 'o', 'd', 'u', 't', ' ', ' ', /* Product : 16 Bytes */
105 ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
106 '0', '.', '0' ,'1', /* Version : 4 Bytes */
109 #ifdef USE_HAL_DRIVER
110 USBD_StorageTypeDef USBD_MSC_MICRO_SDIO_fops
=
115 STORAGE_IsWriteProtected
,
119 (int8_t*)STORAGE_Inquirydata
,
122 USBD_STORAGE_cb_TypeDef USBD_MSC_MICRO_SDIO_fops
=
127 STORAGE_IsWriteProtected
,
131 (int8_t*)STORAGE_Inquirydata
,
135 /*******************************************************************************
136 * Function Name : Read_Memory
137 * Description : Handle the Read operation from the microSD card.
141 *******************************************************************************/
142 static int8_t STORAGE_Init (uint8_t lun
)
147 while (sdcard_poll() == 0);
152 /*******************************************************************************
153 * Function Name : Read_Memory
154 * Description : Handle the Read operation from the STORAGE card.
158 *******************************************************************************/
159 #ifdef USE_HAL_DRIVER
160 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint16_t *block_size
)
162 static int8_t STORAGE_GetCapacity (uint8_t lun
, uint32_t *block_num
, uint32_t *block_size
)
166 *block_num
= sdcard_getMetadata()->numBlocks
;
171 /*******************************************************************************
172 * Function Name : Read_Memory
173 * Description : Handle the Read operation from the STORAGE card.
177 *******************************************************************************/
178 static int8_t STORAGE_IsReady (uint8_t lun
)
188 /*******************************************************************************
189 * Function Name : Read_Memory
190 * Description : Handle the Read operation from the STORAGE card.
194 *******************************************************************************/
195 static int8_t STORAGE_IsWriteProtected (uint8_t lun
)
201 /*******************************************************************************
202 * Function Name : Read_Memory
203 * Description : Handle the Read operation from the STORAGE card.
207 *******************************************************************************/
208 static int8_t STORAGE_Read (uint8_t lun
,
215 for (int i
= 0; i
< blk_len
; i
++) {
216 while (sdcard_readBlock(blk_addr
+ i
, buf
+ (512 * i
), NULL
, 0) == 0);
217 while (sdcard_poll() == 0);
222 /*******************************************************************************
223 * Function Name : Write_Memory
224 * Description : Handle the Write operation to the STORAGE card.
228 *******************************************************************************/
229 static int8_t STORAGE_Write (uint8_t lun
,
236 for (int i
= 0; i
< blk_len
; i
++) {
237 while (sdcard_writeBlock(blk_addr
+ i
, buf
+ (i
* 512), NULL
, 0) != SDCARD_OPERATION_IN_PROGRESS
) {
240 while (sdcard_poll() == 0);
245 /*******************************************************************************
246 * Function Name : Write_Memory
247 * Description : Handle the Write operation to the STORAGE card.
251 *******************************************************************************/
252 static int8_t STORAGE_GetMaxLun (void)
254 return (STORAGE_LUN_NBR
- 1);
257 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/