This repository was archived by the owner on Feb 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathHardwareSerial.cpp
189 lines (152 loc) · 5.26 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
HardwareSerial.cpp - Hardware serial library for Wiring
Copyright (c) 2006 Nicholas Zambetti. All right reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 23 November 2006 by David A. Mellis
Modified 28 September 2010 by Mark Sproul
Modified 14 August 2012 by Alarus
Modified 3 December 2013 by Matthijs Kooijman
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <inttypes.h>
#include "Arduino.h"
#include "HardwareSerial.h"
// Constructors ////////////////////////////////////////////////////////////////
HardwareSerial::HardwareSerial(PinName _rx, PinName _tx)
{
_serial.pin_rx = _rx;
_serial.pin_tx = _tx;
_serial.rx_buff = _rx_buffer;
_serial.rx_head = 0;
_serial.rx_tail = 0;
_serial.tx_buff = _tx_buffer;
_serial.tx_head = 0;
_serial.tx_tail = 0;
}
// Actual interrupt handlers //////////////////////////////////////////////////////////////
void HardwareSerial::_rx_complete_irq(serial_t* obj)
{
// No Parity error, read byte and store it in the buffer if there is
// room
unsigned char c = uart_getc(obj);
rx_buffer_index_t i = (unsigned int)(obj->rx_head + 1) % SERIAL_RX_BUFFER_SIZE;
// if we should be storing the received character into the location
// just before the tail (meaning that the head would advance to the
// current location of the tail), we're about to overflow the buffer
// and so we don't write the character or advance the head.
if (i != obj->rx_tail) {
obj->rx_buff[obj->rx_head] = c;
obj->rx_head = i;
}
}
// Actual interrupt handlers //////////////////////////////////////////////////////////////
int HardwareSerial::_tx_complete_irq(serial_t* obj)
{
// If interrupts are enabled, there must be more data in the output
// buffer. Send the next byte
obj->tx_tail = (obj->tx_tail + 1) % SERIAL_TX_BUFFER_SIZE;
if (obj->tx_head == obj->tx_tail) {
return -1;
}
return 0;
}
// Public Methods //////////////////////////////////////////////////////////////
void HardwareSerial::begin(unsigned long baud, byte config)
{
_serial.baudrate = (uint32_t)baud;
// Could be 8 or 9 bits. Could match with Arduino small data length?
_serial.databits = UART_WORDLENGTH_8B;
if((config & 0x30) == 0x30) {
_serial.parity = UART_PARITY_ODD;
} else if((config & 0x20) == 0x20) {
_serial.parity = UART_PARITY_EVEN;
} else {
_serial.parity = UART_PARITY_NONE;
}
if((config & 0x08) == 0x08) {
_serial.stopbits = UART_STOPBITS_2;
} else {
_serial.stopbits = UART_STOPBITS_1;
}
uart_init(&_serial);
uart_attach_rx_callback(&_serial, _rx_complete_irq);
}
void HardwareSerial::end()
{
// wait for transmission of outgoing data
flush();
uart_deinit(&_serial);
// clear any received data
_serial.rx_head = _serial.rx_tail;
}
int HardwareSerial::available(void)
{
return ((unsigned int)(SERIAL_RX_BUFFER_SIZE + _serial.rx_head - _serial.rx_tail)) % SERIAL_RX_BUFFER_SIZE;
}
int HardwareSerial::peek(void)
{
if (_serial.rx_head == _serial.rx_tail) {
return -1;
} else {
return _serial.rx_buff[_serial.rx_tail];
}
}
int HardwareSerial::read(void)
{
// if the head isn't ahead of the tail, we don't have any characters
if (_serial.rx_head == _serial.rx_tail) {
return -1;
} else {
unsigned char c = _serial.rx_buff[_serial.rx_tail];
_serial.rx_tail = (rx_buffer_index_t)(_serial.rx_tail + 1) % SERIAL_RX_BUFFER_SIZE;
return c;
}
}
int HardwareSerial::availableForWrite(void)
{
tx_buffer_index_t head = _serial.tx_head;
tx_buffer_index_t tail = _serial.tx_tail;
if (head >= tail) return SERIAL_TX_BUFFER_SIZE - 1 - head + tail;
return tail - head - 1;
}
void HardwareSerial::flush()
{
// If we have never written a byte, no need to flush. This special
// case is needed since there is no way to force the TXC (transmit
// complete) bit to 1 during initialization
if (!_written)
return;
while((_serial.tx_head != _serial.tx_tail)) {
// nop, the interrupt handler will free up space for us
}
// If we get here, nothing is queued anymore (DRIE is disabled) and
// the hardware finished tranmission (TXC is set).
}
size_t HardwareSerial::write(uint8_t c)
{
_written = true;
tx_buffer_index_t i = (_serial.tx_head + 1) % SERIAL_TX_BUFFER_SIZE;
// If the output buffer is full, there's nothing for it other than to
// wait for the interrupt handler to empty it a bit
while (i == _serial.tx_tail) {
// nop, the interrupt handler will free up space for us
}
_serial.tx_buff[_serial.tx_head] = c;
_serial.tx_head = i;
if(!serial_tx_active(&_serial)) {
uart_attach_tx_callback(&_serial, _tx_complete_irq);
}
return 1;
}