1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
33
34
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
88
100
101
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
129
130
131
132
133
142
146
147
148
149
154
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
212
213
214
215
220
224
225
228
229
/* ... */
#include "usbd_storage.h"
#include "k_storage.h"
#include "main.h"
/* ... */
/* ... */
Includes
#define STORAGE_LUN_NBR 1
#define STORAGE_BLK_NBR 0x10000
#define STORAGE_BLK_SIZ 0x200
Private define
const int8_t STORAGE_Inquirydata[] = {
0x00,
0x80,
0x02,
0x02,
(STANDARD_INQUIRY_DATA_LEN - 5),
0x00,
0x00,
0x00,
'S', 'T', 'M', ' ', ' ', ' ', ' ', ' ',
'P', 'r', 'o', 'd', 'u', 'c', 't', ' ',
' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
'0', '.', '0','1',
...};
Private variables
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,
(int8_t *)STORAGE_Inquirydata,
...};
static uint32_t prev_state = 1;Private function prototypes
/* ... */
int8_t STORAGE_Init(uint8_t lun)
{
if(k_StorageGetStatus (MSD_DISK_UNIT) == 1)
{
prev_state = 1;
}if (k_StorageGetStatus (MSD_DISK_UNIT) == 1) { ... }
else
{
prev_state = 0;
}else { ... }
return 0;
}{ ... }
/* ... */
int8_t STORAGE_GetCapacity(uint8_t lun, uint32_t *block_num, uint16_t *block_size)
{
HAL_SD_CardInfoTypeDef info;
int8_t ret = -1;
if(k_StorageGetStatus (MSD_DISK_UNIT) == 1)
{
BSP_SD_GetCardInfo(&info);
*block_num = info.LogBlockNbr - 1;
*block_size = info.LogBlockSize;
ret = 0;
}if (k_StorageGetStatus (MSD_DISK_UNIT) == 1) { ... }
return ret;
}{ ... }
/* ... */
int8_t STORAGE_IsReady(uint8_t lun)
{
int8_t ret = -1;
if(k_StorageGetStatus (MSD_DISK_UNIT) == 1)
{
if(prev_state == 0)
{
BSP_SD_Init();
}if (prev_state == 0) { ... }
prev_state = 1;
ret = 0;
}if (k_StorageGetStatus (MSD_DISK_UNIT) == 1) { ... }
else if(prev_state == 1)
{
prev_state = 0;
}else if (prev_state == 1) { ... }
return ret;
}{ ... }
/* ... */
int8_t STORAGE_IsWriteProtected(uint8_t lun)
{
return 0;
}{ ... }
/* ... */
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(k_StorageGetStatus (MSD_DISK_UNIT) == 1)
{
BSP_SD_ReadBlocks((uint32_t *)buf, blk_addr, blk_len, timeout);
while(BSP_SD_GetCardState() != SD_TRANSFER_OK)
{
if (timeout-- == 0)
{
return (-1);
}if (timeout-- == 0) { ... }
}while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { ... }
ret = 0;
}if (k_StorageGetStatus (MSD_DISK_UNIT) == 1) { ... }
return ret;
}{ ... }
/* ... */
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(k_StorageGetStatus (MSD_DISK_UNIT) == 1)
{
BSP_SD_WriteBlocks((uint32_t *)buf, blk_addr, blk_len, timeout);
while(BSP_SD_GetCardState() != SD_TRANSFER_OK)
{
if (timeout-- == 0)
{
return (-1);
}if (timeout-- == 0) { ... }
}while (BSP_SD_GetCardState() != SD_TRANSFER_OK) { ... }
ret = 0;
}if (k_StorageGetStatus (MSD_DISK_UNIT) == 1) { ... }
return ret;
}{ ... }
/* ... */
int8_t STORAGE_GetMaxLun(void)
{
return(STORAGE_LUN_NBR - 1);
}{ ... }
/* ... */
/* ... */