lr4 2.0.0.0
lr4.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 LR4_H
36#define LR4_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 LR4_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 )
78#define LR4_RETVAL uint8_t
79
80#define LR4_OK 0x00
81#define LR4_INIT_ERROR 0xFF
88#define LR4_FRAME_HEADER 0xAA
89#define LR4_RESPONSE_BIT 0x80
96#define LR4_CMD_RESET 0x30
97#define LR4_CMD_FACTORY_RESET 0x31
98#define LR4_CMD_EEPROM_WRITE 0x32
99#define LR4_CMD_EEPROM_READ 0x33
100#define LR4_CMD_GET_FW_VERSION 0x34
101#define LR4_CMD_GET_SERIAL_NO 0x35
102#define LR4_CMD_GET_DEV_EUI 0x36
103#define LR4_CMD_JOIN 0x40
104#define LR4_CMD_GET_ACTIVATION_STATUS 0x42
105#define LR4_CMD_SET_APP_KEY 0x43
106#define LR4_CMD_SET_APP_SESSION_KEY 0x44
107#define LR4_CMD_SET_NWK_SESSION_KEY 0x45
108#define LR4_CMD_TX_MSG 0x46
109#define LR4_CMD_GET_SESSION_STATUS 0x4A
110#define LR4_CMD_SET_NEXT_DR 0x4B
111#define LR4_CMD_SET_BATTERY_LVL 0x50
112#define LR4_CMD_GET_BATTERY_LVL 0x51
113#define LR4_CMD_SET_UPLINK_CNT 0x52
114#define LR4_CMD_GET_UPLINK_CNT 0x53
115#define LR4_CMD_SET_DOWNLINK_CNT 0x54
116#define LR4_CMD_GET_DOWNLINK_CNT 0x55
117#define LR4_CMD_SET_CH_PARAMETERS 0x57
118#define LR4_CMD_GET_CH_PARAMETERS 0x58
119
120#define LR4_INDICATES_JOIN 0x41
121#define LR4_INDICATES_TX_MSG_CONFIRMED 0x47
122#define LR4_INDICATES_TX_MSG_UNCONFIRMED 0x48
123#define LR4_INDICATES_RX_MSG 0x49
124
125#define LR4_OK 0x00
126#define LR4_ERR_ADDR_RANGE 0x01
127#define LR4_ERR_NOT_IDLE 0x02
128
129#define LR4_ERR_PL_LEN 0xFC
130#define LR4_ERR_CHKSUM 0xFD
131#define LR4_ERR_RESPONSE 0xFE
132#define LR4_ERR_HEADER 0xFF
133
134
135#define LR4_JOIN_ACTIVATION_BY_PERSONALIZATION_MODE 0x00
136#define LR4_JOIN_OVER_THE_AIR_ACTIVATION_MODE 0x01
137#define LR4_GET_ACTIVATION_MODE 0x00
138#define LR4_GET_SESSION_STATUS_MODE 0x01
139#define LR4_SET_APP_KEY_MODE 0x00
140#define LR4_SET_NWK_SESSION_KEY_MODE 0x01
141#define LR4_UPLINK_CNT 0x00
142#define LR4_DOWNLINK_CNT 0x01
143#define LR4_CHANNEL_DISABLED 0x00
144#define LR4_CHANNEL_ENABLED 0x01
145#define LR4_UNCONFIRMED_TRANSMISSION 0x00
146#define LR4_CONFIRMED_TRANSMISSION 0x01
147
148
149#define LR4_STATUS_IDLE 0x00
150#define LR4_STATUS_BUSY 0x01
151#define LR4_STATUS_DEV_NOT_ACTIVATED 0x02
152#define LR4_STATUS_DELAYED 0x03
153
154#define LR4_STATUS_NOT_ACTIVATED 0x00
155#define LR4_STATUS_JOINING 0x01
156#define LR4_STATUS_JOINED 0x02
157#define LR4_STATUS_MAC_ERROR 0x03
164#define LR4_MODULE_POWER_ON 1
165#define LR4_MODULE_POWER_OFF 0
172#define DRV_RX_BUFFER_SIZE 500
175 // End group macro
176// --------------------------------------------------------------- PUBLIC TYPES
185typedef void ( *lr4_ind_hdl_t )( uint8_t *cmd, uint8_t* p_load_size, uint8_t *p_load );
186
190typedef struct
191{
192 // Output pins
193
194 digital_out_t rst;
195 digital_out_t wk;
196
197 // Input pins
198
199 digital_in_t int_pin;
200
201 // Modules
202
203 uart_t uart;
204
205 char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
206 char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
207
209} lr4_t;
210
214typedef struct
215{
216 // Communication gpio pins
217
218 pin_name_t rx_pin;
219 pin_name_t tx_pin;
220
221 // Additional gpio pins
222
223 pin_name_t rst;
224 pin_name_t wk;
225 pin_name_t int_pin;
226
227 // static variable
228
229 uint32_t baud_rate; // Clock speed.
231 uart_data_bits_t data_bit; // Data bits.
232 uart_parity_t parity_bit; // Parity bit.
233 uart_stop_bits_t stop_bit; // Stop bits.
234
235} lr4_cfg_t;
236
237typedef struct {
238
239 uint8_t command;
241 uint8_t payload[ 255 ];
242
244
245typedef struct {
246
247 uint8_t *data_in;
248 uint8_t n_bytes;
249 uint8_t port;
250 uint8_t option;
251
253
254 // End types group
255// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
256
262#ifdef __cplusplus
263extern "C"{
264#endif
265
275
285
297int32_t lr4_generic_read ( lr4_t *ctx, char *data_buf, uint16_t max_len );
298
305void lr4_generic_write ( lr4_t *ctx, char *data_buf, uint16_t len );
306
314void lr4_hard_reset ( lr4_t *ctx );
315
324void lr4_wake_up ( lr4_t *ctx, uint8_t state );
325
335
348uint8_t lr4_command ( lr4_t *ctx, lr4_message_t *msg, lr4_message_t *rsp );
349
359
370
382
396LR4_RETVAL lr4_write_eeprom ( lr4_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_in );
397
411LR4_RETVAL lr4_read_eeprom ( lr4_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_out );
412
423LR4_RETVAL lr4_get_fw_version ( lr4_t *ctx, uint32_t *fw_version );
424
435LR4_RETVAL lr4_get_serial_no ( lr4_t *ctx, uint32_t *serial_no );
436
447LR4_RETVAL lr4_get_dev_eui ( lr4_t *ctx, uint8_t *dev_eui );
448
459LR4_RETVAL lr4_join_network ( lr4_t *ctx, uint8_t mode );
460
473LR4_RETVAL lr4_get_status ( lr4_t *ctx, uint8_t status_mode );
474
487LR4_RETVAL lr4_set_key ( lr4_t *ctx, uint8_t *key_data, uint8_t key_mode );
488
509
522LR4_RETVAL lr4_set_next_tx_data_rate ( lr4_t *ctx, uint8_t data_rate );
523
537LR4_RETVAL lr4_set_battery_level ( lr4_t *ctx, uint8_t battery_level );
538
552
564LR4_RETVAL lr4_set_counter ( lr4_t *ctx, uint32_t cnt, uint8_t cnt_mode );
565
577LR4_RETVAL lr4_get_counter ( lr4_t *ctx, uint32_t *cnt, uint8_t cnt_mode );
578
594LR4_RETVAL lr4_set_channel ( lr4_t *ctx, uint8_t index, uint32_t freq, uint8_t data_rate_range, uint8_t status );
595
606uint8_t lr4_chk_sum ( lr4_message_t *cmd );
607
608#ifdef __cplusplus
609}
610#endif
611#endif // _LR4_H_
612
613 // End public_function group
615
616// ------------------------------------------------------------------------- END
#define DRV_RX_BUFFER_SIZE
Definition lr4.h:172
#define LR4_RETVAL
Definition lr4.h:78
LR4_RETVAL lr4_write_eeprom(lr4_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_in)
EEPROM write function.
LR4_RETVAL lr4_get_counter(lr4_t *ctx, uint32_t *cnt, uint8_t cnt_mode)
Get Counter function.
void lr4_generic_write(lr4_t *ctx, char *data_buf, uint16_t len)
Generic write function.
LR4_RETVAL lr4_get_serial_no(lr4_t *ctx, uint32_t *serial_no)
Get Serial Number function.
LR4_RETVAL lr4_get_status(lr4_t *ctx, uint8_t status_mode)
Get Status function.
void lr4_set_ind_handler(lr4_t *ctx, lr4_ind_hdl_t hdl)
Set indication handler function.
LR4_RETVAL lr4_get_dev_eui(lr4_t *ctx, uint8_t *dev_eui)
Get DevEUI function.
LR4_RETVAL lr4_set_next_tx_data_rate(lr4_t *ctx, uint8_t data_rate)
Set Next Data Rate function.
LR4_RETVAL lr4_tx_message(lr4_t *ctx, lr4_tx_msg_t *tx_msg)
Radio frame Transmission function.
LR4_RETVAL lr4_get_fw_version(lr4_t *ctx, uint32_t *fw_version)
Get FW Version function.
LR4_RETVAL lr4_set_channel(lr4_t *ctx, uint8_t index, uint32_t freq, uint8_t data_rate_range, uint8_t status)
Set Channel function.
LR4_RETVAL lr4_soft_reset(lr4_t *ctx)
Software reset function.
LR4_RETVAL lr4_set_battery_level(lr4_t *ctx, uint8_t battery_level)
Set Battery Level function.
void lr4_cfg_setup(lr4_cfg_t *cfg)
Config Object Initialization function.
LR4_RETVAL lr4_init(lr4_t *ctx, lr4_cfg_t *cfg)
Initialization function.
LR4_RETVAL lr4_factory_reset(lr4_t *ctx)
Factory reset function.
LR4_RETVAL lr4_join_network(lr4_t *ctx, uint8_t mode)
Join Network function.
LR4_RETVAL lr4_set_key(lr4_t *ctx, uint8_t *key_data, uint8_t key_mode)
Set Key function.
LR4_RETVAL lr4_read_eeprom(lr4_t *ctx, uint8_t address, uint8_t n_bytes, uint8_t *data_out)
EEPROM read function.
uint8_t lr4_command(lr4_t *ctx, lr4_message_t *msg, lr4_message_t *rsp)
Command function.
LR4_RETVAL lr4_set_counter(lr4_t *ctx, uint32_t cnt, uint8_t cnt_mode)
Set Counter function.
uint8_t lr4_chk_sum(lr4_message_t *cmd)
Check sum function.
int32_t lr4_generic_read(lr4_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
uint8_t lr4_read_message_process(lr4_t *ctx)
Process function.
void lr4_hard_reset(lr4_t *ctx)
Hardware reset function.
void lr4_wake_up(lr4_t *ctx, uint8_t state)
Wake Up function.
LR4_RETVAL lr4_get_battery_level(lr4_t *ctx)
Get Battery Level function.
void(* lr4_ind_hdl_t)(uint8_t *cmd, uint8_t *p_load_size, uint8_t *p_load)
LR4_TYPES Types.
Definition lr4.h:185
Click configuration structure definition.
Definition lr4.h:215
uint32_t baud_rate
Definition lr4.h:229
pin_name_t wk
Definition lr4.h:224
bool uart_blocking
Definition lr4.h:230
uart_data_bits_t data_bit
Definition lr4.h:231
pin_name_t tx_pin
Definition lr4.h:219
pin_name_t rx_pin
Definition lr4.h:218
uart_stop_bits_t stop_bit
Definition lr4.h:233
pin_name_t int_pin
Definition lr4.h:225
uart_parity_t parity_bit
Definition lr4.h:232
pin_name_t rst
Definition lr4.h:223
Definition lr4.h:237
uint8_t command
Definition lr4.h:239
uint8_t payload_size
Definition lr4.h:240
Click ctx object definition.
Definition lr4.h:191
lr4_ind_hdl_t ind_handler
Definition lr4.h:208
uart_t uart
Definition lr4.h:203
digital_in_t int_pin
Definition lr4.h:199
digital_out_t wk
Definition lr4.h:195
digital_out_t rst
Definition lr4.h:194
Definition lr4.h:245
uint8_t option
Definition lr4.h:250
uint8_t port
Definition lr4.h:249
uint8_t n_bytes
Definition lr4.h:248
uint8_t * data_in
Definition lr4.h:247