Skip to content

Commit 6d6fa20

Browse files
committed
[USB] Add USBSerial class
Signed-off-by: Frederic.Pillon <[email protected]>
1 parent 269ba65 commit 6d6fa20

File tree

3 files changed

+270
-0
lines changed

3 files changed

+270
-0
lines changed

cores/arduino/USBSerial.cpp

+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
/*
2+
Copyright (c) 2015 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifdef USBCON
20+
21+
#include "USBSerial.h"
22+
#include "usbd_cdc.h"
23+
#include "usbd_cdc_if.h"
24+
#include "usbd_desc.h"
25+
#include "wiring.h"
26+
27+
#define USB_TIMEOUT 50
28+
/* USB Device Core handle declaration */
29+
extern USBD_HandleTypeDef hUSBD_Device_CDC;
30+
extern __IO uint32_t device_connection_status;
31+
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;
38+
39+
USBSerial SerialUSB;
40+
41+
void USBSerial::begin(uint32_t /* baud_count */) {
42+
// uart config is ignored in USB-CDC
43+
}
44+
45+
void USBSerial::begin(uint32_t /* baud_count */, uint8_t /* config */) {
46+
// uart config is ignored in USB-CDC
47+
}
48+
49+
void USBSerial::end(void) {
50+
51+
USBD_LL_DeInit(&hUSBD_Device_CDC);
52+
}
53+
54+
int USBSerial::availableForWrite(void)
55+
{
56+
int ret_val;
57+
58+
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
59+
/* value that we read is correct, we need to disable TIM Interrupt. */
60+
CDC_disable_TIM_Interrupt();
61+
62+
if (UserTxBufPtrIn >= UserTxBufPtrOut) {
63+
ret_val = (APP_TX_DATA_SIZE - 1 - UserTxBufPtrIn + UserTxBufPtrOut);
64+
} else {
65+
ret_val = (UserTxBufPtrOut - UserTxBufPtrIn - 1);
66+
}
67+
68+
CDC_enable_TIM_Interrupt();
69+
70+
return ret_val;
71+
}
72+
73+
size_t USBSerial::write(uint8_t ch) {
74+
75+
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
76+
/* value that we read is correct, we need to disable TIM Interrupt. */
77+
CDC_disable_TIM_Interrupt();
78+
79+
if (((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE) == UserTxBufPtrOut) {
80+
// Buffer full!!! Force a flush to not loose data and go on
81+
CDC_flush();
82+
}
83+
UserTxBuffer[UserTxBufPtrIn] = ch;
84+
UserTxBufPtrIn = ((UserTxBufPtrIn + 1) % APP_TX_DATA_SIZE);
85+
86+
CDC_enable_TIM_Interrupt();
87+
88+
return 1;
89+
}
90+
91+
size_t USBSerial::write(const uint8_t *buffer, size_t size){
92+
size_t i = 0;
93+
for (i=0; i < size; i++) {
94+
if (write(buffer[i]) != 1) {
95+
break;
96+
}
97+
}
98+
return i;
99+
}
100+
101+
int USBSerial::available(void) {
102+
int ret;
103+
104+
CDC_disable_TIM_Interrupt();
105+
ret = ((APP_RX_DATA_SIZE + (UserRxBufPtrIn - UserRxBufPtrOut)) % APP_RX_DATA_SIZE);
106+
CDC_enable_TIM_Interrupt();
107+
108+
return ret;
109+
}
110+
111+
int USBSerial::read(void) {
112+
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
113+
/* value that we read is correct, we need to disable TIM Interrupt. */
114+
CDC_disable_TIM_Interrupt();
115+
if (UserRxBufPtrOut == UserRxBufPtrIn) {
116+
CDC_enable_TIM_Interrupt();
117+
return -1;
118+
} else {
119+
unsigned char c = UserRxBuffer[UserRxBufPtrOut];
120+
UserRxBufPtrOut = ((UserRxBufPtrOut + 1) % APP_RX_DATA_SIZE);
121+
CDC_enable_TIM_Interrupt();
122+
return c;
123+
}
124+
}
125+
126+
int USBSerial::peek(void)
127+
{
128+
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
129+
/* value that we read is correct, we need to disable TIM Interrupt. */
130+
CDC_disable_TIM_Interrupt();
131+
if (UserRxBufPtrOut == UserRxBufPtrIn) {
132+
CDC_enable_TIM_Interrupt();
133+
return -1;
134+
} else {
135+
unsigned char c = UserRxBuffer[UserRxBufPtrOut];
136+
CDC_enable_TIM_Interrupt();
137+
return c;
138+
}
139+
}
140+
141+
void USBSerial::flush(void)
142+
{
143+
/* UserTxBufPtrOut can be modified by TIM ISR, so in order to be sure that the */
144+
/* value that we read is correct, we need to disable TIM Interrupt. */
145+
CDC_disable_TIM_Interrupt();
146+
CDC_flush();
147+
CDC_enable_TIM_Interrupt();
148+
}
149+
150+
uint8_t USBSerial::pending(void) {
151+
return 0;
152+
}
153+
154+
uint8_t USBSerial::isConnected(void) {
155+
156+
if (device_connection_status == 1) {
157+
return 1;
158+
} else {
159+
return 0;
160+
}
161+
}
162+
163+
uint32_t USBSerial::baud() {
164+
return 115200;
165+
}
166+
167+
uint8_t USBSerial::stopbits() {
168+
return ONE_STOP_BIT;
169+
}
170+
171+
uint8_t USBSerial::paritytype() {
172+
return NO_PARITY;
173+
}
174+
175+
uint8_t USBSerial::numbits() {
176+
return 8;
177+
}
178+
179+
bool USBSerial::dtr(void) {
180+
return false;
181+
}
182+
183+
bool USBSerial::rts(void) {
184+
return false;
185+
}
186+
187+
USBSerial::operator bool() {
188+
bool result = false;
189+
if (lineState == 1)
190+
result = true;
191+
delay(10);
192+
return result;
193+
}
194+
195+
#endif // USBCON

cores/arduino/USBSerial.h

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
Copyright (c) 2015 Arduino LLC. All right reserved.
3+
4+
This library is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
This library is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12+
See the GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with this library; if not, write to the Free Software
16+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17+
*/
18+
19+
#ifndef _USBSERIAL_H_
20+
#define _USBSERIAL_H_
21+
22+
#if defined (USBCON) && defined(USBD_USE_CDC)
23+
#include "Stream.h"
24+
#include "usbd_core.h"
25+
26+
//================================================================================
27+
// Serial over CDC
28+
class USBSerial : public Stream {
29+
public:
30+
USBSerial(void) {};
31+
32+
void begin(uint32_t);
33+
void begin(uint32_t, uint8_t);
34+
void end(void);
35+
36+
virtual int available(void);
37+
virtual int availableForWrite(void);
38+
virtual int peek(void);
39+
virtual int read(void);
40+
virtual void flush(void);
41+
virtual size_t write(uint8_t);
42+
virtual size_t write(const uint8_t *buffer, size_t size);
43+
using Print::write; // pull in write(str) from Print
44+
operator bool(void);
45+
46+
// These return the settings specified by the USB host for the
47+
// serial port. These aren't really used, but are offered here
48+
// in case a sketch wants to act on these settings.
49+
uint32_t baud();
50+
uint8_t stopbits();
51+
uint8_t paritytype();
52+
uint8_t numbits();
53+
bool dtr();
54+
bool rts();
55+
enum {
56+
ONE_STOP_BIT = 0,
57+
ONE_AND_HALF_STOP_BIT = 1,
58+
TWO_STOP_BITS = 2,
59+
};
60+
enum {
61+
NO_PARITY = 0,
62+
ODD_PARITY = 1,
63+
EVEN_PARITY = 2,
64+
MARK_PARITY = 3,
65+
SPACE_PARITY = 4,
66+
};
67+
68+
uint8_t isConnected();
69+
uint8_t pending();
70+
};
71+
72+
extern USBSerial SerialUSB;
73+
#endif /* USBCON */
74+
#endif /* _USBSERIAL_H_ */

cores/arduino/wiring.h

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#ifdef __cplusplus
4444
#include "HardwareSerial.h"
4545
#include "Tone.h"
46+
#include "USBSerial.h"
4647
#include "WCharacter.h"
4748
#include "WMath.h"
4849
#include "WString.h"

0 commit comments

Comments
 (0)