Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 8702e06

Browse files
makarenyafpistm
authored andcommittedFeb 12, 2019
[USB CDC] Remove timer and buffer usage
Signed-off-by: Alexey Makarenya <[email protected]>
1 parent 1e1f14d commit 8702e06

File tree

7 files changed

+138
-292
lines changed

7 files changed

+138
-292
lines changed
 

‎cores/arduino/USBSerial.cpp

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -27,123 +27,97 @@
2727
#define USB_TIMEOUT 50
2828
/* USB Device Core handle declaration */
2929
extern USBD_HandleTypeDef hUSBD_Device_CDC;
30-
extern __IO uint32_t device_connection_status;
3130
extern __IO uint32_t lineState;
32-
extern __IO uint8_t UserTxBuffer[APP_TX_DATA_SIZE];
33-
extern __IO uint8_t UserRxBuffer[APP_RX_DATA_SIZE];
34-
extern __IO uint32_t UserTxBufPtrIn;
35-
extern __IO uint32_t UserTxBufPtrOut;
36-
extern __IO uint32_t UserRxBufPtrIn;
37-
extern __IO uint32_t UserRxBufPtrOut;
3831

3932
USBSerial SerialUSB;
4033
void serialEventUSB() __attribute__((weak));
4134

35+
void USBSerial::begin(void) {
36+
CDC_init();
37+
}
38+
4239
void USBSerial::begin(uint32_t /* baud_count */) {
4340
// uart config is ignored in USB-CDC
41+
begin();
4442
}
4543

4644
void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) {
4745
// uart config is ignored in USB-CDC
46+
begin();
4847
}
4948

50-
void USBSerial::end(void) {
51-
52-
USBD_LL_DeInit(&hUSBD_Device_CDC);
49+
void USBSerial::end() {
50+
CDC_deInit();
5351
}
5452

55-
int USBSerial::availableForWrite(void)
53+
int USBSerial::availableForWrite()
5654
{
57-
int ret_val;
58-
59-
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
60-
/* value that we read is correct, we need to disable TIM Interrupt. */
61-
CDC_disable_TIM_Interrupt();
62-
63-
if (UserTxBufPtrIn >= UserTxBufPtrOut) {
64-
ret_val = (APP_TX_DATA_SIZE - 1 - UserTxBufPtrIn + UserTxBufPtrOut);
65-
} else {
66-
ret_val = (UserTxBufPtrOut - UserTxBufPtrIn - 1);
67-
}
68-
69-
CDC_enable_TIM_Interrupt();
70-
71-
return ret_val;
55+
// Just transmit queue size, available for write
56+
return static_cast<int>(CDC_TransmitQueue_WriteSize(&TransmitQueue));
7257
}
7358

7459
size_t USBSerial::write(uint8_t ch) {
75-
76-
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
77-
/* value that we read is correct, we need to disable TIM Interrupt. */
78-
CDC_disable_TIM_Interrupt();
79-
80-
if (((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE) == UserTxBufPtrOut) {
81-
// Buffer full!!! Force a flush to not loose data and go on
82-
CDC_flush();
83-
}
84-
UserTxBuffer[UserTxBufPtrIn] = ch;
85-
UserTxBufPtrIn = ((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE);
86-
87-
CDC_enable_TIM_Interrupt();
88-
89-
return 1;
60+
// Just write single-byte buffer.
61+
return write(&ch, 1);
9062
}
9163

9264
size_t USBSerial::write(const uint8_t *buffer, size_t size){
93-
size_t i = 0;
94-
for (i=0; i < size; i++) {
95-
if (write(buffer[i]) != 1) {
96-
break;
97-
}
65+
size_t rest = size;
66+
while(rest > 0) {
67+
// Determine buffer size available for write
68+
auto portion = (size_t)CDC_TransmitQueue_WriteSize(&TransmitQueue);
69+
// Truncate it to content size (if rest is greater)
70+
if (rest < portion) {
71+
portion = rest;
72+
}
73+
if (portion > 0) {
74+
// Only if some space in the buffer exists.
75+
// TS: Only main thread calls write and writeSize methods,
76+
// it's thread-safe since IRQ does not affects
77+
// TransmitQueue write position
78+
CDC_TransmitQueue_Enqueue(&TransmitQueue, buffer, portion);
79+
rest -= portion;
80+
buffer += portion;
81+
// After storing data, start transmitting process
82+
CDC_continue_transmit();
83+
}
9884
}
99-
return i;
85+
return size;
10086
}
10187

10288
int USBSerial::available(void) {
103-
return ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
89+
// Just ReceiveQueue size, available for reading
90+
return static_cast<int>(CDC_ReceiveQueue_ReadSize(&ReceiveQueue));
10491
}
10592

10693
int USBSerial::read(void) {
107-
if (UserRxBufPtrOut == UserRxBufPtrIn) {
94+
// Empty ReceiveQueue - nothing to return
95+
if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) {
10896
return -1;
109-
} else {
110-
unsigned char c = UserRxBuffer[UserRxBufPtrOut];
111-
UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE);
112-
CDC_resume_receive();
113-
return c;
11497
}
98+
// Dequeue only one char from queue
99+
// TS: it safe, because only main thread affects ReceiveQueue->read pos
100+
char ch = CDC_ReceiveQueue_Dequeue(&ReceiveQueue);
101+
// Resume receive process, if possible
102+
CDC_resume_receive();
103+
return ch;
115104
}
116105

117106
int USBSerial::peek(void)
118107
{
119-
if (UserRxBufPtrOut == UserRxBufPtrIn) {
108+
// Empty ReceiveQueue - nothing to return
109+
if (CDC_ReceiveQueue_ReadSize(&ReceiveQueue) <= 0) {
120110
return -1;
121-
} else {
122-
unsigned char c = UserRxBuffer[UserRxBufPtrOut];
123-
return c;
124111
}
112+
// Peek one symbol, it can't change receive avaiablity
113+
return CDC_ReceiveQueue_Peek(&ReceiveQueue);
125114
}
126115

127116
void USBSerial::flush(void)
128117
{
129-
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
130-
/* value that we read is correct, we need to disable TIM Interrupt. */
131-
CDC_disable_TIM_Interrupt();
132-
CDC_flush();
133-
CDC_enable_TIM_Interrupt();
134-
}
135-
136-
uint8_t USBSerial::pending(void) {
137-
return 0;
138-
}
139-
140-
uint8_t USBSerial::isConnected(void) {
141-
142-
if (device_connection_status == 1) {
143-
return 1;
144-
} else {
145-
return 0;
146-
}
118+
// Wait for TransmitQueue read size becomes zero
119+
// TS: safe, because it not be stopped while receive 0
120+
while(CDC_TransmitQueue_ReadSize(&TransmitQueue) > 0) {}
147121
}
148122

149123
uint32_t USBSerial::baud() {

‎cores/arduino/USBSerial.h

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
// Serial over CDC
2828
class USBSerial : public Stream {
2929
public:
30-
USBSerial(void) {};
31-
30+
void begin(void);
3231
void begin(uint32_t);
3332
void begin(uint32_t, uint8_t);
3433
void end(void);
@@ -64,9 +63,6 @@ class USBSerial : public Stream {
6463
MARK_PARITY = 3,
6564
SPACE_PARITY = 4,
6665
};
67-
68-
uint8_t isConnected();
69-
uint8_t pending();
7066
};
7167

7268
extern USBSerial SerialUSB;

‎cores/arduino/stm32/usb/cdc/usbd_cdc.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -678,6 +678,7 @@ static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
678678
{
679679
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef*)pdev->pClassData;
680680
PCD_HandleTypeDef *hpcd = pdev->pData;
681+
USBD_CDC_ItfTypeDef* ctrl = (USBD_CDC_ItfTypeDef *)pdev->pUserData;
681682

682683
if(pdev->pClassData != NULL)
683684
{
@@ -692,6 +693,10 @@ static uint8_t USBD_CDC_DataIn (USBD_HandleTypeDef *pdev, uint8_t epnum)
692693
else
693694
{
694695
hcdc->TxState = 0U;
696+
if (ctrl->Transferred)
697+
{
698+
ctrl->Transferred();
699+
}
695700
}
696701
return USBD_OK;
697702
}

‎cores/arduino/stm32/usb/cdc/usbd_cdc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ typedef struct _USBD_CDC_Itf
104104
int8_t (* DeInit) (void);
105105
int8_t (* Control) (uint8_t cmd, uint8_t* pbuf, uint16_t length);
106106
int8_t (* Receive) (uint8_t* Buf, uint32_t *Len);
107+
int8_t (* Transferred) (void);
107108

108109
}USBD_CDC_ItfTypeDef;
109110

‎cores/arduino/stm32/usb/cdc/usbd_cdc_if.c

Lines changed: 74 additions & 167 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#ifdef USBD_USE_CDC
2222

2323
/* Includes ------------------------------------------------------------------*/
24+
#include "usbd_desc.h"
2425
#include "usbd_cdc_if.h"
2526

2627
#ifdef USE_USB_HS
@@ -32,49 +33,31 @@
3233
#endif
3334

3435
/* USBD_CDC Private Variables */
36+
/* USB Device Core CDC handle declaration */
37+
USBD_HandleTypeDef hUSBD_Device_CDC;
3538

36-
/* Create buffer for reception and transmission */
37-
/* It's up to user to redefine and/or remove those define */
38-
extern USBD_HandleTypeDef hUSBD_Device_CDC;
3939
/* Received Data over USB are stored in this buffer */
40-
__IO uint8_t UserRxBuffer[APP_RX_DATA_SIZE];
41-
__IO uint8_t StackRxBuffer[CDC_MAX_PACKET_SIZE];
42-
43-
/* Send Data over USB CDC are stored in this buffer */
44-
__IO uint8_t UserTxBuffer[APP_TX_DATA_SIZE];
45-
__IO uint8_t StackTxBuffer[APP_TX_DATA_SIZE];
46-
47-
__IO uint32_t UserTxBufPtrIn = 0; /* Increment this pointer or roll it back to
48-
start address when data are received over write call */
49-
__IO uint32_t UserTxBufPtrOut = 0; /* Increment this pointer or roll it back to
50-
start address when data are sent over USB */
51-
52-
__IO uint32_t UserRxBufPtrIn = 0; /* Increment this pointer or roll it back to
53-
start address when data are received over USB */
54-
__IO uint32_t UserRxBufPtrOut = 0; /* Increment this pointer or roll it back to
55-
start address when data are sent over read call */
56-
40+
CDC_TransmitQueue_TypeDef TransmitQueue;
41+
CDC_ReceiveQueue_TypeDef ReceiveQueue;
5742
__IO uint32_t lineState = 0;
58-
__IO bool receiveSuspended = false;
59-
__IO bool sendZLP = false;
43+
__IO bool receivePended = false;
6044

61-
stimer_t CDC_TimHandle;
6245

6346
/** USBD_CDC Private Function Prototypes */
6447

6548
static int8_t USBD_CDC_Init (void);
6649
static int8_t USBD_CDC_DeInit (void);
6750
static int8_t USBD_CDC_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length);
6851
static int8_t USBD_CDC_Receive (uint8_t* pbuf, uint32_t *Len);
69-
static void CDC_TIM_Config(void);
70-
void CDC_TIM_PeriodElapsedCallback(stimer_t *htim);
52+
static int8_t USBD_CDC_Transferred (void);
7153

7254
USBD_CDC_ItfTypeDef USBD_CDC_fops =
7355
{
7456
USBD_CDC_Init,
7557
USBD_CDC_DeInit,
7658
USBD_CDC_Control,
77-
USBD_CDC_Receive
59+
USBD_CDC_Receive,
60+
USBD_CDC_Transferred
7861
};
7962

8063
USBD_CDC_LineCodingTypeDef linecoding =
@@ -95,12 +78,11 @@ USBD_CDC_LineCodingTypeDef linecoding =
9578
*/
9679
static int8_t USBD_CDC_Init(void)
9780
{
98-
/* Configure and start the TIM Base generation */
99-
CDC_TIM_Config();
100-
10181
/* Set Application Buffers */
102-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t *)UserTxBuffer, 1);
103-
USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, (uint8_t *)StackRxBuffer);
82+
CDC_TransmitQueue_Init(&TransmitQueue);
83+
CDC_ReceiveQueue_Init(&ReceiveQueue);
84+
receivePended = true;
85+
USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue));
10486

10587
return (USBD_OK);
10688
}
@@ -217,155 +199,80 @@ static int8_t USBD_CDC_Control (uint8_t cmd, uint8_t* pbuf, uint16_t length)
217199
* @param Len: Number of data received (in bytes)
218200
* @retval Result of the operation: USBD_OK if all operations are OK else USBD_FAIL
219201
*/
220-
static int8_t USBD_CDC_Receive (uint8_t* Buf, uint32_t *Len)
221-
{
222-
uint32_t packetSize = *Len;
223-
224-
if (packetSize > 0) {
225-
if (UserRxBufPtrIn + packetSize > APP_RX_DATA_SIZE) {
226-
memcpy(((uint8_t *)UserRxBuffer + UserRxBufPtrIn), &Buf[0],
227-
(APP_RX_DATA_SIZE - UserRxBufPtrIn));
228-
memcpy((uint8_t *)UserRxBuffer,
229-
&Buf[(APP_RX_DATA_SIZE - UserRxBufPtrIn)],
230-
(packetSize - (APP_RX_DATA_SIZE - UserRxBufPtrIn)));
231-
UserRxBufPtrIn = ((UserRxBufPtrIn + packetSize) % APP_RX_DATA_SIZE);
232-
} else {
233-
memcpy(((uint8_t *)UserRxBuffer + UserRxBufPtrIn), Buf, packetSize);
234-
UserRxBufPtrIn = ((UserRxBufPtrIn + packetSize) % APP_RX_DATA_SIZE);
235-
}
236-
}
237-
238-
if ((UserRxBufPtrOut + APP_RX_DATA_SIZE - UserRxBufPtrIn - 1) %
239-
APP_RX_DATA_SIZE + 1 >= CDC_MAX_PACKET_SIZE) {
240-
USBD_CDC_ReceivePacket(
241-
&hUSBD_Device_CDC); // Initiate next USB packet transfer once a packet
242-
// is received and there is enouch space in the
243-
// buffer
244-
} else {
245-
receiveSuspended = true;
246-
}
247-
return (USBD_OK);
202+
static int8_t USBD_CDC_Receive (uint8_t* Buf, uint32_t *Len) {
203+
UNUSED(Buf);
204+
/* It always contains required amount of free space for writing */
205+
CDC_ReceiveQueue_CommitBlock(&ReceiveQueue, (uint16_t)(*Len));
206+
receivePended = false;
207+
/* If enough space in the queue for a full buffer then continue receive */
208+
CDC_resume_receive();
209+
return USBD_OK;
248210
}
249211

250-
void CDC_flush(void)
251-
{
252-
uint8_t status;
253-
254-
if(UserTxBufPtrOut != UserTxBufPtrIn)
255-
{
256-
if(UserTxBufPtrOut > UserTxBufPtrIn) /* Roll-back */
257-
{
258-
memcpy((uint8_t *)&StackTxBuffer[0],
259-
(uint8_t *)&UserTxBuffer[UserTxBufPtrOut],
260-
(APP_TX_DATA_SIZE - UserTxBufPtrOut));
261-
memcpy((uint8_t *)&StackTxBuffer[APP_TX_DATA_SIZE - UserTxBufPtrOut],
262-
(uint8_t *)&UserTxBuffer[0], UserTxBufPtrIn);
263-
264-
USBD_CDC_SetTxBuffer(
265-
&hUSBD_Device_CDC, (uint8_t *)&StackTxBuffer[0],
266-
(APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn));
267-
} else {
268-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC,
269-
(uint8_t *)&UserTxBuffer[UserTxBufPtrOut],
270-
(UserTxBufPtrIn - UserTxBufPtrOut));
271-
}
272-
273-
do {
274-
if (lineState == 0) { // Device disconnected
275-
status = USBD_OK;
276-
} else {
277-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
278-
}
279-
} while (status == USBD_BUSY);
280212

281-
if (status == USBD_OK) {
282-
UserTxBufPtrOut = UserTxBufPtrIn;
283-
}
284-
}
213+
static int8_t USBD_CDC_Transferred (void) {
214+
CDC_continue_transmit();
215+
return (USBD_OK);
285216
}
286217

287-
void CDC_resume_receive(void) {
288-
if (receiveSuspended) {
289-
if ((UserRxBufPtrOut + APP_RX_DATA_SIZE - UserRxBufPtrIn - 1) %
290-
APP_RX_DATA_SIZE + 1 >= CDC_MAX_PACKET_SIZE) {
291-
USBD_CDC_ReceivePacket(
292-
&hUSBD_Device_CDC); // Initiate next USB packet transfer once a packet
293-
// is received and there is enouch space in the
294-
// buffer
295-
receiveSuspended = false;
218+
void CDC_init(void) {
219+
/* Init Device Library */
220+
if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) {
221+
/* Add Supported Class */
222+
if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) {
223+
/* Add CDC Interface Class */
224+
if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == USBD_OK) {
225+
/* Start Device Process */
226+
USBD_Start(&hUSBD_Device_CDC);
227+
}
296228
}
297229
}
298230
}
299231

300-
void CDC_disable_TIM_Interrupt(void)
301-
{
302-
HAL_NVIC_DisableIRQ(CDC_TIM_IRQn);
303-
}
304-
305-
void CDC_enable_TIM_Interrupt(void)
306-
{
307-
HAL_NVIC_EnableIRQ(CDC_TIM_IRQn);
232+
void CDC_deInit(void) {
233+
USBD_Stop(&hUSBD_Device_CDC);
234+
USBD_CDC_DeInit();
235+
USBD_DeInit(&hUSBD_Device_CDC);
308236
}
309237

310-
static void CDC_TIM_Config(void)
311-
{
312-
/* Set TIMx instance */
313-
CDC_TimHandle.timer = CDC_TIM;
314-
/* Initialize CDC_TIM peripheral as follow:
315-
+ Period = 10000 - 1
316-
+ Prescaler = ((SystemCoreClock/2)/10000) - 1
317-
+ ClockDivision = 0
318-
+ Counter direction = Up
319-
*/
320-
TimerHandleInit(&CDC_TimHandle, (uint16_t)((CDC_POLLING_INTERVAL*1000) - 1), ((uint32_t)(getTimerClkFreq(CDC_TIM) / (1000000)) - 1));
321-
HAL_NVIC_SetPriority(CDC_TIM_IRQn, 6, 0);
322-
323-
attachIntHandle(&CDC_TimHandle, CDC_TIM_PeriodElapsedCallback);
324-
}
325-
326-
void CDC_TIM_PeriodElapsedCallback(stimer_t *htim)
327-
{
328-
UNUSED(htim);
329-
if (UserTxBufPtrOut == UserTxBufPtrIn &&
330-
sendZLP == false) // Nothing to do, return immediately
331-
return;
332-
333-
uint8_t status;
334-
uint16_t packetLength;
335-
336-
if (UserTxBufPtrOut > UserTxBufPtrIn) { /* Roll-back */
337-
memcpy((uint8_t *)&StackTxBuffer[0],
338-
(uint8_t *)&UserTxBuffer[UserTxBufPtrOut],
339-
(APP_TX_DATA_SIZE - UserTxBufPtrOut));
340-
memcpy((uint8_t *)&StackTxBuffer[APP_TX_DATA_SIZE - UserTxBufPtrOut],
341-
(uint8_t *)&UserTxBuffer[0], UserTxBufPtrIn);
342-
343-
packetLength = (APP_TX_DATA_SIZE - UserTxBufPtrOut + UserTxBufPtrIn);
344-
345-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, (uint8_t *)&StackTxBuffer[0],
346-
packetLength);
347-
} else if (UserTxBufPtrOut != UserTxBufPtrIn) {
348-
packetLength = (UserTxBufPtrIn - UserTxBufPtrOut);
349-
350-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC,
351-
(uint8_t *)&UserTxBuffer[UserTxBufPtrOut],
352-
packetLength);
353-
} else {
354-
packetLength = 0;
355-
356-
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, NULL, 0); // Send Zero Length Packet
357-
}
358-
359-
if (lineState == 0) { // Device disconnected
360-
status = USBD_OK;
361-
} else {
362-
status = USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
238+
void CDC_continue_transmit(void) {
239+
uint16_t size;
240+
uint8_t *buffer;
241+
USBD_CDC_HandleTypeDef *hcdc = (USBD_CDC_HandleTypeDef *) hUSBD_Device_CDC.pClassData;
242+
/*
243+
* TS: This method can be called both in the main thread
244+
* (via USBSerial::write) and in the IRQ stream (via USBD_CDC_Transferred),
245+
* BUT the main thread cannot pass this condition while waiting for a IRQ!
246+
* This is not possible because TxState is not zero while waiting for data
247+
* transfer ending! The IRQ thread is uninterrupted, since its priority
248+
* is higher than that of the main thread. So this method is thread safe.
249+
*/
250+
if (hcdc->TxState == 0U) {
251+
buffer = CDC_TransmitQueue_ReadBlock(&TransmitQueue, &size);
252+
if (size > 0) {
253+
USBD_CDC_SetTxBuffer(&hUSBD_Device_CDC, buffer, size);
254+
/*
255+
* size never exceed PMA buffer and USBD_CDC_TransmitPacket make full
256+
* copy of block in PMA, so no need to worry about buffer damage
257+
*/
258+
USBD_CDC_TransmitPacket(&hUSBD_Device_CDC);
259+
}
363260
}
261+
}
364262

365-
if (status == USBD_OK) {
366-
UserTxBufPtrOut = UserTxBufPtrIn;
367-
368-
sendZLP = packetLength % CDC_MAX_PACKET_SIZE == 0;
263+
void CDC_resume_receive(void) {
264+
/*
265+
* TS: main and IRQ threads can't pass it at same time, because
266+
* IRQ may occur only if receivePended is true. So it is thread-safe!
267+
*/
268+
if (!receivePended) {
269+
uint8_t* block = CDC_ReceiveQueue_ReserveBlock(&ReceiveQueue);
270+
if (block != NULL) {
271+
receivePended = true;
272+
/* Set new buffer */
273+
USBD_CDC_SetRxBuffer(&hUSBD_Device_CDC, block);
274+
USBD_CDC_ReceivePacket(&hUSBD_Device_CDC);
275+
}
369276
}
370277
}
371278

‎cores/arduino/stm32/usb/cdc/usbd_cdc_if.h

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,7 @@
3030

3131
/* Includes ------------------------------------------------------------------*/
3232
#include "usbd_cdc.h"
33-
#include "timer.h"
34-
35-
#ifndef APP_RX_DATA_SIZE
36-
#define APP_RX_DATA_SIZE 2048
37-
#endif
38-
#ifndef APP_TX_DATA_SIZE
39-
#define APP_TX_DATA_SIZE 2048
40-
#endif
41-
42-
#ifndef CDC_TIM
43-
#ifdef TIM6
44-
#define CDC_TIM TIM6
45-
#else
46-
#define CDC_TIM TIM4
47-
#endif
48-
#endif
49-
#ifndef CDC_TIM_IRQn
50-
#ifdef TIM6
51-
#define CDC_TIM_IRQn TIM6_IRQn
52-
#else
53-
#define CDC_TIM_IRQn TIM4_IRQn
54-
#endif
55-
#endif
33+
#include "cdc_queue.h"
5634

5735
/* Periodically, the state of the buffer "UserTxBuffer" is checked.
5836
The period depends on CDC_POLLING_INTERVAL */
@@ -62,13 +40,16 @@
6240
/* Exported constants --------------------------------------------------------*/
6341

6442
extern USBD_CDC_ItfTypeDef USBD_CDC_fops;
43+
extern CDC_TransmitQueue_TypeDef TransmitQueue;
44+
extern CDC_ReceiveQueue_TypeDef ReceiveQueue;
45+
6546

6647
/* Exported macro ------------------------------------------------------------*/
6748
/* Exported functions ------------------------------------------------------- */
68-
void CDC_disable_TIM_Interrupt(void);
69-
void CDC_enable_TIM_Interrupt(void);
70-
void CDC_flush(void);
49+
void CDC_continue_transmit(void);
7150
void CDC_resume_receive(void);
51+
void CDC_init(void);
52+
void CDC_deInit(void);
7253

7354
#ifdef __cplusplus
7455
}

‎cores/arduino/stm32/usbd_interface.c

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@
5050
#ifdef USBD_USE_HID_COMPOSITE
5151
USBD_HandleTypeDef hUSBD_Device_HID;
5252
#endif /* USBD_USE_HID_COMPOSITE*/
53-
#ifdef USBD_USE_CDC
54-
USBD_HandleTypeDef hUSBD_Device_CDC;
55-
#endif /* USBD_USE_CDC */
5653

5754
/**
5855
* @brief initialize USB devices
@@ -72,21 +69,6 @@ void usbd_interface_init(void)
7269
/* Start Device Process */
7370
USBD_Start(&hUSBD_Device_HID);
7471
#endif /* USBD_USE_HID_COMPOSITE */
75-
#ifdef USBD_USE_CDC
76-
/* Init Device Library */
77-
if (USBD_Init(&hUSBD_Device_CDC, &CDC_Desc, 0) == USBD_OK) {
78-
79-
/* Add Supported Class */
80-
if (USBD_RegisterClass(&hUSBD_Device_CDC, USBD_CDC_CLASS) == USBD_OK) {
81-
82-
/* Add CDC Interface Class */
83-
if (USBD_CDC_RegisterInterface(&hUSBD_Device_CDC, &USBD_CDC_fops) == USBD_OK) {
84-
/* Start Device Process */
85-
USBD_Start(&hUSBD_Device_CDC);
86-
}
87-
}
88-
}
89-
#endif /* USBD_USE_CDC */
9072
}
9173

9274
#ifdef USBD_USE_HID_COMPOSITE

0 commit comments

Comments
 (0)
Please sign in to comment.