1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
23
24
25
26
27
28
29
30
31
32
36
37
38
39
46
47
48
53
54
55
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
127
128
129
130
135
136
137
138
139
140
141
142
143
148
149
150
151
156
157
158
159
160
161
166
167
168
169
170
171
172
177
178
179
180
181
182
187
188
189
190
191
192
193
198
203
204
205
206
207
208
209
210
211
212
213
219
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
272
273
274
275
276
277
278
279
280
281
282
283
284
285
287
288
289
290
291
292
293
294
295
300
308
309
310
311
312
313
314
315
316
317
327
/* ... */
#include "main.h"
Includes
FATFS USBDISKFatFs;
FIL MyFile;
char USBDISKPath[4];
USBH_HandleTypeDef hUSBHost;
typedef enum {
DISCONNECTION_EVENT = 1,
CONNECTION_EVENT,
...}MSC_ApplicationTypeDef;
osMessageQId AppliEvent;
Private variables
static void SystemClock_Config(void);
static void Error_Handler(void);
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id);
static void MSC_Application(void);
static void StartThread(void const *argument);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
SystemClock_Config();
BSP_LED_Init(LED1);
BSP_LED_Init(LED3);
osThreadDef(USER_Thread, StartThread, osPriorityNormal, 0, 8 * configMINIMAL_STACK_SIZE);
osThreadCreate(osThread(USER_Thread), NULL);
osMessageQDef(osqueue, 1, uint16_t);
AppliEvent = osMessageCreate(osMessageQ(osqueue), NULL);
osKernelStart();
for( ;; );
}{ ... }
/* ... */
static void StartThread(void const *argument)
{
osEvent event;
if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
{
USBH_Init(&hUSBHost, USBH_UserProcess, 0);
USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
USBH_Start(&hUSBHost);
for( ;; )
{
event = osMessageGet(AppliEvent, osWaitForever);
if(event.status == osEventMessage)
{
switch(event.value.v)
{
case CONNECTION_EVENT:
MSC_Application();
break;
case CONNECTION_EVENT:
case DISCONNECTION_EVENT:
f_mount(NULL, (TCHAR const*)"", 0);
break;
case DISCONNECTION_EVENT:
default:
break;default
}switch (event.value.v) { ... }
}if (event.status == osEventMessage) { ... }
}for (;;) { ... }
}if (FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0) { ... }
}{ ... }
/* ... */
static void MSC_Application(void)
{
FRESULT res;
uint32_t byteswritten, bytesread;
uint8_t wtext[] = "This is STM32 working with FatFs";
uint8_t rtext[100];
if(f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK)
{
Error_Handler();
}if (f_mount(&USBDISKFatFs, (TCHAR const*)USBDISKPath, 0) != FR_OK) { ... }
else
{
if(f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK)
{
Error_Handler();
}if (f_open(&MyFile, "STM32.TXT", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { ... }
else
{
res = f_write(&MyFile, wtext, sizeof(wtext), (void *)&byteswritten);
if((byteswritten == 0) || (res != FR_OK))
{
Error_Handler();
}if ((byteswritten == 0) || (res != FR_OK)) { ... }
else
{
f_close(&MyFile);
if(f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK)
{
Error_Handler();
}if (f_open(&MyFile, "STM32.TXT", FA_READ) != FR_OK) { ... }
else
{
res = f_read(&MyFile, rtext, sizeof(rtext), (void *)&bytesread);
if((bytesread == 0) || (res != FR_OK))
{
Error_Handler();
}if ((bytesread == 0) || (res != FR_OK)) { ... }
else
{
f_close(&MyFile);
if((bytesread != byteswritten))
{
Error_Handler();
}if ((bytesread != byteswritten)) { ... }
else
{
BSP_LED_On(LED1);
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}else { ... }
}else { ... }
FATFS_UnLinkDriver(USBDISKPath);
}{ ... }
/* ... */
static void USBH_UserProcess(USBH_HandleTypeDef *phost, uint8_t id)
{
switch(id)
{
case HOST_USER_SELECT_CONFIGURATION:
break;
case HOST_USER_SELECT_CONFIGURATION:
case HOST_USER_DISCONNECTION:
osMessagePut(AppliEvent, DISCONNECTION_EVENT, 0);
BSP_LED_Off(LED1);
BSP_LED_Off(LED3);
break;
case HOST_USER_DISCONNECTION:
case HOST_USER_CLASS_ACTIVE:
osMessagePut(AppliEvent, CONNECTION_EVENT, 0);
break;
case HOST_USER_CLASS_ACTIVE:
default:
break;default
}switch (id) { ... }
}{ ... }
/* ... */
static void SystemClock_Config(void)
{
RCC_ClkInitTypeDef RCC_ClkInitStruct;
RCC_OscInitTypeDef RCC_OscInitStruct;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 25;
RCC_OscInitStruct.PLL.PLLN = 336;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
/* ... */
RCC_ClkInitStruct.ClockType = (RCC_CLOCKTYPE_SYSCLK | RCC_CLOCKTYPE_HCLK | RCC_CLOCKTYPE_PCLK1 | RCC_CLOCKTYPE_PCLK2);
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED3);
while(1)
{
}while (1) { ... }
}{ ... }
#ifdef USE_FULL_ASSERT
/* ... */
void assert_failed(uint8_t* file, uint32_t line)
{
/* ... */
while (1)
{
}while (1) { ... }
}assert_failed (uint8_t* file, uint32_t line) { ... }
/* ... */#endifPrivate functions