gsm  2.0.0.0
gsm.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 GSM_H
29 #define GSM_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 GSM_CMD_AT "AT"
70 #define GSM_CMD_ATI "ATI"
71 #define GSM_CMD_CFUN "AT+CFUN"
72 #define GSM_CMD_CREG "AT+CREG"
73 #define GSM_CMD_CGDCONT "AT+CGDCONT"
74 #define GSM_CMD_CSQ "AT+CSQ"
75 #define GSM_CMD_COPS "AT+COPS"
76 #define GSM_CMD_CMGS "AT+CMGS"
77 #define GSM_CMD_CMGF "AT+CMGF"
78 #define GSM_CMD_SGACT "AT#SGACT"
79 #define GSM_CMD_SD "AT#SD"
80 #define GSM_CMD_SH "AT#SH"
81 #define GSM_CMD_SSEND "AT#SSEND"
82 #define GSM_CMD_SRECV "AT#SRECV"
83 
88 #define GSM_RSP_OK "OK"
89 #define GSM_RSP_ERROR "ERROR"
90 
96 #define GSM_RX_DRV_BUFFER_SIZE 256
97 #define GSM_TX_DRV_BUFFER_SIZE 256
98  // gsm_set
100 
115 #define GSM_MAP_MIKROBUS( cfg, mikrobus ) \
116  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
117  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
118  cfg.pwr = MIKROBUS( mikrobus, MIKROBUS_AN ); \
119  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
120  cfg.rts = MIKROBUS( mikrobus, MIKROBUS_CS ); \
121  cfg.gp2 = MIKROBUS( mikrobus, MIKROBUS_PWM ); \
122  cfg.cts = MIKROBUS( mikrobus, MIKROBUS_INT )
123  // gsm_map // gsm
126 
131 typedef struct
132 {
133  // Output pins
134  digital_out_t rst;
135  digital_out_t rts;
137  // Input pins
138  digital_in_t pwr;
139  digital_in_t gp2;
140  digital_in_t cts;
142  // Modules
143  uart_t uart;
145  // Buffers
146  uint8_t uart_rx_buffer[ GSM_RX_DRV_BUFFER_SIZE ];
147  uint8_t uart_tx_buffer[ GSM_TX_DRV_BUFFER_SIZE ];
149 } gsm_t;
150 
155 typedef struct
156 {
157  // Communication gpio pins
158  pin_name_t rx_pin;
159  pin_name_t tx_pin;
161  // Additional gpio pins
162  pin_name_t pwr;
163  pin_name_t rst;
164  pin_name_t rts;
165  pin_name_t gp2;
166  pin_name_t cts;
168  // Static variable
169  uint32_t baud_rate;
171  uart_data_bits_t data_bit;
172  uart_parity_t parity_bit;
173  uart_stop_bits_t stop_bit;
175 } gsm_cfg_t;
176 
181 typedef enum
182 {
183  GSM_OK = 0,
184  GSM_ERROR = -1,
187  GSM_ERROR_UNKNOWN = -4
188 
190 
206 void gsm_cfg_setup ( gsm_cfg_t *cfg );
207 
221 err_t gsm_init ( gsm_t *ctx, gsm_cfg_t *cfg );
222 
235 err_t gsm_generic_write ( gsm_t *ctx, uint8_t *data_in, uint16_t len );
236 
249 err_t gsm_generic_read ( gsm_t *ctx, uint8_t *data_out, uint16_t len );
250 
260 void gsm_set_rts_pin ( gsm_t *ctx, uint8_t state );
261 
271 void gsm_set_rst_pin ( gsm_t *ctx, uint8_t state );
272 
281 uint8_t gsm_get_cts_pin ( gsm_t *ctx );
282 
291 uint8_t gsm_get_pwr_pin ( gsm_t *ctx );
292 
301 uint8_t gsm_get_gp2_pin ( gsm_t *ctx );
302 
312 void gsm_send_cmd ( gsm_t *ctx, uint8_t *cmd );
313 
324 void gsm_send_cmd_with_par ( gsm_t *ctx, uint8_t *at_cmd_buf, uint8_t *param_buf );
325 
335 void gsm_send_cmd_check ( gsm_t *ctx, uint8_t *at_cmd_buf );
336 
346 void gsm_send_cmd_par_check ( gsm_t *ctx, uint8_t *at_cmd_buf );
347 
357 void gsm_set_sim_apn ( gsm_t *ctx, uint8_t *sim_apn );
358 
369 void gsm_send_sms_text ( gsm_t *ctx, uint8_t *phone_number, uint8_t *sms_text );
370 
384 err_t gsm_send_sms_pdu ( gsm_t *ctx, uint8_t *service_center_number, uint8_t *phone_number, uint8_t *sms_text );
385 
386 #ifdef __cplusplus
387 }
388 #endif
389 #endif // GSM_H
390  // gsm
392 
393 // ------------------------------------------------------------------------ END
gsm_set_rts_pin
void gsm_set_rts_pin(gsm_t *ctx, uint8_t state)
GSM set rts pin function.
gsm_t::pwr
digital_in_t pwr
Definition: gsm.h:138
gsm_send_sms_pdu
err_t gsm_send_sms_pdu(gsm_t *ctx, uint8_t *service_center_number, uint8_t *phone_number, uint8_t *sms_text)
GSM send SMS in PDU mode.
gsm_cfg_t::rts
pin_name_t rts
Definition: gsm.h:164
gsm_cfg_t::pwr
pin_name_t pwr
Definition: gsm.h:162
gsm_set_sim_apn
void gsm_set_sim_apn(gsm_t *ctx, uint8_t *sim_apn)
Set sim card APN.
gsm_set_rst_pin
void gsm_set_rst_pin(gsm_t *ctx, uint8_t state)
GSM set rst pin function.
gsm_cfg_t::tx_pin
pin_name_t tx_pin
Definition: gsm.h:159
gsm_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: gsm.h:172
gsm_cfg_t::rst
pin_name_t rst
Definition: gsm.h:163
gsm_cfg_t::gp2
pin_name_t gp2
Definition: gsm.h:165
gsm_get_pwr_pin
uint8_t gsm_get_pwr_pin(gsm_t *ctx)
GSM get pwr pin function.
GSM_ERROR_TIMEOUT
@ GSM_ERROR_TIMEOUT
Definition: gsm.h:185
gsm_send_cmd_par_check
void gsm_send_cmd_par_check(gsm_t *ctx, uint8_t *at_cmd_buf)
Check the command parameters.
GSM_ERROR
@ GSM_ERROR
Definition: gsm.h:184
gsm_generic_read
err_t gsm_generic_read(gsm_t *ctx, uint8_t *data_out, uint16_t len)
GSM data reading function.
gsm_t::rts
digital_out_t rts
Definition: gsm.h:135
gsm_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: gsm.h:173
gsm_cfg_t::rx_pin
pin_name_t rx_pin
Definition: gsm.h:158
gsm_cfg_t::uart_blocking
bool uart_blocking
Definition: gsm.h:170
gsm_generic_write
err_t gsm_generic_write(gsm_t *ctx, uint8_t *data_in, uint16_t len)
GSM data writing function.
gsm_t::cts
digital_in_t cts
Definition: gsm.h:140
gsm_init
err_t gsm_init(gsm_t *ctx, gsm_cfg_t *cfg)
GSM initialization function.
gsm_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: gsm.h:171
gsm_cfg_t
GSM Click configuration object.
Definition: gsm.h:156
gsm_t::uart
uart_t uart
Definition: gsm.h:143
gsm_t::rst
digital_out_t rst
Definition: gsm.h:134
GSM_TX_DRV_BUFFER_SIZE
#define GSM_TX_DRV_BUFFER_SIZE
Definition: gsm.h:97
gsm_t
GSM Click context object.
Definition: gsm.h:132
gsm_cfg_t::cts
pin_name_t cts
Definition: gsm.h:166
gsm_get_gp2_pin
uint8_t gsm_get_gp2_pin(gsm_t *ctx)
GSM get gp2 pin function.
GSM_ERROR_CMD
@ GSM_ERROR_CMD
Definition: gsm.h:186
gsm_send_cmd
void gsm_send_cmd(gsm_t *ctx, uint8_t *cmd)
Send command function.
gsm_send_cmd_with_par
void gsm_send_cmd_with_par(gsm_t *ctx, uint8_t *at_cmd_buf, uint8_t *param_buf)
Send command function with parameter.
gsm_t::gp2
digital_in_t gp2
Definition: gsm.h:139
gsm_cfg_setup
void gsm_cfg_setup(gsm_cfg_t *cfg)
GSM configuration object setup function.
GSM_ERROR_UNKNOWN
@ GSM_ERROR_UNKNOWN
Definition: gsm.h:187
gsm_send_cmd_check
void gsm_send_cmd_check(gsm_t *ctx, uint8_t *at_cmd_buf)
Check the sent command.
gsm_return_value_t
gsm_return_value_t
GSM Click return value data.
Definition: gsm.h:182
GSM_RX_DRV_BUFFER_SIZE
#define GSM_RX_DRV_BUFFER_SIZE
GSM driver buffer size.
Definition: gsm.h:96
gsm_cfg_t::baud_rate
uint32_t baud_rate
Definition: gsm.h:169
gsm_send_sms_text
void gsm_send_sms_text(gsm_t *ctx, uint8_t *phone_number, uint8_t *sms_text)
GSM send SMS in text mode.
GSM_OK
@ GSM_OK
Definition: gsm.h:183
gsm_get_cts_pin
uint8_t gsm_get_cts_pin(gsm_t *ctx)
GSM get cts pin function.