Select one of the symbols to view example projects that use it.
 
Outline
Includes
#include "main.h"
Private define
#define TX_BUFF_SIZE
Private typedef
DEMO_SEND_menu
BLANK_LINE
Private variables
MyFile
FilePos
FileOffset
PrevPos
CDC_TX_Buffer
EnableSendData
use_file
CDC_Handle_Send_Menu()
CDC_SendFile_ProbeKey(JOYState_TypeDef)
SendFileMenu_Init()
ReturnFromSendMenu()
CDC_SendFile(uint8_t)
CDC_ShowFiles(uint8_t, uint8_t)
USBH_CDC_TransmitCallback(USBH_HandleTypeDef *)
Files
loading...
CodeScopeSTM32 Libraries and SamplesCDC_StandaloneSrc/cdc_send.c
 
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/** ****************************************************************************** * @file USB_Host/CDC_Standalone/Src/cdc_send.c * @author MCD Application Team * @brief CDC Send state machine ****************************************************************************** * @attention * * Copyright (c) 2017 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** *//* ... */ /* Includes ------------------------------------------------------------------ */ #include "main.h" Includes /* Private define ------------------------------------------------------------ */ /* The size of the buffer depends on the user's needs */ #define TX_BUFF_SIZE (64 * 1024) Private define /* Private typedef ----------------------------------------------------------- */ uint8_t *DEMO_SEND_menu[] = { (uint8_t *) " 1 - Send Dummy Data ", (uint8_t *) " 2 - Send File ", (uint8_t *) " 3 - Return ", (uint8_t *) " ", ...}; uint8_t BLANK_LINE[] = " "; Private typedef /* Private macro ------------------------------------------------------------- */ /* Private variables --------------------------------------------------------- */ FIL MyFile = { 0 }; uint8_t FilePos = 0; uint8_t FileOffset = 0; uint8_t PrevPos = 0; uint8_t CDC_TX_Buffer[TX_BUFF_SIZE]; uint32_t EnableSendData = 0; uint32_t use_file = 0; Private variables /* Private function prototypes ----------------------------------------------- */ /* Private functions --------------------------------------------------------- */ static void CDC_SendFile(uint8_t fileidx); static void ReturnFromSendMenu(void); static void CDC_ShowFiles(uint8_t offset, uint8_t select); static void SendFileMenu_Init(void); /** * @brief Handles CDC Send Menu. * @param None * @retval None *//* ... */ void CDC_Handle_Send_Menu(void) { if (CdcSelectMode == CDC_SELECT_MENU) { switch (CdcDemo.Send_state) { case CDC_SEND_IDLE: CdcDemo.Send_state = CDC_SEND_WAIT; CDC_SelectItem(DEMO_SEND_menu, 0); CdcDemo.select = 0; PrevPos = 0; USBH_CDC_Stop(&hUSBHost); break; case CDC_SEND_IDLE: case CDC_SEND_WAIT: if (CdcDemo.select != PrevSelect) { PrevSelect = CdcDemo.select; CDC_SelectItem(DEMO_SEND_menu, CdcDemo.select & 0x7F); /* Handle select item */ if (CdcDemo.select & 0x80) { switch (CdcDemo.select & 0x7F) { case 0: memset(CDC_TX_Buffer, 0x5A, TX_BUFF_SIZE); LCD_DbgLog("Sending data ...\n"); USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, TX_BUFF_SIZE); use_file = 0; break; case 0: case 1: if (FileList.ptr > 0) { SendFileMenu_Init(); if (FilePos >= 9) { CDC_ShowFiles(FileOffset, 9); }if (FilePos >= 9) { ... } else { CDC_ShowFiles(FileOffset, FilePos); }else { ... } CdcDemo.Send_state = CDC_SEND_SELECT_FILE; }if (FileList.ptr > 0) { ... } else { LCD_ErrLog("No file on the microSD\n"); }else { ... } break; case 1: case 2: /* Return */ ReturnFromSendMenu(); break;case 2: default: break;default }switch (CdcDemo.select & 0x7F) { ... } }if (CdcDemo.select & 0x80) { ... } }if (CdcDemo.select != PrevSelect) { ... } break; case CDC_SEND_WAIT: default: break;default }switch (CdcDemo.Send_state) { ... } }if (CdcSelectMode == CDC_SELECT_MENU) { ... } else if (CdcSelectMode == CDC_SELECT_FILE) { switch (CdcDemo.Send_state) { case CDC_SEND_SELECT_FILE: if (CdcDemo.select & 0x80) { CdcDemo.select &= 0x7F; CdcDemo.Send_state = CDC_SEND_TRANSMIT_FILE; }if (CdcDemo.select & 0x80) { ... } break; case CDC_SEND_SELECT_FILE: case CDC_SEND_TRANSMIT_FILE: LCD_DbgLog("Sending file ...\n"); use_file = 1; CDC_SendFile(FilePos); CDC_ChangeSelectMode(CDC_SELECT_MENU); LCD_LOG_UpdateDisplay(); CdcDemo.Send_state = CDC_SEND_WAIT; break; case CDC_SEND_TRANSMIT_FILE: default: break;default }switch (CdcDemo.Send_state) { ... } if (FilePos != PrevPos) { if (((FilePos > 9) && (FilePos > PrevPos)) || ((FilePos >= 9) && (FilePos < PrevPos))) { if (FilePos > PrevPos) { FileOffset++; }if (FilePos > PrevPos) { ... } else { FileOffset--; }else { ... } CDC_ShowFiles(FileOffset, 9); }if (((FilePos > 9) && (FilePos > PrevPos)) || ((FilePos >= 9) && (FilePos < PrevPos))) { ... } else { CDC_ShowFiles(0, FilePos); }else { ... } PrevPos = FilePos; }if (FilePos != PrevPos) { ... } }else if (CdcSelectMode == CDC_SELECT_FILE) { ... } CdcDemo.select &= 0x7F; }{ ... } /** * @brief Probes the CDC joystick state. * @param state: Joystick state * @retval None *//* ... */ void CDC_SendFile_ProbeKey(JOYState_TypeDef state) { /* Handle File List Selection */ if ((state == JOY_UP) && (FilePos > 0)) { FilePos--; }if ((state == JOY_UP) && (FilePos > 0)) { ... } else if ((state == JOY_DOWN) && (FilePos < (FileList.ptr - 1))) { FilePos++; }else if ((state == JOY_DOWN) && (FilePos < (FileList.ptr - 1))) { ... } else if (state == JOY_SEL) { CdcDemo.select |= 0x80; }else if (state == JOY_SEL) { ... } }{ ... } /** * @brief Demo_Application * Demo state machine * @param None * @retval None *//* ... */ static void SendFileMenu_Init(void) { BSP_LCD_SetTextColor(LCD_COLOR_GREEN); BSP_LCD_DisplayStringAtLine(14, (uint8_t *) " "); BSP_LCD_DisplayStringAtLine(15, (uint8_t *) "[User Key] to switch menu "); BSP_LCD_DisplayStringAtLine(16, (uint8_t *) "[Joystick Up/Down] to change settings items"); CDC_ChangeSelectMode(CDC_SELECT_FILE); }{ ... } /** * @brief Returns from Send Menu * @param None * @retval None *//* ... */ static void ReturnFromSendMenu(void) { CdcDemo.state = CDC_DEMO_IDLE; CdcDemo.select = 0; /* Restore main menu */ LCD_ClearTextZone(); LCD_LOG_UpdateDisplay(); Menu_Init(); }{ ... } /** * @brief Sends file to the root. * @param fileidx: File index * @retval None *//* ... */ static void CDC_SendFile(uint8_t fileidx) { uint32_t bytesread; if (FileList.ptr > 0) { /* Close any opened file */ f_close(&MyFile); if (f_open (&MyFile, (char *)FileList.file[fileidx].name, FA_OPEN_EXISTING | FA_READ) == FR_OK) { /* Fill the buffer to Send */ f_read(&MyFile, CDC_TX_Buffer, TX_BUFF_SIZE, (void *)&bytesread); /* Send File */ USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, bytesread); }if (f_open (&MyFile, (char *)FileList.file[fileidx].name, FA_OPEN_EXISTING | FA_READ) == FR_OK) { ... } else { LCD_ErrLog("Cannot open %s file\n", FileList.file[0].name); }else { ... } }if (FileList.ptr > 0) { ... } else { LCD_ErrLog("No file on the microSD\n"); }else { ... } }{ ... } /** * @brief Shows files on the root. * @param None * @retval None *//* ... */ static void CDC_ShowFiles(uint8_t offset, uint8_t select) { uint8_t i = 0; if (offset < FileList.ptr) { BSP_LCD_SetTextColor(LCD_COLOR_WHITE); for (i = 4; i < 14; i++) { BSP_LCD_SetBackColor(LCD_COLOR_BLACK); BSP_LCD_DisplayStringAtLine(i, BLANK_LINE); if ((i - 4) < FileList.ptr - offset) { if (i == (select + 4)) { BSP_LCD_SetBackColor(LCD_COLOR_MAGENTA); }if (i == (select + 4)) { ... } BSP_LCD_DisplayStringAtLine(i, FileList.file[i - 4 + offset].name); }if ((i - 4) < FileList.ptr - offset) { ... } }for (i = 4; i < 14; i++) { ... } }if (offset < FileList.ptr) { ... } }{ ... } /** * @brief CDC data transmit callback. * @param phost: Host handle * @retval None *//* ... */ void USBH_CDC_TransmitCallback(USBH_HandleTypeDef * phost) { uint32_t bytesread; if (use_file == 1) { if (MyFile.fptr >= f_size(& MyFile)) { f_close(&MyFile); LCD_DbgLog(">> File sent\n"); use_file = 0; }if (MyFile.fptr >= f_size(& MyFile)) { ... } else { /* Fill the buffer to Send */ f_read(&MyFile, CDC_TX_Buffer, TX_BUFF_SIZE, (void *)&bytesread); /* Send File */ USBH_CDC_Transmit(&hUSBHost, CDC_TX_Buffer, bytesread); }else { ... } }if (use_file == 1) { ... } else { LCD_DbgLog(">> Data sent\n"); }else { ... } }{ ... }