This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
longfiP.h
137 lines (119 loc) · 4.43 KB
/
longfiP.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "longfi.h"
#include "radio/radio.h"
#ifdef __cplusplus
extern "C"
{
#endif
#define LONGFI_NUM_UPLINK_CHANNELS 8
const uint32_t uplink_channel_map[LONGFI_NUM_UPLINK_CHANNELS] = {916000000,
916200000,
916400000,
916600000,
916800000,
917000000,
917200000,
917400000};
#define NUM_SF (4)
typedef enum
{
LongFi_SF7 = 0,
LongFi_SF8 = 1,
LongFi_SF9 = 2,
LongFi_SF10 = 3,
} LongFiSpreading_t;
const uint32_t payload_per_fragment[NUM_SF] = {24, 24, 24, 24};
const uint32_t fragments_per_channel[NUM_SF] = {5, 2, 1, 1};
#define TX_OUTPUT_POWER 22 // dBm
#define LORA_BANDWIDTH \
0 // [0: 125 kHz,
// 1: 250 kHz,
// 2: 500 kHz,
// 3: Reserved]
#define LORA_SPREADING_FACTOR (10) // [SF7..SF12]
#define LORA_CODINGRATE \
(1) // [1: 4/5,
// 2: 4/6,
// 3: 4/7,
// 4: 4/8]
#define LORA_PREAMBLE_LENGTH (8) // Same for Tx and Rx
#define LORA_SYMBOL_TIMEOUT (5) // Symbols
#define LORA_FIX_LENGTH_PAYLOAD_ON (false)
#define LORA_IQ_INVERSION_ON (false)
#pragma pack(push, 1)
// if first byte is 0, is single fragment packet_header
typedef struct
{
uint8_t packet_id; // 0 must be zero
uint32_t oui; // 1:4
uint16_t device_id; // 5:6
uint16_t mac; // 7:8
} packet_header_t;
// if second byte is 0, is multi-fragment packet_header
typedef struct
{
uint8_t packet_id; // 0 must be non-zero
uint8_t fragment_num; // 1 must be zero (byte)
uint8_t num_fragments; // 2 must be non-zero
uint32_t oui; // 3:6
uint16_t device_id; // 7:8
uint16_t mac; // 9:10
} packet_header_multiple_fragments_t;
// else (first and second byte, non-zero), is packet fragment
typedef struct
{
uint8_t packet_id; // 0 must be non-zero
uint8_t fragment_num; // 1 must be non-zero
uint16_t mac; // 2:3
} fragment_header_t;
#pragma pack(pop)
#define RX_TIMEOUT_VALUE 1000
#define BUFFER_SIZE 128 // Define the payload size here
/*
* Functions implemented here that SX1276 will call
*
* As far as I can tell, these are only called from SX1276 when:
* LongFi.dio_irq_handles are dispatched
* Timeout callbacks are dispatched
* With the event refactoring here, these callbacks will only be called
* from `helium_rf_handle_event`
*/
void OnTxDone(void);
void OnRxDone(uint8_t * payload, uint16_t size, int16_t rssi, int8_t snr);
void OnTxTimeout(void);
void OnRxTimeout(void);
void OnRxError(void);
// Internal events are mapped from SX12xx events
typedef enum
{
InternalEvent_None,
InternalEvent_TxDone,
InternalEvent_RxDone,
InternalEvent_TxTimeout,
InternalEvent_RxTimeout,
InternalEvent_RxError,
} InternalEvent_t;
typedef struct
{
uint8_t spreading_factor;
void (*dio_irq_handles[NUM_IRQ_HANDLES])();
RadioEvents_t radio_events;
InternalEvent_t cur_event;
uint8_t * buffer;
size_t buffer_len;
uint32_t rx_len;
uint32_t tx_cnt;
uint32_t tx_len;
} LongFiInternal_t;
/*
* Private helper for handling internal events
*/
ClientEvent_t _handle_internal_event(LongFi_t * handle);
/*
* Private helper for counting bytes
*/
size_t payload_bytes_in_single_fragment_packet();
size_t payload_bytes_in_first_fragment_of_many();
size_t payload_bytes_in_subsequent_fragments();
#ifdef __cplusplus
}
#endif