nbiot  2.0.0.0
nbiot.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 NBIOT_H
36 #define NBIOT_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 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define NBIOT_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
65  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
66  cfg.stat = MIKROBUS( mikrobus, MIKROBUS_AN ); \
67  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST )
68 
74 #define NBIOT_RETVAL uint8_t
75 
76 #define NBIOT_OK 0x00
77 #define NBIOT_INIT_ERROR 0xFF
78 
84 #define NBIOT_CMD_AT "AT"
85 #define NBIOT_CMD_ATI "ATI"
86 #define NBIOT_CMD_CGMR "AT+CGMR"
87 
88 #define NBIOT_CMD_CFUN "AT+CFUN"
89 #define NBIOT_CMD_CEREG "AT+CEREG"
90 #define NBIOT_CMD_CGDCONT "AT+CGDCONT"
91 #define NBIOT_CMD_CIMI "AT+CIMI"
92 #define NBIOT_CMD_CGATT "AT+CGATT"
93 #define NBIOT_CMD_CSQ "AT+CSQ"
94 #define NBIOT_CMD_CESQ "AT+CESQ"
95 #define NBIOT_CMD_COPS "AT+COPS"
96 
102 #define DRV_RX_BUFFER_SIZE 500
103 #define DRV_TX_BUFFER_SIZE 100
104  // End group macro
107 // --------------------------------------------------------------- PUBLIC TYPES
115 typedef struct
116 {
117  // Output pins
118 
119  digital_out_t rst;
120 
121  // Input pins
122 
123  digital_in_t stat;
124 
125  // Modules
126 
127  uart_t uart;
128 
129  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
130  char uart_tx_buffer[ DRV_TX_BUFFER_SIZE ];
131 
132 } nbiot_t;
133 
137 typedef struct
138 {
139  // Communication gpio pins
140 
141  pin_name_t rx_pin;
142  pin_name_t tx_pin;
143 
144  // Additional gpio pins
145 
146  pin_name_t stat;
147  pin_name_t rst;
148 
149  // static variable
150 
151  uint32_t baud_rate; // Clock speed.
153  uart_data_bits_t data_bit; // Data bits.
154  uart_parity_t parity_bit; // Parity bit.
155  uart_stop_bits_t stop_bit; // Stop bits.
156 
157 } nbiot_cfg_t;
158 
162 // Actuator switch type
163 typedef uint8_t nbiot_actuator_sw_t;
164 
165 // Actuator counter type
166 typedef int16_t nbiot_actuator_cnt_t;
167 
168 // Actuator string type
169 typedef char nbiot_actuator_str_t;
170 
171 // Parser error type
172 typedef uint8_t nbiot_error_t;
173  // End types group
175 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
176 
182 #ifdef __cplusplus
183 extern "C"{
184 #endif
185 
196 
211 err_t nbiot_init ( nbiot_t *ctx, nbiot_cfg_t *cfg );
212 
226 err_t nbiot_generic_write ( nbiot_t *ctx, char *data_buf, uint16_t len );
227 
241 err_t nbiot_generic_read ( nbiot_t *ctx, char *data_buf, uint16_t max_len );
242 
250 void nbiot_power_on ( nbiot_t *ctx );
251 
260 void nbiot_set_rst ( nbiot_t *ctx, uint8_t state );
261 
270 uint8_t nbiot_get_stat ( nbiot_t *ctx );
271 
281 void nbiot_send_cmd ( nbiot_t *ctx, char *cmd );
282 
293 void nbiot_send_cmd_with_parameter ( nbiot_t *ctx, char *at_cmd_buf, char *param_buf );
294 
304 void nbiot_send_cmd_check ( nbiot_t *ctx, char *at_cmd_buf );
305 
315 void nbiot_send_cmd_parameter_check ( nbiot_t *ctx, char *at_cmd_buf );
316 
326 void nbiot_set_sim_apn ( nbiot_t *ctx, char *sim_apn );
327 
328 #ifdef __cplusplus
329 }
330 #endif
331 #endif // _NBIOT_H_
332  // End public_function group
335 
336 // ------------------------------------------------------------------------- END
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: nbiot.h:102
nbiot_get_stat
uint8_t nbiot_get_stat(nbiot_t *ctx)
STAT Pin Get function.
nbiot_t::uart
uart_t uart
Definition: nbiot.h:127
nbiot_actuator_sw_t
uint8_t nbiot_actuator_sw_t
Error type.
Definition: nbiot.h:163
nbiot_actuator_cnt_t
int16_t nbiot_actuator_cnt_t
Definition: nbiot.h:166
nbiot_cfg_t
Click configuration structure definition.
Definition: nbiot.h:138
nbiot_t
Click ctx object definition.
Definition: nbiot.h:116
nbiot_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: nbiot.h:155
nbiot_cfg_t::rst
pin_name_t rst
Definition: nbiot.h:147
nbiot_send_cmd_with_parameter
void nbiot_send_cmd_with_parameter(nbiot_t *ctx, char *at_cmd_buf, char *param_buf)
Send command function with parameter.
nbiot_set_rst
void nbiot_set_rst(nbiot_t *ctx, uint8_t state)
Sets state of the RST pin.
nbiot_set_sim_apn
void nbiot_set_sim_apn(nbiot_t *ctx, char *sim_apn)
Set sim card APN.
nbiot_cfg_t::baud_rate
uint32_t baud_rate
Definition: nbiot.h:151
nbiot_init
err_t nbiot_init(nbiot_t *ctx, nbiot_cfg_t *cfg)
NB IoT initialization function.
nbiot_actuator_str_t
char nbiot_actuator_str_t
Definition: nbiot.h:169
nbiot_send_cmd
void nbiot_send_cmd(nbiot_t *ctx, char *cmd)
Send command function.
nbiot_cfg_t::uart_blocking
bool uart_blocking
Definition: nbiot.h:152
nbiot_cfg_t::rx_pin
pin_name_t rx_pin
Definition: nbiot.h:141
nbiot_send_cmd_check
void nbiot_send_cmd_check(nbiot_t *ctx, char *at_cmd_buf)
Check the sent command.
nbiot_t::stat
digital_in_t stat
Definition: nbiot.h:123
nbiot_error_t
uint8_t nbiot_error_t
Definition: nbiot.h:172
nbiot_t::rst
digital_out_t rst
Definition: nbiot.h:119
nbiot_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: nbiot.h:153
DRV_TX_BUFFER_SIZE
#define DRV_TX_BUFFER_SIZE
Definition: nbiot.h:103
nbiot_power_on
void nbiot_power_on(nbiot_t *ctx)
NB IoT module power on.
nbiot_cfg_t::tx_pin
pin_name_t tx_pin
Definition: nbiot.h:142
nbiot_send_cmd_parameter_check
void nbiot_send_cmd_parameter_check(nbiot_t *ctx, char *at_cmd_buf)
Check the command parameters.
nbiot_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: nbiot.h:154
nbiot_generic_write
err_t nbiot_generic_write(nbiot_t *ctx, char *data_buf, uint16_t len)
NB IoT data writing function.
nbiot_cfg_setup
void nbiot_cfg_setup(nbiot_cfg_t *cfg)
NB IoT configuration object setup function.
nbiot_cfg_t::stat
pin_name_t stat
Definition: nbiot.h:146
nbiot_generic_read
err_t nbiot_generic_read(nbiot_t *ctx, char *data_buf, uint16_t max_len)
NB IoT data reading function.