Random wanderings through Microsoft Azure esp. PaaS plumbing, the IoT bits, AI on Micro controllers, AI on Edge Devices, .NET nanoFramework, .NET Core on *nix and ML.NET+ONNX
Again, while doing some stress testing I noticed an odd message go past in the Visual Studio output window. I had multiple devices sending addressed messages (both individual and broadcast) to the Adafruit RFM69 HCW Radio Bonnet, on my Windows 10 IoT Core device while it was sending a message every 5 seconds.
Received From 102 a 15 byte message Hello World:161
23:42:33.343 RegIrqFlags2 01100110
23:42:33.356 RegIrqFlags1 11011001
23:42:33.374 Address 0X99 10011001
Received From 153 a 15 byte message Hello World:106
23:42:33.761 RegIrqFlags2 01100110
23:42:33.774 RegIrqFlags1 11011001
23:42:33.791 Address 0X66 01100110
Received From 102 a 15 byte message Hello World:162
The thread 0xd20 has exited with code 0 (0x0).
23:42:34.500 RegIrqFlags2 01100110
23:42:34.501 Send-hello world 11:42:34 PM
23:42:34.520 RegIrqFlags1 11011001
23:42:34.545 Send-Done
23:42:34.551 Address 0X10 00010000
Received From 16 a 15 byte message h WWWWWWWWoo
23:42:34.686 RegIrqFlags2 00001000
23:42:34.701 RegIrqFlags1 10110000
23:42:34.715 Transmit-Done
Transmit-Done
23:42:34.902 RegIrqFlags2 01100110
23:42:34.915 RegIrqFlags1 11011001
23:42:34.931 Address 0X66 01100110
Received From 102 a 15 byte message Hello World:163
23:42:35.626 RegIrqFlags2 01100110
23:42:35.640 RegIrqFlags1 11011001
23:42:35.659 Address 0X99 10011001
Received From 153 a 15 byte message Hello World:108
23:42:36.042 RegIrqFlags2 01100110
23:42:36.055 RegIrqFlags1 11011001
23:42:36.073 Address 0X66 01100110
The RegIrqFlags2 CrcOk (bit 1) was set and the message was corrupt.
RegIrqFlags2 bit flags from SX1231 datasheet
I have added code to check the CRC on inbound messages if this functionality is enabled. So the library can be used with CRCs disabled I have added a flag to the OnDataReceivedEventArgs class to indicate whether the CRC on the inbound message was OK.
private readonly Object Rfm9XRegFifoLock = new object();
...
private void ProcessPayloadReady(RegIrqFlags1 irqFlags1, RegIrqFlags2 irqFlags2)
{
byte? address = null;
byte numberOfBytes;
byte[] messageBytes;
lock (Rfm9XRegFifoLock)
{
// Read the length of the buffer if variable length packets
if (PacketFormat == RegPacketConfig1PacketFormat.VariableLength)
{
numberOfBytes = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
}
else
{
numberOfBytes = PayloadLength;
}
// Remove the address from start of the payload
if (AddressingEnabled)
{
address = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
Debug.WriteLine("{0:HH:mm:ss.fff} Address 0X{1:X2} {2}", DateTime.Now, address, Convert.ToString((byte)address, 2).PadLeft(8, '0'));
numberOfBytes--;
}
// Allocate a buffer for the payload and read characters from the Fifo
messageBytes = new byte[numberOfBytes];
for (int i = 0; i < numberOfBytes; i++)
{
messageBytes[i] = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
}
}
...
public void SendMessage(byte[] messageBytes)
{
#region Guard conditions
#endregion
lock (Rfm9XRegFifoLock)
{
SetMode(RegOpModeMode.StandBy);
if (PacketFormat == RegPacketConfig1PacketFormat.VariableLength)
{
RegisterManager.WriteByte((byte)Registers.RegFifo, (byte)messageBytes.Length);
}
foreach (byte b in messageBytes)
{
this.RegisterManager.WriteByte((byte)Registers.RegFifo, b);
}
SetMode(RegOpModeMode.Transmit);
}
}
I can most probably reduce the duration which I hold the lock for but that will require some more stress testing.
While doing some stress testing I noticed an odd message go past in the Visual Studio output window. I had multiple devices sending addressed messages (both individual and broadcast) to the Adafruit RFM69 HCW Radio Bonnet, on my Windows 10 IoT Core device while it was sending a message every 5 seconds.
Received From 153 a 13 byte message Hello World:7
18:43:56.544 RegIrqFlags2 01100110
18:43:56.558 RegIrqFlags1 11011001
18:43:56.575 Address 0X66 01100110
Received From 102 a 15 byte message Hello World:162
The thread 0x254 has exited with code 0 (0x0).
18:43:57.699 Send-hello world 6:43:57 PM
18:43:57.699 RegIrqFlags2 01100110
18:43:57.731 RegIrqFlags1 10000000
18:43:57.747 Address 0X66 01100110
18:43:57.765 Send-Done
Received From 102 a 15 byte message Hello Woooooooo
18:43:57.987 RegIrqFlags2 00001000
18:43:58.003 RegIrqFlags1 10110000
18:43:58.017 Transmit-Done
Transmit-Done
18:43:58.825 RegIrqFlags2 01100110
18:43:58.838 RegIrqFlags1 11011001
18:43:58.857 Address 0X66 01100110
Received From 102 a 15 byte message Hello World:164
18:43:59.966 RegIrqFlags2 01100110
18:43:59.979 RegIrqFlags1 11011001
18:43:59.998 Address 0X66 01100110
The odd thing was that the RegIrqFlags2 CrcOk (bit 1) was set but the message was still corrupt.
RegIrqFlags2 bit flags from SX1231 datasheet
After looking at the code I think the problem was the reading of the received message bytes from the device FIFO and the writing of bytes of message to be transmitted into the device FIFO overlapped. To stop this occurring again I have added code to synchronise access (using a Lock) to the FIFO.
private readonly Object Rfm9XRegFifoLock = new object();
...
private void ProcessPayloadReady(RegIrqFlags1 irqFlags1, RegIrqFlags2 irqFlags2)
{
byte? address = null;
byte numberOfBytes;
byte[] messageBytes;
lock (Rfm9XRegFifoLock)
{
// Read the length of the buffer if variable length packets
if (PacketFormat == RegPacketConfig1PacketFormat.VariableLength)
{
numberOfBytes = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
}
else
{
numberOfBytes = PayloadLength;
}
// Remove the address from start of the payload
if (AddressingEnabled)
{
address = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
Debug.WriteLine("{0:HH:mm:ss.fff} Address 0X{1:X2} {2}", DateTime.Now, address, Convert.ToString((byte)address, 2).PadLeft(8, '0'));
numberOfBytes--;
}
// Allocate a buffer for the payload and read characters from the Fifo
messageBytes = new byte[numberOfBytes];
for (int i = 0; i < numberOfBytes; i++)
{
messageBytes[i] = RegisterManager.ReadByte((byte)Rfm69HcwDevice.Registers.RegFifo);
}
}
...
public void SendMessage(byte[] messageBytes)
{
#region Guard conditions
#endregion
lock (Rfm9XRegFifoLock)
{
SetMode(RegOpModeMode.StandBy);
if (PacketFormat == RegPacketConfig1PacketFormat.VariableLength)
{
RegisterManager.WriteByte((byte)Registers.RegFifo, (byte)messageBytes.Length);
}
foreach (byte b in messageBytes)
{
this.RegisterManager.WriteByte((byte)Registers.RegFifo, b);
}
SetMode(RegOpModeMode.Transmit);
}
}
The code has been running for a day without any corrupted messages so the lock appears to be working. I can most probably reduce the duration which I hold the lock for but that will require some more stress testing.
The RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) has configurable digital outputs (RegDIOMapping1 & RegDIOMapping2) . Which I use to trigger interrupts on my Windows 10 IoT Core or Arduino devices. Currently (Sep 2019) the library only supports the mapping of the digital outputs D0 & D1 when the RFM69 is in Packet Mode.
RegiDIOMapping0 & RegDIOMapping2 settings for DIO thru DIO5
I added some additional constants and enumerations for the other settings configured in RegDioMapping1 & RegDioMapping2.
I had several failed attempts at defining suitable enumerations for configuring the RegDioMapping1 & RegDioMapping2 registers. I initially started with an enumeration for each Mode (Sleep, StandBy etc.) but the implementation was quite complex. The initial version only supports DIO0 & DIO1 as most of the shields I have, only DIO0 adn/or DIO1 are connected.
Enums and Masks – Packet lengths, addressing & CRCs
The RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) has configurable (RegSyncConfig) synchronisation sequences (the length, tolerance for errors and the individual byte values).
By default synchronisation is enabled and a default sequence of bytes is used, in my library synchronisation is NOT enabled until a SyncValue is provided.
I added some additional constants and enumerations for the other settings configured in RegSyncConfig.
// RegSyncConfig
// This is private because default ignored and flag set based on SyncValues parameter being specified rather than default
private enum RegSyncConfigSyncOn
{
Off = 0b00000000,
On = 0b10000000
}
public enum RegSyncConfigFifoFileCondition
{
SyncAddressInterrupt = 0b00000000,
FifoFillCondition = 0b01000000
}
private const RegSyncConfigFifoFileCondition SyncFifoFileConditionDefault = RegSyncConfigFifoFileCondition.SyncAddressInterrupt;
readonly byte[] SyncValuesDefault = {0x01, 0x01, 0x01, 0x01};
public const byte SyncValuesSizeDefault = 4;
public const byte SyncValuesSizeMinimum = 1;
public const byte SyncValuesSizeMaximum = 8;
private const byte SyncToleranceDefault = 0;
public const byte SyncToleranceMinimum = 0;
public const byte SyncToleranceMaximum = 7;
I also added some guard conditions to the initialise method which validate the syncFifoFileCondition, syncTolerance and syncValues length.
public void Initialise(RegOpModeMode modeAfterInitialise,
BitRate bitRate = BitRateDefault,
ushort frequencyDeviation = frequencyDeviationDefault,
double frequency = FrequencyDefault,
ListenModeIdleResolution listenModeIdleResolution = ListenModeIdleResolutionDefault, ListenModeRXTime listenModeRXTime = ListenModeRXTimeDefault, ListenModeCrieria listenModeCrieria = ListenModeCrieriaDefault, ListenModeEnd listenModeEnd = ListenModeEndDefault,
byte listenCoefficientIdle = ListenCoefficientIdleDefault,
byte listenCoefficientReceive = ListenCoefficientReceiveDefault,
bool pa0On = pa0OnDefault, bool pa1On = pa1OnDefaut, bool pa2On = pa2OnDefault, byte outputpower = OutputpowerDefault,
PaRamp paRamp = PaRampDefault,
bool ocpOn = OcpOnDefault, byte ocpTrim = OcpTrimDefault,
LnaZin lnaZin = LnaZinDefault, LnaCurrentGain lnaCurrentGain = LnaCurrentGainDefault, LnaGainSelect lnaGainSelect = LnaGainSelectDefault,
byte dccFrequency = DccFrequencyDefault, RxBwMant rxBwMant = RxBwMantDefault, byte RxBwExp = RxBwExpDefault,
byte dccFreqAfc = DccFreqAfcDefault, byte rxBwMantAfc = RxBwMantAfcDefault, byte bxBwExpAfc = RxBwExpAfcDefault,
ushort preambleSize = PreambleSizeDefault,
RegSyncConfigFifoFileCondition? syncFifoFileCondition = null, byte? syncTolerance = null, byte[] syncValues = null,
RegPacketConfig1PacketFormat packetFormat = RegPacketConfig1PacketFormat.FixedLength,
RegPacketConfig1DcFree packetDcFree = RegPacketConfig1DcFreeDefault,
bool packetCrc = PacketCrcOnDefault,
bool packetCrcAutoClearOff = PacketCrcAutoClearOffDefault,
RegPacketConfig1CrcAddressFiltering packetAddressFiltering = PacketAddressFilteringDefault,
byte payloadLength = PayloadLengthDefault,
byte addressNode = NodeAddressDefault, byte addressbroadcast = BroadcastAddressDefault,
TxStartCondition txStartCondition = TxStartConditionDefault, byte fifoThreshold = FifoThresholdDefault,
byte interPacketRxDelay = InterPacketRxDelayDefault, bool restartRx = RestartRxDefault, bool autoRestartRx = AutoRestartRxDefault,
byte[] aesKey = null
)
{
RegOpModeModeCurrent = modeAfterInitialise;
PacketFormat = packetFormat;
#region RegSyncConfig + RegSyncValue1 to RegSyncValue8 guard conditions
if (syncValues != null)
{
// If sync enabled (i.e. SyncValues array provided) check that SyncValues not to short/long and SyncTolerance not to small/big
if ((syncValues.Length < SyncValuesSizeMinimum) || (syncValues.Length > SyncValuesSizeMaximum))
{
throw new ArgumentException($"The syncValues array length must be between {SyncValuesSizeMinimum} and {SyncValuesSizeMaximum} bytes", "syncValues");
}
if (syncTolerance.HasValue)
{
if ((syncTolerance < SyncToleranceMinimum) || (syncTolerance > SyncToleranceMaximum))
{
throw new ArgumentException($"The syncTolerance size must be between {SyncToleranceMinimum} and {SyncToleranceMaximum}", "syncTolerance");
}
}
}
else
{
// If sync not enabled (i.e. SyncValues array null) check that no syncFifoFileCondition or syncTolerance configuration specified
if (syncFifoFileCondition.HasValue)
{
throw new ArgumentException($"If Sync not enabled syncFifoFileCondition is not supported", "syncFifoFileCondition");
}
if (syncTolerance.HasValue)
{
throw new ArgumentException($"If Sync not enabled SyncTolerance is not supported", "syncTolerance");
}
}
#endregion
I also ensure that the syncFifoFileCondition and syncTolerance are not specified if synchronisation is not enabled.
The library also supports the built in RFRM69 node and broadcast addressing which is enabled when the AddressNode and/or AddressBroadcast parameters of the Initialise method are set.
RegPacketConfig1 address filtering options
My first attempt at getting encryption and addressing working together failed badly, the Windows 10 IoT Core device didn’t receive any addressed messages when encryption was enabled. So, I went back and re-read the datasheet again and noticed
“If the address filtering is expected then AddressFiltering must be enabled on the transmitter side as well to prevent address byte to be encrypted”(Sic).
The Arduino client code had to be modified so I could set the node + broadcast address registers and AddressFiltering bit flag in RegPacketConfig1
My RMRFM69.h modifications
enum moduleType {RFM65, RFM65C, RFM69, RFM69C, RFM69H, RFM69HC};
#define ADDRESS_NODE_DEFAULT 0x0
#define ADDRESS_BROADCAST_DEFAULT 0x0
#define ADDRESSING_ENABLED_NODE 0x2
#define ADDRESSING_ENABLED_NODE_AND_BROADCAST 0x4
class RMRFM69
{
public:
RMRFM69(SPIClass &spiPort, byte csPin, byte dio0Pin, byte rstPin);
modulationType Modulation; //OOK/FSK/GFSK
moduleType COB; //Chip on board
uint32_t Frequency; //unit: KHz
uint32_t SymbolTime; //unit: ns
uint32_t Devation; //unit: KHz
word BandWidth; //unit: KHz
byte OutputPower; //unit: dBm range: 0-31 [-18dBm~+13dBm] for RFM69/RFM69C
// range: 0-31 [-11dBm~+20dBm] for RFM69H/RFM69HC
word PreambleLength; //unit: byte
bool CrcDisable; //fasle: CRC enable�� & use CCITT 16bit
//true : CRC disable
bool CrcMode; //false: CCITT
bool FixedPktLength; //false: for contain packet length in Tx message, the same mean with variable lenth
//true : for doesn't include packet length in Tx message, the same mean with fixed length
bool AesOn; //false:
//true:
bool AfcOn; //false:
//true:
byte SyncLength; //unit: none, range: 1-8[Byte], value '0' is not allowed!
byte SyncWord[8];
byte PayloadLength; //PayloadLength is need to be set a value, when FixedPktLength is true.
byte AesKey[16]; //AES Key block, note [0]->[15] == MSB->LSB
byte AddressNode = ADDRESS_NODE_DEFAULT;
byte AddressBroadcast = ADDRESS_BROADCAST_DEFAULT;
void vInitialize(void);
void vConfig(void);
void vGoRx(void);
void vGoStandby(void);
void vGoSleep(void);
bool bSendMessage(byte msg[], byte length);
bool bSendMessage(byte Address, byte msg[], byte length);
byte bGetMessage(byte msg[]);
void vRF69SetAesKey(void);
void vTrigAfc(void);
void vDirectRx(void); //go continuous rx mode, with init. inside
void vChangeFreq(uint32_t freq); //change frequency
byte bReadRssi(void); //read rssi value
void dumpRegisters(Stream& out);
My RMRFM69.cpp modifications in vConfig
if(!CrcDisable)
{
i += CrcOn;
if(CrcMode)
i += CrcCalc_IBM;
else
i += CrcCalc_CCITT;
}
if((AddressNode!=ADDRESS_NODE_DEFAULT) || (AddressBroadcast==ADDRESS_BROADCAST_DEFAULT))
{
i += ADDRESSING_ENABLED_NODE;
}
if((AddressNode!=ADDRESS_NODE_DEFAULT) || (AddressBroadcast!=ADDRESS_BROADCAST_DEFAULT))
{
i += ADDRESSING_ENABLED_NODE_AND_BROADCAST;
}
vSpiWrite(((word)RegPacketConfig1<<8)+i);
I also validate the lengths of the messages to be sent taking into account whether encryption is enabled\disabled.
Every so often my Enums & Masks test harness locked up and stopped receiving messages from my test rig. This seemed to happen more often when the send functionality of my library was not being used.
easysensors RFM69HCW test rig
After 5 to 30 minutes (a couple of times it was 5 to 8 hours overnight) the application stopped receiving messages and wouldn’t resume until the application (device reset) was restarted or the RegOpmode-Mode was quickly changed to sleep then back to receive.
I had noticed this code in the Low Power Lab and wondered what it was for. The HopeRF library didn’t appear to have code like this to restart reception which was interesting.
void RFM69::send(uint16_t toAddress, const void* buffer, uint8_t bufferSize, bool requestACK)
{
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
uint32_t now = millis();
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) receiveDone();
sendFrame(toAddress, buffer, bufferSize, requestACK, false);
}
// should be called immediately after reception in case sender wants ACK
void RFM69::sendACK(const void* buffer, uint8_t bufferSize) {
ACK_REQUESTED = 0; // TWS added to make sure we don't end up in a timing race and infinite loop sending Acks
uint16_t sender = SENDERID;
int16_t _RSSI = RSSI; // save payload received RSSI value
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
uint32_t now = millis();
while (!canSend() && millis() - now < RF69_CSMA_LIMIT_MS) receiveDone();
SENDERID = sender; // TWS: Restore SenderID after it gets wiped out by receiveDone()
sendFrame(sender, buffer, bufferSize, false, true);
RSSI = _RSSI; // restore payload RSSI
}
void RFM69::receiveBegin() {
DATALEN = 0;
SENDERID = 0;
TARGETID = 0;
PAYLOADLEN = 0;
ACK_REQUESTED = 0;
ACK_RECEIVED = 0;
#if defined(RF69_LISTENMODE_ENABLE)
RF69_LISTEN_BURST_REMAINING_MS = 0;
#endif
RSSI = 0;
if (readReg(REG_IRQFLAGS2) & RF_IRQFLAGS2_PAYLOADREADY)
writeReg(REG_PACKETCONFIG2, (readReg(REG_PACKETCONFIG2) & 0xFB) | RF_PACKET2_RXRESTART); // avoid RX deadlocks
writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_01); // set DIO0 to "PAYLOADREADY" in receive mode
setMode(RF69_MODE_RX);
}
In the debug output you can see that clock frequencies of the two test devices are slightly different. Every so often they transmit close enough to corrupt one of the message payloads which causes the deadlock.
Sometimes while testing code you notice something odd. Every so often the Enums & Masks application locks up and stops receiving messages from my test rig.
easysensors RFM69HCW test rig
The symptom is that after 5 to 30 minutes the application stops receiving messages
21:37:37.568 RegIrqFlags1 11011001
21:37:37.583 Address 0X99 10011001
21:37:37 Received 14 byte message Hello World:61
..21:37:38.693 RegIrqFlags2 01100110
21:37:38.706 RegIrqFlags1 11011001
21:37:38.724 Address 0X99 10011001
21:37:38 Received 14 byte message Hello World:62
............The thread 0xba8 has exited with code 0 (0x0).
.................................................................................................................................................The thread 0xf90 has exited with code 0 (0x0).
.....................The thread 0xe30 has exited with code 0 (0x0).
.......................The thread 0xa04 has exited with code 0 (0x0).
................................The thread 0xc8c has exited with code 0 (0x0).
..........................................................................................The thread 0xc38 has exited with code 0 (0x0).
......................The thread 0xf68 has exited with code 0 (0x0).
......................................................................................The thread 0x1c8 has exited with code 0 (0x0).
..........The thread 0xeb8 has exited with code 0 (0x0).
..............................................................The thread 0xbb8 has exited with code 0 (0x0).
..........The thread 0xdc0 has exited with code 0 (0x0).
...............................The thread 0x820 has exited with code 0 (0x0).
....................................The thread 0xaac has exited with code 0 (0x0).
......The thread 0xbf0 has exited with code 0 (0x0).
............................................The thread 0x4e8 has exited with code 0 (0x0).
...............................The thread 0x1b4 has exited with code 0 (0x0).
...............................................................The thread 0xbdc has exited with code 0 (0x0).
....................The thread 0xb60 has exited with code 0 (0x0).
.........................................................................................................The thread 0x510 has exited with code 0 (0x0).
........The thread 0xf60 has exited with code 0 (0x0).
........................................................The thread 0x3c0 has exited with code 0 (0x0).
......................................The thread 0xa4c has exited with code 0 (0x0).
..................................................................The thread 0x9e0 has exited with code 0 (0x0).
....................The thread 0xd74 has exited with code 0 (0x0).
............................The thread 0xfa0 has exited with code 0 (0x0).
..................................................................................................The thread 0xfe0 has exited with code 0 (0x0).
....................................................................................The thread 0xdd4 has exited with code 0 (0x0).
........................The thread 0xc00 has exited with code 0 (0x0).
..................................The thread 0x478 has exited with code 0 (0x0).
.........................The thread 0x88c has exited with code 0 (0x0).
...........................................The thread 0x280 has exited with code 0 (0x0).
..........................................The thread 0x8e4 has exited with code 0 (0x0).
............The thread 0x410 has exited with code 0 (0x0).
..............................................The thread 0xa70 has exited with code 0 (0x0).
................The thread 0x994 has exited with code 0 (0x0).
....................The thread 0x298 has exited with code 0 (0x0).
..............The thread 0x3a4 has exited with code 0 (0x0).
............................................................The thread 0xa2c has exited with code 0 (0x0).
..........The thread 0x208 has exited with code 0 (0x0).
..........................................................................The thread 0xbd4 has exited with code 0 (0x0).
............The thread 0xfdc has exited with code 0 (0x0).
........................................................................The thread 0x36c has exited with code 0 (0x0).
...........22:08:57.638 RegIrqFlags2 01100110
22:08:57.658 RegIrqFlags1 11011001
22:08:57.676 Address 0X66 01100110
22:08:57 Received 15 byte message Hello World:157
22:08:57.807 RegIrqFlags2 01100110
But, every so often it would after many minutes start up again without me doing anything (I noticed this after leaving application running overnight). I could get the application to restart by putting a break point on the Debug.Write(“.”) and toggling the OperationMode from Sleep to Receive
Using Visual Studio Immediate Windows to execute SetMode
I have found if the device is transmitting every so often the lockups are also much less likely. To help with debugging the issue I have wired up the three buttons on the Adafruit Radio Bonnet to call different diagnostic code
Looks like this maybe a bit of a heisenbug as it takes a longish time to appear and poking around in the debugger and adding more diagnostics changes the frequency the error.
Received 16 byte message
Receive-Done
.............................Receive-No wait
Received 16 byte message
Receive-Done
Receive-No wait
Received 16 byte message
Receive-Done
....Receive-No wait
Received 16 byte message
Receive-Done
Receive-No wait
Received 16 byte message
Receive-Done
.............
Pressing button one restarts inbound messages for a while, button two sits in an endless loop, button three reads in a 16 byte message of 0x10 characters, which I think is buffer length. I have added code to catch exceptions and stop re-entrancy but it never seems to get triggered.
The RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) has configurable (RegSyncConfig) synchronisation sequences (the length, tolerance for errors and the individual byte values).
By default synchronisation is enabled and a default sequence of bytes is used, in my library synchronisation is NOT enabled until a SyncValue is provided.
I added some additional constants and enumerations for the other settings configured in RegSyncConfig.
// RegSyncConfig
// This is private because default ignored and flag set based on SyncValues parameter being specified rather than default
private enum RegSyncConfigSyncOn
{
Off = 0b00000000,
On = 0b10000000
}
public enum RegSyncConfigFifoFileCondition
{
SyncAddressInterrupt = 0b00000000,
FifoFillCondition = 0b01000000
}
private const RegSyncConfigFifoFileCondition SyncFifoFileConditionDefault = RegSyncConfigFifoFileCondition.SyncAddressInterrupt;
readonly byte[] SyncValuesDefault = {0x01, 0x01, 0x01, 0x01};
public const byte SyncValuesSizeDefault = 4;
public const byte SyncValuesSizeMinimum = 1;
public const byte SyncValuesSizeMaximum = 8;
private const byte SyncToleranceDefault = 0;
public const byte SyncToleranceMinimum = 0;
public const byte SyncToleranceMaximum = 7;
I also added some guard conditions to the initialise method which validate the syncFifoFileCondition, syncTolerance and syncValues length.
public void Initialise(RegOpModeMode modeAfterInitialise,
BitRate bitRate = BitRateDefault,
ushort frequencyDeviation = frequencyDeviationDefault,
double frequency = FrequencyDefault,
ListenModeIdleResolution listenModeIdleResolution = ListenModeIdleResolutionDefault, ListenModeRXTime listenModeRXTime = ListenModeRXTimeDefault, ListenModeCrieria listenModeCrieria = ListenModeCrieriaDefault, ListenModeEnd listenModeEnd = ListenModeEndDefault,
byte listenCoefficientIdle = ListenCoefficientIdleDefault,
byte listenCoefficientReceive = ListenCoefficientReceiveDefault,
bool pa0On = pa0OnDefault, bool pa1On = pa1OnDefaut, bool pa2On = pa2OnDefault, byte outputpower = OutputpowerDefault,
PaRamp paRamp = PaRampDefault,
bool ocpOn = OcpOnDefault, byte ocpTrim = OcpTrimDefault,
LnaZin lnaZin = LnaZinDefault, LnaCurrentGain lnaCurrentGain = LnaCurrentGainDefault, LnaGainSelect lnaGainSelect = LnaGainSelectDefault,
byte dccFrequency = DccFrequencyDefault, RxBwMant rxBwMant = RxBwMantDefault, byte RxBwExp = RxBwExpDefault,
byte dccFreqAfc = DccFreqAfcDefault, byte rxBwMantAfc = RxBwMantAfcDefault, byte bxBwExpAfc = RxBwExpAfcDefault,
ushort preambleSize = PreambleSizeDefault,
RegSyncConfigFifoFileCondition? syncFifoFileCondition = null, byte? syncTolerance = null, byte[] syncValues = null,
RegPacketConfig1PacketFormat packetFormat = RegPacketConfig1PacketFormat.FixedLength,
RegPacketConfig1DcFree packetDcFree = RegPacketConfig1DcFreeDefault,
bool packetCrc = PacketCrcOnDefault,
bool packetCrcAutoClearOff = PacketCrcAutoClearOffDefault,
RegPacketConfig1CrcAddressFiltering packetAddressFiltering = PacketAddressFilteringDefault,
byte payloadLength = PayloadLengthDefault,
byte addressNode = NodeAddressDefault, byte addressbroadcast = BroadcastAddressDefault,
TxStartCondition txStartCondition = TxStartConditionDefault, byte fifoThreshold = FifoThresholdDefault,
byte interPacketRxDelay = InterPacketRxDelayDefault, bool restartRx = RestartRxDefault, bool autoRestartRx = AutoRestartRxDefault,
byte[] aesKey = null
)
{
RegOpModeModeCurrent = modeAfterInitialise;
PacketFormat = packetFormat;
#region RegSyncConfig + RegSyncValue1 to RegSyncValue8 guard conditions
if (syncValues != null)
{
// If sync enabled (i.e. SyncValues array provided) check that SyncValues not to short/long and SyncTolerance not to small/big
if ((syncValues.Length < SyncValuesSizeMinimum) || (syncValues.Length > SyncValuesSizeMaximum))
{
throw new ArgumentException($"The syncValues array length must be between {SyncValuesSizeMinimum} and {SyncValuesSizeMaximum} bytes", "syncValues");
}
if (syncTolerance.HasValue)
{
if ((syncTolerance < SyncToleranceMinimum) || (syncTolerance > SyncToleranceMaximum))
{
throw new ArgumentException($"The syncTolerance size must be between {SyncToleranceMinimum} and {SyncToleranceMaximum}", "syncTolerance");
}
}
}
else
{
// If sync not enabled (i.e. SyncValues array null) check that no syncFifoFileCondition or syncTolerance configuration specified
if (syncFifoFileCondition.HasValue)
{
throw new ArgumentException($"If Sync not enabled syncFifoFileCondition is not supported", "syncFifoFileCondition");
}
if (syncTolerance.HasValue)
{
throw new ArgumentException($"If Sync not enabled SyncTolerance is not supported", "syncTolerance");
}
}
#endregion
I also ensure that the syncFifoFileCondition and syncTolerance are not specified if synchronisation is not enabled.
The Arduino client code works though I need modify it so I can do more testing of the initialise method parameter options.
The RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) has built in support for 128bit Advanced Encryption Standard(AES) encoding of message payloads. To make encryption easy to configure I added some additional constants and enumerations for the other settings configured in RegPacketConfig2.
// RegPacketConfig2
private const byte InterPacketRxDelayDefault = 0;
public const byte InterPacketRxDelayMinimum = 0x0;
public const byte InterPacketRxDelayMaximum = 0xF;
private const bool RestartRxDefault = false;
[Flags]
private enum RegPacketConfig2RestartRxDefault : byte
{
Off = 0b00000000,
On = 0b00000100,
}
private const bool AutoRestartRxDefault = true;
[Flags]
private enum RegPacketConfig2AutoRestartRxDefault : byte
{
Off = 0b00000000,
On = 0b00000010,
}
[Flags]
private enum RegPacketConfig2Aes : byte
{
Off = 0b00000000,
On = 0b00000001,
}
public const byte AesKeyLength = 16;
I then added some guard conditions to the initialise method to validate the InterPacketRxDelay and the encryption key length.
Based on the approach I used in my RFM9X library this refactor adds enumerations and constants for initialising and then accessing the registers of the RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) .
Adafruit RFM69 Radio Bonnet
There is now a even less code in the Run method in the startup.cs file and the code for configuring the RFM69 is more obvious
The Rasmitic library modifies a number of the default settings (e.g. RegRxBw register) so I had to reverse engineer the values. I also added SendMessage methods for both addressed and un-addressed messages.
Register dump
Register 0x01 - Value 0X04 - Bits 00000100
Register 0x02 - Value 0X00 - Bits 00000000
Register 0x03 - Value 0X1a - Bits 00011010
…
Register 0x4b - Value 0X00 - Bits 00000000
Register 0x4c - Value 0X00 - Bits 00000000
Register 0x4d - Value 0X00 - Bits 00000000
16:52:38.337 Send-Done
16:52:38.456 RegIrqFlags 00001000
16:52:38.472 Transmit-Done
The thread 0xfe4 has exited with code 0 (0x0).
The thread 0x100 has exited with code 0 (0x0).
16:52:43.391 Send-Done
16:52:43.465 RegIrqFlags 00001000
16:52:43.480 Transmit-Done
The thread 0xb94 has exited with code 0 (0x0).
16:52:48.475 Send-Done
16:52:48.550 RegIrqFlags 00001000
16:52:48.563 Transmit-Done
16:52:53.448 RegIrqFlags 01000110
16:52:53 Received 13 byte message Hello world:0
The thread 0x2b4 has exited with code 0 (0x0).
16:52:53.559 Send-Done
16:52:53.633 RegIrqFlags 00001000
16:52:53.648 Transmit-Done
16:52:54.577 RegIrqFlags 01000110
16:52:54 Received 13 byte message Hello world:1
16:52:55.706 RegIrqFlags 01000110
16:52:55 Received 13 byte message Hello world:2
16:52:56.836 RegIrqFlags 01000110
16:52:56 Received 13 byte message Hello world:3
16:52:57.965 RegIrqFlags 01000110
16:52:57 Received 13 byte message Hello world:4
The thread 0x354 has exited with code 0 (0x0).
16:52:58.634 Send-Done
16:52:58.709 RegIrqFlags 00001000
16:52:58.724 Transmit-Done
16:52:59.095 RegIrqFlags 01000110
16:52:59 Received 13 byte message Hello world:5
The program '[3736] backgroundTaskHost.exe' has exited with code -1
The Arduino code works though I need modify it so I can do more testing of the initialise method parameter options.
Most of the initialise method follows a similar pattern, checking parameters associated with a Register and only setting it if the cvalues are not all the default
I have just got to finish the code for RegFifoThresh, RegPacketConfig2 and the RegAesKey1-16 registers.
Other libraries for the RRFM69 support changing configuration while the application is running which significantly increases the complexity and number of test cases. My initial version will only support configuration on start-up.
I had been meaning to refactor the code for accessing the registers of the RFM69CW/RFM69HCW module (based on the Semtech SX1231/SX1231H) registers for a while.
Adafruit RFM69 Radio Bonnet
There is now a lot less code in the startup.cs file and the code for configuring the RFM69 is more obvious
/*
Copyright ® 2019 July devMobile Software, All Rights Reserved
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE
*/
namespace devMobile.IoT.Rfm69Hcw.RefactorRegisterManager
{
using System;
using System.Diagnostics;
using System.Text;
using System.Threading.Tasks;
using Windows.ApplicationModel.Background;
using Windows.Devices.Gpio;
sealed class Rfm69HcwDevice
{
private GpioPin InterruptGpioPin = null;
public RegisterManager RegisterManager = null; // Future refactor this will be made private
public Rfm69HcwDevice(ChipSelectPin chipSelectPin, int resetPin, int interruptPin)
{
RegisterManager = new RegisterManager(chipSelectPin);
// Factory reset pin configuration
GpioController gpioController = GpioController.GetDefault();
GpioPin resetGpioPin = gpioController.OpenPin(resetPin);
resetGpioPin.SetDriveMode(GpioPinDriveMode.Output);
resetGpioPin.Write(GpioPinValue.High);
Task.Delay(100);
resetGpioPin.Write(GpioPinValue.Low);
Task.Delay(10);
// Interrupt pin for RX message & TX done notification
InterruptGpioPin = gpioController.OpenPin(interruptPin);
resetGpioPin.SetDriveMode(GpioPinDriveMode.Input);
InterruptGpioPin.ValueChanged += InterruptGpioPin_ValueChanged;
}
private void InterruptGpioPin_ValueChanged(GpioPin sender, GpioPinValueChangedEventArgs args)
{
if (args.Edge != GpioPinEdge.RisingEdge)
{
return;
}
byte irqFlags = RegisterManager.ReadByte(0x28); // RegIrqFlags2
Debug.WriteLine("{0:HH:mm:ss.fff} RegIrqFlags {1}", DateTime.Now, Convert.ToString((byte)irqFlags, 2).PadLeft(8, '0'));
if ((irqFlags & 0b00000100) == 0b00000100) // PayLoadReady set
{
// Read the length of the buffer
byte numberOfBytes = RegisterManager.ReadByte(0x0);
// Allocate buffer for message
byte[] messageBytes = new byte[numberOfBytes];
for (int i = 0; i < numberOfBytes; i++)
{
messageBytes[i] = RegisterManager.ReadByte(0x00); // RegFifo
}
string messageText = UTF8Encoding.UTF8.GetString(messageBytes);
Debug.WriteLine("{0:HH:mm:ss} Received {1} byte message {2}", DateTime.Now, messageBytes.Length, messageText);
}
if ((irqFlags & 0b00001000) == 0b00001000) // PacketSent set
{
RegisterManager.WriteByte(0x01, 0b00010000); // RegOpMode set ReceiveMode
Debug.WriteLine("{0:HH:mm:ss.fff} Transmit-Done", DateTime.Now);
}
}
public void RegisterDump()
{
RegisterManager.Dump(0x0, 0x40);
}
}
public sealed class StartupTask : IBackgroundTask
{
private const int ResetPin = 25;
private const int InterruptPin = 22;
private Rfm69HcwDevice rfm69Device = new Rfm69HcwDevice(ChipSelectPin.CS1, ResetPin, InterruptPin);
const double RH_RF6M9HCW_FXOSC = 32000000.0;
const double RH_RFM69HCW_FSTEP = RH_RF6M9HCW_FXOSC / 524288.0;
public void Run(IBackgroundTaskInstance taskInstance)
{
//rfm69Device.RegisterDump();
// regOpMode standby
rfm69Device.RegisterManager.WriteByte(0x01, 0b00000100);
// BitRate MSB/LSB
rfm69Device.RegisterManager.WriteByte(0x03, 0x34);
rfm69Device.RegisterManager.WriteByte(0x04, 0x00);
// Frequency deviation
rfm69Device.RegisterManager.WriteByte(0x05, 0x02);
rfm69Device.RegisterManager.WriteByte(0x06, 0x3d);
// Calculate the frequency accoring to the datasheett
byte[] bytes = BitConverter.GetBytes((uint)(915000000.0 / RH_RFM69HCW_FSTEP));
Debug.WriteLine("Byte Hex 0x{0:x2} 0x{1:x2} 0x{2:x2} 0x{3:x2}", bytes[0], bytes[1], bytes[2], bytes[3]);
rfm69Device.RegisterManager.WriteByte(0x07, bytes[2]);
rfm69Device.RegisterManager.WriteByte(0x08, bytes[1]);
rfm69Device.RegisterManager.WriteByte(0x09, bytes[0]);
// RegRxBW
rfm69Device.RegisterManager.WriteByte(0x19, 0x2a);
// RegDioMapping1
rfm69Device.RegisterManager.WriteByte(0x26, 0x01);
// Setup preamble length to 16 (default is 3) RegPreambleMsb RegPreambleLsb
rfm69Device.RegisterManager.WriteByte(0x2C, 0x0);
rfm69Device.RegisterManager.WriteByte(0x2D, 0x10);
// RegSyncConfig Set the Sync length and byte values SyncOn + 3 custom sync bytes
rfm69Device.RegisterManager.WriteByte(0x2e, 0x90);
// RegSyncValues1 thru RegSyncValues3
rfm69Device.RegisterManager.WriteByte(0x2f, 0xAA);
rfm69Device.RegisterManager.WriteByte(0x30, 0x2D);
rfm69Device.RegisterManager.WriteByte(0x31, 0xD4);
// RegPacketConfig1 Variable length with CRC on
rfm69Device.RegisterManager.WriteByte(0x37, 0x90);
rfm69Device.RegisterDump();
while (true)
{
// Standby mode while loading message into FIFO
rfm69Device.RegisterManager.WriteByte(0x01, 0b00000100);
byte[] messageBuffer = UTF8Encoding.UTF8.GetBytes("hello world " + DateTime.Now.ToLongTimeString());
rfm69Device.RegisterManager.WriteByte(0x0, (byte)messageBuffer.Length);
rfm69Device.RegisterManager.Write(0x0, messageBuffer);
// Transmit mode once FIFO loaded
rfm69Device.RegisterManager.WriteByte(0x01, 0b00001100);
Debug.WriteLine("{0:HH:mm:ss.fff} Send-Done", DateTime.Now);
Task.Delay(5000).Wait();
}
}
}
}
I’ll modify the constructor reset pin support to see if I can get the Seegel Systeme hat working.
Register dump
Register 0x00 - Value 0X00 - Bits 00000000
Register 0x01 - Value 0X04 - Bits 00000100
Register 0x02 - Value 0X00 - Bits 00000000
Register 0x03 - Value 0X34 - Bits 00110100
…
Register 0x3e - Value 0X00 - Bits 00000000
Register 0x3f - Value 0X00 - Bits 00000000
Register 0x40 - Value 0X00 - Bits 00000000
19:58:52.828 Send-Done
19:58:53.022 RegIrqFlags 00001000
19:58:53.036 Transmit-Done
19:58:54.188 RegIrqFlags 01000110
19:58:54 Received 14 byte message Hello world:1
The thread 0xa10 has exited with code 0 (0x0).
The thread 0xf90 has exited with code 0 (0x0).
19:58:57.652 RegIrqFlags 01000110
19:58:57 Received 14 byte message Hello world:2
19:58:57.892 Send-Done
19:58:58.039 RegIrqFlags 00001000
19:58:58.053 Transmit-Done
19:59:01.115 RegIrqFlags 01000110
19:59:01 Received 14 byte message Hello world:3
19:59:02.936 Send-Done
19:59:03.083 RegIrqFlags 00001000
19:59:03.096 Transmit-Done
19:59:04.577 RegIrqFlags 01000110
19:59:04 Received 14 byte message Hello world:4
The thread 0xa5c has exited with code 0 (0x0).
19:59:08.001 Send-Done
19:59:08.122 RegIrqFlags 01001000
19:59:08.139 Transmit-Done
19:59:11.504 RegIrqFlags 01000110
19:59:11 Received 14 byte message Hello world:6
The thread 0xb18 has exited with code 0 (0x0).
19:59:13.079 Send-Done
19:59:13.226 RegIrqFlags 00001000
19:59:13.240 Transmit-Done
19:59:14.966 RegIrqFlags 01000110
19:59:14 Received 14 byte message Hello world:7
Based how my rate of progress improved when I did this on the RFM9X library I really should have done this much earlier.