-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathHardwareSerial.cpp
161 lines (131 loc) · 3.77 KB
/
HardwareSerial.cpp
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
/*
// This file is subject to the terms and conditions defined in
// file 'LICENSE.md', which is part of this source code package.
*/
#include "core-extend/HardwareSerial.h"
UART Serial;
// redirect stdout / stdin to Serial
mbed::FileHandle *mbed::mbed_override_console(int)
{
return (mbed::FileHandle*)&Serial;
}
UART::UART(PinName tx, PinName rx, PinName rts, PinName cts) :
UnbufferedSerial(tx, rx)
{
#ifdef DEVICE_SERIAL_FC
mbed::SerialBase::Flow control = mbed::SerialBase::Disabled;
PinName flow1 = rts;
pinName flow2 = cts;
bool has_rts = (rts != NC);
bool has_cts = (cts != NC);
if(has_rts && has_cts){
control = mbed::SerialBase::RTSCTS;
}else{
if(has_rts){
control = mbed::SerialBase::RTS;
flow1 = rts;
}
if(has_cts){
control = mbed::SerialBase::CTS;
flow1 = cts;
}
}
BufferedSerial::set_flow_control(control, flow1, flow2);
#endif // DEVICE_SERIAL_FC
}
UART::UART(pin_size_t tx, pin_size_t rx, pin_size_t rts, pin_size_t cts) :
UART(pinNameByNumber(tx), pinNameByNumber(rx), pinNameByNumber(rts), pinNameByNumber(cts))
{
}
UART::UART( void ) :
UART(STDIO_UART_TX, STDIO_UART_RX)
{
}
UART::~UART( void ){
}
void UART::rxISR( void ){
char c;
while(UnbufferedSerial::readable()) {
UnbufferedSerial::read(&c, 1);
_rxbuf.store_char(c);
}
}
void UART::begin(unsigned long baudrate, uint16_t config){
mbed::SerialBase::Parity parity;
int stop_bits;
int bits;
switch(config & SERIAL_PARITY_MASK){
case SERIAL_PARITY_EVEN : parity = mbed::SerialBase::Even; break;
case SERIAL_PARITY_ODD : parity = mbed::SerialBase::Odd; break;
case SERIAL_PARITY_MARK : parity = mbed::SerialBase::Forced1; break;
case SERIAL_PARITY_SPACE : parity = mbed::SerialBase::Forced0; break;
case SERIAL_PARITY_NONE :
default :
parity = mbed::SerialBase::None;
break;
}
switch(config & SERIAL_STOP_BIT_MASK){
case SERIAL_STOP_BIT_1_5 :
case SERIAL_STOP_BIT_2 :
stop_bits = 2;
break;
case SERIAL_STOP_BIT_1 :
default :
stop_bits = 1;
break;
}
switch(config & SERIAL_DATA_MASK){
case SERIAL_DATA_5 : bits = 5; break;
case SERIAL_DATA_6 : bits = 6; break;
case SERIAL_DATA_7 : bits = 7; break;
case SERIAL_DATA_8 :
default :
bits = 8;
break;
}
// disable that pesky FIFO
AM_CRITICAL_BEGIN
UARTn(0)->LCRH_b.FEN = 0;
UARTn(1)->LCRH_b.FEN = 0;
AM_CRITICAL_END
mbed::UnbufferedSerial::set_blocking (false);
mbed::UnbufferedSerial::baud((int)baudrate);
mbed::UnbufferedSerial::format(bits, parity, stop_bits);
mbed::UnbufferedSerial::attach(mbed::callback(this, &UART::rxISR), mbed::UnbufferedSerial::RxIrq);
}
void UART::begin(unsigned long baudrate){
begin(baudrate, SERIAL_8N1);
}
void UART::end( void ){
}
int UART::available(void){
return _rxbuf.available();
}
int UART::peek(void){
return _rxbuf.peek();
}
int UART::read(void){
return _rxbuf.read_char();
}
void UART::flush(void){
}
size_t UART::write(uint8_t c){
return write(&c, 1);
}
size_t UART::write(const uint8_t* buffer, size_t size){
while (!UnbufferedSerial::writeable()){};
int result = UnbufferedSerial::write((void*)buffer, size);
return (result < 0) ? 0 : result;
}
int UART::printf(const char *format, ...){
va_list args;
va_start(args, format);
const int space = vsnprintf(NULL, 0, format, args) + 1;
char buf[space];
memset(buf, 0x00, space);
vsnprintf(buf, space, format, args);
va_end(args);
int size = strlen(buf);
write(buf, size);
return size;
}