Skip to content

Commit ba6c5ff

Browse files
committed
Implement MinimalSerial class
1 parent e2578db commit ba6c5ff

File tree

9 files changed

+719
-105
lines changed

9 files changed

+719
-105
lines changed

drivers/MinimalSerial.h

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/* mbed Microcontroller Library
2+
* Copyright (c) 2019 ARM Limited
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
#ifndef MBED_MINIMAL_SERIAL_H
18+
#define MBED_MINIMAL_SERIAL_H
19+
20+
#include "platform/platform.h"
21+
22+
#if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
23+
#include "hal/serial_api.h"
24+
#include "platform/NonCopyable.h"
25+
#include "PinNames.h"
26+
27+
namespace mbed {
28+
29+
/**
30+
* \defgroup drivers_MinimalSerial MinimalSerial class
31+
* \ingroup drivers-public-api-uart
32+
* @{
33+
*/
34+
35+
/** A abstract class for serial port implementations
36+
*/
37+
class MinimalSerial : private NonCopyable<MinimalSerial> {
38+
39+
public:
40+
41+
enum Flow {
42+
Disabled = 0,
43+
RTS,
44+
CTS,
45+
RTSCTS
46+
};
47+
48+
#if DEVICE_SERIAL_FC
49+
/** Set the flow control type on the serial port
50+
*
51+
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
52+
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
53+
* @param flow2 the second flow control pin (CTS for RTSCTS)
54+
*/
55+
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
56+
#endif // DEVICE_SERIAL_FC
57+
58+
/** Set the baud rate of the serial port
59+
*
60+
* @param baudrate The baudrate of the serial port.
61+
*/
62+
void baud(int baudrate);
63+
64+
/** Write a char to the serial port
65+
*
66+
* @param c The char to write
67+
*
68+
* @returns The written char
69+
*/
70+
int putc(int c);
71+
72+
/** Read a char to the serial port
73+
*
74+
* @returns The char read from the serial port
75+
*/
76+
int getc();
77+
78+
protected:
79+
MinimalSerial(
80+
PinName tx,
81+
PinName rx,
82+
int baud
83+
);
84+
85+
MinimalSerial(
86+
serial_t *serial_ptr,
87+
bool serial_inited,
88+
PinName tx,
89+
PinName rx,
90+
int baud
91+
);
92+
93+
virtual ~MinimalSerial();
94+
95+
int _base_getc();
96+
97+
int _base_putc(int c);
98+
99+
int _baud;
100+
101+
serial_t _serial;
102+
103+
#if !defined(DOXYGEN_ONLY)
104+
/** Acquire exclusive access to this serial port
105+
*/
106+
virtual void lock(void){};
107+
108+
/** Release exclusive access to this serial port
109+
*/
110+
virtual void unlock(void){};
111+
#endif // !defined(DOXYGEN_ONLY)
112+
113+
private:
114+
void init(PinName tx, PinName rx);
115+
serial_t *_serial_ptr;
116+
};
117+
118+
/** @}*/
119+
120+
} // namespace mbed
121+
122+
#endif // DEVICE_SERIAL || defined(DOXYGEN_ONLY)
123+
124+
#endif // MBED_MINIMAL_SERIAL_H

drivers/SerialBase.h

Lines changed: 3 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* mbed Microcontroller Library
2-
* Copyright (c) 2006-2013 ARM Limited
2+
* Copyright (c) 2006-2019 ARM Limited
33
* SPDX-License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -25,6 +25,7 @@
2525
#include "hal/serial_api.h"
2626
#include "platform/mbed_toolchain.h"
2727
#include "platform/NonCopyable.h"
28+
#include "drivers/MinimalSerial.h"
2829

2930
#if DEVICE_SERIAL_ASYNCH
3031
#include "platform/CThunk.h"
@@ -43,15 +44,9 @@ namespace mbed {
4344
*
4445
* @note Synchronization level: Set by subclass
4546
*/
46-
class SerialBase : private NonCopyable<SerialBase> {
47+
class SerialBase : public MinimalSerial, private NonCopyable<SerialBase> {
4748

4849
public:
49-
/** Set the baud rate of the serial port
50-
*
51-
* @param baudrate The baudrate of the serial port (default = 9600).
52-
*/
53-
void baud(int baudrate);
54-
5550
enum Parity {
5651
None = 0,
5752
Odd,
@@ -67,13 +62,6 @@ class SerialBase : private NonCopyable<SerialBase> {
6762
IrqCnt
6863
};
6964

70-
enum Flow {
71-
Disabled = 0,
72-
RTS,
73-
CTS,
74-
RTSCTS
75-
};
76-
7765
/** Set the transmission format used by the serial port
7866
*
7967
* @param bits The number of bits in a word (5-8; default = 8)
@@ -155,29 +143,8 @@ class SerialBase : private NonCopyable<SerialBase> {
155143
*/
156144
void send_break();
157145

158-
#if !defined(DOXYGEN_ONLY)
159-
protected:
160-
161-
/** Acquire exclusive access to this serial port
162-
*/
163-
virtual void lock(void);
164-
165-
/** Release exclusive access to this serial port
166-
*/
167-
virtual void unlock(void);
168-
#endif
169146
public:
170147

171-
#if DEVICE_SERIAL_FC
172-
/** Set the flow control type on the serial port
173-
*
174-
* @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
175-
* @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
176-
* @param flow2 the second flow control pin (CTS for RTSCTS)
177-
*/
178-
void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
179-
#endif
180-
181148
static void _irq_handler(uint32_t id, SerialIrq irq_type);
182149

183150
#if DEVICE_SERIAL_ASYNCH
@@ -291,10 +258,6 @@ class SerialBase : private NonCopyable<SerialBase> {
291258
SerialBase(PinName tx, PinName rx, int baud);
292259
virtual ~SerialBase();
293260

294-
int _base_getc();
295-
296-
int _base_putc(int c);
297-
298261
#if DEVICE_SERIAL_ASYNCH
299262
CThunk<SerialBase> _thunk_irq;
300263
DMAUsage _tx_usage;
@@ -304,10 +267,7 @@ class SerialBase : private NonCopyable<SerialBase> {
304267
bool _tx_asynch_set;
305268
bool _rx_asynch_set;
306269
#endif
307-
308-
serial_t _serial;
309270
Callback<void()> _irq[IrqCnt];
310-
int _baud;
311271
#endif
312272
};
313273

0 commit comments

Comments
 (0)