Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "usbd_storage.h"
#include "stm324x9i_eval_sd.h"
Private define
#define STORAGE_LUN_NBR
#define STORAGE_BLK_NBR
#define STORAGE_BLK_SIZ
Private variables
writestatus
readstatus
STORAGE_Inquirydata
Private function prototypes
USBD_DISK_fops
STORAGE_Init(uint8_t)
STORAGE_GetCapacity(uint8_t, uint32_t *, uint16_t *)
STORAGE_IsReady(uint8_t)
STORAGE_IsWriteProtected(uint8_t)
STORAGE_Read(uint8_t, uint8_t *, uint32_t, uint16_t)
STORAGE_Write(uint8_t, uint8_t *, uint32_t, uint16_t)
STORAGE_GetMaxLun()
BSP_SD_WriteCpltCallback()
BSP_SD_ReadCpltCallback()
Files
loading...
SourceVuSTM32 Libraries and SamplesMSC_StandaloneSrc/usbd_storage.c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Device/MSC_Standalone/Src/usbd_storage.c * @author MCD Application Team * @brief Memory management layer ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------ */ #include "usbd_storage.h" #include "stm324x9i_eval_sd.h" Includes /* Private typedef ----------------------------------------------------------- */ /* Private define ------------------------------------------------------------ */ #define STORAGE_LUN_NBR 1 #define STORAGE_BLK_NBR 0x10000 #define STORAGE_BLK_SIZ 0x200 Private define /* Private macro ------------------------------------------------------------- */ /* Private variables --------------------------------------------------------- */ __IO uint32_t writestatus, readstatus = 0; /* USB Mass storage Standard Inquiry Data */ int8_t STORAGE_Inquirydata[] = { /* 36 */ /* LUN 0 */ 0x00, 0x80, 0x02, 0x02, (STANDARD_INQUIRY_DATA_LEN - 5), 0x00, 0x00, 0x00, 'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ', /* Manufacturer: 8 bytes */ 'P', 'r', 'o', 'd', 'u', 'c', 't', ' ', /* Product : 16 Bytes */ ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', '0', '.', '0', '1', /* Version : 4 Bytes */ ...}; Private variables /* Private function prototypes ----------------------------------------------- */ int8_t STORAGE_Init(uint8_t lun); int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t * block_num, uint16_t * block_size); int8_t STORAGE_IsReady(uint8_t lun); int8_t STORAGE_IsWriteProtected(uint8_t lun); int8_t STORAGE_Read(uint8_t lun, uint8_t * buf, uint32_t blk_addr, uint16_t blk_len); int8_t STORAGE_Write(uint8_t lun, uint8_t * buf, uint32_t blk_addr, uint16_t blk_len); int8_t STORAGE_GetMaxLun(void); USBD_StorageTypeDef USBD_DISK_fops = { STORAGE_Init, STORAGE_GetCapacity, STORAGE_IsReady, STORAGE_IsWriteProtected, STORAGE_Read, STORAGE_Write, STORAGE_GetMaxLun, STORAGE_Inquirydata, ...}; Private function prototypes /* Private functions --------------------------------------------------------- */ /** * @brief Initializes the storage unit (medium) * @param lun: Logical unit number * @retval Status (0 : Ok / -1 : Error) *//* ... */ int8_t STORAGE_Init(uint8_t lun) { BSP_SD_Init(); return 0; }{ ... } /** * @brief Returns the medium capacity. * @param lun: Logical unit number * @param block_num: Number of total block number * @param block_size: Block size * @retval Status (0: Ok / -1: Error) *//* ... */ int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t * block_num, uint16_t * block_size) { HAL_SD_CardInfoTypeDef info; int8_t ret = -1; if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { BSP_SD_GetCardInfo(&info); *block_num = info.LogBlockNbr - 1; *block_size = info.LogBlockSize; ret = 0; }if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { ... } return ret; }{ ... } /** * @brief Checks whether the medium is ready. * @param lun: Logical unit number * @retval Status (0: Ok / -1: Error) *//* ... */ int8_t STORAGE_IsReady(uint8_t lun) { static int8_t prev_status = 0; int8_t ret = -1; if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { if (prev_status < 0) { BSP_SD_Init(); prev_status = 0; }if (prev_status < 0) { ... } if (BSP_SD_GetCardState() == SD_TRANSFER_OK) { ret = 0; }if (BSP_SD_GetCardState() == SD_TRANSFER_OK) { ... } }if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { ... } else if (prev_status == 0) { prev_status = -1; }else if (prev_status == 0) { ... } return ret; }{ ... } /** * @brief Checks whether the medium is write protected. * @param lun: Logical unit number * @retval Status (0: write enabled / -1: otherwise) *//* ... */ int8_t STORAGE_IsWriteProtected(uint8_t lun) { return 0; }{ ... } /** * @brief Reads data from the medium. * @param lun: Logical unit number * @param blk_addr: Logical block address * @param blk_len: Blocks number * @retval Status (0: Ok / -1: Error) *//* ... */ int8_t STORAGE_Read(uint8_t lun, uint8_t * buf, uint32_t blk_addr, uint16_t blk_len) { int8_t ret = -1; uint32_t timeout = 100000; if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { BSP_SD_ReadBlocks_DMA((uint32_t *) buf, blk_addr, blk_len); /* Wait for Rx Transfer completion */ while (readstatus == 0) { }while (readstatus == 0) { ... } readstatus = 0; /* Wait until SD card is ready to use for new operation */ while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { if (timeout-- == 0) { return ret; }if (timeout-- == 0) { ... } }while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { ... } ret = 0; }if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { ... } return ret; }{ ... } /** * @brief Writes data into the medium. * @param lun: Logical unit number * @param blk_addr: Logical block address * @param blk_len: Blocks number * @retval Status (0 : Ok / -1 : Error) *//* ... */ int8_t STORAGE_Write(uint8_t lun, uint8_t * buf, uint32_t blk_addr, uint16_t blk_len) { int8_t ret = -1; uint32_t timeout = 100000; if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { BSP_SD_WriteBlocks_DMA((uint32_t *) buf, blk_addr, blk_len); /* Wait for Tx Transfer completion */ while (writestatus == 0) { }while (writestatus == 0) { ... } writestatus = 0; /* Wait until SD card is ready to use for new operation */ while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { if (timeout-- == 0) { return ret; }if (timeout-- == 0) { ... } }while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { ... } ret = 0; }if (BSP_SD_IsDetected() != SD_NOT_PRESENT) { ... } return ret; }{ ... } /** * @brief Returns the Max Supported LUNs. * @param None * @retval Lun(s) number *//* ... */ int8_t STORAGE_GetMaxLun(void) { return (STORAGE_LUN_NBR - 1); }{ ... } /** * @brief BSP Tx Transfer completed callbacks * @param None * @retval None *//* ... */ void BSP_SD_WriteCpltCallback(void) { writestatus = 1; }{ ... } /** * @brief BSP Rx Transfer completed callbacks * @param None * @retval None *//* ... */ void BSP_SD_ReadCpltCallback(void) { readstatus = 1; }{ ... }
Details