mbed-drivers
SerialBase.h
1 /*
2  * Copyright (c) 2006-2016, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * 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, WITHOUT
13  * 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.h"
21 
22 #if DEVICE_SERIAL
23 
24 #include "Stream.h"
25 #include "core-util/FunctionPointer.h"
26 #include "serial_api.h"
27 #include "Transaction.h"
28 
29 #if DEVICE_SERIAL_ASYNCH
30 #include "CThunk.h"
31 #include "dma_api.h"
32 #endif
33 
34 namespace mbed {
35 
39 class SerialBase {
40 
41 public:
46  void baud(int baudrate);
47 
48  enum Parity {
49  None = 0,
50  Odd,
51  Even,
52  Forced1,
53  Forced0
54  };
55 
56  enum IrqType {
57  RxIrq = 0,
58  TxIrq
59  };
60 
61  enum Flow {
62  Disabled = 0,
63  RTS,
64  CTS,
65  RTSCTS
66  };
67 
74  void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1);
75 
86  int readable();
87 
94  int writeable();
95 
101  void attach(void (*fptr)(void), IrqType type=RxIrq);
102 
109  template<typename T>
110  void attach(T* tptr, void (T::*mptr)(void), IrqType type=RxIrq) {
111  if((mptr != NULL) && (tptr != NULL)) {
112  _irq[type].attach(tptr, mptr);
113  serial_irq_set(&_serial, (SerialIrq)type, 1);
114  }
115  }
116 
119  void send_break();
120 
121 #if DEVICE_SERIAL_FC
122 
128  void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC);
129 #endif
130 
131  static void _irq_handler(uint32_t id, SerialIrq irq_type);
132 
133 #if DEVICE_SERIAL_ASYNCH
134 
138  typedef mbed::util::FunctionPointer2<void, Buffer, int> event_callback_t;
139 
147  int write(void *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
148 
155  int write(const Buffer& buf, const event_callback_t& callback, int event = SERIAL_EVENT_TX_COMPLETE);
156 
159  void abort_write();
160 
169  int read(void *buffer, int length, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
170 
178  int read(const Buffer& buffer, const event_callback_t& callback, int event = SERIAL_EVENT_RX_COMPLETE, unsigned char char_match = SERIAL_RESERVED_CHAR_MATCH);
179 
182  void abort_read();
183 
189  int set_dma_usage_tx(DMAUsage usage);
190 
196  int set_dma_usage_rx(DMAUsage usage);
197 
198 protected:
199  void start_read(const Buffer& buffer, char buffer_width, const event_callback_t& callback, int event, unsigned char char_match);
200  void start_write(const Buffer& buffer, char buffer_width, const event_callback_t& callback, int event);
201  void interrupt_handler_asynch(void);
202 #endif
203 
204 protected:
205  SerialBase(PinName tx, PinName rx);
206  virtual ~SerialBase() {
207  }
208 
209  int _base_getc();
210  int _base_putc(int c);
211 
212 #if DEVICE_SERIAL_ASYNCH
215 
216  CThunk<SerialBase> _thunk_irq;
217  transaction_data_t _current_tx_transaction;
218  transaction_data_t _current_rx_transaction;
219  DMAUsage _tx_usage;
220  DMAUsage _rx_usage;
221 #endif
222 
223  serial_t _serial;
224  mbed::util::FunctionPointer _irq[2];
225  int _baud;
226 
227 };
228 
229 } // namespace mbed
230 
231 #endif
232 
233 #endif
Definition: Transaction.h:48
void attach(T *tptr, void(T::*mptr)(void), IrqType type=RxIrq)
Definition: SerialBase.h:110
void abort_write()
Definition: SerialBase.cpp:136
void send_break()
Definition: SerialBase.cpp:76
int readable()
Definition: SerialBase.cpp:44
void set_flow_control(Flow type, PinName flow1=NC, PinName flow2=NC)
Definition: SerialBase.cpp:91
int write(void *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_TX_COMPLETE)
Definition: SerialBase.cpp:115
void format(int bits=8, Parity parity=SerialBase::None, int stop_bits=1)
Definition: SerialBase.cpp:40
void baud(int baudrate)
Definition: SerialBase.cpp:35
int writeable()
Definition: SerialBase.cpp:49
Definition: SerialBase.h:39
void abort_read()
Definition: SerialBase.cpp:141
int set_dma_usage_rx(DMAUsage usage)
Definition: SerialBase.cpp:155
int read(void *buffer, int length, const event_callback_t &callback, int event=SERIAL_EVENT_RX_COMPLETE, unsigned char char_match=SERIAL_RESERVED_CHAR_MATCH)
Definition: SerialBase.cpp:164
mbed::util::FunctionPointer2< void, Buffer, int > event_callback_t
Definition: SerialBase.h:138
Definition: CThunk.h:70
Definition: Buffer.h:30
int set_dma_usage_tx(DMAUsage usage)
Definition: SerialBase.cpp:146
Definition: BusIn.cpp:19
void attach(void(*fptr)(void), IrqType type=RxIrq)
Definition: SerialBase.cpp:53