Mistake on this page?
Report an issue in GitHub or email us
serial_api.h
1 
2 /** \addtogroup hal */
3 /** @{*/
4 /* mbed Microcontroller Library
5  * Copyright (c) 2006-2013 ARM Limited
6  * SPDX-License-Identifier: Apache-2.0
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 #ifndef MBED_SERIAL_API_H
21 #define MBED_SERIAL_API_H
22 
23 #include "device.h"
24 #include "pinmap.h"
25 #include "hal/buffer.h"
26 #include "hal/dma_api.h"
27 
28 #if DEVICE_SERIAL
29 
30 #define SERIAL_EVENT_TX_SHIFT (2)
31 #define SERIAL_EVENT_RX_SHIFT (8)
32 
33 #define SERIAL_EVENT_TX_MASK (0x00FC)
34 #define SERIAL_EVENT_RX_MASK (0x3F00)
35 
36 #define SERIAL_EVENT_ERROR (1 << 1)
37 
38 /**
39  * @defgroup SerialTXEvents Serial TX Events Macros
40  *
41  * @{
42  */
43 #define SERIAL_EVENT_TX_COMPLETE (1 << (SERIAL_EVENT_TX_SHIFT + 0))
44 #define SERIAL_EVENT_TX_ALL (SERIAL_EVENT_TX_COMPLETE)
45 /**@}*/
46 
47 /**
48  * @defgroup SerialRXEvents Serial RX Events Macros
49  *
50  * @{
51  */
52 #define SERIAL_EVENT_RX_COMPLETE (1 << (SERIAL_EVENT_RX_SHIFT + 0))
53 #define SERIAL_EVENT_RX_OVERRUN_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 1))
54 #define SERIAL_EVENT_RX_FRAMING_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 2))
55 #define SERIAL_EVENT_RX_PARITY_ERROR (1 << (SERIAL_EVENT_RX_SHIFT + 3))
56 #define SERIAL_EVENT_RX_OVERFLOW (1 << (SERIAL_EVENT_RX_SHIFT + 4))
57 #define SERIAL_EVENT_RX_CHARACTER_MATCH (1 << (SERIAL_EVENT_RX_SHIFT + 5))
58 #define SERIAL_EVENT_RX_ALL (SERIAL_EVENT_RX_OVERFLOW | SERIAL_EVENT_RX_PARITY_ERROR | \
59  SERIAL_EVENT_RX_FRAMING_ERROR | SERIAL_EVENT_RX_OVERRUN_ERROR | \
60  SERIAL_EVENT_RX_COMPLETE | SERIAL_EVENT_RX_CHARACTER_MATCH)
61 /**@}*/
62 
63 #define SERIAL_RESERVED_CHAR_MATCH (255)
64 
65 typedef enum {
66  ParityNone = 0,
67  ParityOdd = 1,
68  ParityEven = 2,
69  ParityForced1 = 3,
70  ParityForced0 = 4
71 } SerialParity;
72 
73 typedef enum {
74  RxIrq,
75  TxIrq
76 } SerialIrq;
77 
78 typedef enum {
79  FlowControlNone,
80  FlowControlRTS,
81  FlowControlCTS,
82  FlowControlRTSCTS
83 } FlowControl;
84 
85 typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
86 
87 #if DEVICE_SERIAL_ASYNCH
88 /** Asynch serial HAL structure
89  */
90 typedef struct {
91  struct serial_s serial; /**< Target specific serial structure */
92  struct buffer_s tx_buff; /**< TX buffer */
93  struct buffer_s rx_buff; /**< RX buffer */
94  uint8_t char_match; /**< Character to be matched */
95  uint8_t char_found; /**< State of the matched character */
96 } serial_t;
97 
98 #else
99 /** Non-asynch serial HAL structure
100  */
101 typedef struct serial_s serial_t;
102 
103 #endif
104 
105 #ifdef __cplusplus
106 extern "C" {
107 #endif
108 
109 /**
110  * \defgroup hal_GeneralSerial Serial Configuration Functions
111  * @{
112  */
113 
114 /** Initialize the serial peripheral. It sets the default parameters for serial
115  * peripheral, and configures its specifieds pins.
116  *
117  * @param obj The serial object
118  * @param tx The TX pin name
119  * @param rx The RX pin name
120  */
121 void serial_init(serial_t *obj, PinName tx, PinName rx);
122 
123 /** Release the serial peripheral, not currently invoked. It requires further
124  * resource management.
125  *
126  * @param obj The serial object
127  */
128 void serial_free(serial_t *obj);
129 
130 /** Configure the baud rate
131  *
132  * @param obj The serial object
133  * @param baudrate The baud rate to be configured
134  */
135 void serial_baud(serial_t *obj, int baudrate);
136 
137 /** Configure the format. Set the number of bits, parity and the number of stop bits
138  *
139  * @param obj The serial object
140  * @param data_bits The number of data bits
141  * @param parity The parity
142  * @param stop_bits The number of stop bits
143  */
144 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
145 
146 /** The serial interrupt handler registration
147  *
148  * @param obj The serial object
149  * @param handler The interrupt handler which will be invoked when the interrupt fires
150  * @param id The SerialBase object
151  */
152 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
153 
154 /** Configure serial interrupt. This function is used for word-approach
155  *
156  * @param obj The serial object
157  * @param irq The serial IRQ type (RX or TX)
158  * @param enable Set to non-zero to enable events, or zero to disable them
159  */
160 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable);
161 
162 /** Get character. This is a blocking call, waiting for a character
163  *
164  * @param obj The serial object
165  */
166 int serial_getc(serial_t *obj);
167 
168 /** Send a character. This is a blocking call, waiting for a peripheral to be available
169  * for writing
170  *
171  * @param obj The serial object
172  * @param c The character to be sent
173  */
174 void serial_putc(serial_t *obj, int c);
175 
176 /** Check if the serial peripheral is readable
177  *
178  * @param obj The serial object
179  * @return Non-zero value if a character can be read, 0 if nothing to read
180  */
181 int serial_readable(serial_t *obj);
182 
183 /** Check if the serial peripheral is writable
184  *
185  * @param obj The serial object
186  * @return Non-zero value if a character can be written, 0 otherwise.
187  */
188 int serial_writable(serial_t *obj);
189 
190 /** Clear the serial peripheral
191  *
192  * @param obj The serial object
193  */
194 void serial_clear(serial_t *obj);
195 
196 /** Set the break
197  *
198  * @param obj The serial object
199  */
200 void serial_break_set(serial_t *obj);
201 
202 /** Clear the break
203  *
204  * @param obj The serial object
205  */
206 void serial_break_clear(serial_t *obj);
207 
208 /** Configure the TX pin for UART function.
209  *
210  * @param tx The pin name used for TX
211  */
212 void serial_pinout_tx(PinName tx);
213 
214 /** Configure the serial for the flow control. It sets flow control in the hardware
215  * if a serial peripheral supports it, otherwise software emulation is used.
216  *
217  * @param obj The serial object
218  * @param type The type of the flow control. Look at the available FlowControl types.
219  * @param rxflow The TX pin name
220  * @param txflow The RX pin name
221  */
222 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow);
223 
224 /** Get the pins that support Serial TX
225  *
226  * Return a PinMap array of pins that support Serial TX. The
227  * array is terminated with {NC, NC, 0}.
228  *
229  * @return PinMap array
230  */
231 const PinMap *serial_tx_pinmap(void);
232 
233 /** Get the pins that support Serial RX
234  *
235  * Return a PinMap array of pins that support Serial RX. The
236  * array is terminated with {NC, NC, 0}.
237  *
238  * @return PinMap array
239  */
240 const PinMap *serial_rx_pinmap(void);
241 
242 /** Get the pins that support Serial CTS
243  *
244  * Return a PinMap array of pins that support Serial CTS. The
245  * array is terminated with {NC, NC, 0}.
246  *
247  * @return PinMap array
248  */
249 const PinMap *serial_cts_pinmap(void);
250 
251 /** Get the pins that support Serial RTS
252  *
253  * Return a PinMap array of pins that support Serial RTS. The
254  * array is terminated with {NC, NC, 0}.
255  *
256  * @return PinMap array
257  */
258 const PinMap *serial_rts_pinmap(void);
259 
260 #if DEVICE_SERIAL_ASYNCH
261 
262 /**@}*/
263 
264 /**
265  * \defgroup hal_AsynchSerial Asynchronous Serial Hardware Abstraction Layer
266  * @{
267  */
268 
269 /** Begin asynchronous TX transfer. The used buffer is specified in the serial object,
270  * tx_buff
271  *
272  * @param obj The serial object
273  * @param tx The transmit buffer
274  * @param tx_length The number of bytes to transmit
275  * @param tx_width Deprecated argument
276  * @param handler The serial handler
277  * @param event The logical OR of events to be registered
278  * @param hint A suggestion for how to use DMA with this transfer
279  * @return Returns number of data transfered, otherwise returns 0
280  */
281 int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint);
282 
283 /** Begin asynchronous RX transfer (enable interrupt for data collecting)
284  * The used buffer is specified in the serial object - rx_buff
285  *
286  * @param obj The serial object
287  * @param rx The receive buffer
288  * @param rx_length The number of bytes to receive
289  * @param rx_width Deprecated argument
290  * @param handler The serial handler
291  * @param event The logical OR of events to be registered
292  * @param handler The serial handler
293  * @param char_match A character in range 0-254 to be matched
294  * @param hint A suggestion for how to use DMA with this transfer
295  */
296 void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint);
297 
298 /** Attempts to determine if the serial peripheral is already in use for TX
299  *
300  * @param obj The serial object
301  * @return Non-zero if the RX transaction is ongoing, 0 otherwise
302  */
303 uint8_t serial_tx_active(serial_t *obj);
304 
305 /** Attempts to determine if the serial peripheral is already in use for RX
306  *
307  * @param obj The serial object
308  * @return Non-zero if the RX transaction is ongoing, 0 otherwise
309  */
310 uint8_t serial_rx_active(serial_t *obj);
311 
312 /** The asynchronous TX and RX handler.
313  *
314  * @param obj The serial object
315  * @return Returns event flags if an RX transfer termination condition was met; otherwise returns 0
316  */
318 
319 /** Abort the ongoing TX transaction. It disables the enabled interupt for TX and
320  * flushes the TX hardware buffer if TX FIFO is used
321  *
322  * @param obj The serial object
323  */
325 
326 /** Abort the ongoing RX transaction. It disables the enabled interrupt for RX and
327  * flushes the RX hardware buffer if RX FIFO is used
328  *
329  * @param obj The serial object
330  */
332 
333 /**@}*/
334 
335 #endif
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #endif
342 
343 #endif
344 
345 /** @}*/
const PinMap * serial_tx_pinmap(void)
Get the pins that support Serial TX.
void serial_baud(serial_t *obj, int baudrate)
Configure the baud rate.
Generic buffer structure.
Definition: buffer.h:27
void serial_tx_abort_asynch(serial_t *obj)
Abort the ongoing TX transaction.
void serial_break_clear(serial_t *obj)
Clear the break.
void serial_rx_asynch(serial_t *obj, void *rx, size_t rx_length, uint8_t rx_width, uint32_t handler, uint32_t event, uint8_t char_match, DMAUsage hint)
Begin asynchronous RX transfer (enable interrupt for data collecting) The used buffer is specified in...
int serial_writable(serial_t *obj)
Check if the serial peripheral is writable.
int serial_getc(serial_t *obj)
Get character.
const PinMap * serial_cts_pinmap(void)
Get the pins that support Serial CTS.
int serial_irq_handler_asynch(serial_t *obj)
The asynchronous TX and RX handler.
uint8_t char_match
Character to be matched.
Definition: serial_api.h:94
void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits)
Configure the format.
void serial_free(serial_t *obj)
Release the serial peripheral, not currently invoked.
void serial_rx_abort_asynch(serial_t *obj)
Abort the ongoing RX transaction.
void serial_init(serial_t *obj, PinName tx, PinName rx)
Initialize the serial peripheral.
void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id)
The serial interrupt handler registration.
const PinMap * serial_rts_pinmap(void)
Get the pins that support Serial RTS.
void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable)
Configure serial interrupt.
const PinMap * serial_rx_pinmap(void)
Get the pins that support Serial RX.
void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow)
Configure the serial for the flow control.
Definition: pinmap.h:30
Asynch serial HAL structure.
Definition: serial_api.h:90
uint8_t serial_rx_active(serial_t *obj)
Attempts to determine if the serial peripheral is already in use for RX.
uint8_t char_found
State of the matched character.
Definition: serial_api.h:95
int serial_tx_asynch(serial_t *obj, const void *tx, size_t tx_length, uint8_t tx_width, uint32_t handler, uint32_t event, DMAUsage hint)
Begin asynchronous TX transfer.
void serial_clear(serial_t *obj)
Clear the serial peripheral.
void serial_break_set(serial_t *obj)
Set the break.
void serial_pinout_tx(PinName tx)
Configure the TX pin for UART function.
uint8_t serial_tx_active(serial_t *obj)
Attempts to determine if the serial peripheral is already in use for TX.
void serial_putc(serial_t *obj, int c)
Send a character.
int serial_readable(serial_t *obj)
Check if the serial peripheral is readable.
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.