forked from CommunityGD32Cores/ArduinoCore-GD32
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFatFsTest.ino
More file actions
278 lines (246 loc) · 6.41 KB
/
FatFsTest.ino
File metadata and controls
278 lines (246 loc) · 6.41 KB
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#include "Arduino.h"
#include "FatFs.h"
#include "usbh_msc_core.h"
#include "usbh_usr.h"
// Select Tools->USB Support->USB Host
FATFS FatFs; /* FatFs work area needed for each volume */
FIL file; /* File object needed for each open file */
static bool user_app_runs = false;
void test_fatfs() {
UINT bytes_written = 0;
FRESULT fr;
Serial2.println("USB file write test");
fr = f_open(&file, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS); /* Create a file */
if (fr != FR_OK) {
Serial2.print("File open error ");
Serial2.println(fr);
} else {
fr = f_write(&file, "It works!\r\n", 11, &bytes_written); /* Write data to the file */
if (fr != FR_OK) {
Serial2.print("File write error ");
Serial2.println(fr);
}
fr = f_close(&file); /* Close the file */
if (fr == FR_OK && bytes_written == 11) { /* Lights green LED if data written well */
Serial2.println("File written OK");
} else {
Serial2.print("File close error ");
Serial2.println(fr);
}
}
}
void setup() {
delay(1000);
Serial2.begin(19200);
Serial2.println("FatFsTest");
// Do not access USB in setup(), it is not ready yet
}
void loop() {
// put your main code here, to run repeatedly:
static bool loop_entered = false;
if (!loop_entered) {
Serial2.println("loop()");
loop_entered = true;
}
static bool test_done = false;
if (user_app_runs && !test_done) {
// try to mount default drive
if (f_mount(&FatFs, "", 0) == FR_OK) {
test_fatfs();
test_done = true;
}
}
}
// The functions below can be safely removed
// except for usbh_usr_msc_application()
/*!
\brief user operation for host-mode initialization
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_init(void)
{
Serial2.println("usbh_user_init");
}
/*!
\brief user operation for device attached
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_device_connected(void)
{
Serial2.println("usbh_user_device_connected");
}
/*!
\brief user operation when unrecoveredError happens
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_unrecovered_error(void)
{
Serial2.println("usbh_user_unrecovered_error");
}
/*!
\brief user operation for device disconnect event
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_device_disconnected(void)
{
Serial2.println("usbh_user_device_disconnected");
}
/*!
\brief user operation for reset USB Device
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_device_reset(void)
{
Serial2.println("usbh_user_device_reset");
}
/*!
\brief user operation for detectting device speed
\param[in] device_speed: device speed
\param[out] none
\retval none
*/
void usbh_user_device_speed_detected(uint32_t device_speed)
{
Serial2.println("usbh_user_device_speed_detected");
}
/*!
\brief user operation when device descriptor is available
\param[in] device_desc: device descriptor
\param[out] none
\retval none
*/
void usbh_user_device_desc_available(void *device_desc)
{
Serial2.println("usbh_user_device_desc_available");
}
/*!
\brief usb device is successfully assigned the Address
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_device_address_assigned(void)
{
Serial2.println("usbh_user_device_address_assigned");
}
/*!
\brief user operation when configuration descriptor is available
\param[in] cfg_desc: pointer to configuration descriptor
\param[in] itf_desc: pointer to interface descriptor
\param[in] ep_desc: pointer to endpoint descriptor
\param[out] none
\retval none
*/
void usbh_user_configuration_descavailable(usb_desc_config *cfg_desc,
usb_desc_itf *itf_desc,
usb_desc_ep *ep_desc)
{
Serial2.println("usbh_user_configuration_descavailable");
}
/*!
\brief user operation when manufacturer string exists
\param[in] manufacturer_string: manufacturer string of usb device
\param[out] none
\retval none
*/
void usbh_user_manufacturer_string(void *manufacturer_string)
{
Serial2.print("usbh_user_manufacturer_string: ");
Serial2.println((char *)manufacturer_string);
}
/*!
\brief user operation when product string exists
\param[in] product_string: product string of usb device
\param[out] none
\retval none
*/
void usbh_user_product_string(void *product_string)
{
Serial2.print("usbh_user_product_string: ");
Serial2.println((char *)product_string);
}
/*!
\brief user operatin when serialNum string exists
\param[in] serial_num_string: serialNum string of usb device
\param[out] none
\retval none
*/
void usbh_user_serialnum_string(void *serial_num_string)
{
Serial2.print("usbh_user_serialnum_string: ");
Serial2.println((char *)serial_num_string);
}
/*!
\brief user response request is displayed to ask for application jump to class
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_enumeration_finish(void)
{
Serial2.println("usbh_user_enumeration_finish");
}
/*!
\brief user operation when device is not supported
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_device_not_supported(void)
{
Serial2.println("usbh_user_device_not_supported");
}
/*!
\brief user action for application state entry
\param[in] none
\param[out] none
\retval user response for user key
*/
usbh_user_status usbh_user_userinput(void)
{
Serial2.println("usbh_user_userinput");
return USBH_USER_RESP_OK;
}
/*!
\brief user operation for device overcurrent detection event
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_over_current_detected(void)
{
Serial2.println("usbh_user_over_current_detected");
}
/*!
\brief demo application for mass storage
\param[in] pudev: pointer to device
\param[in] id: no use here
\param[out] none
\retval status
*/
int usbh_usr_msc_application(void)
{
// Serial2.println("usbh_usr_msc_application");
user_app_runs = true;
return 0;
}
/*!
\brief deinit user state and associated variables
\param[in] none
\param[out] none
\retval none
*/
void usbh_user_deinit(void)
{
Serial2.println("usbh_user_deinit");
}