-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpacket_handler.cpp
321 lines (258 loc) · 12.2 KB
/
packet_handler.cpp
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* AIS packet handler for MSP430 + Si4362
* Author: Adrian Studer
* License: CC BY-SA Creative Commons Attribution-ShareAlike
* https://creativecommons.org/licenses/by-sa/4.0/
* Please contact the author if you want to use this work in a closed-source project
*
* Modified and ported to ESP32 Arduino by Andre Janowicz - <aj@main-void.de>
* github.com/andrejanowicz/Navinaut-AIS
*/
#include <Arduino.h>
#include "radio.h"
#include "fifo.h"
#include "packet_handler.h"
#include "defs.h"
// sync word for AIS - only used for test
#define AIS_SYNC_WORD 0x7e
// paramters for package detection
#define PH_PREAMBLE_LENGTH 8 // minimum number of alternating bits we need for a valid preamble
#define PH_SYNC_TIMEOUT 16 // number of bits we wait for a preamble to start before changing channel
#define PH_RSSI_THRESHOLD -95 // threshold in dBm for valid signal, comment out to ignore signal strength
// pins that packet handler uses to receive data
#define PH_DATA_CLK_PIN RADIO_GPIO_2 // RX data clock
#define PH_DATA_PIN RADIO_GPIO_3 // RX data
volatile uint8_t newData = 0;
// preamble and start flag detection states
enum PH_SYNC_STATE {
PH_SYNC_RESET = 0, // reset/restart sync detection
PH_SYNC_0, // last bit was a 0
PH_SYNC_1, // last bit was a 1
PH_SYNC_FLAG // detecting start flag
};
volatile uint8_t ph_state = PH_STATE_OFF;
volatile uint8_t ph_last_error = PH_ERROR_NONE;
volatile uint8_t ph_radio_channel = 0;
volatile uint8_t ph_message_type = 0;
volatile int16_t ph_rssi = 0;
static uint16_t rx_bitstream; // shift register with incoming data
static uint16_t rx_bit_count; // bit counter for various purposes
static uint16_t rx_crc; // word for AIS payload CRC calculation
static uint8_t rx_one_count; // counter of 1's to identify stuff bits
static uint8_t rx_data_byte; // byte to receive actual package data
uint8_t rx_this_bit_NRZI; // current bit for NRZI decoding
static uint8_t rx_prev_bit_NRZI; // previous bit for NRZI decoding
uint8_t rx_bit; // current decoded bit
static uint8_t rx_sync_state; // state of preamble and start flag detection
static uint8_t rx_sync_count; // length of valid bits in current sync sequence
uint8_t wake_up = 0; // if set, LPM bits will be cleared
// setup packet handler
void ph_setup(void)
{
fifo_reset();
}
// start packet handler operation, including ISR
void ph_start(void)
{
// start radio, wait until it's spun up
radio_start_rx(ph_radio_channel, 0, 0, RADIO_STATE_NO_CHANGE, RADIO_STATE_NO_CHANGE, RADIO_STATE_NO_CHANGE);
radio_wait_for_CTS();
// reset packet handler state machine
ph_last_error = PH_ERROR_NONE;
ph_radio_channel = 0;
ph_state = PH_STATE_RESET;
// enable interrupt on positive edge of pin wired to DATA_CLK (GPIO2 as configured in radio_config.h)
pinMode(PH_DATA_PIN, INPUT_PULLUP);
pinMode(PH_DATA_CLK_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PH_DATA_CLK_PIN), modemdata_handle, RISING);
}
// ISR is now running and will operate radio, don't call radio library until ph ISR is stopped
// interrupt handler for receiving raw modem data via DATA/DATA_CLK pins
void IRAM_ATTR modemdata_handle(void) // every 100us, 10kHz
{
rx_this_bit_NRZI = (GPIO.in1.val >> 1) & 0x1; // fastest method of reading GPIO on ESP32
rx_bit = !(rx_prev_bit_NRZI ^ rx_this_bit_NRZI); // NRZI decoding: change = 0-bit, no change = 1-bit, i.e. 00,11=>1, 01,10=>0, i.e. NOT(A XOR B)
rx_prev_bit_NRZI = rx_this_bit_NRZI; // store encoded bit for next round of decoding
// add decoded bit to bit-stream (receiving LSB first)
rx_bitstream >>= 1;
if (rx_bit) {
rx_bitstream |= 0x8000;
}
newData = true;
}
void IRAM_ATTR packet_handle(){
if (!newData) {
return;
}
newData = false;
// packet handler state machine
switch (ph_state) {
// STATE: OFF
case PH_STATE_OFF: // state: off, do nothing
break;
// STATE: RESET
case PH_STATE_RESET: // state: reset, prepare state machine for next packet
rx_bitstream &= 8000; // reset bit-stream (but don't throw away incoming bit)
rx_bit_count = 0; // reset bit counter
fifo_new_packet(); // reset fifo packet
fifo_write_byte(ph_radio_channel); // indicate channel for this packet
ph_state = PH_STATE_WAIT_FOR_SYNC; // next state: wait for training sequence
rx_sync_state = PH_SYNC_RESET;
break;
// STATE: WAIT FOR PREAMBLE AND START FLAG
case PH_STATE_WAIT_FOR_SYNC: // state: waiting for preamble and start flag
rx_bit_count++; // count processed bits since reset
// START OF SYNC STATE MACHINE
switch (rx_sync_state) {
// SYNC STATE: RESET
case PH_SYNC_RESET: // sub-state: (re)start sync process
if (rx_bit_count > PH_SYNC_TIMEOUT) // if we exceeded sync time out
ph_state = PH_STATE_RESET; // reset state machine, will trigger channel hop
else { // else
rx_sync_count = 0; // start new preamble
if (rx_bit)
rx_sync_state = PH_SYNC_1; // we started with a 1
else
rx_sync_state = PH_SYNC_0; // we started with a 0
}
break;
// SYNC STATE: 0-BIT
case PH_SYNC_0: // sub-state: last bit was a 0
if (rx_bit) { // if we get a 1
rx_sync_count++; // valid preamble bit
rx_sync_state = PH_SYNC_1; // next state
} else { // if we get another 0
if (rx_sync_count > PH_PREAMBLE_LENGTH) { // if we have a sufficient preamble length
rx_sync_count = 7; // treat this as part of start flag, we already have 1 out of 8 bits (0.......)
rx_sync_state = PH_SYNC_FLAG; // next state flag detection
}
else // if not
rx_sync_state = PH_SYNC_RESET; // invalid preamble bit, restart preamble detection
}
break;
// SYNC STATE: 1-BIT
case PH_SYNC_1: // sub-state: last bit was a 1
if (!rx_bit) { // if we get a 0
rx_sync_count++; // valid preamble bit
rx_sync_state = PH_SYNC_0; // next state
} else { // if we get another 1
if (rx_sync_count > PH_PREAMBLE_LENGTH) { // if we have a sufficient preamble length
rx_sync_count = 5; // treat this as part of start flag, we already have 3 out of 8 bits (011.....)
rx_sync_state = PH_SYNC_FLAG; // next state flag detection
}
else // if not
rx_sync_state = PH_SYNC_RESET; // treat this as invalid preamble bit
}
break;
// SYNC STATE: START FLAG
case PH_SYNC_FLAG: // sub-state: start flag detection
rx_sync_count--; // count down bits
if (rx_sync_count != 0) { // if this is not the last bit of start flag
if (!rx_bit) // we expect a 1, 0 is an error
rx_sync_state = PH_SYNC_RESET; // restart preamble detection
} else { // if this is the last bit of start flag
if (!rx_bit) { // we expect a 0
rx_bit_count = 0; // reset bit counter
ph_state = PH_STATE_PREFETCH; // next state: start receiving packet
wake_up = 1; // main thread might want to do something on sync detect
} else // 1 is an error
rx_sync_state = PH_SYNC_RESET; // restart preamble detection
}
break;
}
// END OF SYNC STATE MACHINE - preamble and start flag detected
break;
// STATE: PREFETCH FIRST PACKET BYTE
case PH_STATE_PREFETCH: // state: pre-fill receive buffer with 8 bits
rx_bit_count++; // increase bit counter
if (rx_bit_count == 8) { // after 8 bits arrived
rx_bit_count = 0; // reset bit counter
rx_one_count = 0; // reset counter for stuff bits
rx_data_byte = 0; // reset buffer for data byte
rx_crc = 0xffff; // init CRC calculation
ph_state = PH_STATE_RECEIVE_PACKET; // next state: receive and process packet
ph_message_type = rx_bitstream >> 10; // store AIS message type for debugging
break;
}
break; // do nothing for the first 8 bits to fill buffer
// STATE: RECEIVE PACKET
case PH_STATE_RECEIVE_PACKET: // state: receiving packet data
rx_bit = rx_bitstream & 0x80; // extract data bit for processing
if (rx_one_count == 5) { // if we expect a stuff-bit..
if (rx_bit) { // if stuff bit is not zero the packet is invalid
ph_last_error = PH_ERROR_STUFFBIT; // report invalid stuff-bit error
ph_state = PH_STATE_RESET; // reset state machine
} else
rx_one_count = 0; // else ignore bit and reset stuff-bit counter
break;
}
rx_data_byte = rx_data_byte >> 1 | rx_bit; // shift bit into current data byte
if (rx_bit) { // if current bit is a 1
rx_one_count++; // count 1's to identify stuff bit
rx_bit = 1; // avoid shifting for CRC
} else
rx_one_count = 0; // or reset stuff-bit counter
if (rx_bit ^ (rx_crc & 0x0001)) // CCITT CRC calculation (according to Dr. Dobbs)
rx_crc = (rx_crc >> 1) ^ 0x8408;
else
rx_crc >>= 1;
if ((rx_bit_count & 0x07) == 0x07) { // every 8th bit.. (counter started at 0)
fifo_write_byte(rx_data_byte); // add buffered byte to FIFO
rx_data_byte = 0; // reset buffer
}
rx_bit_count++; // count valid, de-stuffed data bits
if ((rx_bitstream & 0xff00) == 0x7e00) { // if we found the end flag 0x7e we're done
if (rx_crc != 0xf0b8) { // if CRC verification failed
ph_last_error = PH_ERROR_CRC; // report CRC error
} else {
fifo_commit_packet(); // else commit packet in FIFO
}
ph_state = PH_STATE_RESET; // reset state machine
break;
}
if (rx_bit_count > 1020) { // if packet is too long, it's probably invalid
ph_last_error = PH_ERROR_NOEND; // report error
ph_state = PH_STATE_RESET; // reset state machine
break;
}
break;
}
//}
// END OF PACKET HANDLER STATE MACHINE
if (ph_state == PH_STATE_RESET) { // if next state is reset
ph_radio_channel ^= 1; // toggle radio channel between 0 and 1
wake_up = 1; // wake up main thread for packet processing and error reporting
}
}
// stop receiving and processing packets
void ph_stop(void)
{
detachInterrupt(digitalPinToInterrupt(PH_DATA_CLK_PIN)); // disable interrupt on pin wired to GPIO2
ph_state = PH_STATE_OFF; // turn off packet handler state machine
// ISR is no longer invoked, it's now save to do radio operations
radio_change_state(RADIO_STATE_READY); // transition radio from RX to READY
}
// get current state of packet handler state machine
uint8_t ph_get_state(void)
{
return ph_state;
}
// get last error reported by packet handler
uint8_t ph_get_last_error(void)
{
uint8_t error = ph_last_error;
ph_last_error = PH_ERROR_NONE; // clear error
return error;
}
uint8_t ph_get_radio_channel(void)
{
return ph_radio_channel;
}
int16_t ph_get_radio_rssi(void)
{
return ph_rssi;
}
uint8_t ph_get_message_type(void)
{
return ph_message_type;
}