usbuart  2.0.0.0
usbuart.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright (c) 2019, MikroElektronika - www.mikroe.com
4  * All rights reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef USBUART_H
36 #define USBUART_H
37 
38 #include "drv_digital_out.h"
39 #include "drv_digital_in.h"
40 #include "drv_uart.h"
41 
42 
43 // -------------------------------------------------------------- PUBLIC MACROS
53 #define USBUART_MAP_MIKROBUS( cfg, mikrobus ) \
54  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
55  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
56  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_RST ); \
57  cfg.slp = MIKROBUS( mikrobus, MIKROBUS_CS ); \
58  cfg.pwr = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
59  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT )
60 
66 #define USBUART_RETVAL uint8_t
67 
68 #define USBUART_OK 0x00
69 #define USBUART_INIT_ERROR 0xFF
70 
76 #define DRV_RX_BUFFER_SIZE 500
77 
83 #define USBUART_POWER_ON 1
84 #define USBUART_POWER_OFF 0
85 
91 #define USBUART_CTS_ACTIVE 1
92 #define USBUART_CTS_NO_ACTIVE 0
93 
99 #define USBUART_MODE_SLEEP 1
100 #define USBUART_MODE_NORMAL 0
101  // End group macro
104 // --------------------------------------------------------------- PUBLIC TYPES
112 typedef struct
113 {
114  // Output pins
115 
116  digital_out_t cts;
117  digital_out_t slp;
118  digital_out_t pwr;
119 
120  // Input pins
121 
122  digital_in_t rts;
123 
124  // Modules
125 
126  uart_t uart;
127 
128  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
129  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
130 
131 } usbuart_t;
132 
136 typedef struct
137 {
138  // Communication gpio pins
139 
140  pin_name_t rx_pin;
141  pin_name_t tx_pin;
142 
143  // Additional gpio pins
144 
145  pin_name_t cts;
146  pin_name_t slp;
147  pin_name_t pwr;
148  pin_name_t rts;
149 
150  // static variable
151 
152  uint32_t baud_rate; // Clock speed.
154  uart_data_bits_t data_bit; // Data bits.
155  uart_parity_t parity_bit; // Parity bit.
156  uart_stop_bits_t stop_bit; // Stop bits.
157 
158 } usbuart_cfg_t;
159 
163 typedef uint8_t usbuart_error_t;
164  // End types group
166 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
167 
173 #ifdef __cplusplus
174 extern "C"{
175 #endif
176 
185 void usbuart_cfg_setup ( usbuart_cfg_t *cfg );
186 
195 
202 void usbuart_generic_write ( usbuart_t *ctx, char *data_buf, uint16_t len );
203 
211 uint16_t usbuart_generic_read ( usbuart_t *ctx, char *data_buf, uint16_t max_len );
212 
219 void usbuart_pwr_ctrl ( usbuart_t *ctx, uint8_t state );
220 
227 void usbuart_set_cts ( usbuart_t *ctx, uint8_t state );
228 
235 void usbuart_set_mode ( usbuart_t *ctx, uint8_t mode );
236 
243 uint8_t usbuart_get_rts ( usbuart_t *ctx );
244 
251 void usbuart_send_command ( usbuart_t *ctx, char *command );
252 
253 #ifdef __cplusplus
254 }
255 #endif
256 #endif // _USBUART_H_
257  // End public_function group
260 
261 // ------------------------------------------------------------------------- END
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: usbuart.h:76
usbuart_cfg_t::slp
pin_name_t slp
Definition: usbuart.h:146
USBUART_RETVAL
#define USBUART_RETVAL
Definition: usbuart.h:66
usbuart_send_command
void usbuart_send_command(usbuart_t *ctx, char *command)
Function for send command.
usbuart_cfg_t::rx_pin
pin_name_t rx_pin
Definition: usbuart.h:140
usbuart_cfg_t::tx_pin
pin_name_t tx_pin
Definition: usbuart.h:141
usbuart_cfg_t::rts
pin_name_t rts
Definition: usbuart.h:148
usbuart_set_cts
void usbuart_set_cts(usbuart_t *ctx, uint8_t state)
Set CTS (Clear to send)
usbuart_cfg_t
Click configuration structure definition.
Definition: usbuart.h:136
usbuart_set_mode
void usbuart_set_mode(usbuart_t *ctx, uint8_t mode)
Set device mode.
usbuart_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: usbuart.h:154
usbuart_cfg_t::cts
pin_name_t cts
Definition: usbuart.h:145
usbuart_t::rts
digital_in_t rts
Definition: usbuart.h:122
usbuart_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: usbuart.h:156
usbuart_init
USBUART_RETVAL usbuart_init(usbuart_t *ctx, usbuart_cfg_t *cfg)
Initialization function.
usbuart_t
Click ctx object definition.
Definition: usbuart.h:112
usbuart_get_rts
uint8_t usbuart_get_rts(usbuart_t *ctx)
Set device mode.
usbuart_t::pwr
digital_out_t pwr
Definition: usbuart.h:118
usbuart_generic_write
void usbuart_generic_write(usbuart_t *ctx, char *data_buf, uint16_t len)
Generic write function.
usbuart_cfg_t::uart_blocking
bool uart_blocking
Definition: usbuart.h:153
usbuart_cfg_t::baud_rate
uint32_t baud_rate
Definition: usbuart.h:152
usbuart_pwr_ctrl
void usbuart_pwr_ctrl(usbuart_t *ctx, uint8_t state)
Power control.
usbuart_cfg_setup
void usbuart_cfg_setup(usbuart_cfg_t *cfg)
Config Object Initialization function.
usbuart_error_t
uint8_t usbuart_error_t
Error type.
Definition: usbuart.h:163
usbuart_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: usbuart.h:155
usbuart_cfg_t::pwr
pin_name_t pwr
Definition: usbuart.h:147
usbuart_t::uart
uart_t uart
Definition: usbuart.h:126
usbuart_t::cts
digital_out_t cts
Definition: usbuart.h:116
usbuart_t::slp
digital_out_t slp
Definition: usbuart.h:117
usbuart_generic_read
uint16_t usbuart_generic_read(usbuart_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.