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 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_uart.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define UARTMUX_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
64  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
65  cfg.inh = MIKROBUS( mikrobus, MIKROBUS_RST ); \
66  cfg.a = MIKROBUS( mikrobus, MIKROBUS_CS ); \
67  cfg.b = MIKROBUS( mikrobus, MIKROBUS_PWM )
68 
74 #define UARTMUX_RETVAL uint8_t
75 
76 #define UARTMUX_OK 0x00
77 #define UARTMUX_INIT_ERROR 0xFF
78 
84 #define UARTMUX_PIN_STATE_HIGH 1
85 #define UARTMUX_PIN_STATE_LOW 0
86 
92 #define UARTMUX_STATE_A_CHANNEL_1 0
93 #define UARTMUX_STATE_B_CHANNEL_1 0
94 
100 #define UARTMUX_STATE_A_CHANNEL_2 1
101 #define UARTMUX_STATE_B_CHANNEL_2 0
102 
108 #define UARTMUX_STATE_A_CHANNEL_3 0
109 #define UARTMUX_STATE_B_CHANNEL_3 1
110 
116 #define UARTMUX_STATE_A_CHANNEL_4 1
117 #define UARTMUX_STATE_B_CHANNEL_4 1
118 
124 #define DRV_RX_BUFFER_SIZE 500
125  // End group macro
128 // --------------------------------------------------------------- PUBLIC TYPES
134 typedef struct
135 {
136  uint8_t state_a;
137  uint8_t state_b;
138 
140 
144 typedef struct
145 {
146  // Output pins
147 
148  digital_out_t inh;
149  digital_out_t a;
150  digital_out_t b;
151 
152  // Modules
153 
154  uart_t uart;
155 
156  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
157  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
158 
159  //uartmux_channel_t channel;
160 
161 } uartmux_t;
162 
166 typedef struct
167 {
168  // Communication gpio pins
169 
170  pin_name_t rx_pin;
171  pin_name_t tx_pin;
172 
173  // Additional gpio pins
174 
175  pin_name_t inh;
176  pin_name_t a;
177  pin_name_t b;
178 
179  // static variable
180 
181  uint32_t baud_rate; // Clock speed.
183  uart_data_bits_t data_bit; // Data bits.
184  uart_parity_t parity_bit; // Parity bit.
185  uart_stop_bits_t stop_bit; // Stop bits.
186 
187 } uartmux_cfg_t;
188 
192 typedef uint8_t uartmux_error_t;
193  // End types group
195 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
196 
202 #ifdef __cplusplus
203 extern "C"{
204 #endif
205 
215 
224 
233 void uartmux_generic_write ( uartmux_t *ctx, char *data_buf, uint16_t data_len, uartmux_channel_t *channel );
244 int32_t uartmux_generic_read ( uartmux_t *ctx, char *data_buf, uint16_t max_len, uartmux_channel_t *channel );
245 
252 void uartmux_set_inhibit_communication ( uartmux_t *ctx, uint8_t state );
253 
261 
262 #ifdef __cplusplus
263 }
264 #endif
265 #endif // _UARTMUX_H_
266  // End public_function group
269 
270 // ------------------------------------------------------------------------- END
uartmux_channel_t::state_a
uint8_t state_a
Definition: uartmux.h:136
uartmux_t::b
digital_out_t b
Definition: uartmux.h:150
uartmux_cfg_t::b
pin_name_t b
Definition: uartmux.h:177
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: uartmux.h:124
uartmux_t
Click ctx object definition.
Definition: uartmux.h:145
uartmux_channel_t::state_b
uint8_t state_b
Definition: uartmux.h:137
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:182
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:149
UARTMUX_RETVAL
#define UARTMUX_RETVAL
Definition: uartmux.h:74
uartmux_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: uartmux.h:183
uartmux_error_t
uint8_t uartmux_error_t
Error type.
Definition: uartmux.h:192
uartmux_channel_t
Definition: uartmux.h:135
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:167
uartmux_cfg_t::inh
pin_name_t inh
Definition: uartmux.h:175
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:185
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:171
uartmux_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: uartmux.h:184
uartmux_t::uart
uart_t uart
Definition: uartmux.h:154
uartmux_cfg_t::a
pin_name_t a
Definition: uartmux.h:176
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:148
uartmux_cfg_t::rx_pin
pin_name_t rx_pin
Definition: uartmux.h:170
uartmux_cfg_t::baud_rate
uint32_t baud_rate
Definition: uartmux.h:181