uart_half_duplex.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser General
5  * Public License v2.1. See the file LICENSE in the top level directory for more
6  * details.
7  */
8 
24 #ifndef UART_HALF_DUPLEX_H
25 #define UART_HALF_DUPLEX_H
26 
27 #include <stdlib.h>
28 
29 #include "periph/uart.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #ifndef UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US
36 #define UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US (20000LU)
37 #endif
38 
42 typedef struct {
43  void (*init)(uart_t);
44  void (*enable_tx)(uart_t);
45  void (*disable_tx)(uart_t);
47 
48 #define UART_HALF_DUPLEX_DIR_NONE { NULL, NULL, NULL }
53 typedef struct {
55  uint32_t baudrate;
58 
62 typedef struct {
63  uint8_t *buffer;
64  size_t size;
65  size_t buffer_max_size;
66  uint32_t timeout_us;
69 
73 enum {
80 };
81 
97 int uart_half_duplex_init(uart_half_duplex_t *dev, uint8_t *buffer, size_t buffer_max_size, const uart_half_duplex_params_t *params);
98 
105 {
106  dev->size = dev->buffer_max_size;
107 }
108 
115 {
116  dev->size = 0;
117 }
118 
127 size_t uart_half_duplex_send(const uart_half_duplex_t *dev, size_t size);
128 
137 size_t uart_half_duplex_recv(const uart_half_duplex_t *dev, size_t size);
138 
139 #ifdef __cplusplus
140 }
141 #endif
142 
143 #endif /* UART_HALF_DUPLEX_H */
144 
UART_NODEV
@ UART_NODEV
invalid UART device given
Definition: uart.h:116
UART_NOMODE
@ UART_NOMODE
given mode is not applicable
Definition: uart.h:119
uart_half_duplex_recv
size_t uart_half_duplex_recv(const uart_half_duplex_t *dev, size_t size)
Recv data an fill the driver's buffer.
UART_HALF_DUPLEX_INTERR
@ UART_HALF_DUPLEX_INTERR
all other internal errors
Definition: uart_half_duplex.h:77
uart_half_duplex_params_t::uart
uart_t uart
the half-duplex UART bus to use
Definition: uart_half_duplex.h:54
uart_half_duplex_t::timeout_us
uint32_t timeout_us
the maximum duration (in microseconds) for waiting data
Definition: uart_half_duplex.h:66
UART_HALF_DUPLEX_NOMODE
@ UART_HALF_DUPLEX_NOMODE
given mode is not applicable
Definition: uart_half_duplex.h:78
uart_half_duplex_t::params
uart_half_duplex_params_t params
the half-duplex UART configuration
Definition: uart_half_duplex.h:67
UART_INTERR
@ UART_INTERR
all other internal errors
Definition: uart.h:118
UART_HALF_DUPLEX_NOBUFF
@ UART_HALF_DUPLEX_NOBUFF
invalid buffer given
Definition: uart_half_duplex.h:79
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.h
Low-level UART peripheral driver interface definition.
UART_HALF_DUPLEX_NODEV
@ UART_HALF_DUPLEX_NODEV
invalid UART device given
Definition: uart_half_duplex.h:75
uart_half_duplex_dir_t
half-duplex UART direction management method type
Definition: uart_half_duplex.h:42
uart_half_duplex_t::buffer
uint8_t * buffer
the buffer used for TX and RX
Definition: uart_half_duplex.h:63
uart_half_duplex_params_t::dir
uart_half_duplex_dir_t dir
the direction management method
Definition: uart_half_duplex.h:56
uart_half_duplex_t
Descriptor struct for half-duplex UART.
Definition: uart_half_duplex.h:62
UART_HALF_DUPLEX_OK
@ UART_HALF_DUPLEX_OK
everything in order
Definition: uart_half_duplex.h:74
UART_HALF_DUPLEX_NOBAUD
@ UART_HALF_DUPLEX_NOBAUD
given baudrate is not applicable
Definition: uart_half_duplex.h:76
uart_half_duplex_send
size_t uart_half_duplex_send(const uart_half_duplex_t *dev, size_t size)
Send the data contained in the driver's buffer.
uart_half_duplex_params_t
Configuration for half-duplex UART.
Definition: uart_half_duplex.h:53
uart_half_duplex_set_rx
static void uart_half_duplex_set_rx(uart_half_duplex_t *dev)
Set the half-duplex UART bus in RX mode.
Definition: uart_half_duplex.h:114
uart_half_duplex_init
int uart_half_duplex_init(uart_half_duplex_t *dev, uint8_t *buffer, size_t buffer_max_size, const uart_half_duplex_params_t *params)
Initialize the half-duplex UART bus to communicate with devices.
UART_OK
@ UART_OK
everything in order
Definition: uart.h:115
uart_half_duplex_t::buffer_max_size
size_t buffer_max_size
the buffer size
Definition: uart_half_duplex.h:65
uart_half_duplex_set_tx
static void uart_half_duplex_set_tx(uart_half_duplex_t *dev)
Set the half-duplex UART bus in TX mode.
Definition: uart_half_duplex.h:104
uart_half_duplex_t::size
size_t size
the number of available elements for TX/RX
Definition: uart_half_duplex.h:64
uart_half_duplex_params_t::baudrate
uint32_t baudrate
the baudrate to use
Definition: uart_half_duplex.h:55