usbuart  2.0.0.0
usbuart.h
Go to the documentation of this file.
1 /****************************************************************************
2 ** Copyright (C) 2020 MikroElektronika d.o.o.
3 ** Contact: https://www.mikroe.com/contact
4 **
5 ** Permission is hereby granted, free of charge, to any person obtaining a copy
6 ** of this software and associated documentation files (the "Software"), to deal
7 ** in the Software without restriction, including without limitation the rights
8 ** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 ** copies of the Software, and to permit persons to whom the Software is
10 ** furnished to do so, subject to the following conditions:
11 ** The above copyright notice and this permission notice shall be
12 ** included in all copies or substantial portions of the Software.
13 **
14 ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16 ** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18 ** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19 ** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 ** USE OR OTHER DEALINGS IN THE SOFTWARE.
21 ****************************************************************************/
22 
28 #ifndef USBUART_H
29 #define USBUART_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
39 #ifdef PREINIT_SUPPORTED
40 #include "preinit.h"
41 #endif
42 
43 #ifdef MikroCCoreVersion
44  #if MikroCCoreVersion >= 1
45  #include "delays.h"
46  #endif
47 #endif
48 
49 #include "drv_digital_out.h"
50 #include "drv_digital_in.h"
51 #include "drv_uart.h"
52 
74 #define USBUART_MODE_SLEEP 1
75 #define USBUART_MODE_NORMAL 0
76 
77 #define USBUART_CTS_ACTIVE 1
78 #define USBUART_CTS_NO_ACTIVE 0
79 
80 #define USBUART_POWER_ON 1
81 #define USBUART_POWER_OFF 0
82 
88 #define DRV_BUFFER_SIZE 500
89  // usbuart_set
91 
106 #define USBUART_MAP_MIKROBUS( cfg, mikrobus ) \
107  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
108  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
109  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_RST ); \
110  cfg.slp = MIKROBUS( mikrobus, MIKROBUS_CS ); \
111  cfg.pwr = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
112  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
113  // usbuart_map // usbuart
116 
121 typedef struct
122 {
123  // Output pins
124 
125  digital_out_t cts;
126  digital_out_t slp;
127  digital_out_t pwr;
128  // Input pins
129 
130  digital_in_t rts;
131 
132  // Modules
133  uart_t uart;
135  // Buffers
136  char uart_rx_buffer[ DRV_BUFFER_SIZE ];
137  char uart_tx_buffer[ DRV_BUFFER_SIZE ];
139 } usbuart_t;
140 
145 typedef struct
146 {
147  // Communication gpio pins
148  pin_name_t rx_pin;
149  pin_name_t tx_pin;
151  // Additional gpio pins
152  pin_name_t cts;
153  pin_name_t slp;
154  pin_name_t pwr;
155  pin_name_t rts;
156 
157  // Static variable
158  uint32_t baud_rate;
160  uart_data_bits_t data_bit;
161  uart_parity_t parity_bit;
162  uart_stop_bits_t stop_bit;
164 } usbuart_cfg_t;
165 
170 typedef enum
171 {
173  USBUART_ERROR = -1
174 
176 
193 
208 err_t usbuart_init ( usbuart_t *ctx, usbuart_cfg_t *cfg );
209 
223 void usbuart_generic_write ( usbuart_t *ctx, char *data_buf, uint16_t len );
224 
238 uint32_t usbuart_generic_read ( usbuart_t *ctx, char *data_buf, uint16_t max_len );
239 
248 void usbuart_pwr_ctrl ( usbuart_t *ctx, uint8_t state );
249 
258 void usbuart_set_cts ( usbuart_t *ctx, uint8_t state );
259 
268 void usbuart_set_mode ( usbuart_t *ctx, uint8_t mode );
269 
276 uint8_t usbuart_get_rts ( usbuart_t *ctx );
277 
285 void usbuart_send_command ( usbuart_t *ctx, char *command );
286 #ifdef __cplusplus
287 }
288 #endif
289 #endif // USBUART_H
290  // usbuart
292 
293 // ------------------------------------------------------------------------ END
usbuart_return_value_t
usbuart_return_value_t
USB UART Click return value data.
Definition: usbuart.h:171
usbuart_send_command
void usbuart_send_command(usbuart_t *ctx, char *command)
Function for send command.
usbuart_cfg_t::slp
pin_name_t slp
Definition: usbuart.h:153
USBUART_OK
@ USBUART_OK
Definition: usbuart.h:172
DRV_BUFFER_SIZE
#define DRV_BUFFER_SIZE
USB UART driver buffer size.
Definition: usbuart.h:88
usbuart_cfg_t::rx_pin
pin_name_t rx_pin
Definition: usbuart.h:148
usbuart_cfg_t::tx_pin
pin_name_t tx_pin
Definition: usbuart.h:149
usbuart_cfg_setup
void usbuart_cfg_setup(usbuart_cfg_t *cfg)
USB UART configuration object setup function.
usbuart_pwr_ctrl
void usbuart_pwr_ctrl(usbuart_t *ctx, uint8_t state)
Power control.
usbuart_cfg_t::rts
pin_name_t rts
Definition: usbuart.h:155
usbuart_generic_read
uint32_t usbuart_generic_read(usbuart_t *ctx, char *data_buf, uint16_t max_len)
USB UART data reading function.
usbuart_cfg_t
USB UART Click configuration object.
Definition: usbuart.h:146
usbuart_set_cts
void usbuart_set_cts(usbuart_t *ctx, uint8_t state)
Set CTS (Clear to send)
usbuart_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: usbuart.h:160
usbuart_cfg_t::cts
pin_name_t cts
Definition: usbuart.h:152
usbuart_t::rts
digital_in_t rts
Definition: usbuart.h:130
usbuart_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: usbuart.h:162
usbuart_generic_write
void usbuart_generic_write(usbuart_t *ctx, char *data_buf, uint16_t len)
USB UART data writing function.
usbuart_t
USB UART Click context object.
Definition: usbuart.h:122
usbuart_t::pwr
digital_out_t pwr
Definition: usbuart.h:127
usbuart_cfg_t::uart_blocking
bool uart_blocking
Definition: usbuart.h:159
usbuart_cfg_t::baud_rate
uint32_t baud_rate
Definition: usbuart.h:158
usbuart_get_rts
uint8_t usbuart_get_rts(usbuart_t *ctx)
Set device mode.
usbuart_set_mode
void usbuart_set_mode(usbuart_t *ctx, uint8_t mode)
Set device mode.
USBUART_ERROR
@ USBUART_ERROR
Definition: usbuart.h:173
usbuart_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: usbuart.h:161
usbuart_cfg_t::pwr
pin_name_t pwr
Definition: usbuart.h:154
usbuart_t::uart
uart_t uart
Definition: usbuart.h:133
usbuart_init
err_t usbuart_init(usbuart_t *ctx, usbuart_cfg_t *cfg)
USB UART initialization function.
usbuart_t::cts
digital_out_t cts
Definition: usbuart.h:125
usbuart_t::slp
digital_out_t slp
Definition: usbuart.h:126