1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
22
23
24
25
26
27
28
29
30
31
36
37
38
39
45
46
47
52
53
54
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
100
101
102
105
106
107
112
113
114
115
116
117
118
119
120
125
126
127
128
133
134
135
136
137
138
143
144
145
146
147
148
149
154
155
156
157
158
159
164
165
166
167
168
169
170
175
180
181
182
183
184
185
186
187
188
189
190
196
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
265
266
267
268
269
270
271
272
273
278
286
287
288
289
290
291
292
293
294
295
305
/* ... */
#include "main.h"
Includes
FATFS USBDISKFatFs;
FIL MyFile;
char USBDISKPath[4];
USBH_HandleTypeDef hUSBHost;
typedef enum {
APPLICATION_IDLE = 0,
APPLICATION_START,
APPLICATION_RUNNING,
...}MSC_ApplicationTypeDef;
MSC_ApplicationTypeDef Appli_state = APPLICATION_IDLE;
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);
Private function prototypes
/* ... */
int main(void)
{
/* ... */
HAL_Init();
BSP_LED_Init(LED3);
BSP_LED_Init(LED4);
SystemClock_Config();
if(FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0)
{
USBH_Init(&hUSBHost, USBH_UserProcess, 0);
USBH_RegisterClass(&hUSBHost, USBH_MSC_CLASS);
USBH_Start(&hUSBHost);
while (1)
{
USBH_Process(&hUSBHost);
switch(Appli_state)
{
case APPLICATION_START:
MSC_Application();
Appli_state = APPLICATION_IDLE;
break;
case APPLICATION_START:
case APPLICATION_IDLE:
default:
break; default
}switch (Appli_state) { ... }
}while (1) { ... }
}if (FATFS_LinkDriver(&USBH_Driver, USBDISKPath) == 0) { ... }
while (1)
{
}while (1) { ... }
}{ ... }
/* ... */
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(LED3);
}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:
Appli_state = APPLICATION_IDLE;
BSP_LED_Off(LED3);
BSP_LED_Off(LED4);
f_mount(NULL, (TCHAR const*)"", 0);
break;
case HOST_USER_DISCONNECTION:
case HOST_USER_CLASS_ACTIVE:
Appli_state = APPLICATION_START;
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 = 8;
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(LED4);
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