Skip to content

Commit 08fde28

Browse files
author
Owen
authored
Merge pull request #2 from sparkfun/add-hardwareserial
2 parents 78638d2 + 8b583f5 commit 08fde28

File tree

3 files changed

+241
-0
lines changed

3 files changed

+241
-0
lines changed

core-extend/ArduinoAPI.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@ SOFTWARE.
2424
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_
2525

2626
#include "core-extend/Common.h"
27+
#include "core-extend/HardwareSerial.h"
2728

2829
#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_ARDUINOAPI_H_

core-extend/HardwareSerial.h

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#ifndef _ARDUINO_MBED_BRIDGE_CORE_EXTEND_HARDWARESERIAL_H_
24+
#define _ARDUINO_MBED_BRIDGE_CORE_EXTEND_HARDWARESERIAL_H_
25+
26+
#include "bridge/pins.h"
27+
#include "core-api/api/RingBuffer.h"
28+
29+
#define RX_BUF_LEN (256)
30+
31+
class UART : public HardwareSerial, public mbed::UnbufferedSerial {
32+
private:
33+
RingBufferN<RX_BUF_LEN> _rxbuf;
34+
35+
protected:
36+
public:
37+
UART(PinName tx, PinName rx, PinName rts = NC, PinName cts = NC);
38+
UART(pin_size_t tx, pin_size_t rx, pin_size_t rts = variantPinCount, pin_size_t cts = variantPinCount);
39+
UART( void );
40+
~UART( void );
41+
42+
void rxISR( void );
43+
44+
void begin(unsigned long baudrate, uint16_t config);
45+
void begin(unsigned long baudrate);
46+
void end( void );
47+
int available(void);
48+
int peek(void);
49+
int read(void);
50+
void flush(void);
51+
size_t write(uint8_t c);
52+
size_t write(const uint8_t* buffer, size_t size);
53+
using Print::write;
54+
int printf(const char *format, ...);
55+
operator bool(){
56+
return true;
57+
}
58+
};
59+
60+
extern UART Serial;
61+
62+
#endif // _ARDUINO_MBED_BRIDGE_CORE_EXTEND_HARDWARESERIAL_H_

core-implement/HardwareSerial.cpp

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
/*
2+
Copyright (c) 2020 SparkFun Electronics
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy
5+
of this software and associated documentation files (the "Software"), to deal
6+
in the Software without restriction, including without limitation the rights
7+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
copies of the Software, and to permit persons to whom the Software is
9+
furnished to do so, subject to the following conditions:
10+
11+
The above copyright notice and this permission notice shall be included in all
12+
copies or substantial portions of the Software.
13+
14+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
SOFTWARE.
21+
*/
22+
23+
#include "core-extend/HardwareSerial.h"
24+
25+
UART Serial;
26+
27+
// redirect stdout / stdin to Serial
28+
FileHandle *mbed::mbed_override_console(int)
29+
{
30+
return (FileHandle*)&Serial;
31+
}
32+
33+
UART::UART(PinName tx, PinName rx, PinName rts, PinName cts) :
34+
UnbufferedSerial(tx, rx)
35+
{
36+
#ifdef DEVICE_SERIAL_FC
37+
mbed::SerialBase::Flow control = mbed::SerialBase::Disabled;
38+
PinName flow1 = rts;
39+
pinName flow2 = cts;
40+
bool has_rts = (rts != NC);
41+
bool has_cts = (cts != NC);
42+
if(has_rts && has_cts){
43+
control = mbed::SerialBase::RTSCTS;
44+
}else{
45+
if(has_rts){
46+
control = mbed::SerialBase::RTS;
47+
flow1 = rts;
48+
}
49+
if(has_cts){
50+
control = mbed::SerialBase::CTS;
51+
flow1 = cts;
52+
}
53+
}
54+
BufferedSerial::set_flow_control(control, flow1, flow2);
55+
#endif // DEVICE_SERIAL_FC
56+
}
57+
58+
UART::UART(pin_size_t tx, pin_size_t rx, pin_size_t rts, pin_size_t cts) :
59+
UART(pinNameByNumber(tx), pinNameByNumber(rx), pinNameByNumber(rts), pinNameByNumber(cts))
60+
{
61+
62+
}
63+
64+
UART::UART( void ) :
65+
UART(STDIO_UART_TX, STDIO_UART_RX)
66+
{
67+
68+
}
69+
70+
UART::~UART( void ){
71+
72+
}
73+
74+
void UART::rxISR( void ){
75+
char c;
76+
while(UnbufferedSerial::readable()) {
77+
UnbufferedSerial::read(&c, 1);
78+
_rxbuf.store_char(c);
79+
}
80+
}
81+
82+
void UART::begin(unsigned long baudrate, uint16_t config){
83+
mbed::SerialBase::Parity parity;
84+
int stop_bits;
85+
int bits;
86+
87+
switch(config & SERIAL_PARITY_MASK){
88+
case SERIAL_PARITY_EVEN : parity = mbed::SerialBase::Even; break;
89+
case SERIAL_PARITY_ODD : parity = mbed::SerialBase::Odd; break;
90+
case SERIAL_PARITY_MARK : parity = mbed::SerialBase::Forced1; break;
91+
case SERIAL_PARITY_SPACE : parity = mbed::SerialBase::Forced0; break;
92+
case SERIAL_PARITY_NONE :
93+
default :
94+
parity = mbed::SerialBase::None;
95+
break;
96+
}
97+
98+
switch(config & SERIAL_STOP_BIT_MASK){
99+
case SERIAL_STOP_BIT_1_5 :
100+
case SERIAL_STOP_BIT_2 :
101+
stop_bits = 2;
102+
break;
103+
case SERIAL_STOP_BIT_1 :
104+
default :
105+
stop_bits = 1;
106+
break;
107+
}
108+
109+
switch(config & SERIAL_DATA_MASK){
110+
case SERIAL_DATA_5 : bits = 5; break;
111+
case SERIAL_DATA_6 : bits = 6; break;
112+
case SERIAL_DATA_7 : bits = 7; break;
113+
case SERIAL_DATA_8 :
114+
default :
115+
bits = 8;
116+
break;
117+
}
118+
119+
// disable that pesky FIFO
120+
AM_CRITICAL_BEGIN
121+
UARTn(0)->LCRH_b.FEN = 0;
122+
UARTn(1)->LCRH_b.FEN = 0;
123+
AM_CRITICAL_END
124+
125+
mbed::UnbufferedSerial::set_blocking (false);
126+
mbed::UnbufferedSerial::baud((int)baudrate);
127+
mbed::UnbufferedSerial::format(bits, parity, stop_bits);
128+
mbed::UnbufferedSerial::attach(mbed::callback(this, &UART::rxISR), mbed::UnbufferedSerial::RxIrq);
129+
}
130+
131+
void UART::begin(unsigned long baudrate){
132+
begin(baudrate, SERIAL_8N1);
133+
}
134+
135+
void UART::end( void ){
136+
137+
}
138+
139+
int UART::available(void){
140+
return _rxbuf.available();
141+
}
142+
143+
int UART::peek(void){
144+
return _rxbuf.peek();
145+
}
146+
147+
int UART::read(void){
148+
return _rxbuf.read_char();
149+
}
150+
151+
void UART::flush(void){
152+
153+
}
154+
155+
size_t UART::write(uint8_t c){
156+
return write(&c, 1);
157+
}
158+
159+
size_t UART::write(const uint8_t* buffer, size_t size){
160+
while (!UnbufferedSerial::writeable()){};
161+
int result = UnbufferedSerial::write((void*)buffer, size);
162+
return (result < 0) ? 0 : result;
163+
}
164+
165+
int UART::printf(const char *format, ...){
166+
167+
va_list args;
168+
va_start(args, format);
169+
const int space = vsnprintf(NULL, 0, format, args) + 1;
170+
char buf[space];
171+
memset(buf, 0x00, space);
172+
vsnprintf(buf, space, format, args);
173+
va_end(args);
174+
175+
int size = strlen(buf);
176+
write(buf, size);
177+
return size;
178+
}

0 commit comments

Comments
 (0)