Loading...
Searching...
No Matches
rn2xx3.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Inria
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
19
20#include <stdint.h>
21
22#include "ztimer.h"
23#include "periph/uart.h"
24#include "periph/gpio.h"
25#include "net/netdev.h"
26
27#include "net/loramac.h"
28
29#ifdef __cplusplus
30extern "C" {
31#endif
32
36#define RN2XX3_MAX_BUF (40U)
37
43#define RN2XX3_RX_MAX_BUF (250U)
44
48#define RN2XX3_REPLY_DELAY_TIMEOUT (60U)
49
53#define RN2XX3_SLEEP_MIN (100U)
54
63#ifndef CONFIG_RN2XX3_DEFAULT_SLEEP
64#define CONFIG_RN2XX3_DEFAULT_SLEEP (5000U)
65#endif
66
80#ifdef DOXYGEN
81#define CONFIG_RN2XX3_DEFAULT_AR
82#endif
84
85#if defined(MODULE_RN2903)
86#define RN2XX3_FREQ_BAND (915U)
87#elif defined(MODULE_RN2483)
88#define RN2XX3_FREQ_BAND (868U)
89#else
90#error "Unsupported module type, use either RN2483 or RN2903"
91#endif
92
96enum {
100 /* all other error codes */
112 /* all other reply codes */
121};
122
126enum {
135};
136
147
151typedef struct {
153 uint32_t baudrate;
154 gpio_t pin_reset;
157
161typedef struct {
163 /* device driver specific fields */
166
167 /* values for the UART TX state machine */
171 uint8_t int_state;
172
173 /* buffer and synchronization for command responses */
177 uint16_t resp_size;
178 uint8_t resp_done;
179
180 /* buffer for RX messages */
181 char rx_tmp_buf[2];
185 uint16_t rx_size;
186
187 /* timers */
189 uint32_t sleep;
190} rn2xx3_t;
191
198void rn2xx3_setup(rn2xx3_t *dev, const rn2xx3_params_t *params);
199
210
223
239
249
259
280
292
310
326int rn2xx3_wait_reply(rn2xx3_t *dev, uint8_t timeout);
327
345int rn2xx3_mac_tx(rn2xx3_t *dev, uint8_t *payload, uint8_t payload_len);
346
362
376
377/* Get/Set functions definitions */
378
387void rn2xx3_mac_get_deveui(rn2xx3_t *dev, uint8_t *eui);
388
397void rn2xx3_mac_set_deveui(rn2xx3_t *dev, const uint8_t *eui);
398
407void rn2xx3_mac_get_appeui(rn2xx3_t *dev, uint8_t *eui);
408
417void rn2xx3_mac_set_appeui(rn2xx3_t *dev, const uint8_t *eui);
418
427void rn2xx3_mac_set_appkey(rn2xx3_t *dev, const uint8_t *key);
428
437void rn2xx3_mac_set_appskey(rn2xx3_t *dev, const uint8_t *key);
438
447void rn2xx3_mac_set_nwkskey(rn2xx3_t *dev, const uint8_t *key);
448
457void rn2xx3_mac_get_devaddr(rn2xx3_t *dev, uint8_t *addr);
458
467void rn2xx3_mac_set_devaddr(rn2xx3_t *dev, const uint8_t *addr);
468
477
485
494
502
511
521
528void rn2xx3_mac_set_adr(rn2xx3_t *dev, bool adr);
529
541void rn2xx3_mac_set_battery(rn2xx3_t *dev, uint8_t battery);
542
551
558void rn2xx3_mac_set_retx(rn2xx3_t *dev, uint8_t retx);
559
566void rn2xx3_mac_set_linkchk_interval(rn2xx3_t *dev, uint16_t linkchk);
567
576
583void rn2xx3_mac_set_rx1_delay(rn2xx3_t *dev, uint16_t rx1);
584
593
602
609void rn2xx3_mac_set_ar(rn2xx3_t *dev, bool ar);
610
619
627
636
643void rn2xx3_mac_set_rx2_freq(rn2xx3_t *dev, uint32_t freq);
644
653
660void rn2xx3_mac_set_tx_port(rn2xx3_t *dev, uint8_t port);
661
670
678
687
694void rn2xx3_sys_set_sleep_duration(rn2xx3_t *dev, uint32_t sleep);
695
696#ifdef __cplusplus
697}
698#endif
699
Definitions low-level network driver interface.
Low-level GPIO peripheral driver interface definitions.
struct netdev netdev_t
Forward declaration for netdev struct.
Definition netdev.h:285
uint_fast8_t uart_t
Define default UART type identifier.
Definition uart.h:109
void rn2xx3_mac_set_tx_power(rn2xx3_t *dev, loramac_tx_pwr_idx_t power)
Sets the rn2xx3 LoRaMAC transmission power index.
int rn2xx3_wait_response(rn2xx3_t *dev)
Waits for a response to a command from the device.
uint16_t rn2xx3_mac_get_rx1_delay(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC interval delay before the first reception window (in ms)
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)
void rn2xx3_mac_set_nwkskey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC network session key.
void rn2xx3_mac_set_deveui(rn2xx3_t *dev, const uint8_t *eui)
Sets the rn2xx3 LoRaMAC device EUI.
int rn2xx3_mac_save(rn2xx3_t *dev)
Saves current LoRaMAC configuration to internal EEPROM.
int rn2xx3_sys_sleep(rn2xx3_t *dev)
Puts the RN2XX2 device in sleep mode.
void rn2xx3_mac_set_retx(rn2xx3_t *dev, uint8_t retx)
Sets the rn2xx3 LoRaMAC uplink retransmission retries number.
void rn2xx3_mac_set_tx_mode(rn2xx3_t *dev, loramac_tx_mode_t mode)
Sets the rn2xx3 LoRaMAC TX mode.
int rn2xx3_write_cmd(rn2xx3_t *dev)
Writes a command to the RN2XX3 device.
#define RN2XX3_MAX_BUF
Maximum length of an exchanged messages with commands.
Definition rn2xx3.h:36
void rn2xx3_mac_set_appskey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC application session key.
void rn2xx3_mac_set_battery(rn2xx3_t *dev, uint8_t battery)
Sets the rn2xx3 battery level measured by the end device.
int rn2xx3_mac_tx(rn2xx3_t *dev, uint8_t *payload, uint8_t payload_len)
Sends data to LoRaWAN server.
uint32_t rn2xx3_mac_get_rx2_freq(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC frequency used during second receive window (in Hz)
loramac_dr_idx_t rn2xx3_mac_get_rx2_dr(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC datarate used for second receive window.
uint8_t rn2xx3_mac_get_retx(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC uplink retransmission retries number.
void rn2xx3_mac_set_appkey(rn2xx3_t *dev, const uint8_t *key)
Sets the rn2xx3 LoRaMAC application key.
uint16_t rn2xx3_mac_get_rx2_delay(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC interval delay before the second reception window (in ms)
void rn2xx3_mac_get_devaddr(rn2xx3_t *dev, uint8_t *addr)
Gets the rn2xx3 LoRaMAC device address.
bool rn2xx3_mac_get_adr(rn2xx3_t *dev)
Checks if the rn2xx3 LoRaMAC adaptive datarate is enabled/disabled.
int rn2xx3_sys_factory_reset(rn2xx3_t *dev)
Performs a factory reset of the module.
int rn2xx3_wait_reply(rn2xx3_t *dev, uint8_t timeout)
Waits for a reply from the LoRaWAN network.
bool rn2xx3_mac_get_ar(rn2xx3_t *dev)
Checks the rn2xx3 LoRaMAC automatic reply state.
void rn2xx3_mac_set_rx2_freq(rn2xx3_t *dev, uint32_t freq)
Sets the rn2xx3 LoRaMAC frequency used during second receive window (in Hz)
int rn2xx3_sys_reset(rn2xx3_t *dev)
Restarts the RN2XX2 device.
uint8_t rn2xx3_mac_get_rx_port(rn2xx3_t *dev)
Parses the response buffer to get the LoRaWAN RX port.
loramac_tx_pwr_idx_t rn2xx3_mac_get_tx_power(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX radio power index.
loramac_tx_mode_t rn2xx3_mac_get_tx_mode(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX mode.
int rn2xx3_write_cmd_no_wait(rn2xx3_t *dev)
Writes a command to the RN2XX3 device but don't wait for timeout or response.
#define RN2XX3_RX_MAX_BUF
Maximum length of an RX message.
Definition rn2xx3.h:43
void rn2xx3_mac_set_tx_port(rn2xx3_t *dev, uint8_t port)
Sets the rn2xx3 LoRaMAC TX port.
int rn2xx3_mac_init(rn2xx3_t *dev)
Initializes the RN2XX3 device MAC layer.
void rn2xx3_mac_get_deveui(rn2xx3_t *dev, uint8_t *eui)
Gets the rn2xx3 LoRaMAC device EUI.
void rn2xx3_mac_set_appeui(rn2xx3_t *dev, const uint8_t *eui)
Sets the rn2xx3 LoRaMAC application EUI.
void rn2xx3_mac_set_ar(rn2xx3_t *dev, bool ar)
Enables/disables LoRaMAC rn2xx3 MAC automatic reply state.
void rn2xx3_mac_get_appeui(rn2xx3_t *dev, uint8_t *eui)
Gets the rn2xx3 LoRaMAC application EUI.
void rn2xx3_mac_set_adr(rn2xx3_t *dev, bool adr)
Enables/disables the rn2xx3 LoRaMAC adaptive datarate.
void rn2xx3_sys_set_sleep_duration(rn2xx3_t *dev, uint32_t sleep)
Sets the rn2xx3 sleep mode duration (in ms)
void rn2xx3_mac_set_rx2_dr(rn2xx3_t *dev, loramac_dr_idx_t dr)
Sets the rn2xx3 LoRaMAC datarate used for second receive window.
uint8_t rn2xx3_mac_get_tx_port(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC TX port.
void rn2xx3_setup(rn2xx3_t *dev, const rn2xx3_params_t *params)
Prepares the given RN2XX3 device.
void rn2xx3_mac_set_devaddr(rn2xx3_t *dev, const uint8_t *addr)
Sets the rn2xx3 LoRaMAC device address.
loramac_dr_idx_t rn2xx3_mac_get_dr(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC datarate.
void rn2xx3_mac_set_linkchk_interval(rn2xx3_t *dev, uint16_t linkchk)
Sets the rn2xx3 LoRaMAC link check interval (in seconds)
uint16_t rn2xx3_mac_get_band(rn2xx3_t *dev)
Gets the rn2xx3 LoRaMAC frequency band in operation.
void rn2xx3_mac_set_dr(rn2xx3_t *dev, loramac_dr_idx_t dr)
Sets the rn2xx3 LoRaMAC datarate.
int rn2xx3_mac_join_network(rn2xx3_t *dev, loramac_join_mode_t mode)
Starts network activation procedure.
int rn2xx3_init(rn2xx3_t *dev)
Initializes the RN2XX3 device.
@ RN2XX3_ERR_SLEEP_MODE
Failure because device is in sleep mode.
Definition rn2xx3.h:111
@ RN2XX3_REPLY_JOIN_DENIED
Join procedure failed.
Definition rn2xx3.h:118
@ RN2XX3_REPLY_OTHER
Unknown reply.
Definition rn2xx3.h:120
@ RN2XX3_REPLY_TX_MAC_RX
Data received from server.
Definition rn2xx3.h:116
@ RN2XX3_ERR_MAC_INIT
Device mac initialization failed.
Definition rn2xx3.h:101
@ RN2XX3_ERR_FR_CNT_REJOIN_NEEDED
Frame counter rolled over.
Definition rn2xx3.h:106
@ RN2XX3_ERR_SILENT
Device is in Silent Immediately state.
Definition rn2xx3.h:105
@ RN2XX3_OK
Command is valid.
Definition rn2xx3.h:97
@ RN2XX3_TIMEOUT
Command timeout.
Definition rn2xx3.h:99
@ RN2XX3_REPLY_TX_INVALID_DATA_LEN
Application payload too large.
Definition rn2xx3.h:115
@ RN2XX3_ERR_BUSY
MAC is not in Idle state.
Definition rn2xx3.h:107
@ RN2XX3_REPLY_TIMEOUT
No MAC reply received from server.
Definition rn2xx3.h:119
@ RN2XX3_REPLY_TX_MAC_OK
MAC transmission successful.
Definition rn2xx3.h:113
@ RN2XX3_ERR_INVALID_DATA_LENGTH
Wrong payload given.
Definition rn2xx3.h:109
@ RN2XX3_REPLY_JOIN_ACCEPTED
Join procedure successful.
Definition rn2xx3.h:117
@ RN2XX3_ERR_MAC_PAUSED
MAC was paused.
Definition rn2xx3.h:108
@ RN2XX3_REPLY_TX_MAC_ERR
MAC transmission failed.
Definition rn2xx3.h:114
@ RN2XX3_ERR_NOT_JOINED
Network is not joined.
Definition rn2xx3.h:103
@ RN2XX3_ERR_NO_FREE_CH
All channels are busy.
Definition rn2xx3.h:104
@ RN2XX3_ERR_KEYS_NOT_INIT
Keys not configured ("mac join" command)
Definition rn2xx3.h:110
@ RN2XX3_ERR_INVALID_PARAM
Wrong command given.
Definition rn2xx3.h:102
@ RN2XX3_DATA
Command returned data.
Definition rn2xx3.h:98
@ RN2XX3_INT_STATE_CMD
waiting command response
Definition rn2xx3.h:128
@ RN2XX3_INT_STATE_RESET
the device is being reset or initialized
Definition rn2xx3.h:127
@ RN2XX3_INT_STATE_SLEEP
sleep mode
Definition rn2xx3.h:130
@ RN2XX3_INT_STATE_MAC_TX
waiting for mac tx reply
Definition rn2xx3.h:132
@ RN2XX3_INT_STATE_IDLE
waiting for incoming commands
Definition rn2xx3.h:129
@ RN2XX3_INT_STATE_MAC_RX_MESSAGE
waiting for mac rx message
Definition rn2xx3.h:134
@ RN2XX3_INT_STATE_MAC_RX_PORT
waiting for mac rx port
Definition rn2xx3.h:133
@ RN2XX3_INT_STATE_MAC_JOIN
waiting for mac join procedure reply
Definition rn2xx3.h:131
loramac_join_mode_t
LoRaMAC network join procedure type.
Definition loramac.h:671
loramac_tx_mode_t
LoRaMAC transmission mode.
Definition loramac.h:679
loramac_dr_idx_t
LoRaMAC datarate indexes.
Definition loramac.h:690
loramac_tx_pwr_idx_t
LoRaMAC transmission power indexes.
Definition loramac.h:877
LoRaMAC header definitions.
LoRaMAC communication settings.
Definition rn2xx3.h:140
loramac_dr_idx_t rx2_dr
Datarate used for second receive window.
Definition rn2xx3.h:142
uint32_t rx2_freq
Center frequency used for second receive window.
Definition rn2xx3.h:141
uint8_t tx_port
Application TX port (between 1 and 223)
Definition rn2xx3.h:143
loramac_tx_mode_t tx_mode
TX mode, either confirmable of unconfirmable.
Definition rn2xx3.h:144
uint8_t rx_port
RX port (between 1 and 223)
Definition rn2xx3.h:145
Mutex structure.
Definition mutex.h:36
Configuration parameters for RN2483/RN2903 devices.
Definition rn2xx3.h:151
uart_t uart
UART interfaced the device is connected to.
Definition rn2xx3.h:152
uint32_t baudrate
baudrate to use
Definition rn2xx3.h:153
gpio_t pin_reset
GPIO pin that is connected to the STATUS pin set to GPIO_UNDEF if not used.
Definition rn2xx3.h:154
RN2483/RN2903 device descriptor.
Definition rn2xx3.h:161
char resp_buf[RN2XX3_MAX_BUF]
command response data buffer
Definition rn2xx3.h:176
uint32_t sleep
module sleep duration
Definition rn2xx3.h:189
uint16_t rx_size
counter for received char in RX
Definition rn2xx3.h:185
ztimer_t sleep_timer
Timer used to count module sleep time.
Definition rn2xx3.h:188
mutex_t resp_lock
mutex for waiting for command response
Definition rn2xx3.h:174
loramac_settings_t loramac
loramac communication settings
Definition rn2xx3.h:165
uint16_t resp_size
counter for received char in response
Definition rn2xx3.h:177
char cmd_buf[RN2XX3_MAX_BUF]
command to send data buffer
Definition rn2xx3.h:168
uint8_t rx_buf[RN2XX3_RX_MAX_BUF]
RX data buffer.
Definition rn2xx3.h:184
char rx_tmp_buf[2]
Temporary RX buffer used to convert 2 hex characters in one byte on the fly.
Definition rn2xx3.h:181
mutex_t cmd_lock
mutex to allow only one command at a time
Definition rn2xx3.h:169
rn2xx3_params_t p
configuration parameters
Definition rn2xx3.h:164
netdev_t netdev
Netdev parent struct.
Definition rn2xx3.h:162
uint8_t int_state
current state of the device
Definition rn2xx3.h:171
uint8_t resp_done
check if response has completed
Definition rn2xx3.h:178
ztimer structure
Definition ztimer.h:319
Low-level UART peripheral driver interface definition.
ztimer API