1#include "sx1262/sx1262.h"
8static volatile int is_in_rx_mode = 0;
9byte sx1262_initialized = 0;
10extern int sx1262_transmit_fail_counter;
11void sx1262_set_lora_packet_type(
void *ctx) {
19void sx1262_set_packet_params(
void *ctx,
int payload_size) {
27 xbuf[4] = payload_size;
32void sx1262_set_lora_parameters(
void *ctx) {
37static char *int2str(
int status,
int comp,
char *name) {
46 if ((res = int2str(status, SX126X_CHIP_MODE_UNUSED,
"UNUSED")) != NULL) {
49 if ((res = int2str(status, SX126X_CHIP_MODE_RFU,
"RFU")) != NULL) {
52 if ((res = int2str(status, SX126X_CHIP_MODE_STBY_RC,
"STBY_RC")) != NULL) {
55 if ((res = int2str(status, SX126X_CHIP_MODE_STBY_XOSC,
"STBY_XOSC")) != NULL) {
58 if ((res = int2str(status, SX126X_CHIP_MODE_FS,
"FS")) != NULL) {
61 if ((res = int2str(status, SX126X_CHIP_MODE_RX,
"RX")) != NULL) {
64 if ((res = int2str(status, SX126X_CHIP_MODE_TX,
"TX")) != NULL) {
74 if ((res = int2str(status, SX126X_CMD_STATUS_RESERVED,
"RESERVED")) != NULL) {
77 if ((res = int2str(status, SX126X_CMD_STATUS_RFU,
"RFU")) != NULL) {
80 if ((res = int2str(status, SX126X_CMD_STATUS_DATA_AVAILABLE,
"DATA_AVAILABLE")) != NULL) {
83 if ((res = int2str(status, SX126X_CMD_STATUS_CMD_TIMEOUT,
"CMD_TIMEOUT")) != NULL) {
86 if ((res = int2str(status, SX126X_CMD_STATUS_CMD_PROCESS_ERROR,
"CMD_PROCESS_ERROR")) != NULL) {
89 if ((res = int2str(status, SX126X_CMD_STATUS_CMD_EXEC_FAILURE,
"CMD_EXEC_FAILURE")) != NULL) {
92 if ((res = int2str(status, SX126X_CMD_STATUS_CMD_TX_DONE,
"CMD_TX_DONE")) != NULL) {
99int print_chip_status(
void *ctx,
int include_mode) {
105 printf(
"[sx1262] Chip Status:\r\n");
106 printf(
"[sx1262] Cmd :0x%x (%s)\r\n", rs.cmd_status, chip_com_name(rs.cmd_status));
107 printf(
"[sx1262] Mode :0x%x (%s)\r\n", rs.chip_mode, chip_status_name(rs.chip_mode));
109 printf(
"[sx1262] Chip Cmd Status:0x%x (%s)\r\n", rs.cmd_status, chip_com_name(rs.cmd_status));
112 return rs.cmd_status;
114void print_errors(
void *ctx) {
118 printf(
"[sx1262] Errors: %p\r\n", errors);
120int check_register(
void *ctx, uint16_t reg,
int comp) {
125 if (buf[0] != comp) {
126 printf(
"[sx1262] Register 0x%p should be 0x%x, but is 0x%x\r\n", reg, comp, buf[0]);
129 if (print_chip_status(ctx, 1) != 0) {
135int probe_chip(
void *ctx) {
136 printf(
"[sx1262] probing chip...\r\n");
139 sx1262_clear_rx_mode();
141 if (check_register(ctx, 0x6BF, 0x21)) {
144 if (check_register(ctx, 0x6BC, 0x1D)) {
150 if (check_register(ctx, 0x6BF, 0x6C)) {
157 printf(
"[sx1262] Chip probed successfully\r\n\r\n");
165 sx1262_clear_rx_mode();
171 r = mculib_pin_in(nctx->mculib_handle, SX1262_BUSY, HAL_PIN_FASTEST, HAL_PUSHPULL_NONE);
172 MCULIB_ERR_RET(
"BUSYPIN", (r & mask));
174 r = mculib_pin_in(nctx->mculib_handle, SX1262_DIO1, HAL_PIN_FASTEST, HAL_PUSHPULL_NONE);
175 MCULIB_ERR_RET(
"DIOPIN", (r & mask));
176 r = register_softirq(nctx->mculib_handle, SX1262_DIO1, &sx1262_irq, nctx);
177 if ((reinit == 0) && (r != 0)) {
178 printf(
"[sx1262] failed to register irq (%i)\r\n", r);
182 r = mculib_pin_out(nctx->mculib_handle, SX1262_CSN, HAL_PIN_FASTEST);
183 MCULIB_ERR_RET(
"CSNPIN", (r & mask));
185 r = mculib_pin_out(nctx->mculib_handle, SX1262_RESET, HAL_PIN_FASTEST);
186 MCULIB_ERR_RET(
"RESETPIN", (r & mask));
188 r = mculib_spi_master_enable2(nctx->mculib_handle, 2, 0, 0, 0, SPI_DIR_RW);
189 MCULIB_ERR_RET(
"SPIEN", (r & mask));
191 r = mculib_pin_set(nctx->mculib_handle, SX1262_RESET, 0);
192 MCULIB_ERR_RET(
"RESET0", (r & mask));
194 r = mculib_pin_set(nctx->mculib_handle, SX1262_RESET, 1);
195 MCULIB_ERR_RET(
"RESET1", (r & mask));
196 print_chip_status(NULL, 1);
197 r = mculib_pin_set(nctx->mculib_handle, SX1262_CSN, 1);
198 MCULIB_ERR_RET(
"CSN1", (r & mask));
199 sx1262_transmit_fail_counter = 0;
200 sx1262_done_transmit();
204 int r = config_pins(nctx, 0);
206 sx1262_clear_rx_mode();
211 r = probe_chip(nctx);
213 printf(
"[sx1262] Probe FAILURE (%i)!!!!\r\n", r);
222 printf(
"[sx1262] Init sx1262...\r\n");
225 sx1262_clear_rx_mode();
227 r = config_pins(nctx, reinit);
232 r = probe_chip(nctx);
234 printf(
"[sx1262] Probe FAILURE (%i)!!!!\r\n", r);
238 printf(
"[sx1262] Configuring chip...\r\n");
241 print_chip_status(nctx, 0);
244 printf(
"[sx1262] Reg mode: %i\r\n", r);
245 print_chip_status(nctx, 0);
246 sx1262_switch_to_rx();
251 irq_mask |= (1 << 0);
252 irq_mask |= (1 << 1);
253 irq_mask |= (1 << 7);
254 irq_mask |= (1 << 8);
255 irq_mask |= (1 << 9);
257 print_chip_status(nctx, 1);
260 sx1262_initialized = 1;
261 printf(
"[sx1262] Chip configured\r\n");
262 nic_set_transmit_status(nctx, 1);
266 sx1262_clear_rx_mode();
267 return sx1262_init_or_reinit(nctx, 0);
274 if (rs.chip_mode == SX126X_CHIP_MODE_RX) {
277 printf(
"[sx1262] switching to rx mode\r\n");
278 sx1262_set_lora_packet_type(nctx);
279 sx1262_set_lora_parameters(nctx);
282 sx1262_set_packet_params(nctx, 0);
283 struct sx126x_pa_cfg_params_s pa_params = { .pa_duty_cycle = 0x04, .hp_max = 0x07, .device_sel = 0, .pa_lut = 1 };
298 if (rs.chip_mode == SX126X_CHIP_MODE_RX) {
305void sx1262_clear_rx_mode() {
311 sx1262_clear_rx_mode();
316 mculib_pin_set(ctx->mculib_handle, SX1262_CSN, 1);
318 mculib_pin_release(ctx->mculib_handle, SX1262_BUSY);
319 mculib_pin_release(ctx->mculib_handle, SX1262_DIO1);
320 mculib_pin_release(ctx->mculib_handle, SX1262_RESET);
321 mculib_spi_disable(ctx->mculib_handle, 2);
322 nic_set_transmit_status(ctx, 0);
SX126X chip status structure definition.
SX126X LoRa modulation parameters structure definition.
sx126x_lora_sf_t sf
LoRa Spreading Factor.
SX126X power amplifier configuration parameters structure definition.
SX126x radio driver definition.
sx126x_status_t sx126x_set_dio3_as_tcxo_ctrl(struct network_context *nctx, const sx126x_tcxo_ctrl_voltages_t tcxo_voltage, const uint32_t timeout)
Configure the embedded TCXO switch control.
sx126x_status_t sx126x_set_rx_with_timeout_in_rtc_step(struct network_context *nctx, const uint32_t timeout_in_rtc_step)
Set the chip in reception mode.
enum sx126x_cmd_status_e sx126x_cmd_status_t
SX126X command status enumeration definition.
sx126x_status_t sx126x_set_pa_cfg(struct network_context *nctx, const sx126x_pa_cfg_params_t *params)
Configure the PA (Power Amplifier)
sx126x_status_t sx126x_get_status(struct network_context *nctx, sx126x_chip_status_t *radio_status)
Get the chip status.
sx126x_status_t sx126x_set_lora_mod_params(struct network_context *nctx, const sx126x_mod_params_lora_t *params)
Set the modulation parameters for LoRa packets.
sx126x_status_t sx126x_set_rf_freq(struct network_context *nctx, const uint32_t freq_in_hz)
Set the RF frequency for future radio operations.
sx126x_status_t sx126x_read_register(struct network_context *nctx, const uint16_t address, uint8_t *buffer, const uint8_t size)
Read data from register memory space.
#define SX126X_RX_SINGLE_MODE
Timeout parameter in sx126x_set_rx_with_timeout_in_rtc_step to set the chip in reception until a rece...
sx126x_status_t sx126x_write_register(struct network_context *nctx, const uint16_t address, const uint8_t *buffer, const uint8_t size)
Write data into register memory space.
sx126x_status_t sx126x_set_reg_mode(struct network_context *nctx, const sx126x_reg_mod_t mode)
Configure the regulator mode to be used.
sx126x_status_t sx126x_get_device_errors(struct network_context *nctx, sx126x_errors_mask_t *errors)
Get the list of all active errors.
enum sx126x_chip_modes_e sx126x_chip_modes_t
SX126X chip mode enumeration definition.
sx126x_status_t sx126x_set_dio_irq_params(struct network_context *nctx, const uint16_t irq_mask, const uint16_t dio1_mask, const uint16_t dio2_mask, const uint16_t dio3_mask)
Set which interrupt signals are redirected to the dedicated DIO pin.
sx126x_status_t sx126x_clear_device_errors(struct network_context *nctx)
Clear all active errors.
sx126x_status_t sx126x_set_dio2_as_rf_sw_ctrl(struct network_context *nctx, const bool enable)
Configure the embedded RF switch control.
sx126x_status_t sx126x_set_buffer_base_address(struct network_context *nctx, const uint8_t tx_base_address, const uint8_t rx_base_address)
Set buffer start addresses for both Tx and Rx operations.
Hardware Abstraction Layer for SX126x.
sx126x_hal_status_t sx126x_hal_write(struct network_context *nctx, const uint8_t *command, const uint16_t command_length, const uint8_t *data, const uint16_t data_length)