uart.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014-2015 Freie Universität Berlin
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
58 #ifndef PERIPH_UART_H
59 #define PERIPH_UART_H
60 
61 #include <stddef.h>
62 #include <stdint.h>
63 #include <limits.h>
64 
65 #include "periph_cpu.h"
66 #include "periph_conf.h"
67 
68 #ifdef __cplusplus
69 extern "C" {
70 #endif
71 
75 #ifndef HAVE_UART_T
76 typedef unsigned int uart_t;
77 #endif
78 
82 #ifndef UART_UNDEF
83 #define UART_UNDEF (UINT_MAX)
84 #endif
85 
89 #ifndef UART_DEV
90 #define UART_DEV(x) (x)
91 #endif
92 
99 typedef void(*uart_rx_cb_t)(void *arg, uint8_t data);
100 
104 #ifndef HAVE_UART_ISR_CTX_T
105 typedef struct {
107  void *arg;
109 #endif
110 
114 enum {
115  UART_OK = 0,
116  UART_NODEV = -1,
117  UART_NOBAUD = -2,
118  UART_INTERR = -3,
120 };
121 
125 #ifndef HAVE_UART_PARITY_T
126 typedef enum {
132 } uart_parity_t;
133 #endif
134 
138 #ifndef HAVE_UART_DATA_BITS_T
139 typedef enum {
145 #endif
146 
150 #ifndef HAVE_UART_STOP_BITS_T
151 typedef enum {
155 #endif
156 
181 int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg);
182 
183 #if defined(MODULE_PERIPH_UART_RECONFIGURE) || DOXYGEN
184 
199 void uart_deinit_pins(uart_t uart);
200 
216 void uart_init_pins(uart_t uart);
217 
218 #if DOXYGEN
219 
229 gpio_t uart_pin_rx(uart_t uart);
230 
241 gpio_t uart_pin_tx(uart_t uart);
242 
243 #endif /* DOXYGEN */
244 #endif /* MODULE_PERIPH_UART_RECONFIGURE */
245 
257 int uart_mode(uart_t uart, uart_data_bits_t data_bits, uart_parity_t parity,
258  uart_stop_bits_t stop_bits);
259 
272 void uart_write(uart_t uart, const uint8_t *data, size_t len);
273 
279 void uart_poweron(uart_t uart);
280 
286 void uart_poweroff(uart_t uart);
287 
288 #ifdef __cplusplus
289 }
290 #endif
291 
292 #endif /* PERIPH_UART_H */
293 
UART_NODEV
@ UART_NODEV
invalid UART device given
Definition: uart.h:116
UART_PARITY_NONE
@ UART_PARITY_NONE
no parity
Definition: uart.h:127
UART_DATA_BITS_5
@ UART_DATA_BITS_5
5 data bits
Definition: uart.h:140
uart_stop_bits_t
uart_stop_bits_t
Definition of possible stop bits lengths in a UART frame.
Definition: uart.h:151
UART_NOMODE
@ UART_NOMODE
given mode is not applicable
Definition: uart.h:119
uart_mode
int uart_mode(uart_t uart, uart_data_bits_t data_bits, uart_parity_t parity, uart_stop_bits_t stop_bits)
Setup parity, data and stop bits for a given UART device.
UART_STOP_BITS_2
@ UART_STOP_BITS_2
2 stop bits
Definition: uart.h:153
UART_DATA_BITS_8
@ UART_DATA_BITS_8
8 data bits
Definition: uart.h:143
uart_pin_tx
gpio_t uart_pin_tx(uart_t uart)
Get the TX pin of the given UART.
uart_isr_ctx_t
Interrupt context for a UART device.
Definition: uart.h:105
uart_deinit_pins
void uart_deinit_pins(uart_t uart)
Change the pins of the given UART back to plain GPIO functionality.
UART_DATA_BITS_6
@ UART_DATA_BITS_6
6 data bits
Definition: uart.h:141
UART_INTERR
@ UART_INTERR
all other internal errors
Definition: uart.h:118
uart_parity_t
uart_parity_t
Definition of possible parity modes.
Definition: uart.h:126
UART_NOBAUD
@ UART_NOBAUD
given baudrate is not applicable
Definition: uart.h:117
uart_t
unsigned int uart_t
Define default UART type identifier.
Definition: uart.h:76
uart_poweroff
void uart_poweroff(uart_t uart)
Power off the given UART device.
uart_poweron
void uart_poweron(uart_t uart)
Power on the given UART device.
uart_pin_rx
gpio_t uart_pin_rx(uart_t uart)
Get the RX pin of the given UART.
uart_isr_ctx_t::arg
void * arg
argument to both callback routines
Definition: uart.h:107
UART_PARITY_SPACE
@ UART_PARITY_SPACE
space parity
Definition: uart.h:131
uart_isr_ctx_t::rx_cb
uart_rx_cb_t rx_cb
data received interrupt callback
Definition: uart.h:106
uart_init_pins
void uart_init_pins(uart_t uart)
Initialize the used UART pins, i.e.
UART_PARITY_EVEN
@ UART_PARITY_EVEN
even parity
Definition: uart.h:128
UART_OK
@ UART_OK
everything in order
Definition: uart.h:115
UART_DATA_BITS_7
@ UART_DATA_BITS_7
7 data bits
Definition: uart.h:142
UART_PARITY_MARK
@ UART_PARITY_MARK
mark parity
Definition: uart.h:130
UART_STOP_BITS_1
@ UART_STOP_BITS_1
1 stop bit
Definition: uart.h:152
uart_data_bits_t
uart_data_bits_t
Definition of possible data bits lengths in a UART frame.
Definition: uart.h:139
UART_PARITY_ODD
@ UART_PARITY_ODD
odd parity
Definition: uart.h:129
uart_write
void uart_write(uart_t uart, const uint8_t *data, size_t len)
Write data from the given buffer to the specified UART device.
uart_rx_cb_t
void(* uart_rx_cb_t)(void *arg, uint8_t data)
Signature for receive interrupt callback.
Definition: uart.h:99
uart_init
int uart_init(uart_t uart, uint32_t baudrate, uart_rx_cb_t rx_cb, void *arg)
Initialize a given UART device.