px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_uart.h
1#ifndef __PX_UART_H__
2#define __PX_UART_H__
3/* =============================================================================
4 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
5 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
6 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
7 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
8 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
9
10 Copyright (c) 2012 Pieter Conradie <https://piconomix.com>
11
12 License: MIT
13 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
14
15 Title: px_uart.h : UART peripheral driver
16 Author(s): Pieter Conradie
17 Creation Date: 2012-06-10
18
19============================================================================= */
20
21/**
22 * @ingroup AVR
23 * @defgroup AVR_UART px_uart.h : UART peripheral driver
24 *
25 * Driver for the UART peripheral for serial communication.
26 *
27 * File(s):
28 * - arch/avr/inc/px_uart.h
29 * - arch/avr/inc/px_uart_cfg_template.h
30 * - arch/avr/src/px_uart.c
31 *
32 * The driver must be configured by supplying a project specific
33 * "px_uart_cfg.h". "px_uart_cfg_template.h" can be copied, renamed and
34 * modified to supply compile time options.
35 *
36 * @include "avr/test/px_uart_test.c"
37 *
38 * @{
39 */
40
41/* _____STANDARD INCLUDES____________________________________________________ */
42
43/* _____PROJECT INCLUDES_____________________________________________________ */
44#include "px_defs.h"
45
46// Include project specific configuration. See "px_uart_cfg_template.h"
47#include "px_uart_cfg.h"
48
49// Check that all project specific options have been specified in "px_uart_cfg.h"
50#if ( !defined(PX_UART_CFG_UART0_EN ) \
51 || !defined(PX_UART_CFG_UART1_EN ) \
52 || !defined(PX_UART_CFG_DEFAULT_BAUD ) \
53 || !defined(PX_UART_CFG_DEFAULT_UCSRC) \
54 || !defined(PX_UART_CFG_TX_BUF_SIZE ) \
55 || !defined(PX_UART_CFG_RX_BUF_SIZE ) )
56#error "One or more options not defined in 'px_uart_cfg.h'"
57#endif
58#if ( (PX_UART_CFG_UART0_EN > 1) \
59 || (PX_UART_CFG_UART1_EN > 1) )
60#error "PX_UART_CFG_UARTx_EN must be 0 or 1"
61#endif
62
63/// Number of enabled peripherals
64#define PX_UART_CFG_PER_COUNT ( PX_UART_CFG_UART0_EN \
65 + PX_UART_CFG_UART1_EN )
66#if (PX_UART_CFG_PER_COUNT == 0)
67#error "No peripherals enabled"
68#endif
69
70#ifdef __cplusplus
71extern "C" {
72#endif
73/* _____DEFINITIONS__________________________________________________________ */
74
75/* _____TYPE DEFINITIONS_____________________________________________________ */
76/// Specify UART peripheral
77typedef enum
78{
79 PX_UART_NR_0 = 0,
80 PX_UART_NR_1 = 1,
82
83/// Specify UART parity (none, odd or even)
84typedef enum
85{
86 PX_UART_PARITY_NONE = 0,
87 PX_UART_PARITY_ODD,
88 PX_UART_PARITY_EVEN,
90
91// Specify number of data bits used (5..8)
92typedef enum
93{
94 PX_UART_DATA_BITS_5 = 5,
95 PX_UART_DATA_BITS_6 = 6,
96 PX_UART_DATA_BITS_7 = 7,
97 PX_UART_DATA_BITS_8 = 8,
99
100/// Specify number of stop bits (1/2)
101typedef enum
102{
103 PX_UART_STOP_BITS_1 = 1,
104 PX_UART_STOP_BITS_2 = 2,
106
107/// Define UART handle
108typedef struct
109{
110 struct px_uart_per_s * uart_per; ///< USART peripheral data
112
113/* _____GLOBAL VARIABLES_____________________________________________________ */
114
115/* _____GLOBAL FUNCTION DECLARATIONS_________________________________________ */
116/**
117 * Initialise UART driver.
118 */
119void px_uart_init(void);
120
121/**
122 * Open UART peripheral using predefined (default) parameters
123 *
124 * @param handle Pointer to handle data structure
125 * @param uart_nr UART peripheral number. @see px_uart_nr_t
126 *
127 * @retval false Error: peripheral was not opened
128 * @retval true Success: peripheral was opened
129 */
130bool px_uart_open(px_uart_handle_t * handle,
131 px_uart_nr_t uart_nr);
132
133/**
134 * Open UART peripheral using specified parameters
135 *
136 * @param handle Pointer to handle data structure
137 * @param uart_nr UART peripheral number. @see px_uart_nr_t
138 * @param baud Baud rate in bits/s
139 * @param data_bits Data bits. @see px_uart_data_bits_t
140 * @param parity Parity. @see px_uart_parity_t
141 * @param stop_bits Stop bits. @see px_uart_stop_bits_t
142 *
143 * @retval false Error: peripheral was not opened
144 * @retval true Success: peripheral was opened
145 */
146bool px_uart_open2(px_uart_handle_t * handle,
147 px_uart_nr_t uart_nr,
148 uint32_t baud,
149 px_uart_data_bits_t data_bits,
150 px_uart_parity_t parity,
151 px_uart_stop_bits_t stop_bits);
152
153/**
154 * Close specified peripheral.
155 *
156 * @param handle Pointer to handle data structure
157 *
158 * @retval true Success
159 * @retval false Specified peripheral was already closed (or not opened)
160 */
161bool px_uart_close(px_uart_handle_t * handle);
162
163/**
164 * Write one byte.
165 *
166 * This function blocks until space is available in the write buffer.
167 *
168 * @param handle Pointer to handle data structure
169 * @param[in] data Byte to be written
170 */
171void px_uart_putchar(px_uart_handle_t * handle, char data);
172
173/**
174 * Buffer one byte for transmission.
175 *
176 * @param handle Pointer to handle data structure
177 * @param[in] data Byte to be written
178 *
179 * @retval true Byte has been buffered
180 * @retval false Byte has not been buffered, because write buffer is full
181 */
182bool px_uart_wr_u8(px_uart_handle_t * handle, uint8_t data);
183
184/**
185 * Buffer byte(s) for transmission.
186 *
187 * @note The write buffer may not be able to hold all of the specified
188 * data.
189 *
190 * @param handle Pointer to handle data structure
191 * @param[in] data Buffer containing data for transmission
192 * @param[in] nr_of_bytes Number of bytes in buffer to be written
193 *
194 * @return size_t The actual number of bytes buffered for transmission.
195 */
196size_t px_uart_wr(px_uart_handle_t * handle,
197 const void * data,
198 size_t nr_of_bytes);
199
200/**
201 * Read one byte.
202 *
203 * This function blocks until a byte is received.
204 *
205 * @param handle Pointer to handle data structure
206 *
207 * @return Received byte
208 */
209char px_uart_getchar(px_uart_handle_t * handle);
210
211/**
212 * See if a received byte is available and store it in the specified location.
213 *
214 * @param handle Pointer to handle data structure
215 * @param[out] data Pointer to location where data byte must be stored
216 *
217 * @retval true Received byte is stored in specified location
218 * @retval false No received data available (receive buffer empty)
219 */
220bool px_uart_rd_u8(px_uart_handle_t * handle, uint8_t * data);
221
222/**
223 * Copy received data from receive buffer into specified buffer.
224 *
225 * @param handle Pointer to handle data structure
226 * @param[out] buf Buffer to copy received data into
227 * @param[in] nr_of_bytes Maximum number of received bytes to copy into buffer
228 *
229 * @return size_t Number of received bytes copied into buffer
230 */
231size_t px_uart_rd(px_uart_handle_t * handle,
232 void * buf,
233 size_t nr_of_bytes);
234
235/**
236 * See if transmit buffer can accept more data.
237 *
238 * @param handle Pointer to handle data structure
239 *
240 * @retval true Transmit buffer is full
241 * @retval false Transmit buffer has space for at least one byte
242 */
244
245/**
246 * See if transmit buffer is empty.
247 *
248 * @note Buffer may be empty, but UART peripheral may still be busy
249 * with the transmission of the last byte in the buffer.
250 * @see px_uart_tx_finished.
251 *
252 * @param handle Pointer to handle data structure
253 *
254 * @retval true Transmit buffer is empty
255 * @retval false Transmit buffer has space for at least one byte
256 */
258
259/**
260 * See if all transmission has finished, including last byte.
261 *
262 * This functions is usefull for communication standards like RS-485
263 * where the mode must be changed manually from TX to RX after transmission.
264 *
265 * @param handle Pointer to handle data structure
266 *
267 * @retval true Transmision completely finished
268 * @retval false Busy with transmission
269 */
271
272/**
273 * See if there is received data in the receive buffer.
274 *
275 * @param handle Pointer to handle data structure
276 *
277 * @retval true There is received data in the receive buffer
278 * @retval false The receive buffer is empty
279 */
281
282/**
283 * Change UART peripheral baud rate.
284 *
285 * @param handle Pointer to handle data structure
286 * @param baud Baud rate in bits/s
287 *
288 * @retval true Success. Baud was changed
289 * @retval false Error. Requested baud invalid
290 */
291bool px_uart_change_baud(px_uart_handle_t * handle, uint32_t baud);
292
293/* _____MACROS_______________________________________________________________ */
294
295#ifdef __cplusplus
296}
297#endif
298
299/// @}
300#endif
bool px_uart_rd_buf_is_empty(px_uart_handle_t *handle)
See if there is received data in the receive buffer.
Definition: px_uart.c:817
bool px_uart_wr_u8(px_uart_handle_t *handle, uint8_t data)
Buffer one byte for transmission.
Definition: px_uart.c:651
bool px_uart_open2(px_uart_handle_t *handle, px_uart_nr_t uart_nr, uint32_t baud, px_uart_data_bits_t data_bits, px_uart_parity_t parity, px_uart_stop_bits_t stop_bits)
Open UART peripheral using specified parameters.
Definition: px_uart.c:483
bool px_uart_rd_u8(px_uart_handle_t *handle, uint8_t *data)
See if a received byte is available and store it in the specified location.
Definition: px_uart.c:726
bool px_uart_close(px_uart_handle_t *handle)
Close specified peripheral.
Definition: px_uart.c:544
px_uart_parity_t
Specify UART parity (none, odd or even)
Definition: px_uart.h:118
px_uart_data_bits_t
Specify number of data bits used (7..9)
Definition: px_uart.h:126
size_t px_uart_rd(px_uart_handle_t *handle, void *buf, size_t nr_of_bytes)
Copy received data from receive buffer into specified buffer.
Definition: px_uart.c:743
void px_uart_putchar(px_uart_handle_t *handle, char data)
Write one byte.
Definition: px_uart.c:632
bool px_uart_wr_buf_is_empty(px_uart_handle_t *handle)
See if transmit buffer is empty.
Definition: px_uart.c:778
size_t px_uart_wr(px_uart_handle_t *handle, const void *data, size_t nr_of_bytes)
Buffer byte(s) for transmission.
Definition: px_uart.c:677
px_uart_stop_bits_t
Specify number of stop bits (1/2)
Definition: px_uart.h:134
px_uart_nr_t
Specify UART peripheral.
Definition: px_uart.h:108
void px_uart_init(void)
Initialise UART driver.
Definition: px_uart.c:452
bool px_uart_change_baud(px_uart_handle_t *handle, uint32_t baud)
Change UART peripheral baud rate.
Definition: px_uart.c:833
char px_uart_getchar(px_uart_handle_t *handle)
Read one byte.
Definition: px_uart.c:706
bool px_uart_wr_is_done(px_uart_handle_t *handle)
See if all transmission has finished, including last byte.
Definition: px_uart.c:794
bool px_uart_wr_buf_is_full(px_uart_handle_t *handle)
See if transmit buffer can accept more data.
Definition: px_uart.c:762
bool px_uart_open(px_uart_handle_t *handle, px_uart_nr_t uart_nr)
Open UART peripheral using predefined (default) parameters.
Definition: px_uart.c:472
Define UART handle.
Definition: px_uart.h:141