lr3  2.0.0.0
lr3.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 LR3_H
36 #define LR3_H
37 
42 #ifdef PREINIT_SUPPORTED
43 #include "preinit.h"
44 #endif
45 
46 #ifdef MikroCCoreVersion
47  #if MikroCCoreVersion >= 1
48  #include "delays.h"
49  #endif
50 #endif
51 
52 #include "drv_digital_out.h"
53 #include "drv_digital_in.h"
54 #include "drv_uart.h"
55 
56 // -------------------------------------------------------------- PUBLIC MACROS
66 #define LR3_MAP_MIKROBUS( cfg, mikrobus ) \
67  cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
68  cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
69  cfg.rst = MIKROBUS( mikrobus, MIKROBUS_RST ); \
70  cfg.wk = MIKROBUS( mikrobus, MIKROBUS_CS ); \
71  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
72 
78 #define LR3_RETVAL uint8_t
79 
80 #define LR3_OK 0x00
81 #define LR3_INIT_ERROR 0xFF
82 
88 #define FRAME_HEADER 0xAA
89 #define RESPONSE_BIT 0x80
90 
96 #define LR3_CMD_RESET 0x30
97 #define LR3_CMD_FACTORY_RESET 0x31
98 #define LR3_CMD_EEPROM_WRITE 0x32
99 #define LR3_CMD_EEPROM_READ 0x33
100 #define LR3_CMD_GET_FW_VERSION 0x34
101 #define LR3_CMD_GET_SERIAL_NO 0x35
102 #define LR3_ENABLE_PAIRING 0x40
103 #define LR3_GET_NETWORK_TABLE_SIZE 0x42
104 #define LR3_GET_NETWORK_TABLE_ROW 0x43
105 #define LR3_DEL_NET_TABLE_ROW 0x44
106 #define LR3_DEL_ALL_NETWORK_TABLE 0x45
107 #define LR3_GET_PAIRING_REQUEST 0x48
108 #define LR3_GET_ACTIVATION_STATUS 0x4A
109 #define LR3_TX_MESSAGE 0x50
110 #define LR3_CHECK_LINK_REQUEST 0x56
111 #define LR3_SET_APP_KEY 0x58
112 
113 #define LR3_INDICATES_DEVICE_PAIRING 0x41
114 #define LR3_INDICATES_PAIRING_CONFIRM 0x49
115 #define LR3_INDICATES_TX_MSG_CONFIRMED 0x51
116 #define LR3_INDICATES_TX_MSG_UNCONFIRMED 0x52
117 #define LR3_INDICATES_RX_MSG 0x53
118 #define LR3_INDICATES_LINK_CHECK 0x57
119 #define LR3_INDICATES_TX_SESSION_ABORT 0x59
120 
121 #define LR3_OK 0x00
122 #define LR3_ERR_ADDR_RANGE 0x01
123 #define LR3_ERR_NOT_IDLE 0x02
124 
125 #define LR3_ERR_PL_LEN 0xFC
126 #define LR3_ERR_CHKSUM 0xFD
127 #define LR3_ERR_RESPONSE 0xFE
128 #define LR3_ERR_HEADER 0xFF
129 
130 #define LR3_UNCONFIRMED_DATA_TX 0x00
131 #define LR3_CONFIRMED_DATA_TX 0x01
132 
133 #define LR3_BROADCAST_MESSAGE 0xFFFFFFFF
134 
140 #define LR3_MODULE_POWER_ON 1
141 #define LR3_MODULE_POWER_OFF 0
142 
148 #define DRV_RX_BUFFER_SIZE 500
149  // End group macro
152 // --------------------------------------------------------------- PUBLIC TYPES
161 typedef void ( *lr3_ind_hdl_t )( uint8_t *cmd, uint8_t* p_load_size, uint8_t *p_load );
162 
166 typedef struct
167 {
168  // Output pins
169 
170  digital_out_t rst;
171  digital_out_t wk;
172 
173  // Input pins
174 
175  digital_in_t int_pin;
176 
177  // Modules
178 
179  uart_t uart;
180 
181  char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
182  char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
183 
185 } lr3_t;
186 
190 typedef struct
191 {
192  // Communication gpio pins
193 
194  pin_name_t rx_pin;
195  pin_name_t tx_pin;
196 
197  // Additional gpio pins
198 
199  pin_name_t rst;
200  pin_name_t wk;
201  pin_name_t int_pin;
202 
203  // static variable
204 
205  uint32_t baud_rate; // Clock speed.
207  uart_data_bits_t data_bit; // Data bits.
208  uart_parity_t parity_bit; // Parity bit.
209  uart_stop_bits_t stop_bit; // Stop bits.
210 
211 } lr3_cfg_t;
212 
213 typedef struct {
214 
215  uint8_t command;
216  uint8_t payload_size;
217  uint8_t payload[ 255 ];
218 
219 } lr3_message_t;
220 
221 typedef struct {
222 
223  uint8_t *data_in;
224  uint8_t n_bytes;
225  uint32_t destination_id;
226  uint8_t option;
227 
228 } lr3_tx_msg_t;
229  // End types group
231 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
232 
238 #ifdef __cplusplus
239 extern "C"{
240 #endif
241 
250 void lr3_cfg_setup ( lr3_cfg_t *cfg );
251 
261 
272 uint8_t lr3_chk_sum ( lr3_message_t *cmd );
273 
285 int32_t lr3_generic_read ( lr3_t *ctx, char *data_buf, uint16_t max_len );
286 
293 void lr3_generic_write ( lr3_t *ctx, char *data_buf, uint16_t len );
294 
302 void lr3_hard_reset ( lr3_t *ctx );
303 
312 void lr3_wake_up ( lr3_t *ctx, uint8_t state );
313 
323 
336 uint8_t lr3_command ( lr3_t *ctx, lr3_message_t *msg, lr3_message_t *rsp );
337 
347 
358 
370 
384 LR3_RETVAL lr3_write_eeprom ( lr3_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_in );
385 
399 LR3_RETVAL lr3_read_eeprom ( lr3_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_out );
400 
411 LR3_RETVAL lr3_get_fw_version ( lr3_t *ctx, uint32_t *fw_version );
412 
423 LR3_RETVAL lr3_get_serial_no ( lr3_t *ctx, uint32_t *serial_no );
424 
439 LR3_RETVAL lr3_enable_pairing ( lr3_t *ctx, uint8_t state );
440 
452 
465 LR3_RETVAL lr3_get_network_table_row( lr3_t *ctx, uint8_t index, uint8_t *data_out );
466 
478 LR3_RETVAL lr3_delete_network_table_row ( lr3_t *ctx, uint8_t *device_id );
479 
490 
511 
522 
534 LR3_RETVAL lr3_get_activation_status ( lr3_t *ctx, uint8_t *data_out );
535 
550 LR3_RETVAL lr3_check_link_request ( lr3_t *ctx, uint8_t power, uint8_t message_num, uint8_t message_th );
551 
563 LR3_RETVAL lr3_set_app_key ( lr3_t *ctx, uint8_t *app_key );
564 
565 #ifdef __cplusplus
566 }
567 #endif
568 #endif // _LR3_H_
569  // End public_function group
572 
573 // ------------------------------------------------------------------------- END
lr3_cfg_t::rst
pin_name_t rst
Definition: lr3.h:199
DRV_RX_BUFFER_SIZE
#define DRV_RX_BUFFER_SIZE
Definition: lr3.h:148
lr3_tx_msg_t::destination_id
uint32_t destination_id
Definition: lr3.h:225
lr3_cfg_t::parity_bit
uart_parity_t parity_bit
Definition: lr3.h:208
lr3_enable_pairing
LR3_RETVAL lr3_enable_pairing(lr3_t *ctx, uint8_t state)
Enable Pairing function.
lr3_get_pairing_request
LR3_RETVAL lr3_get_pairing_request(lr3_t *ctx)
Get Pairing Request function.
lr3_generic_write
void lr3_generic_write(lr3_t *ctx, char *data_buf, uint16_t len)
Generic write function.
lr3_cfg_t::baud_rate
uint32_t baud_rate
Definition: lr3.h:205
lr3_tx_message
LR3_RETVAL lr3_tx_message(lr3_t *ctx, lr3_tx_msg_t *tx_msg)
Radio frame Transmission function.
lr3_chk_sum
uint8_t lr3_chk_sum(lr3_message_t *cmd)
Check sum function.
lr3_cfg_t::wk
pin_name_t wk
Definition: lr3.h:200
lr3_hard_reset
void lr3_hard_reset(lr3_t *ctx)
Hardware reset function.
lr3_cfg_t::stop_bit
uart_stop_bits_t stop_bit
Definition: lr3.h:209
lr3_set_app_key
LR3_RETVAL lr3_set_app_key(lr3_t *ctx, uint8_t *app_key)
Set App Key function.
lr3_get_activation_status
LR3_RETVAL lr3_get_activation_status(lr3_t *ctx, uint8_t *data_out)
Get Activation Status function.
lr3_write_eeprom
LR3_RETVAL lr3_write_eeprom(lr3_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_in)
EEPROM write function.
lr3_cfg_t::uart_blocking
bool uart_blocking
Definition: lr3.h:206
lr3_get_network_table_size
LR3_RETVAL lr3_get_network_table_size(lr3_t *ctx)
Get Network Table Size function.
lr3_generic_read
int32_t lr3_generic_read(lr3_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
lr3_cfg_setup
void lr3_cfg_setup(lr3_cfg_t *cfg)
Config Object Initialization function.
lr3_cfg_t::rx_pin
pin_name_t rx_pin
Definition: lr3.h:194
lr3_cfg_t::data_bit
uart_data_bits_t data_bit
Definition: lr3.h:207
lr3_t
Click ctx object definition.
Definition: lr3.h:167
lr3_cfg_t::tx_pin
pin_name_t tx_pin
Definition: lr3.h:195
lr3_get_network_table_row
LR3_RETVAL lr3_get_network_table_row(lr3_t *ctx, uint8_t index, uint8_t *data_out)
Get Network Table Row function.
lr3_read_message_process
uint8_t lr3_read_message_process(lr3_t *ctx)
Process function.
lr3_soft_reset
LR3_RETVAL lr3_soft_reset(lr3_t *ctx)
Software reset function.
lr3_delete_network_table_row
LR3_RETVAL lr3_delete_network_table_row(lr3_t *ctx, uint8_t *device_id)
Delete Network Table Row function.
lr3_t::ind_handler
lr3_ind_hdl_t ind_handler
Definition: lr3.h:184
lr3_command
uint8_t lr3_command(lr3_t *ctx, lr3_message_t *msg, lr3_message_t *rsp)
Command function.
lr3_tx_msg_t::data_in
uint8_t * data_in
Definition: lr3.h:223
lr3_tx_msg_t::option
uint8_t option
Definition: lr3.h:226
lr3_t::wk
digital_out_t wk
Definition: lr3.h:171
lr3_message_t::payload_size
uint8_t payload_size
Definition: lr3.h:216
lr3_message_t::command
uint8_t command
Definition: lr3.h:215
lr3_message_t
Definition: lr3.h:213
lr3_cfg_t::int_pin
pin_name_t int_pin
Definition: lr3.h:201
lr3_cfg_t
Click configuration structure definition.
Definition: lr3.h:191
lr3_read_eeprom
LR3_RETVAL lr3_read_eeprom(lr3_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_out)
EEPROM read function.
lr3_get_serial_no
LR3_RETVAL lr3_get_serial_no(lr3_t *ctx, uint32_t *serial_no)
Get Serial Number function.
lr3_tx_msg_t::n_bytes
uint8_t n_bytes
Definition: lr3.h:224
lr3_wake_up
void lr3_wake_up(lr3_t *ctx, uint8_t state)
Wake Up function.
lr3_t::rst
digital_out_t rst
Definition: lr3.h:170
lr3_t::int_pin
digital_in_t int_pin
Definition: lr3.h:175
lr3_tx_msg_t
Definition: lr3.h:221
lr3_get_fw_version
LR3_RETVAL lr3_get_fw_version(lr3_t *ctx, uint32_t *fw_version)
Get FW Version function.
LR3_RETVAL
#define LR3_RETVAL
Definition: lr3.h:78
lr3_set_ind_handler
void lr3_set_ind_handler(lr3_t *ctx, lr3_ind_hdl_t hdl)
Set indication handler function.
lr3_ind_hdl_t
void(* lr3_ind_hdl_t)(uint8_t *cmd, uint8_t *p_load_size, uint8_t *p_load)
LR3_TYPES Types.
Definition: lr3.h:161
lr3_delete_all_network_table
LR3_RETVAL lr3_delete_all_network_table(lr3_t *ctx)
Delete All Network Table function.
lr3_init
LR3_RETVAL lr3_init(lr3_t *ctx, lr3_cfg_t *cfg)
Initialization function.
lr3_check_link_request
LR3_RETVAL lr3_check_link_request(lr3_t *ctx, uint8_t power, uint8_t message_num, uint8_t message_th)
Radio frame Transmission function.
lr3_t::uart
uart_t uart
Definition: lr3.h:179
lr3_factory_reset
LR3_RETVAL lr3_factory_reset(lr3_t *ctx)
Factory reset function.