Explorar el Código

Document some radio stuff. Make it easier to switch between 4- and 5-byte syncwords length

Christopher Mullins hace 6 años
padre
commit
a504a45b79
Se han modificado 2 ficheros con 61 adiciones y 10 borrados
  1. 33 9
      lib/Radio/MiLightRadioConfig.h
  2. 28 1
      lib/Radio/PL1167_nRF24.cpp

+ 33 - 9
lib/Radio/MiLightRadioConfig.h

@@ -11,6 +11,23 @@
 class MiLightRadioConfig {
 public:
   static const size_t NUM_CHANNELS = 3;
+
+  // We can set this to two possible values.  It only has an affect on the nRF24 radio.  The
+  // LT8900/PL1167 radio will always use the raw syncwords.  For the nRF24, this controls what
+  // we set the "address" to, which roughly corresponds to the LT8900 syncword.
+  //
+  // The PL1167 packet is structured as follows (lengths in bits):
+  //  Preamble ( 8) | Syncword (32) | Trailer ( 4) | Packet Len ( 8) | Packet (...)
+  //
+  // 4 -- Use the raw syncword bits as the address.  This means the Trailer will be included in
+  //      the packet data.  Since the Trailer is 4 bits, packet data will not be byte-aligned,
+  //      and the data must be bitshifted every time it's received.
+  //
+  // 5 -- Include the Trailer in the syncword.  Avoids us needing to bitshift packet data. The
+  //      downside is that the Trailer is hardcoded and assumed based on received packets.
+  //
+  // In general, this should be set to 5 unless packets that should be showing up are
+  // mysteriously not present.
   static const uint8_t SYNCWORD_LENGTH = 5;
 
   MiLightRadioConfig(
@@ -35,15 +52,22 @@ public:
     // precompute the syncword for the nRF24.  we include the fixed preamble and trailer in the
     // syncword to avoid needing to bitshift packets.  trailer is 4 bits, so the actual syncword
     // is no longer byte-aligned.
-    syncwordBytes[ --ix ] = reverseBits(
-      ((syncword0 << 4) & 0xF0) | (preamble & 0x0F)
-    );
-    syncwordBytes[ --ix ] = reverseBits((syncword0 >> 4) & 0xFF);
-    syncwordBytes[ --ix ] = reverseBits(((syncword0 >> 12) & 0x0F) + ((syncword3 << 4) & 0xF0));
-    syncwordBytes[ --ix ] = reverseBits((syncword3 >> 4) & 0xFF);
-    syncwordBytes[ --ix ] = reverseBits(
-      ((syncword3 >> 12) & 0x0F) | ((trailer << 4) & 0xF0)
-    );
+    if (SYNCWORD_LENGTH == 5) {
+      syncwordBytes[ --ix ] = reverseBits(
+        ((syncword0 << 4) & 0xF0) | (preamble & 0x0F)
+      );
+      syncwordBytes[ --ix ] = reverseBits((syncword0 >> 4) & 0xFF);
+      syncwordBytes[ --ix ] = reverseBits(((syncword0 >> 12) & 0x0F) + ((syncword3 << 4) & 0xF0));
+      syncwordBytes[ --ix ] = reverseBits((syncword3 >> 4) & 0xFF);
+      syncwordBytes[ --ix ] = reverseBits(
+        ((syncword3 >> 12) & 0x0F) | ((trailer << 4) & 0xF0)
+      );
+    } else {
+      syncwordBytes[ --ix ] = reverseBits(syncword0 & 0xff);
+      syncwordBytes[ --ix ] = reverseBits( (syncword0 >> 8) & 0xff);
+      syncwordBytes[ --ix ] = reverseBits(syncword3 & 0xff);
+      syncwordBytes[ --ix ] = reverseBits( (syncword3 >> 8) & 0xff);
+    }
   }
 
   uint8_t channels[3];

+ 28 - 1
lib/Radio/PL1167_nRF24.cpp

@@ -38,6 +38,11 @@ int PL1167_nRF24::recalc_parameters() {
   // +2 for CRC
   int packet_length = _maxPacketLength + 2;
 
+  // Read an extra byte if we don't include the trailer in the syncword
+  if (_syncwordLength < 5) {
+    ++packet_length;
+  }
+
   if (packet_length > sizeof(_packet) || nrf_address_length < 3) {
     return -1;
   }
@@ -173,6 +178,28 @@ int PL1167_nRF24::internal_receive() {
   // HACK HACK HACK: Reset radio
   open();
 
+// Currently, the syncword width is set to 5 in order to include the
+// PL1167 trailer.  The trailer is 4 bits, which pushes packet data
+// out of byte-alignment.
+//
+// The following code reads un-byte-aligned packet data.
+//
+// #ifdef DEBUG_PRINTF
+//   Serial.printf_P(PSTR("Packet received (%d bytes) RAW: "), outp);
+//   for (int i = 0; i < _receive_length; i++) {
+//     Serial.printf_P(PSTR("%02X "), tmp[i]);
+//   }
+//   Serial.print(F("\n"));
+// #endif
+//
+//   uint16_t buffer = tmp[0];
+//
+//   for (int inp = 1; inp < _receive_length; inp++) {
+//     uint8_t currentByte = tmp[inp];
+//     tmp[outp++] = reverseBits((buffer << 4) | (currentByte >> 4));
+//     buffer = (buffer << 8) | currentByte;
+//   }
+
   for (int inp = 0; inp < _receive_length; inp++) {
     tmp[outp++] = reverseBits(tmp[inp]);
   }
@@ -197,7 +224,7 @@ int PL1167_nRF24::internal_receive() {
 
   if ( crc != recvCrc ) {
 #ifdef DEBUG_PRINTF
-    Serial.printf_P(PSTR("Failed CRC: expected %04X, got %04X"), crc, recvCrc);
+    Serial.printf_P(PSTR("Failed CRC: expected %04X, got %04X\n"), crc, recvCrc);
 #endif
     return 0;
   }