Blame view

RIOT/drivers/uart_half_duplex/include/uart_half_duplex.h 4.51 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
  /*
   * Copyright (C) 2017 Inria
   *
   * This file is subject to the terms and conditions of the GNU Lesser General
   * Public License v2.1. See the file LICENSE in the top level directory for more
   * details.
   */
  
  /**
   * @defgroup    drivers_uart_half_duplex half-duplex UART Driver
   * @ingroup     drivers_actuators
   *
   * This module contains drivers for UART half-duplex communication bus.
   * It needs to manage the communication direction by enabling or disabling TX.
   *
   * @{
   *
   * @file
   * @brief       Interface definition for half-duplex UART driver
   *
   * @author      Loïc Dauphin <loic.dauphin@inria.fr>
   */
  
  #ifndef UART_HALF_DUPLEX_H
  #define UART_HALF_DUPLEX_H
  
  #include <stdlib.h>
  
  #include "periph/uart.h"
  
  #ifdef __cplusplus
  extern "C" {
  #endif
  
  #ifndef UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US
  #define UART_HALF_DUPLEX_DEFAULT_TIMEOUT_US (20000LU) /**< Default recv timeout (in microseconds) */
  #endif
  
  /**
   * @brief half-duplex UART direction management method type
   */
  typedef struct {
      void (*init)(uart_t);       /**< function initializing direction management method */
      void (*enable_tx)(uart_t);  /**< function enabling TX */
      void (*disable_tx)(uart_t); /**< function disabling TX */
  } uart_half_duplex_dir_t;
  
  #define UART_HALF_DUPLEX_DIR_NONE { NULL, NULL, NULL }  /**< Don't manage direction */
  
  /**
   * @brief Configuration for half-duplex UART
   */
  typedef struct {
      uart_t uart;                /**< the half-duplex UART bus to use */
      uint32_t baudrate;          /**< the baudrate to use */
      uart_half_duplex_dir_t dir; /**< the direction management method */
  } uart_half_duplex_params_t;
  
  /**
   * @brief Descriptor struct for half-duplex UART
   */
  typedef struct {
      uint8_t *buffer;                  /**< the buffer used for TX and RX */
      size_t size;                      /**< the number of available elements for TX/RX */
      size_t buffer_max_size;                     /**< the buffer size */
      uint32_t timeout_us;              /**< the maximum duration (in microseconds) for waiting data */
      uart_half_duplex_params_t params; /**< the half-duplex UART configuration */
  } uart_half_duplex_t;
  
  /**
   * @brief   Possible UART_HALF_DUPLEX return values
   */
  enum {
      UART_HALF_DUPLEX_OK     = UART_OK,       /**< everything in order */
      UART_HALF_DUPLEX_NODEV  = UART_NODEV,    /**< invalid UART device given */
      UART_HALF_DUPLEX_NOBAUD = UART_NOBAUD,   /**< given baudrate is not applicable */
      UART_HALF_DUPLEX_INTERR = UART_INTERR,   /**< all other internal errors */
      UART_HALF_DUPLEX_NOMODE = UART_NOMODE,   /**< given mode is not applicable */
      UART_HALF_DUPLEX_NOBUFF = -5             /**< invalid buffer given */
  };
  
  /**
   * @brief Initialize the half-duplex UART bus to communicate with devices
   *
   * @param[out] dev             the device
   * @param[in] buffer           the buffer used for TX and RX
   * @param[in] buffer_max_size  the buffer size
   * @param[in] params           the initialization parameters
   *
   * @return     UART_HALF_DUPLEX_OK if everything is in order
   * @return     UART_HALF_DUPLEX_NODEV if invalid UART device was given
   * @return     UART_HALF_DUPLEX_NOBAUD if given baudrate is not applicable
   * @return     UART_HALF_DUPLEX_INTERR if an other internal error occured
   * @return     UART_HALF_DUPLEX_NOMODE if the given mode is not applicable
   * @return     UART_HALF_DUPLEX_NOBUFF if an invalid buffer was given
   */
  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);
  
  /**
   * @brief Set the half-duplex UART bus in TX mode
   *
   * @param[in] dev        the device
   */
  static inline void uart_half_duplex_set_tx(uart_half_duplex_t *dev)
  {
      dev->size = dev->buffer_max_size;
  }
  
  /**
   * @brief Set the half-duplex UART bus in RX mode
   *
   * @param[in] dev        the device
   */
  static inline void uart_half_duplex_set_rx(uart_half_duplex_t *dev)
  {
      dev->size = 0;
  }
  
  /**
   * @brief Send the data contained in the driver's buffer
   *
   * @param[in] dev        the device
   * @param[in] size       the number of characters to send
   *
   * @return               the number of characters actually sent
   */
  size_t uart_half_duplex_send(const uart_half_duplex_t *dev, size_t size);
  
  /**
   * @brief Recv data an fill the driver's buffer
   *
   * @param[in] dev        the device
   * @param[in] size       the number of characters to receive
   *
   * @return               the number of characters actually received
   */
  size_t uart_half_duplex_recv(const uart_half_duplex_t *dev, size_t size);
  
  #ifdef __cplusplus
  }
  #endif
  
  #endif /* UART_HALF_DUPLEX_H */
  /** @} */