uartmux  2.0.0.0
uartmux.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef UARTMUX_H
36 #define UARTMUX_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_uart.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define UARTMUX_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
68  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
69  cfg.inh = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.a = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.b = MIKROBUS( mikrobus, MIKROBUS_PWM )
72 
78 #define UARTMUX_RETVAL uint8_t
79 
80 #define UARTMUX_OK 0x00
81 #define UARTMUX_INIT_ERROR 0xFF
82 
88 #define UARTMUX_PIN_STATE_HIGH 1
89 #define UARTMUX_PIN_STATE_LOW 0
90 
96 #define UARTMUX_STATE_A_CHANNEL_1 0
97 #define UARTMUX_STATE_B_CHANNEL_1 0
98 
104 #define UARTMUX_STATE_A_CHANNEL_2 1
105 #define UARTMUX_STATE_B_CHANNEL_2 0
106 
112 #define UARTMUX_STATE_A_CHANNEL_3 0
113 #define UARTMUX_STATE_B_CHANNEL_3 1
114 
120 #define UARTMUX_STATE_A_CHANNEL_4 1
121 #define UARTMUX_STATE_B_CHANNEL_4 1
122 
128 #define DRV_RX_BUFFER_SIZE 500
129  // End group macro
132 // --------------------------------------------------------------- PUBLIC TYPES
138 typedef struct
139 {
140  uint8_t state_a;
141  uint8_t state_b;
142 
144 
148 typedef struct
149 {
150  // Output pins
151 
152  digital_out_t inh;
153  digital_out_t a;
154  digital_out_t b;
155 
156  // Modules
157 
158  uart_t uart;
159 
160  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
161  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
162 
163  //uartmux_channel_t channel;
164 
165 } uartmux_t;
166 
170 typedef struct
171 {
172  // Communication gpio pins
173 
174  pin_name_t rx_pin;
175  pin_name_t tx_pin;
176 
177  // Additional gpio pins
178 
179  pin_name_t inh;
180  pin_name_t a;
181  pin_name_t b;
182 
183  // static variable
184 
185  uint32_t baud_rate; // Clock speed.
187  uart_data_bits_t data_bit; // Data bits.
188  uart_parity_t parity_bit; // Parity bit.
189  uart_stop_bits_t stop_bit; // Stop bits.
190 
191 } uartmux_cfg_t;
192 
196 typedef uint8_t uartmux_error_t;
197  // End types group
199 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
200 
206 #ifdef __cplusplus
207 extern "C"{
208 #endif
209 
219 
228 
237 void uartmux_generic_write ( uartmux_t *ctx, char *data_buf, uint16_t data_len, uartmux_channel_t *channel );
248 int32_t uartmux_generic_read ( uartmux_t *ctx, char *data_buf, uint16_t max_len, uartmux_channel_t *channel );
249 
256 void uartmux_set_inhibit_communication ( uartmux_t *ctx, uint8_t state );
257 
265 
266 #ifdef __cplusplus
267 }
268 #endif
269 #endif // _UARTMUX_H_
270  // End public_function group
273 
274 // ------------------------------------------------------------------------- END
uartmux_channel_t::state_a
uint8_t state_a
Definition: uartmux.h:140
uartmux_t::b
digital_out_t b
Definition: uartmux.h:154
uartmux_cfg_t::b
pin_name_t b
Definition: uartmux.h:181
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: uartmux.h:128
uartmux_t
Click ctx object definition.
Definition: uartmux.h:149
uartmux_channel_t::state_b
uint8_t state_b
Definition: uartmux.h:141
uartmux_choose_channel
void uartmux_choose_channel(uartmux_t *ctx, uartmux_channel_t *channel)
Choose channel.
uartmux_cfg_t::uart_blocking
bool uart_blocking
Definition: uartmux.h:186
uartmux_cfg_setup
void uartmux_cfg_setup(uartmux_cfg_t *cfg)
Config Object Initialization function.
uartmux_t::a
digital_out_t a
Definition: uartmux.h:153
UARTMUX_RETVAL
#define UARTMUX_RETVAL
Definition: uartmux.h:78
uartmux_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: uartmux.h:187
uartmux_error_t
uint8_t uartmux_error_t
Error type.
Definition: uartmux.h:196
uartmux_channel_t
Definition: uartmux.h:139
uartmux_set_inhibit_communication
void uartmux_set_inhibit_communication(uartmux_t *ctx, uint8_t state)
Set INT pin.
uartmux_cfg_t
Click configuration structure definition.
Definition: uartmux.h:171
uartmux_cfg_t::inh
pin_name_t inh
Definition: uartmux.h:179
uartmux_generic_write
void uartmux_generic_write(uartmux_t *ctx, char *data_buf, uint16_t data_len, uartmux_channel_t *channel)
Generic write function.
uartmux_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: uartmux.h:189
uartmux_init
UARTMUX_RETVAL uartmux_init(uartmux_t *ctx, uartmux_cfg_t *cfg)
Initialization function.
uartmux_cfg_t::tx_pin
pin_name_t tx_pin
Definition: uartmux.h:175
uartmux_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: uartmux.h:188
uartmux_t::uart
uart_t uart
Definition: uartmux.h:158
uartmux_cfg_t::a
pin_name_t a
Definition: uartmux.h:180
uartmux_generic_read
int32_t uartmux_generic_read(uartmux_t *ctx, char *data_buf, uint16_t max_len, uartmux_channel_t *channel)
Generic read function.
uartmux_t::inh
digital_out_t inh
Definition: uartmux.h:152
uartmux_cfg_t::rx_pin
pin_name_t rx_pin
Definition: uartmux.h:174
uartmux_cfg_t::baud_rate
uint32_t baud_rate
Definition: uartmux.h:185