From 7b2d3e7229ed4ccfef0d57a50f66973d5953b42c Mon Sep 17 00:00:00 2001 From: Nat Moore Date: Fri, 3 Dec 2021 23:57:17 -0600 Subject: [PATCH] fix comm w some arduinos; remove unnecessary ui --- fw/ino/kfd-avr/SerialProtocol.cpp | 59 ++- fw/ino/kfd-avr/Versions.h | 4 +- .../Protocol/Serial/SerialProtocol.cs | 99 ++-- .../KFDtool.Gui/Control/UtilInitAdapter.xaml | 16 - .../Control/UtilInitAdapter.xaml.cs | 462 ------------------ .../Control/UtilUpdateAdapterFw.xaml | 15 - .../Control/UtilUpdateAdapterFw.xaml.cs | 344 ------------- sw/control/KFDtool.Gui/KFDtool.Gui.csproj | 14 - sw/control/KFDtool.Gui/MainWindow.xaml | 2 - sw/control/KFDtool.Gui/MainWindow.xaml.cs | 16 +- .../TransferConstructs/InteractTwiKfdtool.cs | 6 +- sw/driver/kfdtool.cat | Bin 9615 -> 0 bytes sw/driver/kfdtool.inf | 123 ----- sw/installer/KFDtoolApp/KFDtoolApp.wixproj | 59 --- sw/installer/KFDtoolApp/Product.wxs | 176 ------- sw/installer/KFDtoolApp/license.rtf | Bin 1258 -> 0 bytes sw/installer/KFDtoolSetup.sln | 31 -- 17 files changed, 104 insertions(+), 1322 deletions(-) delete mode 100644 sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml delete mode 100644 sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml.cs delete mode 100644 sw/control/KFDtool.Gui/Control/UtilUpdateAdapterFw.xaml delete mode 100644 sw/control/KFDtool.Gui/Control/UtilUpdateAdapterFw.xaml.cs delete mode 100644 sw/driver/kfdtool.cat delete mode 100644 sw/driver/kfdtool.inf delete mode 100644 sw/installer/KFDtoolApp/KFDtoolApp.wixproj delete mode 100644 sw/installer/KFDtoolApp/Product.wxs delete mode 100644 sw/installer/KFDtoolApp/license.rtf delete mode 100644 sw/installer/KFDtoolSetup.sln diff --git a/fw/ino/kfd-avr/SerialProtocol.cpp b/fw/ino/kfd-avr/SerialProtocol.cpp index 916225b..6a974cd 100644 --- a/fw/ino/kfd-avr/SerialProtocol.cpp +++ b/fw/ino/kfd-avr/SerialProtocol.cpp @@ -1,14 +1,17 @@ #include "SerialProtocol.h" +#include "hal.h" #include -#define SOM_EOM 0x61 -#define SOM_EOM_PLACEHOLDER 0x62 -#define ESC 0x63 -#define ESC_PLACEHOLDER 0x64 + +#define SOM 0x61 +#define SOM_PLACEHOLDER 0x62 +#define EOM 0x63 +#define EOM_PLACEHOLDER 0x64 +#define ESC 0x70 +#define ESC_PLACEHOLDER 0x71 uint16_t inDataCount = 0; uint8_t inData[128]; -bool messageStarted = false; void spConnect(void) { @@ -24,13 +27,22 @@ void spDisconnect(void) uint16_t spRxData(uint8_t* outData) { + uint8_t inByte; while (Serial.available() > 0) { - inData[inDataCount] = Serial.read(); + inByte = Serial.read(); + + // reset the buffer if we have a start of message flag coming in + if (inByte == SOM) + { + inDataCount = 0; + } + + inData[inDataCount] = inByte; inDataCount++; } - // don't process partial frames - if (inDataCount < 3 || inData[0] != SOM_EOM || inData[inDataCount - 1] != SOM_EOM) + // don't process until we receive EOM + if (inData[inDataCount - 1] != EOM) { return 0; } @@ -45,14 +57,19 @@ uint16_t spRxData(uint8_t* outData) { inIndex++; - if (inData[inIndex] == SOM_EOM_PLACEHOLDER) + if (inData[inIndex] == SOM_PLACEHOLDER) + { + outData[outIndex] = SOM; + } + else if (inData[inIndex] == EOM_PLACEHOLDER) { - outData[outIndex] = SOM_EOM; + outData[outIndex] = EOM; } else if (inData[inIndex] == ESC_PLACEHOLDER) { outData[outIndex] = ESC; } + } else { @@ -64,8 +81,7 @@ uint16_t spRxData(uint8_t* outData) // we've already processed the message and set the pointer // reset the count (mark the buffer as free) - inDataCount = 0; - + inDataCount = 0; return outIndex; } @@ -79,7 +95,7 @@ uint16_t spFrameData(const uint8_t* inData, for (i = 0; i < inLength; i++) { - if ((inData[i] == SOM_EOM) || (inData[i] == ESC)) + if ((inData[i] == SOM) || (inData[i] == EOM) || (inData[i] == ESC)) { escCharsNeeded++; } @@ -87,18 +103,25 @@ uint16_t spFrameData(const uint8_t* inData, uint16_t totalCharsNeeded = 1 + inLength + escCharsNeeded + 1; - *(outData + 0) = SOM_EOM; + *(outData + 0) = SOM; uint16_t j; uint16_t k = 1; for (j = 0; j < inLength; j++) { - if (inData[j] == SOM_EOM) + if (inData[j] == SOM) { *(outData + k) = ESC; k++; - *(outData + k) = SOM_EOM_PLACEHOLDER; + *(outData + k) = SOM_PLACEHOLDER; + k++; + } + else if (inData[j] == EOM) + { + *(outData + k) = ESC; + k++; + *(outData + k) = EOM_PLACEHOLDER; k++; } else if (inData[j] == ESC) @@ -115,7 +138,7 @@ uint16_t spFrameData(const uint8_t* inData, } } - *(outData + (totalCharsNeeded - 1)) = SOM_EOM; + *(outData + (totalCharsNeeded - 1)) = EOM; return totalCharsNeeded; } @@ -140,4 +163,4 @@ void spTxDataWait(const uint8_t* inData, outLength = spFrameData(inData, inLength, outData); Serial.write(outData, outLength); -} +} \ No newline at end of file diff --git a/fw/ino/kfd-avr/Versions.h b/fw/ino/kfd-avr/Versions.h index 9b575bc..ecbfceb 100644 --- a/fw/ino/kfd-avr/Versions.h +++ b/fw/ino/kfd-avr/Versions.h @@ -3,11 +3,11 @@ /* FIRMWARE VERSION */ #define VER_FW_MAJOR 0x01 -#define VER_FW_MINOR 0x03 +#define VER_FW_MINOR 0x04 #define VER_FW_PATCH 0x00 /* ADAPTER PROTOCOL VERSION */ -#define VER_AP_MAJOR 0x01 +#define VER_AP_MAJOR 0x02 #define VER_AP_MINOR 0x00 #define VER_AP_PATCH 0x00 diff --git a/sw/control/KFDtool.Adapter/Protocol/Serial/SerialProtocol.cs b/sw/control/KFDtool.Adapter/Protocol/Serial/SerialProtocol.cs index b37fbe3..56df7e0 100644 --- a/sw/control/KFDtool.Adapter/Protocol/Serial/SerialProtocol.cs +++ b/sw/control/KFDtool.Adapter/Protocol/Serial/SerialProtocol.cs @@ -12,10 +12,12 @@ public class SerialProtocol { private static NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger(); - const byte SOM_EOM = 0x61; - const byte SOM_EOM_PLACEHOLDER = 0x62; - const byte ESC = 0x63; - const byte ESC_PLACEHOLDER = 0x64; + const byte SOM = 0x61; + const byte SOM_PLACEHOLDER = 0x62; + const byte EOM = 0x63; + const byte EOM_PLACEHOLDER = 0x64; + const byte ESC = 0x70; + const byte ESC_PLACEHOLDER = 0x71; private static AutoResetEvent CancelRead = new AutoResetEvent(false); @@ -50,7 +52,15 @@ public SerialProtocol(string portName) public void Open() { - Port.Open(); + // don't open the port if it is open already + if (!Port.IsOpen) + { + Port.Open(); + } + else + { + Logger.Debug("Not opening already open serial port"); + } } public void Close() @@ -75,7 +85,7 @@ public void Send(List data) { List frameData = new List(); - frameData.Add(SOM_EOM); + frameData.Add(SOM); foreach (byte b in data) { @@ -84,10 +94,15 @@ public void Send(List data) frameData.Add(ESC); frameData.Add(ESC_PLACEHOLDER); } - else if (b == SOM_EOM) + else if (b == SOM) { frameData.Add(ESC); - frameData.Add(SOM_EOM_PLACEHOLDER); + frameData.Add(SOM_PLACEHOLDER); + } + else if (b == EOM) + { + frameData.Add(ESC); + frameData.Add(EOM_PLACEHOLDER); } else { @@ -95,7 +110,7 @@ public void Send(List data) } } - frameData.Add(SOM_EOM); + frameData.Add(EOM); byte[] outData = frameData.ToArray(); @@ -192,50 +207,54 @@ private void OnDataReceived(object sender, SerialDataReceivedEventArgs e) { Logger.Trace("new byte: 0x{0:X2}", b); - if (b == SOM_EOM) + if (b == SOM) { FoundStart = true; - - if (FrameBuffer.Count > 0) + Logger.Trace("found start byte"); + } + else if (b == EOM) + { + for (int i = 0; i < FrameBuffer.Count; i++) { - for (int i = 0; i < FrameBuffer.Count; i++) + if (FrameBuffer[i] == ESC) { - if (FrameBuffer[i] == ESC) + FrameBuffer.RemoveAt(i); + + if (i == FrameBuffer.Count) { - FrameBuffer.RemoveAt(i); - - if (i == FrameBuffer.Count) - { - throw new Exception("escape character at end"); - } - - if (FrameBuffer[i] == ESC_PLACEHOLDER) - { - FrameBuffer[i] = ESC; - } - else if (FrameBuffer[i] == SOM_EOM_PLACEHOLDER) - { - FrameBuffer[i] = SOM_EOM; - } - else - { - throw new Exception("invalid character after escape character"); - } + throw new Exception("escape character at end"); + } + + if (FrameBuffer[i] == ESC_PLACEHOLDER) + { + FrameBuffer[i] = ESC; + } + else if (FrameBuffer[i] == SOM_PLACEHOLDER) + { + FrameBuffer[i] = SOM; + } + else if (FrameBuffer[i] == EOM_PLACEHOLDER) + { + FrameBuffer[i] = EOM; + } + else + { + throw new Exception("invalid character after escape character"); } } + } - List packet = new List(); + List packet = new List(); - packet.AddRange(FrameBuffer); + packet.AddRange(FrameBuffer); - PacketBuffer.Add(packet); + PacketBuffer.Add(packet); - Logger.Debug("packet contents: {0}", BitConverter.ToString(packet.ToArray())); + Logger.Debug("packet contents: {0}", BitConverter.ToString(packet.ToArray())); - FrameBuffer.Clear(); + FrameBuffer.Clear(); - Logger.Debug("packet buffer length: {0}", PacketBuffer.Count); - } + Logger.Debug("packet buffer length: {0}", PacketBuffer.Count); } else { diff --git a/sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml b/sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml deleted file mode 100644 index 3bbb2a3..0000000 --- a/sw/control/KFDtool.Gui/Control/UtilInitAdapter.xaml +++ /dev/null @@ -1,16 +0,0 @@ - - -