This repository was archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathusbd_cdc_if.c
547 lines (460 loc) · 17.5 KB
/
usbd_cdc_if.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/**
******************************************************************************
* @file : usbd_cdc_if.c
* @brief :
******************************************************************************
* COPYRIGHT(c) 2016 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
#ifdef USBCON
#ifdef USBD_USE_CDC
/* Includes ------------------------------------------------------------------*/
#include "usbd_cdc_if.h"
#include "stm32f4xx_hal.h"
#ifdef __cplusplus
extern "C" {
#endif
/** @addtogroup STM32_USB_OTG_DEVICE_LIBRARY
* @{
*/
/** @defgroup USBD_CDC
* @brief usbd core module
* @{
*/
/** @defgroup USBD_CDC_Private_TypesDefinitions
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CDC_Private_Defines
* @{
*/
/* Define size for the receive and transmit buffer over CDC */
/* It's up to user to redefine and/or remove those define */
/**
* @}
*/
/** @defgroup USBD_CDC_Private_Macros
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CDC_Private_Variables
* @{
*/
/* Create buffer for reception and transmission */
/* It's up to user to redefine and/or remove those define */
extern USBD_HandleTypeDef hUSBD_Device_CDC;
/* Received Data over USB are stored in this buffer */
__IO uint8_t UserRxBufferFS[APP_RX_DATA_SIZE];
__IO uint8_t StackRxBufferFS[APP_RX_DATA_SIZE];
/* Send Data over USB CDC are stored in this buffer */
__IO uint8_t UserTxBufferFS[APP_TX_DATA_SIZE];
__IO uint8_t StackTxBufferFS[APP_TX_DATA_SIZE];
__IO uint32_t UserTxBufPtrIn = 0; /* Increment this pointer or roll it back to
start address when data are received over write call */
__IO uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
start address when data are sent over USB */
__IO uint32_t UserRxBufPtrIn = 0; /* Increment this pointer or roll it back to
start address when data are received over USB */
__IO uint32_t UserRxBufPtrOut = 0; /* Increment this pointer or roll it back to
start address when data are sent over read call */
__IO uint32_t SLP;
__IO uint32_t GetRxCount;
__IO uint32_t lineState = 0;
uint8_t cptlineState = 0;
volatile uint32_t USB_received = 0;
uint8_t USBBuffer[64];
uint8_t USBPackSize;
/* Default configuration: 115200, 8N1 */
uint8_t lineSetup[] = {0x00, 0xc2, 0x01, 0x00, 0x00, 0x00, 0x08};
#define CDC_POLLING_INTERVAL 2 /* in ms. The max is 65 and the min is 1 */
stimer_t TimHandle;
volatile uint8_t dfu_request = 0;
/* For a bug in some Linux 64 bit PC we need to delay the reset of the CPU of 500ms seconds */
int counter_dfu_reset = 250; /* the unit is equal to CDC_POLLING_INTERVAL that is 2ms by default */
/* In order to implement the flow control on USB serial, we need to understand */
/* how many bytes we can recieve or send every timer loop. By default this value is */
/* computing using a baudrate of 115200. The value should be computed again every */
/* time the user change the baudrate by application. */
volatile uint32_t num_bytes_to_send_receive = (((115200 / 8) * CDC_POLLING_INTERVAL) / 1000);
int32_t delta_num_bytes_to_send_receive = 0;
static void TIM_Config(void);
/* USB handler declaration */
/* Handle for USB Full Speed IP */
// USBD_HandleTypeDef *hUsbDevice_0;
/**
* @}
*/
/** @defgroup USBD_CDC_IF_Exported_Variables
* @{
*/
/**
* @}
*/
/** @defgroup USBD_CDC_Private_FunctionPrototypes
* @{
*/
static int8_t CDC_Init_FS (void);
static int8_t CDC_DeInit_FS (void);
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length);
static int8_t CDC_Receive_FS (uint8_t* pbuf, uint32_t *Len);
/**
* @}
*/
USBD_CDC_ItfTypeDef USBD_Interface_fops_FS =
{
CDC_Init_FS,
CDC_DeInit_FS,
CDC_Control_FS,
CDC_Receive_FS
};
/* Private functions ---------------------------------------------------------*/
/**
* @brief CDC_Init_FS
* Initializes the CDC media low layer over the FS USB IP
* @param None
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Init_FS(void)
{
/*##-3- Configure and start the TIM Base generation #################################*/
TIM_Config();
/* Set Application Buffers */
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, UserTxBufferFS, 1);
USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, StackRxBufferFS);
return (USBD_OK);
}
/**
* @brief CDC_DeInit_FS
* DeInitializes the CDC media low layer
* @param None
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_DeInit_FS(void)
{
return (USBD_OK);
}
/**
* @brief CDC_Control_FS
* Manage the CDC class requests
* @param cmd: Command code
* @param pbuf: Buffer containing command data (request parameters)
* @param length: Number of data to be sent (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Control_FS (uint8_t cmd, uint8_t* pbuf, uint16_t length)
{
switch (cmd)
{
case CDC_SEND_ENCAPSULATED_COMMAND:
break;
case CDC_GET_ENCAPSULATED_RESPONSE:
break;
case CDC_SET_COMM_FEATURE:
break;
case CDC_GET_COMM_FEATURE:
break;
case CDC_CLEAR_COMM_FEATURE:
break;
/*******************************************************************************/
/* Line Coding Structure */
/*-----------------------------------------------------------------------------*/
/* Offset | Field | Size | Value | Description */
/* 0 | dwDTERate | 4 | Number |Data terminal rate, in bits per second*/
/* 4 | bCharFormat | 1 | Number | Stop bits */
/* 0 - 1 Stop bit */
/* 1 - 1.5 Stop bits */
/* 2 - 2 Stop bits */
/* 5 | bParityType | 1 | Number | Parity */
/* 0 - None */
/* 1 - Odd */
/* 2 - Even */
/* 3 - Mark */
/* 4 - Space */
/* 6 | bDataBits | 1 | Number Data bits (5, 6, 7, 8 or 16). */
/*******************************************************************************/
case CDC_SET_LINE_CODING:
memcpy(lineSetup, pbuf, 7);
num_bytes_to_send_receive = ((((*((uint32_t*)pbuf)) / 8) * CDC_POLLING_INTERVAL) / 1000);
if(*((uint32_t*)pbuf) == 1200){
dfu_request = 1;
}
break;
case CDC_GET_LINE_CODING:
memcpy(pbuf, lineSetup, 7);
break;
case CDC_SET_CONTROL_LINE_STATE:
cptlineState++;
if(cptlineState == 2)
lineState = 1;
break;
case CDC_SEND_BREAK:
break;
default:
break;
}
return (USBD_OK);
}
/**
* @brief CDC_Receive_FS
* Data received over USB OUT endpoint are sent over CDC interface
* through this function.
*
* @note
* This function will block any OUT packet reception on USB endpoint
* untill exiting this function. If you exit this function before transfer
* is complete on CDC interface (ie. using DMA controller) it will result
* in receiving more data while previous ones are still not sent.
*
* @param Buf: Buffer of data to be received
* @param Len: Number of data received (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
*/
static int8_t CDC_Receive_FS (uint8_t* Buf, uint32_t *Len)
{
/* Initiate next USB packet transfer once a packet is received */
USBPackSize = *Len;
memcpy(USBBuffer, Buf, USBPackSize);
USB_received = 1;
return (USBD_OK);
}
/**
* @brief CDC_Transmit_FS
* Data send over USB IN endpoint are sent over CDC interface
* through this function.
* @note
*
*
* @param Buf: Buffer of data to be send
* @param Len: Number of data to be send (in bytes)
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL or USBD_BUSY
*/
uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len)
{
uint8_t result = USBD_OK;
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, UserTxBufferFS, 1);
result = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
return result;
}
void CDC_flush(void)
{
uint8_t status;
if(UserTxBufPtrOut != UserTxBufPtrIn)
{
if(UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
{
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = UserTxBufPtrIn;
}
}
else
{
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = UserTxBufPtrIn;
}
}
}
}
void CDC_disable_TIM_Interrupt(void)
{
HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn);
}
void CDC_enable_TIM_Interrupt(void)
{
HAL_NVIC_EnableIRQ(TIM6_DAC_IRQn);
}
static void TIM_Config(void)
{
/* Set TIMx instance */
TimHandle.timer = TIM6;
/* Initialize TIM6 peripheral as follow:
+ Period = ((CDC_POLLING_INTERVAL*1000) - 1)
+ Prescaler = ((SystemCoreClock/2)/1000000) - 1
+ ClockDivision = 0
+ Counter direction = Up
*/
TimerHandleInit(&TimHandle, (uint16_t)((CDC_POLLING_INTERVAL*1000) - 1), ((uint32_t)(getTimerClkFreq(TIM6) / (1000000)) - 1)); //CDC_POLLING_INTERVAL
HAL_NVIC_SetPriority(TIM6_DAC_IRQn, 6, 0);
timer_attach_interrupt_handle(&TimHandle.handle, TIM6_PeriodElapsedCallback);
}
void TIM6_PeriodElapsedCallback(stimer_t *htim)
{
uint8_t status;
if(dfu_request)
{
counter_dfu_reset--;
if(counter_dfu_reset == 0)
{
dfu_request = 0;
/* Set magic number in SRAM so application enters DFU after reset */
*((unsigned int*)0x2004FFF0) = 0xb311a21a;
NVIC_SystemReset();
}
}
if(USB_received)
{
USB_received = 0;
if((USBPackSize > 0))
{
if(UserRxBufPtrIn + USBPackSize > APP_RX_DATA_SIZE)
{
memcpy(&UserRxBufferFS[UserRxBufPtrIn], &USBBuffer[0], (APP_RX_DATA_SIZE - UserRxBufPtrIn));
memcpy(&UserRxBufferFS[0], &USBBuffer[(APP_RX_DATA_SIZE - UserRxBufPtrIn)], (USBPackSize - (APP_RX_DATA_SIZE - UserRxBufPtrIn)));
UserRxBufPtrIn = ((UserRxBufPtrIn + USBPackSize) % APP_RX_DATA_SIZE);
}
else
{
memcpy(&UserRxBufferFS[UserRxBufPtrIn], USBBuffer, USBPackSize);
UserRxBufPtrIn = ((UserRxBufPtrIn + USBPackSize) % APP_RX_DATA_SIZE);
}
delta_num_bytes_to_send_receive += USBPackSize;
}
delta_num_bytes_to_send_receive -= num_bytes_to_send_receive;
if(delta_num_bytes_to_send_receive <= 0)
{
delta_num_bytes_to_send_receive = 0;
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
}
}
else
{
if (delta_num_bytes_to_send_receive > 0)
{
delta_num_bytes_to_send_receive -= num_bytes_to_send_receive;
if(delta_num_bytes_to_send_receive <= 0)
{
delta_num_bytes_to_send_receive = 0;
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
}
}
}
if(UserTxBufPtrOut != UserTxBufPtrIn)
{
if(UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
{
if((APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn) < num_bytes_to_send_receive)
{
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], UserTxBufPtrIn);
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = UserTxBufPtrIn;
}
}
else
{
if((APP_TX_DATA_SIZE - UserTxBufPtrOut) < num_bytes_to_send_receive)
{
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (APP_TX_DATA_SIZE - UserTxBufPtrOut));
memcpy((uint8_t*)&StackTxBufferFS[APP_TX_DATA_SIZE - UserTxBufPtrOut], (uint8_t*)&UserTxBufferFS[0], (num_bytes_to_send_receive + UserTxBufPtrOut - APP_TX_DATA_SIZE));
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], num_bytes_to_send_receive);
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = (UserTxBufPtrOut + num_bytes_to_send_receive) % APP_TX_DATA_SIZE;
}
}
else
{
memcpy((uint8_t*)&StackTxBufferFS[0], (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], num_bytes_to_send_receive);
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&StackTxBufferFS[0], num_bytes_to_send_receive);
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = (UserTxBufPtrOut + num_bytes_to_send_receive) % APP_TX_DATA_SIZE;
}
}
}
}
else
{
if((UserTxBufPtrIn - UserTxBufPtrOut) < num_bytes_to_send_receive)
{
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], (UserTxBufPtrIn - UserTxBufPtrOut));
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = UserTxBufPtrIn;
}
}
else
{
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t*)&UserTxBufferFS[UserTxBufPtrOut], num_bytes_to_send_receive);
do
{
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
} while(status == USBD_BUSY && !(((&hUSBD_Device_CDC)->dev_state != USBD_STATE_CONFIGURED) || ((&hUSBD_Device_CDC)->ep0_state == USBD_EP0_STATUS_IN)));
if(status == USBD_OK)
{
UserTxBufPtrOut = (UserTxBufPtrOut + num_bytes_to_send_receive) % APP_TX_DATA_SIZE;
}
}
}
}
}
/**
* @}
*/
/**
* @}
*/
#ifdef __cplusplus
}
#endif
#endif // USBD_USE_CDC
#endif // USBCON
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/