SingingCat 0
application
loop_chip.c
1#include "sx1262/sx1262.h"
2#include "sx1262/sx126x.h"
3#include "mculibrary.h"
4
5static uint32_t last_cmd_status = 0;
6static uint32_t last_chip_mode = 0;
7static uint32_t rx_time_ctr = 0;
8static long int rx_time = 0;
9static byte need_reset = 0;
10extern int sx1262_transmit_fail_counter;
11void maybe_print_chip(struct network_context *nctx) {
12 struct sx126x_chip_status_s rs;
13
14 sx126x_get_status(nctx, &rs);
15 if ((rs.cmd_status == last_cmd_status) && (rs.chip_mode == last_chip_mode)) {
16 return;
17 }
18 last_cmd_status = rs.cmd_status;
19 last_chip_mode = rs.chip_mode;
20 // now check if we've left the RX mode for too long (e.g. missed an IRQ)
21 if (rs.chip_mode == SX126X_CHIP_MODE_RX) {
22 rx_time_ctr = 0;
23 if (sx1262_is_transmitting()) {
24 // we think chip is transmitting, but chip says it's in RX mode
25 // we might have missed an IRQ, so call it here:
26 transmit_complete_irq();
27 }
28 }
29 if (mculib_has_time_passed(2, &rx_time)) {
30 rx_time_ctr++;
31 }
32 if (rx_time_ctr > 5) {
33 print_chip_status(nctx, 1);
34 printf("[sx1262] detected chip is no longer in rx mode (ctr=%i). fixing. \r\n", rx_time_ctr);
35 sx1262_switch_to_rx(nctx);
36 rx_time_ctr = 0;
37 need_reset++;
38 }
39}
40
41int sx1262_loop(struct network_context *nctx) {
42 if (sx1262_is_transmitting() == 1) {
43 Delay(50);
44 sx1262_done_transmit();
45 }
46 if (sx1262_is_transmitting() != 0) {
47 struct sx126x_chip_status_s rs;
48 sx126x_get_status(nctx, &rs);
49 if (rs.chip_mode != SX126X_CHIP_MODE_TX) {
50 // we missed some IRQDone event
51 transmit_complete_irq();
52 }
53 }
54
55 maybe_print_chip(nctx);
56 if (need_reset > 10) {
57 // is there a race condition between sending and transmitting, just perhaps?
58 //sx1262_init_or_reinit(nctx,1);
59 need_reset = 0;
60 }
61 if (sx1262_transmit_fail_counter > 10) {
62 sx1262_init_or_reinit(nctx, 1);
63 }
64 return 0;
65}
SX126X chip status structure definition.
Definition: sx126x.h:480
SX126x radio driver definition.
sx126x_status_t sx126x_get_status(struct network_context *nctx, sx126x_chip_status_t *radio_status)
Get the chip status.
Definition: sx126x.c:718