Skip to content

Commit 24de27c

Browse files
author
Hasnain Virk
committed
Major Refactoring & extensions
For keep supporting external APIs with the same name (supposedly there are a larger number of users of those APIs), BufferedSerial and ATParser are being renamed. BufferedSerial becomes UARTSerial, will complement a future USBSerial etc. ATParser becomes ATCmdParser. * UARTSerial moves to /drivers * APN_db.h is moved from platform to cellular/util/. * Original CellularInterface is restored for backward compatability (again, supposedly there are users of that). * A new file, CellularBase is added which will now servce as the base class for all upcoming drivers. * Special restructuring for the driver has been undertaken. This makes a clear cut distinction between an on-board or an off-board implementation. - PPPCellularInterface is a generic network interface that works with a generic FileHandle and PPP. A derived class is needed to pass that FileHandle. - PPPCellularInterface provides some base functionality like network registration, AT setup, PPP connection etc. Lower level job is delegated to the derived classes and various modem specific APIs are provided which are supposed to be overridden. - UARTCellularInterface is derived from PPPCellularInterface. It constructs a FileHandle and passes it back to PPPCellularInterface as well as provides modem hangupf functionality. In future we could proive a USBInterface that would derive from PPPCellularInterface and could pass the FileHandle back. - OnboardCellularInterface is derived from UARTCellularInterfae and provides hooks to the target provided implementation of onbard_modem_api.h. An off-board modem, i.e, a modem on a shield has to override the modem_init(), modem_power_up() etc as it cannot use onboard_modem_api.h.
1 parent b51fbe8 commit 24de27c

File tree

29 files changed

+1274
-800
lines changed

29 files changed

+1274
-800
lines changed

platform/BufferedSerial.cpp renamed to drivers/UARTSerial.cpp

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,68 +17,68 @@
1717
#if DEVICE_SERIAL
1818

1919
#include <errno.h>
20-
#include "platform/BufferedSerial.h"
20+
#include "UARTSerial.h"
2121
#include "platform/mbed_poll.h"
2222
#include "platform/mbed_wait_api.h"
2323

2424
namespace mbed {
2525

26-
BufferedSerial::BufferedSerial(PinName tx, PinName rx, int baud) :
26+
UARTSerial::UARTSerial(PinName tx, PinName rx, int baud) :
2727
SerialBase(tx, rx, baud),
2828
_blocking(true),
2929
_tx_irq_enabled(false),
30-
_dcd(NULL)
30+
_dcd_irq(NULL)
3131
{
3232
/* Attatch IRQ routines to the serial device. */
33-
SerialBase::attach(callback(this, &BufferedSerial::rx_irq), RxIrq);
33+
SerialBase::attach(callback(this, &UARTSerial::rx_irq), RxIrq);
3434
}
3535

36-
BufferedSerial::~BufferedSerial()
36+
UARTSerial::~UARTSerial()
3737
{
38-
delete _dcd;
38+
delete _dcd_irq;
3939
}
4040

41-
void BufferedSerial::DCD_IRQ()
41+
void UARTSerial::dcd_irq()
4242
{
4343
wake();
4444
}
4545

46-
void BufferedSerial::set_data_carrier_detect(PinName DCD_pin, bool active_high)
46+
void UARTSerial::set_data_carrier_detect(PinName dcd_pin, bool active_high)
4747
{
48-
delete _dcd;
49-
_dcd = NULL;
48+
delete _dcd_irq;
49+
_dcd_irq = NULL;
5050

51-
if (DCD_pin != NC) {
52-
_dcd = new InterruptIn(DCD_pin);
51+
if (dcd_pin != NC) {
52+
_dcd_irq = new InterruptIn(dcd_pin);
5353
if (active_high) {
54-
_dcd->fall(callback(this, &BufferedSerial::DCD_IRQ));
54+
_dcd_irq->fall(callback(this, &UARTSerial::dcd_irq));
5555
} else {
56-
_dcd->rise(callback(this, &BufferedSerial::DCD_IRQ));
56+
_dcd_irq->rise(callback(this, &UARTSerial::dcd_irq));
5757
}
5858
}
5959
}
6060

61-
int BufferedSerial::close()
61+
int UARTSerial::close()
6262
{
6363
/* Does not let us pass a file descriptor. So how to close ?
6464
* Also, does it make sense to close a device type file descriptor*/
6565
return 0;
6666
}
6767

68-
int BufferedSerial::isatty()
68+
int UARTSerial::isatty()
6969
{
7070
return 1;
7171

7272
}
7373

74-
off_t BufferedSerial::seek(off_t offset, int whence)
74+
off_t UARTSerial::seek(off_t offset, int whence)
7575
{
7676
/*XXX lseek can be done theoratically, but is it sane to mark positions on a dynamically growing/shrinking
7777
* buffer system (from an interrupt context) */
7878
return -ESPIPE;
7979
}
8080

81-
int BufferedSerial::sync()
81+
int UARTSerial::sync()
8282
{
8383
lock();
8484

@@ -94,7 +94,7 @@ int BufferedSerial::sync()
9494
return 0;
9595
}
9696

97-
void BufferedSerial::sigio(Callback<void()> func) {
97+
void UARTSerial::sigio(Callback<void()> func) {
9898
core_util_critical_section_enter();
9999
_sigio_cb = func;
100100
if (_sigio_cb) {
@@ -106,7 +106,7 @@ void BufferedSerial::sigio(Callback<void()> func) {
106106
core_util_critical_section_exit();
107107
}
108108

109-
ssize_t BufferedSerial::write(const void* buffer, size_t length)
109+
ssize_t UARTSerial::write(const void* buffer, size_t length)
110110
{
111111
size_t data_written = 0;
112112
const char *buf_ptr = static_cast<const char *>(buffer);
@@ -130,9 +130,9 @@ ssize_t BufferedSerial::write(const void* buffer, size_t length)
130130

131131
core_util_critical_section_enter();
132132
if (!_tx_irq_enabled) {
133-
BufferedSerial::tx_irq(); // only write to hardware in one place
133+
UARTSerial::tx_irq(); // only write to hardware in one place
134134
if (!_txbuf.empty()) {
135-
SerialBase::attach(callback(this, &BufferedSerial::tx_irq), TxIrq);
135+
SerialBase::attach(callback(this, &UARTSerial::tx_irq), TxIrq);
136136
_tx_irq_enabled = true;
137137
}
138138
}
@@ -143,7 +143,7 @@ ssize_t BufferedSerial::write(const void* buffer, size_t length)
143143
return data_written;
144144
}
145145

146-
ssize_t BufferedSerial::read(void* buffer, size_t length)
146+
ssize_t UARTSerial::read(void* buffer, size_t length)
147147
{
148148
size_t data_read = 0;
149149

@@ -171,19 +171,19 @@ ssize_t BufferedSerial::read(void* buffer, size_t length)
171171
return data_read;
172172
}
173173

174-
bool BufferedSerial::hup() const
174+
bool UARTSerial::hup() const
175175
{
176-
return _dcd && _dcd->read() != 0;
176+
return _dcd_irq && _dcd_irq->read() != 0;
177177
}
178178

179-
void BufferedSerial::wake()
179+
void UARTSerial::wake()
180180
{
181181
if (_sigio_cb) {
182182
_sigio_cb();
183183
}
184184
}
185185

186-
short BufferedSerial::poll(short events) const {
186+
short UARTSerial::poll(short events) const {
187187

188188
short revents = 0;
189189
/* Check the Circular Buffer if space available for writing out */
@@ -205,17 +205,17 @@ short BufferedSerial::poll(short events) const {
205205
return revents;
206206
}
207207

208-
void BufferedSerial::lock(void)
208+
void UARTSerial::lock(void)
209209
{
210210
_mutex.lock();
211211
}
212212

213-
void BufferedSerial::unlock(void)
213+
void UARTSerial::unlock(void)
214214
{
215215
_mutex.unlock();
216216
}
217217

218-
void BufferedSerial::rx_irq(void)
218+
void UARTSerial::rx_irq(void)
219219
{
220220
bool was_empty = _rxbuf.empty();
221221

@@ -237,7 +237,7 @@ void BufferedSerial::rx_irq(void)
237237
}
238238

239239
// Also called from write to start transfer
240-
void BufferedSerial::tx_irq(void)
240+
void UARTSerial::tx_irq(void)
241241
{
242242
bool was_full = _txbuf.full();
243243

drivers/UARTSerial.h

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2006-2017 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef MBED_UARTSERIAL_H
18+
#define MBED_UARTSERIAL_H
19+
20+
#include "platform/platform.h"
21+
22+
#if DEVICE_SERIAL
23+
24+
#include "FileHandle.h"
25+
#include "SerialBase.h"
26+
#include "InterruptIn.h"
27+
#include "PlatformMutex.h"
28+
#include "serial_api.h"
29+
#include "CircularBuffer.h"
30+
31+
#ifndef MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE
32+
#define MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE 256
33+
#endif
34+
35+
#ifndef MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE
36+
#define MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE 256
37+
#endif
38+
39+
namespace mbed {
40+
41+
class UARTSerial : private SerialBase, public FileHandle {
42+
43+
public:
44+
45+
/** Create a UARTSerial port, connected to the specified transmit and receive pins, with a particular baud rate.
46+
* @param tx Transmit pin
47+
* @param rx Receive pin
48+
* @param baud The baud rate of the serial port (optional, defaults to MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE)
49+
*/
50+
UARTSerial(PinName tx, PinName rx, int baud = MBED_CONF_PLATFORM_DEFAULT_SERIAL_BAUD_RATE);
51+
virtual ~UARTSerial();
52+
53+
/** Equivalent to POSIX poll(). Derived from FileHandle.
54+
* Provides a mechanism to multiplex input/output over a set of file handles.
55+
*/
56+
virtual short poll(short events) const;
57+
58+
/** Write the contents of a buffer to a file
59+
*
60+
* @param buffer The buffer to write from
61+
* @param size The number of bytes to write
62+
* @return The number of bytes written, negative error on failure
63+
*/
64+
virtual ssize_t write(const void* buffer, size_t length);
65+
66+
/** Read the contents of a file into a buffer
67+
*
68+
* Follows POSIX semantics:
69+
*
70+
* * if no data is available, and non-blocking set return -EAGAIN
71+
* * if no data is available, and blocking set, wait until data is available
72+
* * If any data is available, call returns immediately
73+
*
74+
* @param buffer The buffer to read in to
75+
* @param size The number of bytes to read
76+
* @return The number of bytes read, 0 at end of file, negative error on failure
77+
*/
78+
virtual ssize_t read(void* buffer, size_t length);
79+
80+
/** Acquire mutex */
81+
virtual void lock(void);
82+
83+
/** Release mutex */
84+
virtual void unlock(void);
85+
86+
/** Close a file
87+
*
88+
* @return 0 on success, negative error code on failure
89+
*/
90+
virtual int close();
91+
92+
/** Check if the file in an interactive terminal device
93+
*
94+
* @return True if the file is a terminal
95+
* @return False if the file is not a terminal
96+
* @return Negative error code on failure
97+
*/
98+
virtual int isatty();
99+
100+
/** Move the file position to a given offset from from a given location
101+
*
102+
* Not valid for a device type FileHandle like UARTSerial.
103+
* In case of UARTSerial, returns ESPIPE
104+
*
105+
* @param offset The offset from whence to move to
106+
* @param whence The start of where to seek
107+
* SEEK_SET to start from beginning of file,
108+
* SEEK_CUR to start from current position in file,
109+
* SEEK_END to start from end of file
110+
* @return The new offset of the file, negative error code on failure
111+
*/
112+
virtual off_t seek(off_t offset, int whence);
113+
114+
/** Flush any buffers associated with the file
115+
*
116+
* @return 0 on success, negative error code on failure
117+
*/
118+
virtual int sync();
119+
120+
/** Set blocking or non-blocking mode
121+
* The default is blocking.
122+
*
123+
* @param blocking true for blocking mode, false for non-blocking mode.
124+
*/
125+
virtual int set_blocking(bool blocking)
126+
{
127+
_blocking = blocking;
128+
return 0;
129+
}
130+
131+
/** Register a callback on state change of the file.
132+
*
133+
* The specified callback will be called on state changes such as when
134+
* the file can be written to or read from.
135+
*
136+
* The callback may be called in an interrupt context and should not
137+
* perform expensive operations.
138+
*
139+
* Note! This is not intended as an attach-like asynchronous api, but rather
140+
* as a building block for constructing such functionality.
141+
*
142+
* The exact timing of when the registered function
143+
* is called is not guaranteed and susceptible to change. It should be used
144+
* as a cue to make read/write/poll calls to find the current state.
145+
*
146+
* @param func Function to call on state change
147+
*/
148+
virtual void sigio(Callback<void()> func);
149+
150+
/** Setup interrupt handler for DCD line
151+
*
152+
* If DCD line is connected, an IRQ handler will be setup.
153+
* Does nothing if DCD is NC, i.e., not connected.
154+
*
155+
* @param dcd_pin Pin-name for DCD
156+
* @param active_high a boolean set to true if DCD polarity is active low
157+
*/
158+
void set_data_carrier_detect(PinName dcd_pin, bool active_high = false);
159+
160+
private:
161+
162+
/** Software serial buffers
163+
* By default buffer size is 256 for TX and 256 for RX. Configurable through mbed_app.json
164+
*/
165+
CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_RXBUF_SIZE> _rxbuf;
166+
CircularBuffer<char, MBED_CONF_DRIVERS_UART_SERIAL_TXBUF_SIZE> _txbuf;
167+
168+
PlatformMutex _mutex;
169+
170+
Callback<void()> _sigio_cb;
171+
172+
bool _blocking;
173+
bool _tx_irq_enabled;
174+
InterruptIn *_dcd_irq;
175+
176+
/** Device Hanged up
177+
* Determines if the device hanged up on us.
178+
*
179+
* @return True, if hanged up
180+
*/
181+
bool hup() const;
182+
183+
/** ISRs for serial
184+
* Routines to handle interrupts on serial pins.
185+
* Copies data into Circular Buffer.
186+
* Reports the state change to File handle.
187+
*/
188+
void tx_irq(void);
189+
void rx_irq(void);
190+
191+
void wake(void);
192+
193+
void dcd_irq(void);
194+
};
195+
} //namespace mbed
196+
197+
#endif //DEVICE_SERIAL
198+
#endif //MBED_UARTSERIAL_H

drivers/mbed_lib.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "drivers",
3+
"config": {
4+
"uart-serial-txbuf-size": {
5+
"help": "Default TX buffer size for a UARTSerial instance (unit Bytes))",
6+
"value": 256
7+
},
8+
"uart-serial-rxbuf-size": {
9+
"help": "Default RX buffer size for a UARTSerial instance (unit Bytes))",
10+
"value": 256
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)