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
103
106
107
108
109
114
115
116
117
118
119
120
121
122
127
128
129
130
135
136
137
138
139
140
145
146
147
148
149
150
151
156
157
158
159
160
161
166
167
168
169
170
171
172
177
182
183
184
185
186
187
188
189
190
191
192
198
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
250
251
252
253
254
255
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
276
277
278
279
280
281
282
283
284
285
287
288
289
290
291
292
293
297
298
299
304
312
313
314
315
316
317
318
319
320
321
331
/* ... */
#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 AppliState = 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();
SystemClock_Config();
BSP_LED_Init(LED_BLUE);
BSP_LED_Init(LED_RED);
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(AppliState)
{
case APPLICATION_START:
MSC_Application();
AppliState = APPLICATION_IDLE;
break;
case APPLICATION_START:
case APPLICATION_IDLE:
default:
break; default
}switch (AppliState) { ... }
}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(LED_BLUE);
}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:
AppliState = APPLICATION_IDLE;
BSP_LED_Off(LED_BLUE);
BSP_LED_Off(LED_RED);
f_mount(NULL, (TCHAR const*)"", 0);
break;
case HOST_USER_DISCONNECTION:
case HOST_USER_CLASS_ACTIVE:
AppliState = 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;
RCC_PeriphCLKInitTypeDef PeriphClkInitStruct;
HAL_StatusTypeDef ret = HAL_OK;
__HAL_RCC_PWR_CLK_ENABLE();
/* ... */
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 360;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 7;
RCC_OscInitStruct.PLL.PLLR = 2;
ret = HAL_RCC_OscConfig(&RCC_OscInitStruct);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
PeriphClkInitStruct.PLLSAI.PLLSAIM = 8;
PeriphClkInitStruct.PLLSAI.PLLSAIN = 384;
PeriphClkInitStruct.PLLSAI.PLLSAIP = RCC_PLLSAIP_DIV8;
PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_CK48;
PeriphClkInitStruct.Clk48ClockSelection = RCC_CK48CLKSOURCE_PLLSAIP;
HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct);
/* ... */
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;
ret = HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
if(ret != HAL_OK)
{
while(1) { ; }
}if (ret != HAL_OK) { ... }
}{ ... }
/* ... */
static void Error_Handler(void)
{
BSP_LED_On(LED_RED);
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