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 /** Serial interrupt sources
74  */
75 typedef enum {
76  RxIrq, /**< Receive Data Register Full */
77  TxIrq /**< Transmit Data Register Empty */
78 } SerialIrq;
79 
80 typedef enum {
81  FlowControlNone,
82  FlowControlRTS,
83  FlowControlCTS,
84  FlowControlRTSCTS
85 } FlowControl;
86 
87 typedef void (*uart_irq_handler)(uint32_t id, SerialIrq event);
88 
89 #if DEVICE_SERIAL_ASYNCH
90 /** Asynch serial HAL structure
91  */
92 typedef struct {
93  struct serial_s serial; /**< Target specific serial structure */
94  struct buffer_s tx_buff; /**< TX buffer */
95  struct buffer_s rx_buff; /**< RX buffer */
96  uint8_t char_match; /**< Character to be matched */
97  uint8_t char_found; /**< State of the matched character */
98 } serial_t;
99 
100 #else
101 /** Non-asynch serial HAL structure
102  */
103 typedef struct serial_s serial_t;
104 
105 #endif
106 
107 #ifdef __cplusplus
108 extern "C" {
109 #endif
110 
111 /**
112  * \defgroup hal_GeneralSerial Serial Configuration Functions
113  *
114  * # Defined behavior
115  * * ::serial_init initializes the ::serial_t
116  * * ::serial_init sets the default parameters for serial peripheral (9600 bps, 8N1 format)
117  * * ::serial_init configures the specified pins
118  * * ::serial_free releases the serial peripheral
119  * * ::serial_baud configures the baud rate
120  * * at least 9600 bps the baud rate must be supported
121  * * ::serial_format configures the transmission format (number of bits, parity and the number of stop bits)
122  * * at least 8N1 format must be supported
123  * * ::serial_irq_handler registers the interrupt handler which will be invoked when the interrupt fires.
124  * * ::serial_irq_set enables or disables the serial RX or TX IRQ.
125  * * If `RxIrq` is enabled by ::serial_irq_set, ::serial_irq_handler will be invoked whenever
126  * Receive Data Register Full IRQ is generated.
127  * * If `TxIrq` is enabled by ::serial_irq_set, ::serial_irq_handler will be invoked whenever
128  * Transmit Data Register Empty IRQ is generated.
129  * * If the interrupt condition holds true, when the interrupt is enabled with ::serial_irq_set,
130  * the ::serial_irq_handler is called instantly.
131  * * ::serial_getc returns the character from serial buffer.
132  * * ::serial_getc is a blocking call (waits for the character).
133  * * ::serial_putc sends a character.
134  * * ::serial_putc is a blocking call (waits for a peripheral to be available).
135  * * ::serial_readable returns non-zero value if a character can be read, 0 otherwise.
136  * * ::serial_writable returns non-zero value if a character can be written, 0 otherwise.
137  * * ::serial_clear clears the ::serial_t RX/TX buffers
138  * * ::serial_break_set sets the break signal.
139  * * ::serial_break_clear clears the break signal.
140  * * ::serial_pinout_tx configures the TX pin as an output (to be used in half-duplex mode).
141  * * ::serial_set_flow_control configures serial flow control.
142  * * ::serial_set_flow_control sets flow control in the hardware if a serial peripheral supports it,
143  * otherwise software emulation is used.
144  * * ::serial_tx_asynch starts the serial asynchronous transfer.
145  * * ::serial_tx_asynch writes `tx_length` bytes from the `tx` to the bus.
146  * * ::serial_tx_asynch must support 8 data bits
147  * * The callback given to ::serial_tx_asynch is invoked when the transfer completes.
148  * * ::serial_tx_asynch specifies the logical OR of events to be registered.
149  * * The ::serial_tx_asynch function may use the `DMAUsage` hint to select the appropriate async algorithm.
150  * * ::serial_rx_asynch starts the serial asynchronous transfer.
151  * * ::serial_rx_asynch reads `rx_length` bytes to the `rx` buffer.
152  * * ::serial_rx_asynch must support 8 data bits
153  * * The callback given to ::serial_rx_asynch is invoked when the transfer completes.
154  * * ::serial_rx_asynch specifies the logical OR of events to be registered.
155  * * The ::serial_rx_asynch function may use the `DMAUsage` hint to select the appropriate async algorithm.
156  * * ::serial_rx_asynch specifies a character in range 0-254 to be matched, 255 is a reserved value.
157  * * If SERIAL_EVENT_RX_CHARACTER_MATCH event is not registered, the `char_match` is ignored.
158  * * The SERIAL_EVENT_RX_CHARACTER_MATCH event is set in the callback when SERIAL_EVENT_RX_CHARACTER_MATCH event is
159  * registered AND `char_match` is present in the received data.
160  * * ::serial_tx_active returns non-zero if the TX transaction is ongoing, 0 otherwise.
161  * * ::serial_rx_active returns non-zero if the RX transaction is ongoing, 0 otherwise.
162  * * ::serial_irq_handler_asynch returns event flags if a transfer termination condition was met, otherwise returns 0.
163  * * ::serial_irq_handler_asynch takes no longer than one packet transfer time (packet_bits / baudrate) to execute.
164  * * ::serial_tx_abort_asynch aborts the ongoing TX transaction.
165  * * ::serial_tx_abort_asynch disables the enabled interupt for TX.
166  * * ::serial_tx_abort_asynch flushes the TX hardware buffer if TX FIFO is used.
167  * * ::serial_rx_abort_asynch aborts the ongoing RX transaction.
168  * * ::serial_rx_abort_asynch disables the enabled interupt for RX.
169  * * ::serial_rx_abort_asynch flushes the TX hardware buffer if RX FIFO is used.
170  * * Correct operation guaranteed when interrupt latency is shorter than one packet transfer time (packet_bits / baudrate)
171  * if the flow control is not used.
172  * * Correct operation guaranteed regardless of interrupt latency if the flow control is used.
173  *
174  * # Undefined behavior
175  * * Calling ::serial_init multiple times on the same `serial_t` without ::serial_free.
176  * * Passing invalid pin to ::serial_init, ::serial_pinout_tx.
177  * * Calling any function other than ::serial_init on am uninitialized or freed `serial_t`.
178  * * Passing an invalid pointer as `obj` to any function.
179  * * Passing an invalid pointer as `handler` to ::serial_irq_handler, ::serial_tx_asynch, ::serial_rx_asynch.
180  * * Calling ::serial_tx_abort while no async TX transfer is being processed.
181  * * Calling ::serial_rx_abort while no async RX transfer is being processed.
182  * * Devices behavior is undefined when the interrupt latency is longer than one packet transfer time
183  * (packet_bits / baudrate) if the flow control is not used.
184  * @{
185  */
186 
187 /**
188  * \defgroup hal_GeneralSerial_tests Serial hal tests
189  * The Serial HAL tests ensure driver conformance to defined behavior.
190  *
191  * To run the Serial hal tests use the command:
192  *
193  * mbed test -t <toolchain> -m <target> -n tests-mbed_hal_fpga_ci_test_shield-uart
194  *
195  */
196 
197 /** Initialize the serial peripheral. It sets the default parameters for serial
198  * peripheral, and configures its specifieds pins.
199  *
200  * @param obj The serial object
201  * @param tx The TX pin name
202  * @param rx The RX pin name
203  */
204 void serial_init(serial_t *obj, PinName tx, PinName rx);
205 
206 /** Release the serial peripheral, not currently invoked. It requires further
207  * resource management.
208  *
209  * @param obj The serial object
210  */
211 void serial_free(serial_t *obj);
212 
213 /** Configure the baud rate
214  *
215  * @param obj The serial object
216  * @param baudrate The baud rate to be configured
217  */
218 void serial_baud(serial_t *obj, int baudrate);
219 
220 /** Configure the format. Set the number of bits, parity and the number of stop bits
221  *
222  * @param obj The serial object
223  * @param data_bits The number of data bits
224  * @param parity The parity
225  * @param stop_bits The number of stop bits
226  */
227 void serial_format(serial_t *obj, int data_bits, SerialParity parity, int stop_bits);
228 
229 /** The serial interrupt handler registration
230  *
231  * @param obj The serial object
232  * @param handler The interrupt handler which will be invoked when the interrupt fires
233  * @param id The SerialBase object
234  */
235 void serial_irq_handler(serial_t *obj, uart_irq_handler handler, uint32_t id);
236 
237 /** Configure serial interrupt. This function is used for word-approach
238  *
239  * @param obj The serial object
240  * @param irq The serial IRQ type (RX or TX)
241  * @param enable Set to non-zero to enable events, or zero to disable them
242  */
243 void serial_irq_set(serial_t *obj, SerialIrq irq, uint32_t enable);
244 
245 /** Get character. This is a blocking call, waiting for a character
246  *
247  * @param obj The serial object
248  */
249 int serial_getc(serial_t *obj);
250 
251 /** Send a character. This is a blocking call, waiting for a peripheral to be available
252  * for writing
253  *
254  * @param obj The serial object
255  * @param c The character to be sent
256  */
257 void serial_putc(serial_t *obj, int c);
258 
259 /** Check if the serial peripheral is readable
260  *
261  * @param obj The serial object
262  * @return Non-zero value if a character can be read, 0 if nothing to read
263  */
264 int serial_readable(serial_t *obj);
265 
266 /** Check if the serial peripheral is writable
267  *
268  * @param obj The serial object
269  * @return Non-zero value if a character can be written, 0 otherwise.
270  */
271 int serial_writable(serial_t *obj);
272 
273 /** Clear the serial peripheral
274  *
275  * @param obj The serial object
276  */
277 void serial_clear(serial_t *obj);
278 
279 /** Set the break
280  *
281  * @param obj The serial object
282  */
283 void serial_break_set(serial_t *obj);
284 
285 /** Clear the break
286  *
287  * @param obj The serial object
288  */
289 void serial_break_clear(serial_t *obj);
290 
291 /** Configure the TX pin for UART function.
292  *
293  * @param tx The pin name used for TX
294  */
295 void serial_pinout_tx(PinName tx);
296 
297 #if DEVICE_SERIAL_FC
298 /** Configure the serial for the flow control. It sets flow control in the hardware
299  * if a serial peripheral supports it, otherwise software emulation is used.
300  *
301  * @param obj The serial object
302  * @param type The type of the flow control. Look at the available FlowControl types.
303  * @param rxflow The TX pin name
304  * @param txflow The RX pin name
305  */
306 void serial_set_flow_control(serial_t *obj, FlowControl type, PinName rxflow, PinName txflow);
307 #endif
308 
309 /** Get the pins that support Serial TX
310  *
311  * Return a PinMap array of pins that support Serial TX. The
312  * array is terminated with {NC, NC, 0}.
313  *
314  * @return PinMap array
315  */
316 const PinMap *serial_tx_pinmap(void);
317 
318 /** Get the pins that support Serial RX
319  *
320  * Return a PinMap array of pins that support Serial RX. The
321  * array is terminated with {NC, NC, 0}.
322  *
323  * @return PinMap array
324  */
325 const PinMap *serial_rx_pinmap(void);
326 
327 #if DEVICE_SERIAL_FC
328 /** Get the pins that support Serial CTS
329  *
330  * Return a PinMap array of pins that support Serial CTS. The
331  * array is terminated with {NC, NC, 0}.
332  *
333  * @return PinMap array
334  */
335 const PinMap *serial_cts_pinmap(void);
336 
337 /** Get the pins that support Serial RTS
338  *
339  * Return a PinMap array of pins that support Serial RTS. The
340  * array is terminated with {NC, NC, 0}.
341  *
342  * @return PinMap array
343  */
344 const PinMap *serial_rts_pinmap(void);
345 #endif
346 
347 #if DEVICE_SERIAL_ASYNCH
348 
349 /**@}*/
350 
351 /**
352  * \defgroup hal_AsynchSerial Asynchronous Serial Hardware Abstraction Layer
353  * @{
354  */
355 
356 /** Begin asynchronous TX transfer. The used buffer is specified in the serial object,
357  * tx_buff
358  *
359  * @param obj The serial object
360  * @param tx The transmit buffer
361  * @param tx_length The number of bytes to transmit
362  * @param tx_width Deprecated argument
363  * @param handler The serial handler
364  * @param event The logical OR of events to be registered
365  * @param hint A suggestion for how to use DMA with this transfer
366  * @return Returns number of data transfered, otherwise returns 0
367  */
368 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);
369 
370 /** Begin asynchronous RX transfer (enable interrupt for data collecting)
371  * The used buffer is specified in the serial object - rx_buff
372  *
373  * @param obj The serial object
374  * @param rx The receive buffer
375  * @param rx_length The number of bytes to receive
376  * @param rx_width Deprecated argument
377  * @param handler The serial handler
378  * @param event The logical OR of events to be registered
379  * @param char_match A character in range 0-254 to be matched
380  * @param hint A suggestion for how to use DMA with this transfer
381  */
382 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);
383 
384 /** Attempts to determine if the serial peripheral is already in use for TX
385  *
386  * @param obj The serial object
387  * @return Non-zero if the RX transaction is ongoing, 0 otherwise
388  */
389 uint8_t serial_tx_active(serial_t *obj);
390 
391 /** Attempts to determine if the serial peripheral is already in use for RX
392  *
393  * @param obj The serial object
394  * @return Non-zero if the RX transaction is ongoing, 0 otherwise
395  */
396 uint8_t serial_rx_active(serial_t *obj);
397 
398 /** The asynchronous TX and RX handler.
399  *
400  * @param obj The serial object
401  * @return Returns event flags if an RX transfer termination condition was met; otherwise returns 0
402  */
404 
405 /** Abort the ongoing TX transaction. It disables the enabled interupt for TX and
406  * flushes the TX hardware buffer if TX FIFO is used
407  *
408  * @param obj The serial object
409  */
411 
412 /** Abort the ongoing RX transaction. It disables the enabled interrupt for RX and
413  * flushes the RX hardware buffer if RX FIFO is used
414  *
415  * @param obj The serial object
416  */
418 
419 /**@}*/
420 
421 #endif
422 
423 #ifdef __cplusplus
424 }
425 #endif
426 
427 #endif
428 
429 #endif
430 
431 /** @}*/
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.
Transmit Data Register Empty.
Definition: serial_api.h:77
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:96
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.
SerialIrq
Serial interrupt sources.
Definition: serial_api.h:75
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.
Receive Data Register Full.
Definition: serial_api.h:76
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:92
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:97
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.