Mistake on this page?
Report an issue in GitHub or email us
SerialBase.h
1 /* mbed Microcontroller Library
2  * Copyright (c) 2006-2013 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_SERIALBASE_H
18 #define MBED_SERIALBASE_H
19 
20 #include "platform/platform.h"
21 
22 #if DEVICE_SERIAL || defined(DOXYGEN_ONLY)
23 
24 #include "platform/Callback.h"
25 #include "hal/serial_api.h"
26 #include "platform/mbed_toolchain.h"
27 #include "platform/NonCopyable.h"
28 
29 #if DEVICE_SERIAL_ASYNCH
30 #include "platform/CThunk.h"
31 #include "hal/dma_api.h"
32 #endif
33 
34 namespace mbed {
35 /** \addtogroup drivers */
36 
37 /** A base class for serial port implementations
38  * Can't be instantiated directly (use Serial or RawSerial)
39  *
40  * @note Synchronization level: Set by subclass
41  * @ingroup drivers
42  */
43 class SerialBase : private NonCopyable<SerialBase> {
44 
45 public:
46  /** Set the baud rate of the serial port
47  *
48  * @param baudrate The baudrate of the serial port (default = 9600).
49  */
50  void baud(int baudrate);
51 
52  enum Parity {
53  None = 0,
54  Odd,
55  Even,
56  Forced1,
57  Forced0
58  };
59 
60  enum IrqType {
61  RxIrq = 0,
62  TxIrq,
63 
64  IrqCnt
65  };
66 
67  enum Flow {
68  Disabled = 0,
69  RTS,
70  CTS,
71  RTSCTS
72  };
73 
74  /** Set the transmission format used by the serial port
75  *
76  * @param bits The number of bits in a word (5-8; default = 8)
77  * @param parity The parity used (SerialBase::None, SerialBase::Odd, SerialBase::Even, SerialBase::Forced1, SerialBase::Forced0; default = SerialBase::None)
78  * @param stop_bits The number of stop bits (1 or 2; default = 1)
79  */
80  void format(int bits = 8, Parity parity = SerialBase::None, int stop_bits = 1);
81 
82  /** Determine if there is a character available to read
83  *
84  * @returns
85  * 1 if there is a character available to read,
86  * 0 otherwise
87  */
88  int readable();
89 
90  /** Determine if there is space available to write a character
91  *
92  * @returns
93  * 1 if there is space to write a character,
94  * 0 otherwise
95  */
96  int writeable();
97 
98  /** Attach a function to call whenever a serial interrupt is generated
99  *
100  * @param func A pointer to a void function, or 0 to set as none
101  * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
102  */
103  void attach(Callback<void()> func, IrqType type = RxIrq);
104 
105  /** Attach a member function to call whenever a serial interrupt is generated
106  *
107  * @param obj pointer to the object to call the member function on
108  * @param method pointer to the member function to be called
109  * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
110  * @deprecated
111  * The attach function does not support cv-qualifiers. Replaced by
112  * attach(callback(obj, method), type).
113  */
114  template<typename T>
115  MBED_DEPRECATED_SINCE("mbed-os-5.1",
116  "The attach function does not support cv-qualifiers. Replaced by "
117  "attach(callback(obj, method), type).")
118  void attach(T *obj, void (T::*method)(), IrqType type = RxIrq)
119  {
120  attach(callback(obj, method), type);
121  }
122 
123  /** Attach a member function to call whenever a serial interrupt is generated
124  *
125  * @param obj pointer to the object to call the member function on
126  * @param method pointer to the member function to be called
127  * @param type Which serial interrupt to attach the member function to (Serial::RxIrq for receive, TxIrq for transmit buffer empty)
128  * @deprecated
129  * The attach function does not support cv-qualifiers. Replaced by
130  * attach(callback(obj, method), type).
131  */
132  template<typename T>
133  MBED_DEPRECATED_SINCE("mbed-os-5.1",
134  "The attach function does not support cv-qualifiers. Replaced by "
135  "attach(callback(obj, method), type).")
136  void attach(T *obj, void (*method)(T *), IrqType type = RxIrq)
137  {
138  attach(callback(obj, method), type);
139  }
140 
141  /** Generate a break condition on the serial line
142  * NOTE: Clear break needs to run at least one frame after set_break is called
143  */
144  void set_break();
145 
146  /** Clear a break condition on the serial line
147  * NOTE: Should be run at least one frame after set_break is called
148  */
149  void clear_break();
150 
151  /** Generate a break condition on the serial line
152  */
153  void send_break();
154 
155 #if !defined(DOXYGEN_ONLY)
156 protected:
157 
158  /** Acquire exclusive access to this serial port
159  */
160  virtual void lock(void);
161 
162  /** Release exclusive access to this serial port
163  */
164  virtual void unlock(void);
165 #endif
166 public:
167 
168 #if DEVICE_SERIAL_FC
169  /** Set the flow control type on the serial port
170  *
171  * @param type the flow control type (Disabled, RTS, CTS, RTSCTS)
172  * @param flow1 the first flow control pin (RTS for RTS or RTSCTS, CTS for CTS)
173  * @param flow2 the second flow control pin (CTS for RTSCTS)
174  */
175  void set_flow_control(Flow type, PinName flow1 = NC, PinName flow2 = NC);
176 #endif
177 
178  static void _irq_handler(uint32_t id, SerialIrq irq_type);
179 
180 #if DEVICE_SERIAL_ASYNCH
181 
182  /** Begin asynchronous write using 8bit buffer.
183  *
184  * The write operation ends with any of the enabled events and invokes
185  * registered callback function (which can be NULL to not receive callback at all).
186  * Events that are not enabled by event argument are simply ignored.
187  * Operation has to be ended explicitly by calling abort_write() when
188  * no events are enabled.
189  * This function locks the deep sleep until any event has occurred.
190  *
191  * @param buffer The buffer where received data will be stored
192  * @param length The buffer length in bytes
193  * @param callback The event callback function
194  * @param event The logical OR of TX events that should end operation
195  * @return Zero if new transaction was started, -1 if transaction is already on-going
196  */
197  int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
198 
199  /** Begin asynchronous write using 16bit buffer.
200  *
201  * The write operation ends with any of the enabled events and invokes
202  * registered callback function (which can be NULL to not receive callback at all).
203  * Events that are not enabled by event argument are simply ignored.
204  * Operation has to be ended explicitly by calling abort_write() when
205  * no events are enabled.
206  * This function locks the deep sleep until any event has occurred.
207  *
208  * @param buffer The buffer where received data will be stored
209  * @param length The buffer length in bytes
210  * @param callback The event callback function
211  * @param event The logical OR of TX events that should end operation
212  * @return Zero if new transaction was started, -1 if transaction is already on-going
213  */
214  int write(const uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_TX_COMPLETE);
215 
216  /** Abort the on-going write transfer
217  *
218  * It is safe to call abort_write() when there is no on-going transaction.
219  */
220  void abort_write();
221 
222  /** Begin asynchronous reading using 8bit buffer.
223  *
224  * The read operation ends with any of the enabled events and invokes registered
225  * callback function (which can be NULL to not receive callback at all).
226  * Events that are not enabled by event argument are simply ignored.
227  * Operation has to be ended explicitly by calling abort_read() when
228  * no events are enabled.
229  * This function locks the deep sleep until any event has occurred.
230  *
231  * @param buffer The buffer where received data will be stored
232  * @param length The buffer length in bytes
233  * @param callback The event callback function
234  * @param event The logical OR of RX events that should end operation
235  * @param char_match The matching character
236  * @return Zero if new transaction was started, -1 if transaction is already on-going
237  */
238  int read(uint8_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
239 
240  /** Begin asynchronous reading using 16bit buffer.
241  *
242  * The read operation ends with any of the enabled events and invokes registered
243  * callback function (which can be NULL to not receive callback at all).
244  * Events that are not enabled by event argument are simply ignored.
245  * Operation has to be ended explicitly by calling abort_read() when
246  * no events are enabled.
247  * This function locks the deep sleep until any event has occurred.
248  *
249  * @param buffer The buffer where received data will be stored
250  * @param length The buffer length in bytes
251  * @param callback The event callback function
252  * @param event The logical OR of RX events that should end operation
253  * @param char_match The matching character
254  * @return Zero if new transaction was started, -1 if transaction is already on-going
255  */
256  int read(uint16_t *buffer, int length, const event_callback_t &callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
257 
258  /** Abort the on-going read transfer
259  *
260  * It is safe to call abort_read() when there is no on-going transaction.
261  */
262  void abort_read();
263 
264  /** Configure DMA usage suggestion for non-blocking TX transfers
265  *
266  * @param usage The usage DMA hint for peripheral
267  * @return Zero if the usage was set, -1 if a transaction is on-going
268  */
269  int set_dma_usage_tx(DMAUsage usage);
270 
271  /** Configure DMA usage suggestion for non-blocking RX transfers
272  *
273  * @param usage The usage DMA hint for peripheral
274  * @return Zero if the usage was set, -1 if a transaction is on-going
275  */
276  int set_dma_usage_rx(DMAUsage usage);
277 
278 #if !defined(DOXYGEN_ONLY)
279 protected:
280  void start_read(void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event, unsigned char char_match);
281  void start_write(const void *buffer, int buffer_size, char buffer_width, const event_callback_t &callback, int event);
282  void interrupt_handler_asynch(void);
283 #endif
284 #endif
285 
286 #if !defined(DOXYGEN_ONLY)
287 protected:
288  SerialBase(PinName tx, PinName rx, int baud);
289  virtual ~SerialBase();
290 
291  int _base_getc();
292  int _base_putc(int c);
293 
294 #if DEVICE_SERIAL_ASYNCH
295  CThunk<SerialBase> _thunk_irq;
296  DMAUsage _tx_usage;
297  DMAUsage _rx_usage;
298  event_callback_t _tx_callback;
299  event_callback_t _rx_callback;
300  bool _tx_asynch_set;
301  bool _rx_asynch_set;
302 #endif
303 
304  serial_t _serial;
305  Callback<void()> _irq[IrqCnt];
306  int _baud;
307 #endif
308 };
309 
310 } // namespace mbed
311 
312 #endif
313 
314 #endif
void send_break()
Generate a break condition on the serial line.
void baud(int baudrate)
Set the baud rate of the serial port.
int write(const uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
Begin asynchronous write using 8bit buffer.
Prevents generation of copy constructor and copy assignment operator in derived classes.
Definition: NonCopyable.h:168
Callback< R()> callback(R(*func)()=0)
Create a callback class with type inferred from the arguments.
Definition: Callback.h:3848
int writeable()
Determine if there is space available to write a character.
A base class for serial port implementations Can&#39;t be instantiated directly (use Serial or RawSerial)...
Definition: SerialBase.h:43
void clear_break()
Clear a break condition on the serial line NOTE: Should be run at least one frame after set_break is ...
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Set the flow control type on the serial port.
int readable()
Determine if there is a character available to read.
int read(uint8_t *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
Begin asynchronous reading using 8bit buffer.
void attach(Callback< void()> func, IrqType type=RxIrq)
Attach a function to call whenever a serial interrupt is generated.
Asynch serial HAL structure.
Definition: serial_api.h:90
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
Set the transmission format used by the serial port.
Class for created a pointer with data bound to it.
Definition: CThunk.h:50
void set_break()
Generate a break condition on the serial line NOTE: Clear break needs to run at least one frame after...
void abort_read()
Abort the on-going read transfer.
int set_dma_usage_tx(DMAUsage usage)
Configure DMA usage suggestion for non-blocking TX transfers.
Callback class based on template specialization.
Definition: Callback.h:39
int set_dma_usage_rx(DMAUsage usage)
Configure DMA usage suggestion for non-blocking RX transfers.
#define MBED_DEPRECATED_SINCE(D, M)
MBED_DEPRECATED("message string") Mark a function declaration as deprecated, if it used then a warnin...
void abort_write()
Abort the on-going write transfer.
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.