XCore SDK
XCore Software Development Kit
uart.h
Go to the documentation of this file.
1 // Copyright 2022 XMOS LIMITED.
2 // This Software is subject to the terms of the XMOS Public Licence: Version 1.
3 #pragma once
4 
9 #include <stdlib.h> /* for size_t */
10 #include <stdint.h>
11 #include <xcore/port.h>
12 #include <xcore/triggerable.h>
13 #include <xcore/hwtimer.h>
14 #include <xcore/interrupt.h>
15 
16 #include "uart_util.h"
17 
29 typedef enum uart_parity_t {
30  UART_PARITY_NONE = 0,
31  UART_PARITY_EVEN,
32  UART_PARITY_ODD
34 
39 #define UART_START_BIT_ERROR_VAL 2
40 typedef enum {
41  UART_RX_COMPLETE = 0,
42  UART_UNDERRUN_ERROR = 1, //Buffered Tx Error only (buffer empty)
43  UART_START_BIT_ERROR = UART_START_BIT_ERROR_VAL, //Rx Only
44  UART_PARITY_ERROR = UART_START_BIT_ERROR_VAL + 1, //Rx Only
45  UART_FRAMING_ERROR = UART_START_BIT_ERROR_VAL + 2, //Rx Only
46  UART_OVERRUN_ERROR = UART_START_BIT_ERROR_VAL + 3, //Buffered Rx only
47 } uart_callback_code_t;
48 
53 typedef enum {
54  UART_IDLE = 0,
55  UART_START,
56  UART_DATA,
57  UART_PARITY,
58  UART_STOP
59 } uart_state_t;
60 
61 
67 #define HIL_UART_TX_CALLBACK_ATTR __attribute__((fptrgroup("hil_uart_tx_callback")))
68 #define HIL_UART_RX_CALLBACK_ATTR __attribute__((fptrgroup("hil_uart_rx_callback")))
69 
70 
77 typedef struct {
78  uart_state_t state;
79  port_t tx_port;
80  uint32_t bit_time_ticks;
81  uint32_t next_event_time_ticks;
82  uart_parity_t parity;
83  uint8_t num_data_bits;
84  uint8_t current_data_bit;
85  uint8_t uart_data;
86  uint8_t stop_bits;
87  uint8_t current_stop_bit;
88 
89  HIL_UART_TX_CALLBACK_ATTR void(*uart_tx_empty_callback_fptr)(void* app_data);
90  void *app_data;
91  hwtimer_t tmr;
92  uart_buffer_t buffer;
93 } uart_tx_t;
94 
101 typedef struct {
102  uart_state_t state;
103  port_t rx_port;
104  uint32_t bit_time_ticks;
105  uint32_t next_event_time_ticks;
106  uart_parity_t parity;
107  uint8_t num_data_bits;
108  uint8_t current_data_bit;
109  uint8_t uart_data;
110  uint8_t stop_bits;
111  uint8_t current_stop_bit;
112 
113  uart_callback_code_t cb_code;
114  HIL_UART_RX_CALLBACK_ATTR void(*uart_rx_complete_callback_arg)(void* app_data);
115  HIL_UART_RX_CALLBACK_ATTR void(*uart_rx_error_callback_arg)(uart_callback_code_t callback_code, void* app_data);
116  void *app_data;
117  hwtimer_t tmr;
118  uart_buffer_t buffer;
119 } uart_rx_t;
120 
147  uart_tx_t *uart,
148  port_t tx_port,
149  uint32_t baud_rate,
150  uint8_t data_bits,
151  uart_parity_t parity,
152  uint8_t stop_bits,
153 
154  hwtimer_t tmr,
155  uint8_t *tx_buff,
156  size_t buffer_size_plus_one,
157  void(*uart_tx_empty_callback_fptr)(void* app_data),
158  void *app_data
159  );
160 
161 
177  uart_tx_t *uart_cfg,
178  port_t tx_port,
179  uint32_t baud_rate,
180  uint8_t num_data_bits,
181  uart_parity_t parity,
182  uint8_t stop_bits,
183  hwtimer_t tmr);
184 
185 
192 void uart_tx(
193  uart_tx_t *uart,
194  uint8_t data);
195 
203  uart_tx_t *uart);
204 
205 
206 
235  uart_rx_t *uart,
236  port_t rx_port,
237  uint32_t baud_rate,
238  uint8_t data_bits,
239  uart_parity_t parity,
240  uint8_t stop_bits,
241 
242  hwtimer_t tmr,
243  uint8_t *rx_buff,
244  size_t buffer_size,
245  void(*uart_rx_complete_callback_fptr)(void *app_data),
246  void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data),
247  void *app_data
248  );
249 
270  uart_rx_t *uart,
271  port_t rx_port,
272  uint32_t baud_rate,
273  uint8_t data_bits,
274  uart_parity_t parity,
275  uint8_t stop_bits,
276  hwtimer_t tmr,
277  void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data),
278  void *app_data
279  );
280 
289 uint8_t uart_rx(uart_rx_t *uart);
290 
298 
299 
300  // END: addtogroup hil_uart
void uart_tx_deinit(uart_tx_t *uart)
#define UART_START_BIT_ERROR_VAL
Definition: uart.h:39
void uart_rx_deinit(uart_rx_t *uart)
uart_state_t
Definition: uart.h:53
uart_parity_t
Definition: uart.h:29
void uart_tx(uart_tx_t *uart, uint8_t data)
uint8_t uart_rx(uart_rx_t *uart)
void uart_rx_init(uart_rx_t *uart, port_t rx_port, uint32_t baud_rate, uint8_t data_bits, uart_parity_t parity, uint8_t stop_bits, hwtimer_t tmr, uint8_t *rx_buff, size_t buffer_size, void(*uart_rx_complete_callback_fptr)(void *app_data), void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data), void *app_data)
void uart_tx_blocking_init(uart_tx_t *uart_cfg, port_t tx_port, uint32_t baud_rate, uint8_t num_data_bits, uart_parity_t parity, uint8_t stop_bits, hwtimer_t tmr)
#define HIL_UART_TX_CALLBACK_ATTR
Definition: uart.h:67
void uart_rx_blocking_init(uart_rx_t *uart, port_t rx_port, uint32_t baud_rate, uint8_t data_bits, uart_parity_t parity, uint8_t stop_bits, hwtimer_t tmr, void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data), void *app_data)
void uart_tx_init(uart_tx_t *uart, port_t tx_port, uint32_t baud_rate, uint8_t data_bits, uart_parity_t parity, uint8_t stop_bits, hwtimer_t tmr, uint8_t *tx_buff, size_t buffer_size_plus_one, void(*uart_tx_empty_callback_fptr)(void *app_data), void *app_data)
Definition: uart.h:101
Definition: uart.h:77