g2c  2.1.0.0
g2c.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 G2C_H
29 #define G2C_H
30 
31 #ifdef __cplusplus
32 extern "C"{
33 #endif
34 
35 #include "mikrosdk_version.h"
36 
37 #ifdef __GNUC__
38 #if mikroSDK_GET_VERSION < 20800ul
39 #include "rcu_delays.h"
40 #else
41 #include "delays.h"
42 #endif
43 #endif
44 
45 #include "drv_digital_out.h"
46 #include "drv_digital_in.h"
47 #include "drv_uart.h"
48 
69 #define G2C_CMD_AT "AT" // Communication test
70 #define G2C_CMD_GMR "AT+GMR" // Firmware version
71 #define G2C_CMD_ATE1 "ATE1" // Enable echo
72 #define G2C_CMD_ATE0 "ATE0" // Disable echo
73 #define G2C_CMD_RST "AT+RST" // Reset device
74 #define G2C_CMD_CRST "AT+CRST" // Connector module reset
75 #define G2C_CMD_CEN "AT+CEN" // Enable connector module
76 #define G2C_CMD_GPEN "AT+GPEN" // Enable GPIO outputs
77 #define G2C_CMD_W "AT+W" // Store configuration
78 #define G2C_CMD_R "AT+R" // Restore configuration
79 #define G2C_CMD_NWP "AT+NWP" // Network parameters
80 #define G2C_CMD_NWCR "AT+NWCR" // Network credentials
81 #define G2C_CMD_NWC "AT+NWC" // Connect to network
82 #define G2C_CMD_BRCR "AT+BRCR" // Broker credentials
83 #define G2C_CMD_BRC "AT+BRC" // Connect to broker
84 #define G2C_CMD_ASTA "AT+ASTA" // Actuator status
85 #define G2C_CMD_DSET "AT+DSET" // Data set
86 #define G2C_CMD_PUB "AT+PUB" // Publish data
87 
92 #define G2C_RSP_OK "OK"
93 #define G2C_RSP_ERROR "ERROR"
94 #define G2C_RSP_ERR "+ERR"
95 #define G2C_RSP_ACT "+ACT"
96 
102 #define G2C_TX_DRV_BUFFER_SIZE 300
103 #define G2C_RX_DRV_BUFFER_SIZE 300
104  // g2c_cmd
106 
121 #define G2C_MAP_MIKROBUS( cfg, mikrobus ) \
122  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
123  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
124  cfg.gp0 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
125  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
126  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
127  cfg.gp1 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
128  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_INT );
129  // g2c_map // g2c
132 
137 typedef struct
138 {
139  // Output pins
140  digital_out_t rst;
141  digital_out_t cts;
143  // Input pins
144  digital_in_t gp0;
145  digital_in_t gp1;
146  digital_in_t rts;
148  // Modules
149  uart_t uart;
151  // Buffers
152  uint8_t uart_rx_buffer[ G2C_RX_DRV_BUFFER_SIZE ];
153  uint8_t uart_tx_buffer[ G2C_TX_DRV_BUFFER_SIZE ];
155 } g2c_t;
156 
161 typedef struct
162 {
163  // Communication gpio pins
164  pin_name_t rx_pin;
165  pin_name_t tx_pin;
167  // Additional gpio pins
168  pin_name_t gp0;
169  pin_name_t rst;
170  pin_name_t cts;
171  pin_name_t gp1;
172  pin_name_t rts;
174  // Static variable
175  uint32_t baud_rate;
177  uart_data_bits_t data_bit;
178  uart_parity_t parity_bit;
179  uart_stop_bits_t stop_bit;
181 } g2c_cfg_t;
182 
187 typedef enum
188 {
189  G2C_OK = 0,
190  G2C_ERROR = -1,
193  G2C_ERROR_UNKNOWN = -4
194 
196 
212 void g2c_cfg_setup ( g2c_cfg_t *cfg );
213 
227 err_t g2c_init ( g2c_t *ctx, g2c_cfg_t *cfg );
228 
241 err_t g2c_generic_write ( g2c_t *ctx, uint8_t *data_in, uint16_t len );
242 
255 err_t g2c_generic_read ( g2c_t *ctx, uint8_t *data_out, uint16_t len );
256 
266 void g2c_set_cts_pin ( g2c_t *ctx, uint8_t state );
267 
277 void g2c_set_rst_pin ( g2c_t *ctx, uint8_t state );
278 
287 uint8_t g2c_get_gp0_pin ( g2c_t *ctx );
288 
297 uint8_t g2c_get_gp1_pin ( g2c_t *ctx );
298 
307 uint8_t g2c_get_rts_pin ( g2c_t *ctx );
308 
317 void g2c_reset_device ( g2c_t *ctx );
318 
328 void g2c_send_cmd ( g2c_t *ctx, uint8_t *cmd );
329 
340 void g2c_send_cmd_with_par ( g2c_t *ctx, uint8_t *at_cmd_buf, uint8_t *param_buf );
341 
351 void g2c_send_cmd_check ( g2c_t *ctx, uint8_t *at_cmd_buf );
352 
362 void g2c_send_cmd_par_check ( g2c_t *ctx, uint8_t *at_cmd_buf );
363 
374 void g2c_set_net_creds ( g2c_t *ctx, uint8_t *wifi_ssid, uint8_t *wifi_pass );
375 
386 void g2c_set_broker_creds ( g2c_t *ctx, uint8_t *dev_key, uint8_t *password );
387 
388 #ifdef __cplusplus
389 }
390 #endif
391 #endif // G2C_H
392  // g2c
394 
395 // ------------------------------------------------------------------------ END
g2c_send_cmd_with_par
void g2c_send_cmd_with_par(g2c_t *ctx, uint8_t *at_cmd_buf, uint8_t *param_buf)
G2C send command function with parameter.
g2c_get_rts_pin
uint8_t g2c_get_rts_pin(g2c_t *ctx)
G2C get rts pin function.
g2c_init
err_t g2c_init(g2c_t *ctx, g2c_cfg_t *cfg)
G2C initialization function.
g2c_cfg_t::gp1
pin_name_t gp1
Definition: g2c.h:171
g2c_send_cmd_check
void g2c_send_cmd_check(g2c_t *ctx, uint8_t *at_cmd_buf)
G2C check the sent command.
G2C_ERROR_UNKNOWN
@ G2C_ERROR_UNKNOWN
Definition: g2c.h:193
G2C_RX_DRV_BUFFER_SIZE
#define G2C_RX_DRV_BUFFER_SIZE
Definition: g2c.h:103
g2c_t::gp0
digital_in_t gp0
Definition: g2c.h:144
g2c_t::rts
digital_in_t rts
Definition: g2c.h:146
g2c_t::cts
digital_out_t cts
Definition: g2c.h:141
G2C_OK
@ G2C_OK
Definition: g2c.h:189
g2c_t::rst
digital_out_t rst
Definition: g2c.h:140
g2c_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: g2c.h:178
g2c_cfg_t::rst
pin_name_t rst
Definition: g2c.h:169
g2c_cfg_t::baud_rate
uint32_t baud_rate
Definition: g2c.h:175
g2c_get_gp0_pin
uint8_t g2c_get_gp0_pin(g2c_t *ctx)
G2C get gp0 pin function.
g2c_set_cts_pin
void g2c_set_cts_pin(g2c_t *ctx, uint8_t state)
G2C set cts pin function.
g2c_cfg_t::gp0
pin_name_t gp0
Definition: g2c.h:168
G2C_ERROR_TIMEOUT
@ G2C_ERROR_TIMEOUT
Definition: g2c.h:191
g2c_t::gp1
digital_in_t gp1
Definition: g2c.h:145
g2c_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: g2c.h:179
g2c_t::uart
uart_t uart
Definition: g2c.h:149
g2c_send_cmd
void g2c_send_cmd(g2c_t *ctx, uint8_t *cmd)
G2C send command function.
g2c_cfg_t::uart_blocking
bool uart_blocking
Definition: g2c.h:176
G2C_TX_DRV_BUFFER_SIZE
#define G2C_TX_DRV_BUFFER_SIZE
G2C driver buffer size.
Definition: g2c.h:102
g2c_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: g2c.h:177
g2c_t
G2C Click context object.
Definition: g2c.h:138
g2c_send_cmd_par_check
void g2c_send_cmd_par_check(g2c_t *ctx, uint8_t *at_cmd_buf)
G2C check the command parameters.
g2c_set_broker_creds
void g2c_set_broker_creds(g2c_t *ctx, uint8_t *dev_key, uint8_t *password)
G2C set broker credentials.
G2C_ERROR_CMD
@ G2C_ERROR_CMD
Definition: g2c.h:192
g2c_reset_device
void g2c_reset_device(g2c_t *ctx)
G2C reset device function.
g2c_get_gp1_pin
uint8_t g2c_get_gp1_pin(g2c_t *ctx)
G2C get gp1 pin function.
g2c_return_value_t
g2c_return_value_t
G2C Click return value data.
Definition: g2c.h:188
g2c_cfg_t::rts
pin_name_t rts
Definition: g2c.h:172
g2c_cfg_t::cts
pin_name_t cts
Definition: g2c.h:170
g2c_cfg_t
G2C Click configuration object.
Definition: g2c.h:162
g2c_cfg_t::rx_pin
pin_name_t rx_pin
Definition: g2c.h:164
G2C_ERROR
@ G2C_ERROR
Definition: g2c.h:190
g2c_set_net_creds
void g2c_set_net_creds(g2c_t *ctx, uint8_t *wifi_ssid, uint8_t *wifi_pass)
G2C set network credentials.
g2c_cfg_t::tx_pin
pin_name_t tx_pin
Definition: g2c.h:165
g2c_cfg_setup
void g2c_cfg_setup(g2c_cfg_t *cfg)
G2C configuration object setup function.
g2c_generic_write
err_t g2c_generic_write(g2c_t *ctx, uint8_t *data_in, uint16_t len)
G2C data writing function.
g2c_set_rst_pin
void g2c_set_rst_pin(g2c_t *ctx, uint8_t state)
G2C set rst pin function.
g2c_generic_read
err_t g2c_generic_read(g2c_t *ctx, uint8_t *data_out, uint16_t len)
G2C data reading function.