Skip to content

Commit 9b3d9c6

Browse files
committed
Add TX of command and responses. Rename packet types.
Remove F() prints.
1 parent 67b6b6e commit 9b3d9c6

File tree

10 files changed

+566
-408
lines changed

10 files changed

+566
-408
lines changed

Firmware/LoRaSerial_Firmware/Begin.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void beginLoRa()
112112
int state = radio.begin(centerFreq); //Doesn't matter what freq we start at
113113
if (state != RADIOLIB_ERR_NONE)
114114
{
115-
systemPrint(F("Radio init failed with code: "));
115+
systemPrint("Radio init failed with code: ");
116116
systemPrintln(state);
117117
while (1)
118118
{

Firmware/LoRaSerial_Firmware/Commands.ino

Lines changed: 220 additions & 283 deletions
Large diffs are not rendered by default.

Firmware/LoRaSerial_Firmware/LoRaSerial_Firmware.ino

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ uint16_t txHead = 0;
142142
uint16_t txTail = 0;
143143
uint16_t rxHead = 0;
144144
uint16_t rxTail = 0;
145+
146+
uint8_t commandRXBuffer[700]; //Bytes received from remote, waiting for printing or AT parsing
147+
uint8_t commandTXBuffer[700]; //Bytes waiting to be transmitted to the remote unit
148+
uint16_t commandTXHead = 0;
149+
uint16_t commandTXTail = 0;
150+
uint16_t commandRXHead = 0;
151+
uint16_t commandRXTail = 0;
152+
145153
unsigned long lastByteReceived_ms = 0; //Track when last transmission was. Send partial buffer once time has expired.
146154

147155
char platformPrefix[15]; //Used for printing platform specific device name
@@ -185,16 +193,18 @@ Settings originalSettings; //Create a duplicate of settings during training so t
185193
uint8_t trainNetID; //New netID passed during training
186194
uint8_t trainEncryptionKey[16]; //New AES key passed during training
187195

196+
bool inCommandMode = false; //Normal data is prevented from entering serial output when in command mode
188197
char commandBuffer[100]; //Received serial gets stored into buffer until \r or \n is received
189198
uint8_t commandLength = 0;
190-
uint8_t settingsDelivered; //Tracks how many times we successfully delivered new settings to remote unit
191199

192200
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
193201

194202
//Global variables
195203
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
196204
long startTime = 0; //Used for air time of TX frames
197205
long stopTime = 0;
206+
207+
bool confirmDeliveryBeforeRadioConfig = false; //Goes true when we have remotely configured a radio
198208
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
199209

200210
void setup()
@@ -216,7 +226,7 @@ void setup()
216226

217227
beginWDT(); //Start watchdog timer
218228

219-
systemPrintln(F("LRS"));
229+
systemPrintln("LRS");
220230
}
221231

222232
void loop()

Firmware/LoRaSerial_Firmware/NVM.ino

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ void loadSettings()
88
uint32_t testRead = 0;
99
if (EEPROM.get(0, testRead) == 0xFFFFFFFF)
1010
{
11-
//systemPrintln(F("EEPROM is blank. Default settings applied."));
11+
//systemPrintln("EEPROM is blank. Default settings applied.");
1212
recordSystemSettings(); //Record default settings to EEPROM. At power on, settings are in default state
1313
}
1414

@@ -18,7 +18,7 @@ void loadSettings()
1818
EEPROM.get(0, tempSize); //Load the sizeOfSettings
1919
if (tempSize != sizeof(settings))
2020
{
21-
//systemPrintln(F("Settings wrong size. Default settings applied."));
21+
//systemPrintln("Settings wrong size. Default settings applied.");
2222
recordSystemSettings(); //Record default settings to EEPROM. At power on, settings are in default state
2323
}
2424

@@ -27,9 +27,9 @@ void loadSettings()
2727
EEPROM.get(sizeof(int), tempIdentifier); //Load the identifier from the EEPROM location after sizeOfSettings (int)
2828
if (tempIdentifier != LRS_IDENTIFIER)
2929
{
30-
//systemPrint(F("Settings are not valid for this variant of STR "));
30+
//systemPrint("Settings are not valid for this variant of STR ");
3131
//systemPrint((String)platformPrefix);
32-
//systemPrintln(F(". Default settings applied."));
32+
//systemPrintln(". Default settings applied.");
3333
recordSystemSettings(); //Record default settings to EEPROM. At power on, settings are in default state
3434
}
3535

Firmware/LoRaSerial_Firmware/Radio.ino

Lines changed: 98 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ PacketType identifyPacketType()
3131
if (receivedBytes < 2)
3232
{
3333
LRS_DEBUG_PRINTLN(F("Bad packet"));
34-
return (PROCESS_BAD_PACKET);
34+
return (PACKET_BAD);
3535
}
3636

3737
//Pull out control header
@@ -58,13 +58,13 @@ PacketType identifyPacketType()
5858
{
5959
LRS_DEBUG_PRINT(F("NetID mismatch: "));
6060
LRS_DEBUG_PRINTLN(receivedNetID);
61-
return (PROCESS_NETID_MISMATCH);
61+
return (PACKET_NETID_MISMATCH);
6262
}
6363

64-
if (receiveTrailer.ack == 1 && receiveTrailer.remoteCommand == 0)
64+
if (receiveTrailer.ack == 1 && receiveTrailer.remoteCommand == 0 && receiveTrailer.remoteCommandResponse == 0)
6565
{
6666
LRS_DEBUG_PRINTLN(F("RX: Ack packet"));
67-
return (PROCESS_ACK_PACKET);
67+
return (PACKET_ACK);
6868
}
6969

7070
if (receiveTrailer.resend == 1)
@@ -77,7 +77,7 @@ PacketType identifyPacketType()
7777
if (memcmp(lastPacket, incomingBuffer, lastPacketSize) == 0)
7878
{
7979
LRS_DEBUG_PRINTLN(F("Duplicate received. Acking again."));
80-
return (PROCESS_DUPLICATE_PACKET); //It's a duplicate. Ack then ignore
80+
return (PACKET_DUPLICATE); //It's a duplicate. Ack then ignore
8181
}
8282
}
8383
else
@@ -94,7 +94,7 @@ PacketType identifyPacketType()
9494
if (receiveTrailer.train == 1)
9595
{
9696
LRS_DEBUG_PRINTLN(F("RX: Training Control Packet"));
97-
return (PROCESS_TRAINING_CONTROL_PACKET);
97+
return (PACKET_TRAINING_PING);
9898
}
9999

100100
//If this packet is marked as a remote command, it's either an ack or a zero length packet (not known)
@@ -107,12 +107,25 @@ PacketType identifyPacketType()
107107
}
108108

109109
LRS_DEBUG_PRINTLN(F("RX: Unknown Command"));
110-
return (PROCESS_BAD_PACKET);
110+
return (PACKET_BAD);
111+
}
112+
113+
//If this packet is marked as a remote command response, it's either an ack or a zero length packet (not known)
114+
else if (receiveTrailer.remoteCommandResponse == 1)
115+
{
116+
if (receiveTrailer.ack == 1)
117+
{
118+
LRS_DEBUG_PRINTLN(F("RX: Command Response Ack"));
119+
return (PACKET_COMMAND_RESPONSE_ACK);
120+
}
121+
122+
LRS_DEBUG_PRINTLN(F("RX: Unknown Response Command"));
123+
return (PACKET_BAD);
111124
}
112125

113126
//Not training, not command packet, just a ping
114127
LRS_DEBUG_PRINTLN(F("RX: Control Packet"));
115-
return (PROCESS_CONTROL_PACKET);
128+
return (PACKET_PING);
116129
}
117130

118131
//Update lastPacket details with current packet
@@ -123,8 +136,8 @@ PacketType identifyPacketType()
123136
//payload contains new AES key and netID which will be processed externally
124137
if (receiveTrailer.train == 1)
125138
{
126-
LRS_DEBUG_PRINTLN(F("RX: Training data packet"));
127-
return (PROCESS_TRAINING_DATA_PACKET);
139+
LRS_DEBUG_PRINTLN(F("RX: Training Data"));
140+
return (PACKET_TRAINING_DATA);
128141
}
129142

130143
else if (receiveTrailer.remoteCommand == 1)
@@ -134,8 +147,15 @@ PacketType identifyPacketType()
134147
return (PACKET_COMMAND_DATA);
135148
}
136149

137-
LRS_DEBUG_PRINTLN(F("RX: Data packet"));
138-
return (PROCESS_DATA_PACKET);
150+
else if (receiveTrailer.remoteCommandResponse == 1)
151+
{
152+
//New response data from remote
153+
LRS_DEBUG_PRINTLN(F("RX: Command Response Data"));
154+
return (PACKET_COMMAND_RESPONSE_DATA);
155+
}
156+
157+
LRS_DEBUG_PRINTLN(F("RX: Data"));
158+
return (PACKET_DATA);
139159
}
140160

141161
//Apply settings to radio
@@ -205,7 +225,7 @@ void configureRadio()
205225
default:
206226
if (settings.debug == true)
207227
{
208-
systemPrint(F("Unknown airSpeed: "));
228+
systemPrint("Unknown airSpeed: ");
209229
systemPrintln(settings.airSpeed);
210230
}
211231
break;
@@ -268,23 +288,23 @@ void configureRadio()
268288

269289
if (settings.debug == true)
270290
{
271-
systemPrint(F("Freq: "));
291+
systemPrint("Freq: ");
272292
systemPrintln(channels[0], 3);
273-
systemPrint(F("radioBandwidth: "));
293+
systemPrint("radioBandwidth: ");
274294
systemPrintln(settings.radioBandwidth);
275-
systemPrint(F("radioSpreadFactor: "));
295+
systemPrint("radioSpreadFactor: ");
276296
systemPrintln(settings.radioSpreadFactor);
277-
systemPrint(F("radioCodingRate: "));
297+
systemPrint("radioCodingRate: ");
278298
systemPrintln(settings.radioCodingRate);
279-
systemPrint(F("radioSyncWord: "));
299+
systemPrint("radioSyncWord: ");
280300
systemPrintln(settings.radioSyncWord);
281-
systemPrint(F("radioPreambleLength: "));
301+
systemPrint("radioPreambleLength: ");
282302
systemPrintln(settings.radioPreambleLength);
283-
systemPrint(F("calcSymbolTime: "));
303+
systemPrint("calcSymbolTime: ");
284304
systemPrintln(calcSymbolTime());
285-
systemPrint(F("HoppingPeriod: "));
305+
systemPrint("HoppingPeriod: ");
286306
systemPrintln(hoppingPeriod);
287-
systemPrint(F("controlPacketAirTime: "));
307+
systemPrint("controlPacketAirTime: ");
288308
systemPrintln(controlPacketAirTime);
289309
}
290310

@@ -293,10 +313,10 @@ void configureRadio()
293313
reportERROR();
294314
if (settings.debug == true)
295315
{
296-
systemPrintln(F("Radio init failed. Check settings."));
316+
systemPrintln("Radio init failed. Check settings.");
297317
}
298318
}
299-
LRS_DEBUG_PRINTLN(F("Radio online"));
319+
LRS_DEBUG_PRINTLN("Radio online");
300320
}
301321

302322
void returnToReceiving()
@@ -346,6 +366,7 @@ void sendPingPacket()
346366
responseTrailer.resend = 0; //This is not a resend
347367
responseTrailer.train = 0; //This is not a training packet
348368
responseTrailer.remoteCommand = 0; //This is not a remote command packet
369+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous remote command
349370

350371
packetSize = 2;
351372
packetSent = 0; //Reset the number of times we've sent this packet
@@ -373,6 +394,7 @@ void sendDataPacket()
373394
responseTrailer.resend = 0; //This is not a resend
374395
responseTrailer.train = 0; //This is not a training packet
375396
responseTrailer.remoteCommand = 0; //This is not a remote command packet
397+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous remote command
376398

377399
packetSize += 2; //Make room for control bytes
378400
packetSent = 0; //Reset the number of times we've sent this packet
@@ -398,6 +420,7 @@ void sendResendPacket()
398420
responseTrailer.resend = 1; //This is a resend
399421
responseTrailer.train = 0; //This is not a training packet
400422
//responseTrailer.remoteCommand = ; //Don't modify the remoteCommand bit; we may be resending a command packet
423+
//responseTrailer.remoteCommandResponse = ; //Don't modify the remoteCommand bit; we may be resending a command packet
401424

402425
//packetSize += 2; //Don't adjust the packet size
403426
//packetSent = 0; //Don't reset
@@ -413,6 +436,7 @@ void sendAckPacket()
413436
responseTrailer.resend = 0; //This is not a resend
414437
responseTrailer.train = 0; //This is not a training packet
415438
responseTrailer.remoteCommand = 0; //This is not a remote command packet
439+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous remote command
416440

417441
packetSize = 2;
418442
packetSent = 0; //Reset the number of times we've sent this packet
@@ -428,6 +452,7 @@ void sendTrainingPingPacket()
428452
responseTrailer.resend = 0; //This is not a resend
429453
responseTrailer.train = 1; //This is a training packet
430454
responseTrailer.remoteCommand = 0; //This is not a remote command packet
455+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous remote command
431456

432457
packetSize = 2;
433458
packetSent = 0; //Reset the number of times we've sent this packet
@@ -446,6 +471,7 @@ void sendTrainingDataPacket()
446471
responseTrailer.resend = 0; //This is not a resend
447472
responseTrailer.train = 1; //This is training packet
448473
responseTrailer.remoteCommand = 0; //This is not a remote command packet
474+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous remote command
449475

450476
packetSize = sizeof(trainEncryptionKey) + sizeof(trainNetID);
451477

@@ -470,6 +496,7 @@ void sendCommandAckPacket()
470496
responseTrailer.resend = 0; //This is not a resend
471497
responseTrailer.train = 0; //This is not a training packet
472498
responseTrailer.remoteCommand = 1; //This is a remote command packet
499+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous command
473500

474501
packetSize = 2;
475502
packetSent = 0; //Reset the number of times we've sent this packet
@@ -485,6 +512,49 @@ void sendCommandDataPacket()
485512
responseTrailer.resend = 0; //This is not a resend
486513
responseTrailer.train = 0; //This is not training packet
487514
responseTrailer.remoteCommand = 1; //This is a remote control packet
515+
responseTrailer.remoteCommandResponse = 0; //This is not a response to a previous command
516+
517+
packetSize += 2; //Make room for control bytes
518+
packetSent = 0; //Reset the number of times we've sent this packet
519+
520+
//SF6 requires an implicit header which means there is no dataLength in the header
521+
if (settings.radioSpreadFactor == 6)
522+
{
523+
//Manually store actual data length 3 bytes from the end (before NetID)
524+
//Manual packet size is whatever has been processed + 1 for the manual packetSize byte
525+
outgoingPacket[255 - 3] = packetSize + 1;
526+
packetSize = 255; //We're now going to transmit 255 bytes
527+
}
528+
529+
expectingAck = true; //We expect destination to ack
530+
sendPacket();
531+
}
532+
533+
//Create short packet of 2 control bytes - do not expect ack
534+
void sendCommandResponseAckPacket()
535+
{
536+
LRS_DEBUG_PRINT(F("TX: Command Response Ack "));
537+
responseTrailer.ack = 1; //This is an ACK to a previous reception
538+
responseTrailer.resend = 0; //This is not a resend
539+
responseTrailer.train = 0; //This is not a training packet
540+
responseTrailer.remoteCommand = 0; //This is not a remote command packet
541+
responseTrailer.remoteCommandResponse = 1; //This is a response to a previous command
542+
543+
packetSize = 2;
544+
packetSent = 0; //Reset the number of times we've sent this packet
545+
expectingAck = false; //We do not expect destination to ack
546+
sendPacket();
547+
}
548+
549+
//Create packet of serial command with remote command = 1, ack = 0
550+
void sendCommandResponseDataPacket()
551+
{
552+
LRS_DEBUG_PRINT(F("TX: Command Response Data "));
553+
responseTrailer.ack = 0; //This is not an ACK to a previous transmission.
554+
responseTrailer.resend = 0; //This is not a resend
555+
responseTrailer.train = 0; //This is not training packet
556+
responseTrailer.remoteCommand = 0; //This is a remote control packet
557+
responseTrailer.remoteCommandResponse = 1; //This is a response to a previous command
488558

489559
packetSize += 2; //Make room for control bytes
490560
packetSent = 0; //Reset the number of times we've sent this packet
@@ -643,23 +713,22 @@ void generateHopTable()
643713

644714
if (settings.debug == true)
645715
{
646-
systemPrint(F("channelSpacing: "));
716+
systemPrint("channelSpacing: ");
647717
systemPrintln(channelSpacing, 3);
648718

649-
systemPrintln(F("Channel table:"));
719+
systemPrintln("Channel table:");
650720
for (int x = 0 ; x < settings.numberOfChannels ; x++)
651721
{
652722
systemPrint(x);
653-
systemPrint(F(": "));
723+
systemPrint(": ");
654724
systemPrint(channels[x], 3);
655725
systemPrintln();
656726
}
657727

658-
systemPrint(F("AES IV:"));
728+
systemPrint("AES IV:");
659729
for (uint8_t i = 0 ; i < 12 ; i++)
660730
{
661731
systemPrint(" 0x");
662-
if (AESiv[i] < 0x10) systemPrint("0");
663732
systemPrint(AESiv[i], HEX);
664733
}
665734
systemPrintln();

0 commit comments

Comments
 (0)