rn2xx3.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2017 Inria
3  *
4  * This file is subject to the terms and conditions of the GNU Lesser
5  * General Public License v2.1. See the file LICENSE in the top level
6  * directory for more details.
7  */
8 
21 #ifndef RN2XX3_H
22 #define RN2XX3_H
23 
24 #include <stdint.h>
25 
26 #include "xtimer.h"
27 #include "periph/uart.h"
28 #include "periph/gpio.h"
29 #include "net/netdev.h"
30 
31 #include "net/loramac.h"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
40 #define RN2XX3_MAX_BUF (40U)
41 
47 #define RN2XX3_RX_MAX_BUF (250U)
48 
52 #define RN2XX3_REPLY_DELAY_TIMEOUT (60U)
53 
57 #define RN2XX3_SLEEP_MIN (100U)
58 
67 #ifndef CONFIG_RN2XX3_DEFAULT_SLEEP
68 #define CONFIG_RN2XX3_DEFAULT_SLEEP (5000U)
69 #endif
70 
72 #if defined(MODULE_RN2903)
73 #define RN2XX3_FREQ_BAND (915U)
74 #elif defined(MODULE_RN2483)
75 #define RN2XX3_FREQ_BAND (868U)
76 #else
77 #error "Unsupported module type, use either RN2483 or RN2903"
78 #endif
79 
83 enum {
87  /* all other error codes */
99  /* all other reply codes */
108 };
109 
113 enum {
122 };
123 
127 typedef struct {
128  uint32_t rx2_freq;
130  uint8_t tx_port;
132  uint8_t rx_port;
134 
138 typedef struct {
140  uint32_t baudrate;
141  gpio_t pin_reset;
144 
148 typedef struct {
150  /* device driver specific fields */
154  /* values for the UART TX state machine */
155  char cmd_buf[RN2XX3_MAX_BUF];
158  uint8_t int_state;
160  /* buffer and synchronization for command responses */
163  char resp_buf[RN2XX3_MAX_BUF];
164  uint16_t resp_size;
165  uint8_t resp_done;
167  /* buffer for RX messages */
168  char rx_tmp_buf[2];
171  uint8_t rx_buf[RN2XX3_RX_MAX_BUF];
172  uint16_t rx_size;
174  /* timers */
176  uint32_t sleep;
177 } rn2xx3_t;
178 
185 void rn2xx3_setup(rn2xx3_t *dev, const rn2xx3_params_t *params);
186 
196 int rn2xx3_init(rn2xx3_t *dev);
197 
209 int rn2xx3_sys_reset(rn2xx3_t *dev);
210 
226 
235  int rn2xx3_sys_sleep(rn2xx3_t *dev);
236 
245 int rn2xx3_mac_init(rn2xx3_t *dev);
246 
266 int rn2xx3_write_cmd(rn2xx3_t *dev);
267 
279 
297 
313 int rn2xx3_wait_reply(rn2xx3_t *dev, uint8_t timeout);
314 
332 int rn2xx3_mac_tx(rn2xx3_t *dev, uint8_t *payload, uint8_t payload_len);
333 
349 
362 int rn2xx3_mac_save(rn2xx3_t *dev);
363 
364 /* Get/Set functions definitions */
365 
374 void rn2xx3_mac_get_deveui(rn2xx3_t *dev, uint8_t *eui);
375 
384 void rn2xx3_mac_set_deveui(rn2xx3_t *dev, const uint8_t *eui);
385 
394 void rn2xx3_mac_get_appeui(rn2xx3_t *dev, uint8_t *eui);
395 
404 void rn2xx3_mac_set_appeui(rn2xx3_t *dev, const uint8_t *eui);
405 
414 void rn2xx3_mac_set_appkey(rn2xx3_t *dev, const uint8_t *key);
415 
424 void rn2xx3_mac_set_appskey(rn2xx3_t *dev, const uint8_t *key);
425 
434 void rn2xx3_mac_set_nwkskey(rn2xx3_t *dev, const uint8_t *key);
435 
444 void rn2xx3_mac_get_devaddr(rn2xx3_t *dev, uint8_t *addr);
445 
454 void rn2xx3_mac_set_devaddr(rn2xx3_t *dev, const uint8_t *addr);
455 
464 
472 
481 
489 
497  uint16_t rn2xx3_mac_get_band(rn2xx3_t *dev);
498 
507 bool rn2xx3_mac_get_adr(rn2xx3_t *dev);
508 
515 void rn2xx3_mac_set_adr(rn2xx3_t *dev, bool adr);
516 
528 void rn2xx3_mac_set_battery(rn2xx3_t *dev, uint8_t battery);
529 
537 uint8_t rn2xx3_mac_get_retx(rn2xx3_t *dev);
538 
545 void rn2xx3_mac_set_retx(rn2xx3_t *dev, uint8_t retx);
546 
553 void rn2xx3_mac_set_linkchk_interval(rn2xx3_t *dev, uint16_t linkchk);
554 
562 uint16_t rn2xx3_mac_get_rx1_delay(rn2xx3_t *dev);
563 
570 void rn2xx3_mac_set_rx1_delay(rn2xx3_t *dev, uint16_t rx1);
571 
579 uint16_t rn2xx3_mac_get_rx2_delay(rn2xx3_t *dev);
580 
588 bool rn2xx3_mac_get_ar(rn2xx3_t *dev);
589 
596 void rn2xx3_mac_set_ar(rn2xx3_t *dev, bool ar);
597 
606 
614 
622 uint32_t rn2xx3_mac_get_rx2_freq(rn2xx3_t *dev);
623 
630 void rn2xx3_mac_set_rx2_freq(rn2xx3_t *dev, uint32_t freq);
631 
639 uint8_t rn2xx3_mac_get_tx_port(rn2xx3_t *dev);
640 
647 void rn2xx3_mac_set_tx_port(rn2xx3_t *dev, uint8_t port);
648 
657 
665 
673 uint8_t rn2xx3_mac_get_rx_port(rn2xx3_t *dev);
674 
681 void rn2xx3_sys_set_sleep_duration(rn2xx3_t *dev, uint32_t sleep);
682 
683 #ifdef __cplusplus
684 }
685 #endif
686 
687 #endif /* RN2XX3_H */
688 
xtimer
xtimer timer structure
Definition: xtimer.h:81
rn2xx3_mac_set_ar
void rn2xx3_mac_set_ar(rn2xx3_t *dev, bool ar)
Enables/disables LoRaMAC rn2xx3 MAC automatic reply state.
RN2XX3_REPLY_TX_MAC_RX
@ RN2XX3_REPLY_TX_MAC_RX
Data received from server.
Definition: rn2xx3.h:103
rn2xx3_mac_set_adr
void rn2xx3_mac_set_adr(rn2xx3_t *dev, bool adr)
Enables/disables the rn2xx3 LoRaMAC adaptive datarate.
rn2xx3_mac_set_tx_power
void rn2xx3_mac_set_tx_power(rn2xx3_t *dev, loramac_tx_pwr_idx_t power)
Sets the rn2xx3 LoRaMAC transmission power index.
rn2xx3_t::loramac
loramac_settings_t loramac
loramac communication settings
Definition: rn2xx3.h:152
rn2xx3_params_t::pin_reset
gpio_t pin_reset
GPIO pin that is connected to the STATUS pin set to GPIO_UNDEF if not used.
Definition: rn2xx3.h:141
RN2XX3_ERR_MAC_PAUSED
@ RN2XX3_ERR_MAC_PAUSED
MAC was paused.
Definition: rn2xx3.h:95
rn2xx3_t::resp_done
uint8_t resp_done
check if response has completed
Definition: rn2xx3.h:165
RN2XX3_ERR_SILENT
@ RN2XX3_ERR_SILENT
Device is in Silent Immediately state.
Definition: rn2xx3.h:92
RN2XX3_ERR_INVALID_PARAM
@ RN2XX3_ERR_INVALID_PARAM
Wrong command given.
Definition: rn2xx3.h:89
RN2XX3_ERR_NOT_JOINED
@ RN2XX3_ERR_NOT_JOINED
Network is not joined.
Definition: rn2xx3.h:90
RN2XX3_INT_STATE_CMD
@ RN2XX3_INT_STATE_CMD
waiting command response
Definition: rn2xx3.h:115
RN2XX3_INT_STATE_MAC_RX_MESSAGE
@ RN2XX3_INT_STATE_MAC_RX_MESSAGE
waiting for mac rx message
Definition: rn2xx3.h:121
rn2xx3_mac_set_deveui
void rn2xx3_mac_set_deveui(rn2xx3_t *dev, const uint8_t *eui)
Sets the rn2xx3 LoRaMAC device EUI.
RN2XX3_REPLY_TX_MAC_ERR
@ RN2XX3_REPLY_TX_MAC_ERR
MAC transmission failed.
Definition: rn2xx3.h:101
RN2XX3_REPLY_TX_INVALID_DATA_LEN
@ RN2XX3_REPLY_TX_INVALID_DATA_LEN
Application payload too large.
Definition: rn2xx3.h:102
rn2xx3_mac_set_battery
void rn2xx3_mac_set_battery(rn2xx3_t *dev, uint8_t battery)
Sets the rn2xx3 battery level measured by the end device.
loramac.h
LoRaMAC header definitions.
rn2xx3_write_cmd_no_wait
int rn2xx3_write_cmd_no_wait(rn2xx3_t *dev)
Writes a command to the RN2XX3 device but don't wait for timeout or response.
rn2xx3_mac_save
int rn2xx3_mac_save(rn2xx3_t *dev)
Saves current LoRaMAC configuration to internal EEPROM.
RN2XX3_INT_STATE_SLEEP
@ RN2XX3_INT_STATE_SLEEP
sleep mode
Definition: rn2xx3.h:117
rn2xx3_mac_get_rx_port
uint8_t rn2xx3_mac_get_rx_port(rn2xx3_t *dev)
Parses the response buffer to get the LoRaWAN RX port.
loramac_settings_t::rx2_freq
uint32_t rx2_freq
Center frequency used for second receive window.
Definition: rn2xx3.h:128
rn2xx3_mac_tx
int rn2xx3_mac_tx(rn2xx3_t *dev, uint8_t *payload, uint8_t payload_len)
Sends data to LoRaWAN server.
rn2xx3_mac_set_nwkskey
void rn2xx3_mac_set_nwkskey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC network session key.
rn2xx3_mac_get_retx
uint8_t rn2xx3_mac_get_retx(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC uplink retransmission retries number.
RN2XX3_DATA
@ RN2XX3_DATA
Command returned data.
Definition: rn2xx3.h:85
loramac_dr_idx_t
loramac_dr_idx_t
LoRaMAC datarate indexes.
Definition: loramac.h:389
rn2xx3_init
int rn2xx3_init(rn2xx3_t *dev)
Initializes the RN2XX3 device.
rn2xx3_sys_sleep
int rn2xx3_sys_sleep(rn2xx3_t *dev)
Puts the RN2XX2 device in sleep mode.
rn2xx3_mac_get_tx_power
loramac_tx_pwr_idx_t rn2xx3_mac_get_tx_power(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX radio power index.
RN2XX3_INT_STATE_MAC_TX
@ RN2XX3_INT_STATE_MAC_TX
waiting for mac tx reply
Definition: rn2xx3.h:119
RN2XX3_MAX_BUF
#define RN2XX3_MAX_BUF
Maximum length of an exchanged messages with commands.
Definition: rn2xx3.h:40
rn2xx3_wait_response
int rn2xx3_wait_response(rn2xx3_t *dev)
Waits for a response to a command from the device.
rn2xx3_mac_set_rx1_delay
void rn2xx3_mac_set_rx1_delay(rn2xx3_t *dev, uint16_t rx1)
Sets the rn2xx3 LoRaMAC interval delay before the first reception window (in ms)
rn2xx3_mac_set_appeui
void rn2xx3_mac_set_appeui(rn2xx3_t *dev, const uint8_t *eui)
Sets the rn2xx3 LoRaMAC application EUI.
RN2XX3_REPLY_TIMEOUT
@ RN2XX3_REPLY_TIMEOUT
No MAC reply received from server.
Definition: rn2xx3.h:106
rn2xx3_t::resp_size
uint16_t resp_size
counter for received char in response
Definition: rn2xx3.h:164
rn2xx3_mac_get_appeui
void rn2xx3_mac_get_appeui(rn2xx3_t *dev, uint8_t *eui)
Gets the rn2xx3 LoRaMAC application EUI.
RN2XX3_ERR_MAC_INIT
@ RN2XX3_ERR_MAC_INIT
Device mac initialization failed.
Definition: rn2xx3.h:88
uart_t
unsigned int uart_t
Define default UART type identifier.
Definition: uart.h:76
rn2xx3_t::resp_lock
mutex_t resp_lock
mutex for waiting for command response
Definition: rn2xx3.h:161
rn2xx3_sys_factory_reset
int rn2xx3_sys_factory_reset(rn2xx3_t *dev)
Performs a factory reset of the module.
rn2xx3_mac_set_devaddr
void rn2xx3_mac_set_devaddr(rn2xx3_t *dev, const uint8_t *addr)
Sets the rn2xx3 LoRaMAC device address.
uart.h
Low-level UART peripheral driver interface definition.
rn2xx3_mac_get_dr
loramac_dr_idx_t rn2xx3_mac_get_dr(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC datarate.
loramac_settings_t::tx_port
uint8_t tx_port
Application TX port (between 1 and 223)
Definition: rn2xx3.h:130
rn2xx3_t
RN2483/RN2903 device descriptor.
Definition: rn2xx3.h:148
rn2xx3_setup
void rn2xx3_setup(rn2xx3_t *dev, const rn2xx3_params_t *params)
Prepares the given RN2XX3 device.
RN2XX3_ERR_BUSY
@ RN2XX3_ERR_BUSY
MAC is not in Idle state.
Definition: rn2xx3.h:94
rn2xx3_mac_set_appkey
void rn2xx3_mac_set_appkey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC application key.
rn2xx3_write_cmd
int rn2xx3_write_cmd(rn2xx3_t *dev)
Writes a command to the RN2XX3 device.
rn2xx3_mac_set_tx_port
void rn2xx3_mac_set_tx_port(rn2xx3_t *dev, uint8_t port)
Sets the rn2xx3 LoRaMAC TX port.
rn2xx3_t::rx_size
uint16_t rx_size
counter for received char in RX
Definition: rn2xx3.h:172
rn2xx3_mac_get_band
uint16_t rn2xx3_mac_get_band(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC frequency band in operation.
RN2XX3_REPLY_OTHER
@ RN2XX3_REPLY_OTHER
Unknown reply.
Definition: rn2xx3.h:107
netdev.h
Definitions low-level network driver interface.
RN2XX3_REPLY_JOIN_DENIED
@ RN2XX3_REPLY_JOIN_DENIED
Join procedure failed.
Definition: rn2xx3.h:105
RN2XX3_ERR_KEYS_NOT_INIT
@ RN2XX3_ERR_KEYS_NOT_INIT
Keys not configured ("mac join" command)
Definition: rn2xx3.h:97
loramac_tx_pwr_idx_t
loramac_tx_pwr_idx_t
LoRaMAC transmission power indexes.
Definition: loramac.h:576
rn2xx3_mac_get_tx_port
uint8_t rn2xx3_mac_get_tx_port(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX port.
rn2xx3_t::int_state
uint8_t int_state
current state of the device
Definition: rn2xx3.h:158
rn2xx3_t::cmd_lock
mutex_t cmd_lock
mutex to allow only one command at a time
Definition: rn2xx3.h:156
rn2xx3_mac_set_rx2_dr
void rn2xx3_mac_set_rx2_dr(rn2xx3_t *dev, loramac_dr_idx_t dr)
Sets the rn2xx3 LoRaMAC datarate used for second receive window.
loramac_settings_t::rx2_dr
loramac_dr_idx_t rx2_dr
Datarate used for second receive window.
Definition: rn2xx3.h:129
rn2xx3_mac_get_rx2_freq
uint32_t rn2xx3_mac_get_rx2_freq(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC frequency used during second receive window (in Hz)
RN2XX3_INT_STATE_MAC_JOIN
@ RN2XX3_INT_STATE_MAC_JOIN
waiting for mac join procedure reply
Definition: rn2xx3.h:118
loramac_join_mode_t
loramac_join_mode_t
LoRaMAC network join procedure type.
Definition: loramac.h:370
rn2xx3_mac_set_dr
void rn2xx3_mac_set_dr(rn2xx3_t *dev, loramac_dr_idx_t dr)
Sets the rn2xx3 LoRaMAC datarate.
rn2xx3_params_t
Configuration parameters for RN2483/RN2903 devices.
Definition: rn2xx3.h:138
rn2xx3_mac_set_rx2_freq
void rn2xx3_mac_set_rx2_freq(rn2xx3_t *dev, uint32_t freq)
Sets the rn2xx3 LoRaMAC frequency used during second receive window (in Hz)
rn2xx3_sys_reset
int rn2xx3_sys_reset(rn2xx3_t *dev)
Restarts the RN2XX2 device.
rn2xx3_mac_get_deveui
void rn2xx3_mac_get_deveui(rn2xx3_t *dev, uint8_t *eui)
Gets the rn2xx3 LoRaMAC device EUI.
rn2xx3_mac_set_linkchk_interval
void rn2xx3_mac_set_linkchk_interval(rn2xx3_t *dev, uint16_t linkchk)
Sets the rn2xx3 LoRaMAC link check interval (in seconds)
rn2xx3_wait_reply
int rn2xx3_wait_reply(rn2xx3_t *dev, uint8_t timeout)
Waits for a reply from the LoRaWAN network.
rn2xx3_mac_get_tx_mode
loramac_tx_mode_t rn2xx3_mac_get_tx_mode(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX mode.
rn2xx3_t::netdev
netdev_t netdev
Netdev parent struct.
Definition: rn2xx3.h:149
RN2XX3_REPLY_TX_MAC_OK
@ RN2XX3_REPLY_TX_MAC_OK
MAC transmission successful.
Definition: rn2xx3.h:100
netdev
Structure to hold driver state.
Definition: netdev.h:302
rn2xx3_mac_join_network
int rn2xx3_mac_join_network(rn2xx3_t *dev, loramac_join_mode_t mode)
Starts network activation procedure.
RN2XX3_OK
@ RN2XX3_OK
Command is valid.
Definition: rn2xx3.h:84
rn2xx3_mac_get_rx1_delay
uint16_t rn2xx3_mac_get_rx1_delay(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC interval delay before the first reception window (in ms)
rn2xx3_mac_init
int rn2xx3_mac_init(rn2xx3_t *dev)
Initializes the RN2XX3 device MAC layer.
rn2xx3_mac_get_ar
bool rn2xx3_mac_get_ar(rn2xx3_t *dev)
Checks the rn2xx3 LoRaMAC automatic reply state.
rn2xx3_mac_get_devaddr
void rn2xx3_mac_get_devaddr(rn2xx3_t *dev, uint8_t *addr)
Gets the rn2xx3 LoRaMAC device address.
gpio.h
Low-level GPIO peripheral driver interface definitions.
RN2XX3_TIMEOUT
@ RN2XX3_TIMEOUT
Command timeout.
Definition: rn2xx3.h:86
RN2XX3_ERR_NO_FREE_CH
@ RN2XX3_ERR_NO_FREE_CH
All channels are busy.
Definition: rn2xx3.h:91
rn2xx3_mac_set_retx
void rn2xx3_mac_set_retx(rn2xx3_t *dev, uint8_t retx)
Sets the rn2xx3 LoRaMAC uplink retransmission retries number.
rn2xx3_mac_set_appskey
void rn2xx3_mac_set_appskey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC application session key.
rn2xx3_mac_get_rx2_delay
uint16_t rn2xx3_mac_get_rx2_delay(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC interval delay before the second reception window (in ms)
rn2xx3_sys_set_sleep_duration
void rn2xx3_sys_set_sleep_duration(rn2xx3_t *dev, uint32_t sleep)
Sets the rn2xx3 sleep mode duration (in ms)
loramac_settings_t
LoRaMAC communication settings.
Definition: rn2xx3.h:127
RN2XX3_RX_MAX_BUF
#define RN2XX3_RX_MAX_BUF
Maximum length of an RX message.
Definition: rn2xx3.h:47
loramac_settings_t::tx_mode
loramac_tx_mode_t tx_mode
TX mode, either confirmable of unconfirmable.
Definition: rn2xx3.h:131
loramac_tx_mode_t
loramac_tx_mode_t
LoRaMAC transmission mode.
Definition: loramac.h:378
rn2xx3_mac_get_adr
bool rn2xx3_mac_get_adr(rn2xx3_t *dev)
Checks if the rn2xx3 LoRaMAC adaptive datarate is enabled/disabled.
RN2XX3_ERR_INVALID_DATA_LENGTH
@ RN2XX3_ERR_INVALID_DATA_LENGTH
Wrong payload given.
Definition: rn2xx3.h:96
rn2xx3_t::sleep_timer
xtimer_t sleep_timer
Timer used to count module sleep time.
Definition: rn2xx3.h:175
rn2xx3_t::sleep
uint32_t sleep
module sleep duration
Definition: rn2xx3.h:176
RN2XX3_ERR_FR_CNT_REJOIN_NEEDED
@ RN2XX3_ERR_FR_CNT_REJOIN_NEEDED
Frame counter rolled over.
Definition: rn2xx3.h:93
rn2xx3_params_t::uart
uart_t uart
UART interfaced the device is connected to.
Definition: rn2xx3.h:139
loramac_settings_t::rx_port
uint8_t rx_port
RX port (between 1 and 223)
Definition: rn2xx3.h:132
rn2xx3_mac_get_rx2_dr
loramac_dr_idx_t rn2xx3_mac_get_rx2_dr(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC datarate used for second receive window.
RN2XX3_ERR_SLEEP_MODE
@ RN2XX3_ERR_SLEEP_MODE
Failure because device is in sleep mode.
Definition: rn2xx3.h:98
rn2xx3_params_t::baudrate
uint32_t baudrate
baudrate to use
Definition: rn2xx3.h:140
RN2XX3_INT_STATE_IDLE
@ RN2XX3_INT_STATE_IDLE
waiting for incoming commands
Definition: rn2xx3.h:116
RN2XX3_REPLY_JOIN_ACCEPTED
@ RN2XX3_REPLY_JOIN_ACCEPTED
Join procedure successful.
Definition: rn2xx3.h:104
xtimer.h
xtimer interface definitions
mutex_t
Mutex structure.
Definition: mutex.h:120
rn2xx3_mac_set_tx_mode
void rn2xx3_mac_set_tx_mode(rn2xx3_t *dev, loramac_tx_mode_t mode)
Sets the rn2xx3 LoRaMAC TX mode.
RN2XX3_INT_STATE_RESET
@ RN2XX3_INT_STATE_RESET
the device is being reset or initialized
Definition: rn2xx3.h:114
RN2XX3_INT_STATE_MAC_RX_PORT
@ RN2XX3_INT_STATE_MAC_RX_PORT
waiting for mac rx port
Definition: rn2xx3.h:120
rn2xx3_t::p
rn2xx3_params_t p
configuration parameters
Definition: rn2xx3.h:151