Skip to content

Commit

Permalink
Introduce and start using new SEND_BYTES command for improved perform…
Browse files Browse the repository at this point in the history
…ance (#8)

* Support commands up to 512 bytes in length

This is convenient for some larger CMD_SEND_BYTES payloads, but not strictly required. Since we have space in the Arduino memory, might as well use it.

* Introduce new CMD_SEND_BYTES

Create a new CMD_SEND_BYTES and corresponding response, which can be used to send multiple bytes in a single command. This reduces command/response overhead, especially for larger numbers of bytes.

* Start using new CMD_SEND_BYTES from C#

The previous commit introduced CMD_SEND_BYTES to the firmware, now let's start using it. This is done by adding a new function to use the command, and transitioning SendData(data) to use it.

* Update copyrights

* Add WRITE_TRANSFER_SPEED command

Add support for a new command WRITE_TRANSFER_SPEED, which allows the independent setting of tx and rx transfer speeds in kilobaud.

This is useful for some manufacturer-specific features such as high-speed keyloading. Currently not used by the software, being added for future use.

* Fix one remaining version

* Add version-based support for new commands

We don't want to break backwards compatibility with the existing KFDshields using the current adapter protocol version, so add some feature flags that enable the new commands if the adapter FW has already been bumped up. This lets us gracefully fail to existing commands if the KFD hardware is still on old FW.
  • Loading branch information
ilyacodes authored Oct 7, 2023
1 parent 2587f73 commit 78792b9
Show file tree
Hide file tree
Showing 18 changed files with 377 additions and 58 deletions.
1 change: 1 addition & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Copyright 2019-2020 KFDtool, LLC
Copyright 2021-2023 Natalie Moore
Copyright 2023 Ilya Smirnov

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ Contributors

* [Ellie Dugger](https://github.com/duggerd)
* [Matt Ames](https://github.com/mattames)
* [Ilya Smirnov](https://github.com/ilyacodes)

License / Legal
---------------
Expand Down
5 changes: 5 additions & 0 deletions fw/ino/kfd-avr/ControlOpCodes.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#define CMD_SELF_TEST 0x15
#define CMD_SEND_KEY_SIG 0x16
#define CMD_SEND_BYTE 0x17
#define CMD_SEND_BYTES 0x18

/* RESPONSE OPCODES */
#define RSP_ERROR 0x20
Expand All @@ -19,6 +20,7 @@
#define RSP_SELF_TEST 0x25
#define RSP_SEND_KEY_SIG 0x26
#define RSP_SEND_BYTE 0x27
#define RSP_SEND_BYTES 0x28

/* BROADCAST OPCODES */
#define BCST_RECEIVE_BYTE 0x31
Expand All @@ -34,6 +36,9 @@
/* WRITE OPCODES */
#define WRITE_MDL_REV 0x01
#define WRITE_SER 0x02
#define WRITE_DEFAULT_TRANSFER_SPEED 0x03
#define WRITE_TX_TRANSFER_SPEED 0x04
#define WRITE_RX_TRANSFER_SPEED 0x05

/* ERROR OPCODES */
#define ERR_OTHER 0x00
Expand Down
6 changes: 3 additions & 3 deletions fw/ino/kfd-avr/SerialProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#define ESC_PLACEHOLDER 0x71

uint16_t inDataCount = 0;
uint8_t inData[128];
uint8_t inData[512];

void spConnect(void)
{
Expand Down Expand Up @@ -147,7 +147,7 @@ void spTxDataBack(const uint8_t* inData,
uint16_t inLength)
{
uint16_t outLength;
uint8_t outData[128];
uint8_t outData[512];

outLength = spFrameData(inData, inLength, outData);

Expand All @@ -158,7 +158,7 @@ void spTxDataWait(const uint8_t* inData,
uint16_t inLength)
{
uint16_t outLength;
uint8_t outData[128];
uint8_t outData[512];

outLength = spFrameData(inData, inLength, outData);

Expand Down
49 changes: 39 additions & 10 deletions fw/ino/kfd-avr/TwiProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

#include "TwiProtocol.h"

#define BIT_TIME FCPU/4000
#define HALF_BIT_TIME BIT_TIME/2
#define SIG_TIME BIT_TIME*4

#if defined(__AVR_ATmega328P__)
// pin 3 is INT1
#define CLEAR_INTERRUPTS EIFR=2
Expand Down Expand Up @@ -40,6 +36,25 @@ volatile uint16_t TXByte;
volatile uint16_t RXByte;
volatile uint16_t hasReceived;

// Durations for tx/rx/sig
volatile uint16_t bitTimeTx = FCPU / 4000;
volatile uint16_t bitTimeRx = FCPU / 4000;
volatile uint16_t bitTimeRxHalf = FCPU / 8000;
volatile uint16_t bitTimeSig = FCPU / 1000;

void twiSetDefaultTransferSpeed() {
twiSetTxTransferSpeed(4);
twiSetRxTransferSpeed(4);
}

void twiSetTxTransferSpeed(uint8_t kilobaud) {
bitTimeTx = (FCPU / kilobaud) / 1000;
}

void twiSetRxTransferSpeed(uint8_t kilobaud) {
bitTimeRx = (FCPU / kilobaud) / 1000;
}

uint8_t reverseByte(uint8_t b)
{
const uint8_t table[] = {
Expand Down Expand Up @@ -289,7 +304,7 @@ void twiSendKeySig(void)

TCCR1B = 0b00000001; // set prescaler and CTC mode
TIMSK1 = 0b00000010; // set interrupt callback
OCR1A = SIG_TIME; // set value to count up to
OCR1A = bitTimeSig; // set value to count up to
interrupts(); // go!

while (busySending); // wait for completion
Expand All @@ -299,9 +314,8 @@ void twiSendKeySig(void)
ENABLE_KFD_RX_INT
}

void twiSendPhyByte(uint8_t byteToSend)
void twiSendPhyByteHelper(uint8_t byteToSend)
{
DISABLE_KFD_RX_INT
halGpio1High();
halActLedOff();

Expand All @@ -327,13 +341,28 @@ void twiSendPhyByte(uint8_t byteToSend)

TCCR1B = 0b00000001; // set prescaler
TIMSK1 = 0b00000010; // set compare match mode
OCR1A = BIT_TIME; // set value to count up to
OCR1A = bitTimeTx; // set value to count up to
interrupts(); // go!

while (busySending); // wait for completion

halGpio1Low();
halActLedOn();
}

void twiSendPhyBytes(uint8_t* byteToSend, uint16_t count)
{
DISABLE_KFD_RX_INT
for (uint32_t i = 0; i < count; i++) {
twiSendPhyByteHelper(byteToSend[i]);
}
ENABLE_KFD_RX_INT
}

void twiSendPhyByte(uint8_t byteToSend)
{
DISABLE_KFD_RX_INT
twiSendPhyByteHelper(byteToSend);
ENABLE_KFD_RX_INT
}

Expand All @@ -355,7 +384,7 @@ void Port_1(void)

TCCR1B = 0b00000001; // set prescaler
TIMSK1 = 0b00000010; // set compare match mode
OCR1A = HALF_BIT_TIME; // set value to count up to
OCR1A = bitTimeRx / 2; // set value to count up to
interrupts(); // go!
}

Expand All @@ -364,7 +393,7 @@ ISR(TIMER1_COMPA_vect)
TCNT1 = 0; // clear counter value
if (timerType == 0) // receive byte mode
{
OCR1A = BIT_TIME; // set value to count up to
OCR1A = bitTimeRx; // set value to count up to
if (rxBitsLeft == 0)
{
TCCR1B = 0; // stop timer by declocking
Expand Down
5 changes: 5 additions & 0 deletions fw/ino/kfd-avr/TwiProtocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@ void twiInit(void);

uint8_t twiSelfTest(void);

void twiSetDefaultTransferSpeed();
void twiSetTxTransferSpeed(uint8_t kilobaud);
void twiSetRxTransferSpeed(uint8_t kilobaud);

uint16_t twiReceiveByte(uint8_t *c);

void twiSendKeySig(void);

void twiSendPhyBytes(uint8_t* byteToSend, uint16_t count);
void twiSendPhyByte(uint8_t byteToSend);

void Port_1(void);
Expand Down
6 changes: 3 additions & 3 deletions fw/ino/kfd-avr/Versions.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@

/* FIRMWARE VERSION */
#define VER_FW_MAJOR 0x01
#define VER_FW_MINOR 0x04
#define VER_FW_PATCH 0x00
#define VER_FW_MINOR 0x07
#define VER_FW_PATCH 0x03

/* ADAPTER PROTOCOL VERSION */
#define VER_AP_MAJOR 0x02
#define VER_AP_MINOR 0x00
#define VER_AP_MINOR 0x01
#define VER_AP_PATCH 0x00

#endif /* VERSIONS_H_ */
91 changes: 90 additions & 1 deletion fw/ino/kfd-avr/kfd-avr.ino
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "UID.h"

uint16_t cmdCount;
uint8_t cmdData[128];
uint8_t cmdData[512];
uint16_t rxReady;
uint8_t rxTemp;

Expand Down Expand Up @@ -279,6 +279,72 @@ void loop()
spTxDataWait(rspData, sizeof(rspData));
}
}
else if (cmdData[1] == WRITE_DEFAULT_TRANSFER_SPEED) // write default transfer speed
{
if (cmdCount == 2)
{
twiSetDefaultTransferSpeed();

uint8_t rspData[1];

rspData[0] = RSP_WRITE_INFO;

spTxDataWait(rspData, sizeof(rspData));
}
else // invalid command length
{
uint8_t rspData[2];

rspData[0] = RSP_ERROR;
rspData[1] = ERR_INVALID_CMD_LENGTH;

spTxDataWait(rspData, sizeof(rspData));
}
}
else if (cmdData[1] == WRITE_TX_TRANSFER_SPEED) // write new tx transfer speed
{
if (cmdCount == 3)
{
twiSetTxTransferSpeed(cmdData[2]);

uint8_t rspData[1];

rspData[0] = RSP_WRITE_INFO;

spTxDataWait(rspData, sizeof(rspData));
}
else // invalid command length
{
uint8_t rspData[2];

rspData[0] = RSP_ERROR;
rspData[1] = ERR_INVALID_CMD_LENGTH;

spTxDataWait(rspData, sizeof(rspData));
}
}
else if (cmdData[1] == WRITE_RX_TRANSFER_SPEED) // write new rx transfer speed
{
if (cmdCount == 3)
{
twiSetRxTransferSpeed(cmdData[2]);

uint8_t rspData[1];

rspData[0] = RSP_WRITE_INFO;

spTxDataWait(rspData, sizeof(rspData));
}
else // invalid command length
{
uint8_t rspData[2];

rspData[0] = RSP_ERROR;
rspData[1] = ERR_INVALID_CMD_LENGTH;

spTxDataWait(rspData, sizeof(rspData));
}
}
else // invalid write opcode
{
uint8_t rspData[2];
Expand Down Expand Up @@ -427,6 +493,29 @@ void loop()
spTxDataWait(rspData, sizeof(rspData));
}
}
else if (cmdData[0] == CMD_SEND_BYTES) // send bytes
{
uint16_t dataCount = (uint16_t)cmdData[2] * 256 + cmdData[3];
if (cmdCount == dataCount + 4)
{
twiSendPhyBytes(cmdData + 4, dataCount);

uint8_t rspData[1];

rspData[0] = RSP_SEND_BYTES;

spTxDataWait(rspData, sizeof(rspData));
}
else // invalid command length
{
uint8_t rspData[2];

rspData[0] = RSP_ERROR;
rspData[1] = ERR_INVALID_CMD_LENGTH;

spTxDataWait(rspData, sizeof(rspData));
}
}
else // invalid command opcode
{
uint8_t rspData[2];
Expand Down
6 changes: 3 additions & 3 deletions sw/control/KFDtool.Adapter/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("KFDtool")]
[assembly: AssemblyCopyright("Copyright 2019-2020 Ellie Dugger")]
[assembly: AssemblyCopyright("Copyright 2019-2020 Ellie Dugger, 2023 Ilya Smirnov")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand All @@ -32,5 +32,5 @@
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.5.0.0")]
[assembly: AssemblyFileVersion("1.5.0.0")]
[assembly: AssemblyVersion("1.7.3.0")]
[assembly: AssemblyFileVersion("1.7.3.0")]
Loading

0 comments on commit 78792b9

Please sign in to comment.