Skip to content

Commit b704300

Browse files
author
Bogdan Marinescu
committed
Merge remote-tracking branch 'origin/master'
2 parents 1a1dafe + 41b2cd4 commit b704300

File tree

15 files changed

+2131
-287
lines changed

15 files changed

+2131
-287
lines changed

libraries/USBDevice/USBSerial/USBCDC.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bool USBCDC::USBCallback_request(void) {
5252
break;
5353
case CDC_SET_LINE_CODING:
5454
transfer->remaining = 7;
55+
transfer->notify = true;
5556
success = true;
5657
terminal_connected = true;
5758
break;
@@ -67,6 +68,31 @@ bool USBCDC::USBCallback_request(void) {
6768
return success;
6869
}
6970

71+
void USBCDC::USBCallback_requestCompleted(uint8_t *buf, uint32_t length) {
72+
// Request of setting line coding has 7 bytes
73+
if (length != 7) {
74+
return;
75+
}
76+
77+
CONTROL_TRANSFER * transfer = getTransferPtr();
78+
79+
/* Process class-specific requests */
80+
if (transfer->setup.bmRequestType.Type == CLASS_TYPE) {
81+
if (transfer->setup.bRequest == CDC_SET_LINE_CODING) {
82+
if (memcmp(cdc_line_coding, buf, 7)) {
83+
memcpy(cdc_line_coding, buf, 7);
84+
85+
int baud = buf[0] + (buf[1] << 8)
86+
+ (buf[2] << 16) + (buf[3] << 24);
87+
int stop = buf[4];
88+
int bits = buf[6];
89+
int parity = buf[5];
90+
91+
lineCodingChanged(baud, bits, parity, stop);
92+
}
93+
}
94+
}
95+
}
7096

7197
// Called in ISR context
7298
// Set configuration. Return false if the

libraries/USBDevice/USBSerial/USBCDC.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,21 @@ class USBCDC: public USBDevice {
9999
* @returns true if successful
100100
*/
101101
bool readEP_NB(uint8_t * buffer, uint32_t * size);
102+
103+
/*
104+
* Called by USBCallback_requestCompleted when CDC line coding is changed
105+
* Warning: Called in ISR
106+
*
107+
* @param baud The baud rate
108+
* @param bits The number of bits in a word (5-8)
109+
* @param parity The parity
110+
* @param stop The number of stop bits (1 or 2)
111+
*/
112+
virtual void lineCodingChanged(int baud, int bits, int parity, int stop) {};
102113

103114
protected:
104115
virtual bool USBCallback_request();
116+
virtual void USBCallback_requestCompleted(uint8_t *buf, uint32_t length);
105117
virtual bool USBCallback_setConfiguration(uint8_t configuration);
106118
volatile bool terminal_connected;
107119

libraries/USBDevice/USBSerial/USBSerial.h

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ class USBSerial: public USBCDC, public Stream {
5555
* @param product_release Your preoduct_release (default: 0x0001)
5656
*
5757
*/
58-
USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){ };
58+
USBSerial(uint16_t vendor_id = 0x1f00, uint16_t product_id = 0x2012, uint16_t product_release = 0x0001): USBCDC(vendor_id, product_id, product_release), buf(128){
59+
settingsChangedCallback = 0;
60+
};
5961

6062

6163
/**
@@ -79,6 +81,22 @@ class USBSerial: public USBCDC, public Stream {
7981
* @returns the number of bytes available
8082
*/
8183
uint8_t available();
84+
85+
/** Determine if there is a character available to read
86+
*
87+
* @returns
88+
* 1 if there is a character available to read,
89+
* 0 otherwise
90+
*/
91+
int readable() { return available() ? 1 : 0; }
92+
93+
/** Determine if there is space available to write a character
94+
*
95+
* @returns
96+
* 1 if there is space to write a character,
97+
* 0 otherwise
98+
*/
99+
int writeable() { return 1; } // always return 1, for write operation is blocking
82100

83101
/**
84102
* Write a block of data.
@@ -110,19 +128,33 @@ class USBSerial: public USBCDC, public Stream {
110128
*
111129
* @param fptr function pointer
112130
*/
113-
void attach(void (*fn)(void)) {
114-
if(fn != NULL) {
115-
rx.attach(fn);
131+
void attach(void (*fptr)(void)) {
132+
if(fptr != NULL) {
133+
rx.attach(fptr);
116134
}
117135
}
118136

137+
/**
138+
* Attach a callback to call when serial's settings are changed.
139+
*
140+
* @param fptr function pointer
141+
*/
142+
void attach(void (*fptr)(int baud, int bits, int parity, int stop)) {
143+
settingsChangedCallback = fptr;
144+
}
119145

120146
protected:
121147
virtual bool EP2_OUT_callback();
148+
virtual void lineCodingChanged(int baud, int bits, int parity, int stop){
149+
if (settingsChangedCallback) {
150+
settingsChangedCallback(baud, bits, parity, stop);
151+
}
152+
}
122153

123154
private:
124155
FunctionPointer rx;
125156
CircBuffer<uint8_t> buf;
157+
void (*settingsChangedCallback)(int baud, int bits, int parity, int stop);
126158
};
127159

128160
#endif

0 commit comments

Comments
 (0)