rs232  2.0.0.0
rs232.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 RS232_H
36 #define RS232_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 RS232_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
64  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
65  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
66  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_INT )
67 
73 #define RS232_RETVAL uint8_t
74 
75 #define RS232_OK 0x00
76 #define RS232_INIT_ERROR 0xFF
77 
83 #define DRV_RX_BUFFER_SIZE 500
84  // End group macro
87 // --------------------------------------------------------------- PUBLIC TYPES
95 typedef struct
96 {
97  // Output pins
98 
99  digital_out_t rts;
100 
101  // Input pins
102 
103  digital_in_t cts;
104 
105  // Modules
106 
107  uart_t uart;
108 
109  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
110  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
111 
112 } rs232_t;
113 
117 typedef struct
118 {
119  // Communication gpio pins
120 
121  pin_name_t rx_pin;
122  pin_name_t tx_pin;
123 
124  // Additional gpio pins
125 
126  pin_name_t rts;
127  pin_name_t cts;
128 
129  // static variable
130 
131  uint32_t baud_rate; // Clock speed.
133  uart_data_bits_t data_bit; // Data bits.
134  uart_parity_t parity_bit; // Parity bit.
135  uart_stop_bits_t stop_bit; // Stop bits.
136 
137 } rs232_cfg_t;
138 
142 typedef uint8_t rs232_error_t;
143  // End types group
145 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
146 
152 #ifdef __cplusplus
153 extern "C"{
154 #endif
155 
165 
174 
181 void rs232_generic_write ( rs232_t *ctx, char *data_buf, uint16_t len );
182 
190 int32_t rs232_generic_read ( rs232_t *ctx, char *data_buf, uint16_t max_len );
191 
198 uint8_t rs232_get_cts_state ( rs232_t *ctx );
199 
206 void rs232_set_rts_state ( rs232_t *ctx, uint8_t state );
207 
208 #ifdef __cplusplus
209 }
210 #endif
211 #endif // _RS232_H_
212  // End public_function group
215 
216 // ------------------------------------------------------------------------- END
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: rs232.h:83
rs232_t::uart
uart_t uart
Definition: rs232.h:107
rs232_generic_read
int32_t rs232_generic_read(rs232_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
rs232_cfg_t::cts
pin_name_t cts
Definition: rs232.h:127
rs232_t::rts
digital_out_t rts
Definition: rs232.h:99
RS232_RETVAL
#define RS232_RETVAL
Definition: rs232.h:73
rs232_get_cts_state
uint8_t rs232_get_cts_state(rs232_t *ctx)
Get CTS pin state (Clear to send)
rs232_cfg_t
Click configuration structure definition.
Definition: rs232.h:118
rs232_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: rs232.h:135
rs232_error_t
uint8_t rs232_error_t
Error type.
Definition: rs232.h:142
rs232_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: rs232.h:133
rs232_set_rts_state
void rs232_set_rts_state(rs232_t *ctx, uint8_t state)
Set RTS pin state.
rs232_cfg_setup
void rs232_cfg_setup(rs232_cfg_t *cfg)
Config Object Initialization function.
rs232_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: rs232.h:134
rs232_cfg_t::rts
pin_name_t rts
Definition: rs232.h:126
rs232_t::cts
digital_in_t cts
Definition: rs232.h:103
rs232_generic_write
void rs232_generic_write(rs232_t *ctx, char *data_buf, uint16_t len)
Generic write function.
rs232_cfg_t::tx_pin
pin_name_t tx_pin
Definition: rs232.h:122
rs232_t
Click ctx object definition.
Definition: rs232.h:96
rs232_cfg_t::baud_rate
uint32_t baud_rate
Definition: rs232.h:131
rs232_init
RS232_RETVAL rs232_init(rs232_t *ctx, rs232_cfg_t *cfg)
Initialization function.
rs232_cfg_t::rx_pin
pin_name_t rx_pin
Definition: rs232.h:121
rs232_cfg_t::uart_blocking
bool uart_blocking
Definition: rs232.h:132