Skip to content

Commit c8dc9dd

Browse files
authored
Merge pull request #523 from sparkfun/release_candidate
Patch v2.0
2 parents 66da5c4 + 8356bbe commit c8dc9dd

21 files changed

+1830
-1520
lines changed

Firmware/LoRaSerial/Commands.ino

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ bool commandAT(const char * commandString)
8787
case ('B'): //ATB - Break the link
8888

8989
//Compute the time delay
90-
delayMillis = (VC_LINK_BREAK_MULTIPLIER + 2) * settings.heartbeatTimeout;
90+
delayMillis = (LINK_BREAK_MULTIPLIER + 2) * settings.heartbeatTimeout;
9191

9292
//Warn the user of the delay
9393
systemPrint("Delaying ");
@@ -111,7 +111,7 @@ bool commandAT(const char * commandString)
111111
//Idle the system to break the link
112112
//This is required on the server system which does not request an VC number assignment
113113
timer = millis();
114-
while ((millis() - timer) < ((VC_LINK_BREAK_MULTIPLIER + 2) * settings.heartbeatTimeout))
114+
while ((millis() - timer) < ((LINK_BREAK_MULTIPLIER + 2) * settings.heartbeatTimeout))
115115
petWDT();
116116
changeState(RADIO_RESET);
117117

@@ -224,11 +224,11 @@ bool commandAT(const char * commandString)
224224
systemPrintln(" ATI8 - Display system unique ID");
225225
systemPrintln(" ATI9 - Display the maximum datagram size");
226226
systemPrintln(" ATI10 - Display radio metrics");
227-
systemPrintln(" ATI11 - Return myVc value");
228-
systemPrintln(" ATI12 - Display the VC details");
229-
systemPrintln(" ATI13 - Display the SX1276 registers");
230-
systemPrintln(" ATI14 - Dump the radioTxBuffer");
231-
systemPrintln(" ATI15 - Dump the NVM unique ID table");
227+
228+
//Virtual circuit information commands
229+
systemPrintln(" ATI30 - Return myVc value");
230+
systemPrintln(" ATI31 - Display the VC details");
231+
systemPrintln(" ATI32 - Dump the NVM unique ID table");
232232
return true;
233233

234234
case ('0'): //ATI0 - Show user settable parameters
@@ -601,14 +601,22 @@ bool commandAT(const char * commandString)
601601
systemPrintln(" State History");
602602
displayRadioStateHistory();
603603
return true;
604+
}
605+
}
606+
if ((commandString[2] == 'I') && (commandString[3] == '3') && (commandLength == 5))
607+
{
608+
switch (commandString[4])
609+
{
610+
default:
611+
return false;
604612

605-
case ('1'): //ATI11 - Return myVc value
613+
case ('0'): //ATI30 - Return myVc value
606614
systemPrintln();
607615
systemPrint("myVc: ");
608616
systemPrintln(myVc);
609617
return true;
610618

611-
case ('2'): //ATI12 - Display the VC details
619+
case ('1'): //ATI31 - Display the VC details
612620
systemPrintTimestamp();
613621
systemPrint("VC ");
614622
systemPrint(cmdVc);
@@ -706,16 +714,7 @@ bool commandAT(const char * commandString)
706714
}
707715
return true;
708716

709-
case ('3'): //ATI13 - Display the SX1276 registers
710-
printSX1276Registers();
711-
return true;
712-
713-
case ('4'): //ATI14 - Dump the radioTxBuffer
714-
systemPrintln("radioTxBuffer:");
715-
dumpCircularBuffer(radioTxBuffer, radioTxTail, sizeof(radioTxBuffer), availableRadioTXBytes());
716-
return true;
717-
718-
case ('5'): //ATI15 - Dump the NVM unique ID table
717+
case ('2'): //ATI32 - Dump the NVM unique ID table
719718
systemPrintln("NVM Unique ID Table");
720719
for (vcIndex = 0; vcIndex < MAX_VC; vcIndex++)
721720
{
@@ -736,6 +735,33 @@ bool commandAT(const char * commandString)
736735
return true;
737736
}
738737
}
738+
if ((commandString[2] == 'I') && (commandString[3] == '5') && (commandLength == 5))
739+
{
740+
switch (commandString[4])
741+
{
742+
default:
743+
return false;
744+
745+
case ('0'): //ATI50 - Output 80000 bytes, stop when serial is full
746+
for (uint16_t line = 0; line < 1000; line++)
747+
{
748+
const char * text = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV";
749+
750+
if (line < 100)
751+
systemWrite('0');
752+
if (line < 10)
753+
systemWrite('0');
754+
systemPrint(line);
755+
systemWrite(':');
756+
systemPrintln(text);
757+
}
758+
return true;
759+
760+
case ('1'): //ATI51 - Display the SX1276 registers
761+
printSX1276Registers();
762+
return true;
763+
}
764+
}
739765

740766
//Invalid command
741767
return false;
@@ -1165,6 +1191,8 @@ const COMMAND_ENTRY commands[] =
11651191
{'S', 0, 0, 0, 1, 0, TYPE_BOOL, valInt, "FlowControl", &tempSettings.flowControl},
11661192
{'S', 0, 0, 0, 1, 0, TYPE_BOOL, valInt, "InvertCts", &tempSettings.invertCts},
11671193
{'S', 0, 0, 0, 1, 0, TYPE_BOOL, valInt, "InvertRts", &tempSettings.invertRts},
1194+
{'S', 0, 0, 0, SERIAL_RX_BUFFER_SIZE, 0, TYPE_U16, valInt, "RTSOffBytes", &tempSettings.rtsOffBytes},
1195+
{'S', 0, 0, 0, SERIAL_RX_BUFFER_SIZE, 0, TYPE_U16, valInt, "RTSOnBytes", &tempSettings.rtsOnBytes},
11681196
{'S', 0, 0, 10, 2000, 0, TYPE_U16, valInt, "SerialDelay", &tempSettings.serialTimeoutBeforeSendingFrame_ms},
11691197
{'S', 0, 0, 0, 0, 0, TYPE_SPEED_SERIAL, valSpeedSerial, "SerialSpeed", &tempSettings.serialSpeed},
11701198
{'S', 0, 0, 0, 1, 0, TYPE_BOOL, valInt, "UsbSerialWait", &tempSettings.usbSerialWait},

Firmware/LoRaSerial/LoRaSerial.ino

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ const int FIRMWARE_VERSION_MINOR = 0;
8282
#define ENABLE_DEVELOPER false
8383
#endif //ENABLE_DEVELOPER
8484

85+
#define SERIAL_RX_BUFFER_SIZE 1024
86+
#define RTS_ON_BYTES (SERIAL_RX_BUFFER_SIZE / 4)
87+
8588
#include "settings.h"
8689

8790
//Hardware connections
@@ -195,7 +198,7 @@ const uint8_t responseDelayDivisor = 4; //Add on to max response time after pack
195198
//Buffer to receive serial data from the USB or serial ports
196199
uint16_t rxHead = 0;
197200
uint16_t rxTail = 0;
198-
uint8_t serialReceiveBuffer[1024];
201+
uint8_t serialReceiveBuffer[SERIAL_RX_BUFFER_SIZE];
199202

200203
//Buffer to store bytes for transmission via the long range radio
201204
uint16_t radioTxHead = 0;
@@ -490,6 +493,7 @@ bool trainingPreviousRxInProgress = false; //Previous RX status
490493
float originalChannel; //Original channel from HOP table while training is in progress
491494
uint8_t trainingPartnerID[UNIQUE_ID_BYTES]; //Unique ID of the training partner
492495
uint8_t myUniqueId[UNIQUE_ID_BYTES]; // Unique ID of this system
496+
uint32_t buttonPressedTime;
493497

494498
//Virtual-Circuit
495499
int8_t cmdVc; //VC index for ATI commands only

Firmware/LoRaSerial/Radio.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ PacketType rcvDatagram()
23552355
// * Received datagramType
23562356
// * DATAGRAM_DUPLICATE
23572357
// * DATAGRAM_BAD
2358-
PacketType validateDatagram(VIRTUAL_CIRCUIT * vc, PacketType datagramType, uint8_t ackNumber, uint16_t freeBytes)
2358+
PacketType validateDatagram(VIRTUAL_CIRCUIT * vc, PacketType datagramType, uint8_t ackNumber, int freeBytes)
23592359
{
23602360
if (ackNumber != vc->rmtTxAckNumber)
23612361
{
@@ -2393,12 +2393,12 @@ PacketType validateDatagram(VIRTUAL_CIRCUIT * vc, PacketType datagramType, uint8
23932393
}
23942394

23952395
//Verify that there is sufficient space in the serialTransmitBuffer
2396-
if (inCommandMode || ((sizeof(serialTransmitBuffer) - availableTXBytes()) < rxDataBytes))
2396+
if (inCommandMode || (freeBytes < rxDataBytes))
23972397
{
23982398
if (settings.debugReceive || settings.debugDatagrams)
23992399
{
24002400
systemPrintTimestamp();
2401-
systemPrintln("Insufficient space in the serialTransmitBuffer");
2401+
systemPrintln("Insufficient space in the receive buffer");
24022402
}
24032403
insufficientSpace++;
24042404

Firmware/LoRaSerial/Serial.ino

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ bool isCTS()
1414
{
1515
if (pin_cts == PIN_UNDEFINED) return (true); //CTS not implmented on this board
1616
if (settings.flowControl == false) return (true); //CTS turned off
17-
return (digitalRead(pin_cts) == HIGH) ^ settings.invertCts;
17+
//The SAMD21 specification (page 448) indicates that CTS is low when data is flowing
18+
return (digitalRead(pin_cts) == LOW) ^ settings.invertCts;
1819
}
1920

2021
#define NEXT_RX_TAIL(n) ((rxTail + n) % sizeof(serialReceiveBuffer))
@@ -35,6 +36,10 @@ void serialBufferOutput(uint8_t * data, uint16_t dataLength)
3536
{
3637
int length;
3738

39+
//Make sure there is enough room in the buffer
40+
if ((sizeof(serialTransmitBuffer) - availableTXBytes()) < (dataLength + 32))
41+
outputSerialData(true);
42+
3843
length = 0;
3944
if ((txHead + dataLength) > sizeof(serialTransmitBuffer))
4045
{
@@ -55,6 +60,10 @@ void serialOutputByte(uint8_t data)
5560
{
5661
if (printerEndpoint == PRINT_TO_SERIAL)
5762
{
63+
//Make sure there is enough room in the buffer
64+
if ((sizeof(serialTransmitBuffer) - availableTXBytes()) < 32)
65+
outputSerialData(true);
66+
5867
//Add this byte to the serial output buffer
5968
serialTransmitBuffer[txHead++] = data;
6069
txHead %= sizeof(serialTransmitBuffer);
@@ -72,7 +81,8 @@ void updateRTS(bool assertRTS)
7281
{
7382
rtsAsserted = assertRTS;
7483
if (settings.flowControl && (pin_rts != PIN_UNDEFINED))
75-
digitalWrite(pin_rts, assertRTS ^ settings.invertRts);
84+
//The SAMD21 specification (page 448) indicates that RTS is low to enable data flow
85+
digitalWrite(pin_rts, (assertRTS ? 0 : 1) ^ settings.invertRts);
7686
}
7787

7888
//=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
@@ -283,35 +293,37 @@ uint8_t readyOutgoingCommandPacket(uint16_t offset)
283293
//Scan for escape characters
284294
void updateSerial()
285295
{
296+
int bufferSpace;
286297
uint16_t previousHead;
287298
int x;
288299

289300
//Assert RTS when there is enough space in the receive buffer
290301
if ((!rtsAsserted) && (availableRXBytes() < (sizeof(serialReceiveBuffer) / 2))
291-
&& (availableTXBytes() < (sizeof(serialTransmitBuffer) / 4)))
302+
&& (availableTXBytes() <= RTS_ON_BYTES))
292303
updateRTS(true);
293304

294305
//Attempt to empty the serialTransmitBuffer
295306
outputSerialData(false);
296307

297308
//Look for local incoming serial
298309
previousHead = rxHead;
299-
while (rtsAsserted && arch.serialAvailable() && (transactionComplete == false))
310+
bufferSpace = sizeof(serialReceiveBuffer) - 1 - availableRXBytes();
311+
while (bufferSpace-- && arch.serialAvailable() && (transactionComplete == false))
300312
{
301313
blinkSerialRxLed(true); //Turn on LED during serial reception
302314

303315
//Take a break if there are ISRs to attend to
304316
petWDT();
305317
if (timeToHop == true) hopChannel();
306318

307-
//Deassert RTS when the buffer gets full
308-
if (rtsAsserted && (sizeof(serialReceiveBuffer) - availableRXBytes()) < 32)
309-
updateRTS(false);
310-
311319
byte incoming = systemRead();
312320

313321
serialReceiveBuffer[rxHead++] = incoming; //Push char to holding buffer
314322
rxHead %= sizeof(serialReceiveBuffer);
323+
324+
//Deassert RTS when the buffer gets full
325+
if (rtsAsserted && (sizeof(serialReceiveBuffer) - availableRXBytes()) <= settings.rtsOffBytes)
326+
updateRTS(false);
315327
} //End Serial.available()
316328
blinkSerialRxLed(false); //Turn off LED
317329

@@ -417,7 +429,6 @@ void processSerialInput()
417429
commandLength--;
418430

419431
//Erase the previous character
420-
systemWrite(incoming);
421432
systemWrite(' ');
422433
systemWrite(incoming);
423434
}
@@ -931,10 +942,8 @@ void resetSerial()
931942

932943
//Empty the buffers
933944
rxHead = rxTail;
934-
radioTxHead = radioTxTail;
935945
txHead = txTail;
936946
commandRXHead = commandRXTail;
937947
commandTXHead = commandTXTail;
938-
endOfTxData = &outgoingPacket[headerBytes];
939948
commandLength = 0;
940949
}

Firmware/LoRaSerial/States.ino

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -727,6 +727,14 @@ void updateRadioState()
727727
triggerEvent(TRIGGER_RX_DATA);
728728

729729
//Place the data in the serial output buffer
730+
if (settings.debugSerial)
731+
{
732+
systemPrint("Moving ");
733+
systemPrint(rxDataBytes);
734+
systemPrintln(" from incomingBuffer to serialTransmitBuffer");
735+
dumpBuffer(rxData, length);
736+
outputSerialData(true);
737+
}
730738
serialBufferOutput(rxData, rxDataBytes);
731739

732740
//Transmit ACK
@@ -756,6 +764,16 @@ void updateRadioState()
756764
case DATAGRAM_REMOTE_COMMAND:
757765
triggerEvent(TRIGGER_RX_COMMAND);
758766

767+
//Debug the remote command operation
768+
if (settings.debugSerial)
769+
{
770+
systemPrint("Moving ");
771+
systemPrint(rxDataBytes);
772+
systemPrintln(" from incomingBuffer to commandRXBuffer");
773+
dumpBuffer(rxData, length);
774+
outputSerialData(true);
775+
}
776+
759777
//Determine the number of bytes received
760778
length = 0;
761779
if ((commandRXHead + rxDataBytes) > sizeof(commandRXBuffer))
@@ -778,9 +796,16 @@ void updateRadioState()
778796
case DATAGRAM_REMOTE_COMMAND_RESPONSE:
779797
triggerEvent(TRIGGER_RX_COMMAND_RESPONSE);
780798

781-
//Print received data. This is blocking but we do not use the serialTransmitBuffer because we're in command mode (and it's not much data to print).
782-
for (int x = 0 ; x < rxDataBytes ; x++)
783-
Serial.write(rxData[x]);
799+
//Print received data.
800+
if (settings.debugSerial)
801+
{
802+
systemPrint("Moving ");
803+
systemPrint(rxDataBytes);
804+
systemPrintln(" from incomingBuffer to serialTransmitBuffer");
805+
dumpBuffer(rxData, length);
806+
outputSerialData(true);
807+
}
808+
serialBufferOutput(rxData, rxDataBytes);
784809

785810
//Transmit ACK
786811
P2P_SEND_ACK(TRIGGER_TX_ACK);
@@ -969,7 +994,7 @@ void updateRadioState()
969994
//----------
970995
//Always check for link timeout
971996
//----------
972-
if ((millis() - linkDownTimer) >= (P2P_LINK_BREAK_MULTIPLIER * settings.heartbeatTimeout))
997+
if ((millis() - linkDownTimer) >= (LINK_BREAK_MULTIPLIER * settings.heartbeatTimeout))
973998
//Break the link
974999
breakLink();
9751000
break;
@@ -2472,7 +2497,7 @@ void updateRadioState()
24722497
//Determine if the link has timed out
24732498
vc = &virtualCircuitList[index];
24742499
if ((vc->vcState != VC_STATE_LINK_DOWN) && (serverLinkBroken
2475-
|| ((currentMillis - vc->lastTrafficMillis) > (VC_LINK_BREAK_MULTIPLIER * settings.heartbeatTimeout))))
2500+
|| ((currentMillis - vc->lastTrafficMillis) > (LINK_BREAK_MULTIPLIER * settings.heartbeatTimeout))))
24762501
{
24772502
if (index == VC_SERVER)
24782503
{
@@ -2898,7 +2923,10 @@ void breakLink()
28982923
}
28992924

29002925
//Flush the buffers
2901-
resetSerial();
2926+
if (!inCommandMode)
2927+
resetSerial();
2928+
radioTxHead = radioTxTail;
2929+
endOfTxData = &outgoingPacket[headerBytes];
29022930

29032931
//Reset the radio and the link
29042932
triggerEvent(TRIGGER_RADIO_RESET);
@@ -3063,7 +3091,11 @@ void vcBreakLink(int8_t vcIndex)
30633091
//Flush the buffers
30643092
outputSerialData(true);
30653093
if (vcIndex == myVc)
3094+
{
30663095
resetSerial();
3096+
radioTxHead = radioTxTail;
3097+
endOfTxData = &outgoingPacket[headerBytes];
3098+
}
30673099
}
30683100

30693101
//Place VC in LINK-UP state since it is receiving HEARTBEATs from the remote radio

0 commit comments

Comments
 (0)