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 
45 typedef enum {
46  UART_RX_COMPLETE = 0,
47  UART_UNDERRUN_ERROR = 1, //Buffered Tx Error only (buffer empty)
48  UART_START_BIT_ERROR = UART_START_BIT_ERROR_VAL, //Rx Only
49  UART_PARITY_ERROR = UART_START_BIT_ERROR_VAL + 1, //Rx Only
50  UART_FRAMING_ERROR = UART_START_BIT_ERROR_VAL + 2, //Rx Only
51  UART_OVERRUN_ERROR = UART_START_BIT_ERROR_VAL + 3, //Buffered Rx only
53 
58 typedef enum {
59  UART_IDLE = 0,
60  UART_START,
61  UART_DATA,
62  UART_PARITY,
63  UART_STOP
64 } uart_state_t;
65 
66 
72 #define HIL_UART_TX_CALLBACK_ATTR __attribute__((fptrgroup("hil_uart_tx_callback")))
73 
79 #define HIL_UART_RX_CALLBACK_ATTR __attribute__((fptrgroup("hil_uart_rx_callback")))
80 
81 
88 typedef struct {
89  uart_state_t state;
90  port_t tx_port;
91  uint32_t bit_time_ticks;
92  uint32_t next_event_time_ticks;
93  uart_parity_t parity;
94  uint8_t num_data_bits;
95  uint8_t current_data_bit;
96  uint8_t uart_data;
97  uint8_t stop_bits;
98  uint8_t current_stop_bit;
99 
100  HIL_UART_TX_CALLBACK_ATTR void(*uart_tx_empty_callback_fptr)(void* app_data);
101  void *app_data;
102  hwtimer_t tmr;
103  uart_buffer_t buffer;
104 } uart_tx_t;
105 
106 
133  uart_tx_t *uart,
134  port_t tx_port,
135  uint32_t baud_rate,
136  uint8_t data_bits,
137  uart_parity_t parity,
138  uint8_t stop_bits,
139 
140  hwtimer_t tmr,
141  uint8_t *tx_buff,
142  size_t buffer_size_plus_one,
143  void(*uart_tx_empty_callback_fptr)(void* app_data),
144  void *app_data
145  );
146 
147 
163  uart_tx_t *uart,
164  port_t tx_port,
165  uint32_t baud_rate,
166  uint8_t data_bits,
167  uart_parity_t parity,
168  uint8_t stop_bits,
169  hwtimer_t tmr);
170 
171 
178 void uart_tx(
179  uart_tx_t *uart,
180  uint8_t data);
181 
189  uart_tx_t *uart);
190 
191 
192  // END: addtogroup hil_uart_tx
194 
208 typedef struct {
209  uart_state_t state;
210  port_t rx_port;
211  uint32_t bit_time_ticks;
212  uint32_t next_event_time_ticks;
213  uart_parity_t parity;
214  uint8_t num_data_bits;
215  uint8_t current_data_bit;
216  uint8_t uart_data;
217  uint8_t stop_bits;
218  uint8_t current_stop_bit;
219 
220  uart_callback_code_t cb_code;
221  HIL_UART_RX_CALLBACK_ATTR void(*uart_rx_complete_callback_arg)(void* app_data);
222  HIL_UART_RX_CALLBACK_ATTR void(*uart_rx_error_callback_arg)(uart_callback_code_t callback_code, void* app_data);
223  void *app_data;
224  hwtimer_t tmr;
225  uart_buffer_t buffer;
226 } uart_rx_t;
227 
228 
257  uart_rx_t *uart,
258  port_t rx_port,
259  uint32_t baud_rate,
260  uint8_t data_bits,
261  uart_parity_t parity,
262  uint8_t stop_bits,
263 
264  hwtimer_t tmr,
265  uint8_t *rx_buff,
266  size_t buffer_size_plus_one,
267  void(*uart_rx_complete_callback_fptr)(void *app_data),
268  void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data),
269  void *app_data
270  );
271 
292  uart_rx_t *uart,
293  port_t rx_port,
294  uint32_t baud_rate,
295  uint8_t data_bits,
296  uart_parity_t parity,
297  uint8_t stop_bits,
298  hwtimer_t tmr,
299  void(*uart_rx_error_callback_fptr)(uart_callback_code_t callback_code, void *app_data),
300  void *app_data
301  );
302 
311 uint8_t uart_rx(uart_rx_t *uart);
312 
320 
321 
322  // END: addtogroup hil_uart_rx
void uart_rx_deinit(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_plus_one, 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)
uint8_t uart_rx(uart_rx_t *uart)
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_deinit(uart_tx_t *uart)
uart_callback_code_t
Definition: uart.h:45
#define UART_START_BIT_ERROR_VAL
Definition: uart.h:39
uart_state_t
Definition: uart.h:58
uart_parity_t
Definition: uart.h:29
void uart_tx(uart_tx_t *uart, uint8_t data)
void uart_tx_blocking_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)
#define HIL_UART_RX_CALLBACK_ATTR
Definition: uart.h:79
#define HIL_UART_TX_CALLBACK_ATTR
Definition: uart.h:72
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:208
Definition: uart.h:88