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 
9 #pragma once
10 
26 #include <stdlib.h>
27 
28 #include "periph/uart.h"
29 
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33 
34 #ifndef UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US
35 #define UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US (20000LU)
36 #endif
37 
41 typedef struct {
42  void (*init)(uart_t);
43  void (*enable_tx)(uart_t);
44  void (*disable_tx)(uart_t);
46 
47 #define UART_HALF_DUPLEX_DIR_NONE { NULL, NULL, NULL }
52 typedef struct {
54  uint32_t baudrate;
57 
61 typedef struct {
62  uint8_t *buffer;
63  size_t size;
64  size_t buffer_max_size;
65  uint32_t timeout_us;
68 
72 enum {
79 };
80 
96 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);
97 
104 {
105  dev->size = dev->buffer_max_size;
106 }
107 
114 {
115  dev->size = 0;
116 }
117 
126 size_t uart_half_duplex_send(const uart_half_duplex_t *dev, size_t size);
127 
136 size_t uart_half_duplex_recv(const uart_half_duplex_t *dev, size_t size);
137 
138 #ifdef __cplusplus
139 }
140 #endif
141 
uint_fast8_t uart_t
Define default UART type identifier.
Definition: uart.h:84
@ UART_NOBAUD
given symbol rate is not applicable
Definition: uart.h:138
@ UART_OK
everything in order
Definition: uart.h:136
@ UART_NODEV
invalid UART device given
Definition: uart.h:137
@ UART_INTERR
all other internal errors
Definition: uart.h:140
@ UART_NOMODE
given mode is not applicable
Definition: uart.h:139
static void uart_half_duplex_set_tx(uart_half_duplex_t *dev)
Set the half-duplex UART bus in TX mode.
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.
size_t uart_half_duplex_recv(const uart_half_duplex_t *dev, size_t size)
Recv data an fill the driver's buffer.
size_t uart_half_duplex_send(const uart_half_duplex_t *dev, size_t size)
Send the data contained in the driver's buffer.
static void uart_half_duplex_set_rx(uart_half_duplex_t *dev)
Set the half-duplex UART bus in RX mode.
@ UART_HALF_DUPLEX_NOBAUD
given baudrate is not applicable
@ UART_HALF_DUPLEX_NOMODE
given mode is not applicable
@ UART_HALF_DUPLEX_NOBUFF
invalid buffer given
@ UART_HALF_DUPLEX_INTERR
all other internal errors
@ UART_HALF_DUPLEX_OK
everything in order
@ UART_HALF_DUPLEX_NODEV
invalid UART device given
half-duplex UART direction management method type
Configuration for half-duplex UART.
uart_t uart
the half-duplex UART bus to use
uart_half_duplex_dir_t dir
the direction management method
uint32_t baudrate
the baudrate to use
Descriptor struct for half-duplex UART.
uint8_t * buffer
the buffer used for TX and RX
size_t size
the number of available elements for TX/RX
uart_half_duplex_params_t params
the half-duplex UART configuration
uint32_t timeout_us
the maximum duration (in microseconds) for waiting data
size_t buffer_max_size
the buffer size
Low-level UART peripheral driver interface definition.