SingingCat 0
application
irq.c
1#include "sx1262/sx1262.h"
2#include "sx1262/sx126x.h"
3#include "mculibrary.h"
4#include "networkif.h"
5#include "metrics/metrics.h"
6extern byte sx1262_initialized;
7
8void sx1262_process_packet_from_chip(struct network_context *nctx, int len, int pos) {
9 // printf("Bytes received: %i at position %i\r\n",len,pos);
10 uint8_t buf[258];
11
12 buf[len] = 0;
13 buf[len + 1] = 0;
14 buf[len + 2] = 0;
15
16 sx126x_read_buffer(nctx, pos, (uint8_t *)&buf, len);
17 struct sx126x_pkt_status_lora_s pkt_status;
18
19 sx126x_get_lora_pkt_status(nctx, &pkt_status);
20 printf("[sx1262] lora Received rssi=0x%x, snr=0x%x, est_rssi=0x%x: \"%s\"\r\n", pkt_status.rssi_pkt_in_dbm, pkt_status.snr_pkt_in_db, pkt_status.signal_rssi_pkt_in_dbm, buf);
21 sx1262_switch_to_rx(nctx);
22 struct command *com;
23
24 com = command_parse(buf, len);
25 if (com == NULL) {
26 IncMetric_PKTS_LORA_INVALID;
27 return;
28 }
29
30 com->sourcedev = SOURCE_LORA;
31 got_new_packet(com, 255 - pkt_status.rssi_pkt_in_dbm);
32 IncMetric_PKTS_LORA_CTR_IN;
33}
34
35
36void sx1262_irq(int pin, void *opaque) {
37 struct network_context *nctx = opaque;
38
39 if (!sx1262_initialized) {
40 return;
41 }
42 sx126x_irq_mask_t irqs;
43
44 sx126x_get_irq_status(nctx, &irqs);
45 if (irqs == 0) {
46 // we get an IRQ on raising AND falling edge, so check this, we might not have a real IRQ
47 return;
48 }
49#ifdef SX126X_DEBUG
50 printf("[sx1262] IRQ Masks: %p\r\n", irqs);
51#endif
52 sx126x_clear_irq_status(nctx, 0xFFFF);
53 if ((irqs & (1 << 0)) || (irqs & (1 << 9))) { // TxDone or Timeout
54 //tx done: go into rx mode
55 transmit_complete_irq();
56 }
57 if (irqs & (1 << 1)) {
58 // we received a packet!
59 struct sx126x_rx_buffer_status_s bufstat;
60 sx126x_get_rx_buffer_status(nctx, &bufstat);
61 sx1262_process_packet_from_chip(nctx, bufstat.pld_len_in_bytes, bufstat.buffer_start_pointer);
62 }
63}
byte got_new_packet(struct command *com, uint8_t signal_indicator)
stack received a new packet (signal indicator is a 0-255 byte value, interface specific)
Definition: routing.c:111
int com
Definition: command.h:22
SX126X LoRa packet status structure definition.
Definition: sx126x.h:514
SX126X RX buffer status structure definition.
Definition: sx126x.h:488
SX126x radio driver definition.
sx126x_status_t sx126x_get_lora_pkt_status(struct network_context *nctx, sx126x_pkt_status_lora_t *pkt_status)
Get the status of the last LoRa packet received.
Definition: sx126x.c:755
sx126x_status_t sx126x_get_irq_status(struct network_context *nctx, sx126x_irq_mask_t *irq)
Get system interrupt status.
Definition: sx126x.c:474
sx126x_status_t sx126x_read_buffer(struct network_context *nctx, const uint8_t offset, uint8_t *buffer, const uint8_t size)
Read data from radio Rx buffer memory space.
Definition: sx126x.c:437
sx126x_status_t sx126x_get_rx_buffer_status(struct network_context *nctx, sx126x_rx_buffer_status_t *rx_buffer_status)
Get the current Rx buffer status for both LoRa and GFSK Rx operations.
Definition: sx126x.c:737
sx126x_status_t sx126x_clear_irq_status(struct network_context *nctx, const sx126x_irq_mask_t irq_mask)
Clear selected system interrupts.
Definition: sx126x.c:491