1 #ifndef _TWR_CMWX1ZZABZ_H
2 #define _TWR_CMWX1ZZABZ_H
4 #include <twr_scheduler.h>
14 #define TWR_CMWX1ZZABZ_TX_MAX_PACKET_SIZE 230
16 #define TWR_CMWX1ZZABZ_TX_FIFO_BUFFER_SIZE (TWR_CMWX1ZZABZ_TX_MAX_PACKET_SIZE + 25)
17 #define TWR_CMWX1ZZABZ_RX_FIFO_BUFFER_SIZE 220
18 #define TWR_CMWX1ZZABZ_CUSTOM_COMMAND_BUFFER_SIZE 32
19 #define TWR_CMWX1ZZABZ_FW_VERSION_BUFFER_SIZE 64
67 TWR_CMWX1ZZABZ_EVENT_LINK_CHECK_OK = 13,
69 TWR_CMWX1ZZABZ_EVENT_LINK_CHECK_NOK = 14,
71 TWR_CMWX1ZZABZ_EVENT_MODEM_FACTORY_RESET = 15,
73 TWR_CMWX1ZZABZ_EVENT_CUSTOM_AT = 16
85 TWR_CMWX1ZZABZ_CONFIG_MODE_ABP = 0,
86 TWR_CMWX1ZZABZ_CONFIG_MODE_OTAA = 1
94 TWR_CMWX1ZZABZ_CONFIG_BAND_AS923 = 0,
95 TWR_CMWX1ZZABZ_CONFIG_BAND_AU915 = 1,
96 TWR_CMWX1ZZABZ_CONFIG_BAND_EU868 = 5,
97 TWR_CMWX1ZZABZ_CONFIG_BAND_KR920 = 6,
98 TWR_CMWX1ZZABZ_CONFIG_BAND_IN865 = 7,
99 TWR_CMWX1ZZABZ_CONFIG_BAND_US915 = 8
107 TWR_CMWX1ZZABZ_CONFIG_CLASS_A = 0,
108 TWR_CMWX1ZZABZ_CONFIG_CLASS_C = 2
116 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF12_125KHZ = 0,
117 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF11_125KHZ = 1,
118 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF10_125KHZ = 2,
119 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF9_125KHZ = 3,
120 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF8_125KHZ = 4,
121 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF7_125KHZ = 5,
122 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_SF7_250KHZ = 6,
123 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AS923_FSK_50KBPS = 7,
131 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF12_125KHZ = 0,
132 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF11_125KHZ = 1,
133 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF10_125KHZ = 2,
134 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF9_125KHZ = 3,
135 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF8_125KHZ = 4,
136 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF7_125KHZ = 5,
137 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF8_500KHZ = 6,
138 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF12_500KHZ = 8,
139 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF11_500KHZ = 9,
140 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF10_500KHZ = 10,
141 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF9_500KHZ = 11,
142 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF8_500KHZ_2 = 12,
143 TWR_CMWX1ZZABZ_CONFIG_DATARATE_AU915_SF7_500KHZ = 13
152 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF12_125KHZ = 0,
153 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF11_125KHZ = 1,
154 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF10_125KHZ = 2,
155 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF9_125KHZ = 3,
156 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF8_125KHZ = 4,
157 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF7_125KHZ = 5,
158 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_SF7_250KHZ = 6,
159 TWR_CMWX1ZZABZ_CONFIG_DATARATE_EU868_FSK_50KBPS = 7
167 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF12_125KHZ = 0,
168 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF11_125KHZ = 1,
169 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF10_125KHZ = 2,
170 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF9_125KHZ = 3,
171 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF8_125KHZ = 4,
172 TWR_CMWX1ZZABZ_CONFIG_DATARATE_KR920_SF7_125KHZ = 5
180 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF10_125KHZ = 0,
181 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF9_125KHZ = 1,
182 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF8_125KHZ = 2,
183 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF7_125KHZ = 3,
184 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF8_500KHZ = 4,
185 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF12_500KHZ = 8,
186 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF11_500KHZ = 9,
187 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF10_500KHZ = 10,
188 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF9_500KHZ = 11,
189 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF8_500KHZ_2 = 12,
190 TWR_CMWX1ZZABZ_CONFIG_DATARATE_US915_SF7_500KHZ = 13
198 TWR_CMWX1ZZABZ_CONFIG_INDEX_DEVADDR = 0,
199 TWR_CMWX1ZZABZ_CONFIG_INDEX_DEVEUI = 1,
200 TWR_CMWX1ZZABZ_CONFIG_INDEX_APPEUI = 2,
201 TWR_CMWX1ZZABZ_CONFIG_INDEX_NWKSKEY = 3,
202 TWR_CMWX1ZZABZ_CONFIG_INDEX_APPSKEY = 4,
203 TWR_CMWX1ZZABZ_CONFIG_INDEX_APPKEY = 5,
204 TWR_CMWX1ZZABZ_CONFIG_INDEX_BAND = 6,
205 TWR_CMWX1ZZABZ_CONFIG_INDEX_MODE = 7,
206 TWR_CMWX1ZZABZ_CONFIG_INDEX_CLASS = 8,
207 TWR_CMWX1ZZABZ_CONFIG_INDEX_RX2 = 9,
208 TWR_CMWX1ZZABZ_CONFIG_INDEX_NWK = 10,
209 TWR_CMWX1ZZABZ_CONFIG_INDEX_ADAPTIVE_DATARATE = 11,
210 TWR_CMWX1ZZABZ_CONFIG_INDEX_DATARATE = 12,
211 TWR_CMWX1ZZABZ_CONFIG_INDEX_REP = 13,
212 TWR_CMWX1ZZABZ_CONFIG_INDEX_RTYNUM = 14,
213 TWR_CMWX1ZZABZ_CONFIG_INDEX_LAST_ITEM
215 } twr_cmwx1zzabz_config_index_t;
219 TWR_CMWX1ZZABZ_STATE_READY,
220 TWR_CMWX1ZZABZ_STATE_ERROR,
221 TWR_CMWX1ZZABZ_STATE_INITIALIZE,
222 TWR_CMWX1ZZABZ_STATE_INITIALIZE_AT_RESPONSE,
223 TWR_CMWX1ZZABZ_STATE_IDLE,
224 TWR_CMWX1ZZABZ_STATE_INITIALIZE_COMMAND_SEND,
225 TWR_CMWX1ZZABZ_STATE_INITIALIZE_COMMAND_RESPONSE,
227 TWR_CMWX1ZZABZ_STATE_CONFIG_SAVE_SEND,
228 TWR_CMWX1ZZABZ_STATE_CONFIG_SAVE_RESPONSE,
230 TWR_CMWX1ZZABZ_STATE_SEND_MESSAGE_COMMAND,
231 TWR_CMWX1ZZABZ_STATE_SEND_MESSAGE_CONFIRMED_COMMAND,
232 TWR_CMWX1ZZABZ_STATE_SEND_MESSAGE_RESPONSE,
234 TWR_CMWX1ZZABZ_STATE_JOIN_SEND,
235 TWR_CMWX1ZZABZ_STATE_JOIN_RESPONSE,
237 TWR_CMWX1ZZABZ_STATE_CUSTOM_COMMAND_SEND,
238 TWR_CMWX1ZZABZ_STATE_CUSTOM_COMMAND_RESPONSE,
240 TWR_CMWX1ZZABZ_STATE_LINK_CHECK_SEND,
241 TWR_CMWX1ZZABZ_STATE_LINK_CHECK_RESPONSE,
242 TWR_CMWX1ZZABZ_STATE_LINK_CHECK_RESPONSE_ANS,
244 TWR_CMWX1ZZABZ_STATE_RECEIVE,
245 TWR_CMWX1ZZABZ_STATE_RECOVER_BAUDRATE_UART,
246 TWR_CMWX1ZZABZ_STATE_RECOVER_BAUDRATE_REBOOT
248 } twr_cmwx1zzabz_state_t;
258 char nwkskey[32 + 1];
259 char appskey[32 + 1];
261 uint32_t rx2_frequency;
262 uint8_t rx2_datarate;
264 bool adaptive_datarate;
266 uint8_t repetition_unconfirmed;
267 uint8_t repetition_confirmed;
269 } twr_cmwx1zzabz_config;
275 twr_cmwx1zzabz_state_t _state;
276 twr_cmwx1zzabz_state_t _state_after_sleep;
279 uint8_t _tx_fifo_buffer[TWR_CMWX1ZZABZ_TX_FIFO_BUFFER_SIZE];
280 uint8_t _rx_fifo_buffer[TWR_CMWX1ZZABZ_RX_FIFO_BUFFER_SIZE];
283 char _command[TWR_CMWX1ZZABZ_TX_FIFO_BUFFER_SIZE];
284 char _response[TWR_CMWX1ZZABZ_RX_FIFO_BUFFER_SIZE];
285 uint8_t _response_length;
286 uint8_t _message_buffer[TWR_CMWX1ZZABZ_TX_MAX_PACKET_SIZE];
287 size_t _message_length;
288 uint8_t _message_port;
289 uint8_t _init_command_index;
290 uint8_t _save_command_index;
292 uint32_t _save_config_mask;
293 twr_cmwx1zzabz_config _config;
295 bool _link_check_command;
296 bool _custom_command;
297 char _custom_command_buf[TWR_CMWX1ZZABZ_CUSTOM_COMMAND_BUFFER_SIZE];
301 char _fw_version[TWR_CMWX1ZZABZ_FW_VERSION_BUFFER_SIZE];
303 int32_t _cmd_rfq_rssi;
304 int32_t _cmd_rfq_snr;
306 uint32_t _cmd_frmcnt_uplink;
307 uint32_t _cmd_frmcnt_downlink;
309 uint8_t _cmd_link_check_margin;
310 uint8_t _cmd_link_check_gwcnt;
twr_cmwx1zzabz_config_datarate_as923_t
Datarate for AS923.
void twr_cmwx1zzabz_set_adaptive_datarate(twr_cmwx1zzabz_t *self, bool enable)
Set the configuration adaptive data rate.
bool twr_cmwx1zzabz_link_check(twr_cmwx1zzabz_t *self)
Request send of link check packet.
void twr_cmwx1zzabz_set_appeui(twr_cmwx1zzabz_t *self, char *appeui)
Set APPEUI.
void twr_cmwx1zzabz_get_deveui(twr_cmwx1zzabz_t *self, char *deveui)
Get DEVEUI.
void twr_cmwx1zzabz_init(twr_cmwx1zzabz_t *self, twr_uart_channel_t uart_channel)
Initialize CMWX1ZZABZ.
char * twr_cmwx1zzabz_get_error_command(twr_cmwx1zzabz_t *self)
Get pointer to string containg last sent command.
uint8_t twr_cmwx1zzabz_get_nwk_public(twr_cmwx1zzabz_t *self)
Get the configuration if public networks are enabled.
void twr_cmwx1zzabz_set_rx2(twr_cmwx1zzabz_t *self, uint32_t frequency, uint8_t datarate)
Set the frequency and datarate for RX2 receive window.
void twr_cmwx1zzabz_set_nwkskey(twr_cmwx1zzabz_t *self, char *nwkskey)
Set NWKSKEY.
void twr_cmwx1zzabz_get_appkey(twr_cmwx1zzabz_t *self, char *appkey)
Get APPKEY.
uint8_t twr_cmwx1zzabz_get_datarate(twr_cmwx1zzabz_t *self)
Get the configuration of datarate.
bool twr_cmwx1zzabz_send_message(twr_cmwx1zzabz_t *self, const void *buffer, size_t length)
Send LoRa message.
void twr_cmwx1zzabz_set_debug(twr_cmwx1zzabz_t *self, bool debug)
Set debugging flag which prints modem communication to twr_log.
void twr_cmwx1zzabz_get_nwkskey(twr_cmwx1zzabz_t *self, char *nwkskey)
Set NWKSKEY.
void twr_cmwx1zzabz_set_event_handler(twr_cmwx1zzabz_t *self, void(*event_handler)(twr_cmwx1zzabz_t *, twr_cmwx1zzabz_event_t, void *), void *event_param)
Set callback function.
twr_cmwx1zzabz_config_datarate_au915_t
Datarate for AU915.
bool twr_cmwx1zzabz_is_ready(twr_cmwx1zzabz_t *self)
Check if modem is ready for commands.
bool twr_cmwx1zzabz_get_rfq(twr_cmwx1zzabz_t *self, int32_t *rssi, int32_t *snr)
Get RF quality values in callback.
void twr_cmwx1zzabz_set_band(twr_cmwx1zzabz_t *self, twr_cmwx1zzabz_config_band_t band)
Set BAND.
void twr_cmwx1zzabz_set_repeat_unconfirmed(twr_cmwx1zzabz_t *self, uint8_t repeat)
Set number of transmissions of unconfirmed message.
twr_cmwx1zzabz_config_datarate_eu868_t
Datarate for EU868.
bool twr_cmwx1zzabz_frame_counter(twr_cmwx1zzabz_t *self)
Request frame counter value.
twr_cmwx1zzabz_config_mode_t
LoRa mode ABP/OTAA.
bool twr_cmwx1zzabz_get_link_check(twr_cmwx1zzabz_t *self, uint8_t *margin, uint8_t *gateway_count)
Get link check values.
void twr_cmwx1zzabz_set_devaddr(twr_cmwx1zzabz_t *self, char *devaddr)
Set DEVADDR.
twr_cmwx1zzabz_config_datarate_kr920_t
Datarate for KR920.
void twr_cmwx1zzabz_join(twr_cmwx1zzabz_t *self)
Start LoRa OTAA join procedure.
char * twr_cmwx1zzabz_get_fw_version(twr_cmwx1zzabz_t *self)
Get firmware version string.
twr_cmwx1zzabz_config_band_t twr_cmwx1zzabz_get_band(twr_cmwx1zzabz_t *self)
Get BAND.
void twr_cmwx1zzabz_set_port(twr_cmwx1zzabz_t *self, uint8_t port)
Set the port for the transmission of the messages.
void twr_cmwx1zzabz_get_devaddr(twr_cmwx1zzabz_t *self, char *devaddr)
Get DEVADDR.
bool twr_cmwx1zzabz_rfq(twr_cmwx1zzabz_t *self)
Request RF quality of the last received packet (JOIN, LNPARAM, confirmed message)
void twr_cmwx1zzabz_set_class(twr_cmwx1zzabz_t *self, twr_cmwx1zzabz_config_class_t class)
Set device class.
uint32_t twr_cmwx1zzabz_get_received_message_data(twr_cmwx1zzabz_t *self, uint8_t *buffer, uint32_t buffer_size)
Get received message data.
twr_cmwx1zzabz_config_class_t
LoRa device class A or C.
void twr_cmwx1zzabz_reboot(twr_cmwx1zzabz_t *self)
Reboot and initialize CMWX1ZZABZ.
void twr_cmwx1zzabz_get_appskey(twr_cmwx1zzabz_t *self, char *appskey)
Get APPSKEY.
uint32_t twr_cmwx1zzabz_get_received_message_length(twr_cmwx1zzabz_t *self)
Get length of the received message.
void twr_cmwx1zzabz_set_nwk_public(twr_cmwx1zzabz_t *self, uint8_t public)
Set the configuration enabling public networks.
twr_cmwx1zzabz_config_datarate_us915_t
Datarate for US915.
void twr_cmwx1zzabz_set_deveui(twr_cmwx1zzabz_t *self, char *deveui)
Set DEVEUI.
twr_cmwx1zzabz_config_band_t
Frequency modes and standards.
uint8_t twr_cmwx1zzabz_get_port(twr_cmwx1zzabz_t *self)
Get the port for the transmission of the messages.
uint8_t twr_cmwx1zzabz_get_repeat_confirmed(twr_cmwx1zzabz_t *self)
Get number of transmissions of confirmed message.
bool twr_cmwx1zzabz_custom_at(twr_cmwx1zzabz_t *self, char *at_command)
Send custom AT command to LoRa Module.
void twr_cmwx1zzabz_deinit(twr_cmwx1zzabz_t *self)
Deinitialize CMWX1ZZABZ.
uint8_t twr_cmwx1zzabz_get_repeat_unconfirmed(twr_cmwx1zzabz_t *self)
Get number of transmissions of unconfirmed message.
void twr_cmwx1zzabz_set_datarate(twr_cmwx1zzabz_t *self, uint8_t datarate)
Set the configuration of datarate.
void twr_cmwx1zzabz_set_appkey(twr_cmwx1zzabz_t *self, char *appkey)
Set APPKEY.
void twr_cmwx1zzabz_set_appskey(twr_cmwx1zzabz_t *self, char *appskey)
Set APPSKEY.
twr_cmwx1zzabz_config_mode_t twr_cmwx1zzabz_get_mode(twr_cmwx1zzabz_t *self)
Get ABP/OTAA mode.
void twr_cmwx1zzabz_get_appeui(twr_cmwx1zzabz_t *self, char *appeui)
Get APPEUI.
bool twr_cmwx1zzabz_get_adaptive_datarate(twr_cmwx1zzabz_t *self)
Get the configuration if adaptive data rate are enabled.
twr_cmwx1zzabz_config_class_t twr_cmwx1zzabz_get_class(twr_cmwx1zzabz_t *self)
Get device class.
bool twr_cmwx1zzabz_factory_reset(twr_cmwx1zzabz_t *self)
Send factory reset command to LoRa Module.
struct twr_cmwx1zzabz_t twr_cmwx1zzabz_t
CMWX1ZZABZ instance.
bool twr_cmwx1zzabz_get_frame_counter(twr_cmwx1zzabz_t *self, uint32_t *uplink, uint32_t *downlink)
Get frame counter value.
void twr_cmwx1zzabz_get_rx2(twr_cmwx1zzabz_t *self, uint32_t *frequency, uint8_t *datarate)
Get the frequency and datarate for RX2 receive window.
bool twr_cmwx1zzabz_send_message_confirmed(twr_cmwx1zzabz_t *self, const void *buffer, size_t length)
Send LoRa confirmed message.
void twr_cmwx1zzabz_set_mode(twr_cmwx1zzabz_t *self, twr_cmwx1zzabz_config_mode_t mode)
Set ABP/OTAA mode.
char * twr_cmwx1zzabz_get_error_response(twr_cmwx1zzabz_t *self)
Get pointer to string containg response on last sent command.
uint8_t twr_cmwx1zzabz_get_received_message_port(twr_cmwx1zzabz_t *self)
Get port of the received message.
twr_cmwx1zzabz_event_t
Callback events.
void twr_cmwx1zzabz_set_repeat_confirmed(twr_cmwx1zzabz_t *self, uint8_t repeat)
Set number of transmissions of confirmed message.
@ TWR_CMWX1ZZABZ_EVENT_ERROR
Error event.
@ TWR_CMWX1ZZABZ_EVENT_CONFIG_SAVE_DONE
Configuration save done.
@ TWR_CMWX1ZZABZ_EVENT_MESSAGE_RECEIVED
Received message.
@ TWR_CMWX1ZZABZ_EVENT_FRAME_COUNTER
Frame counter response.
@ TWR_CMWX1ZZABZ_EVENT_SEND_MESSAGE_DONE
RF frame transmission finished event.
@ TWR_CMWX1ZZABZ_EVENT_READY
Ready event.
@ TWR_CMWX1ZZABZ_EVENT_MESSAGE_NOT_CONFIRMED
Sent message not confirmed.
@ TWR_CMWX1ZZABZ_EVENT_MESSAGE_CONFIRMED
Sent message confirmed.
@ TWR_CMWX1ZZABZ_EVENT_JOIN_ERROR
OTAA join error.
@ TWR_CMWX1ZZABZ_EVENT_SEND_MESSAGE_START
RF frame transmission started event.
@ TWR_CMWX1ZZABZ_EVENT_JOIN_SUCCESS
OTAA join success.
@ TWR_CMWX1ZZABZ_EVENT_RFQ
RF quality response.
@ TWR_CMWX1ZZABZ_EVENT_MESSAGE_RETRANSMISSION
Retransmission of the confirmed message.
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
uint64_t twr_tick_t
Timestamp data type.
twr_uart_channel_t
UART channels.
Structure of FIFO instance.