From 374393fe9be027f16e2ccae5eae729606da72c7d Mon Sep 17 00:00:00 2001
From: Davide Mor <davide.mor@skywarder.eu>
Date: Wed, 10 Aug 2022 19:51:02 +0200
Subject: [PATCH] [cc3135] Added SPI to entrypoint and updated readHeader to
 only use >4 bytes reads

---
 src/shared/radio/CC3135/CC3135.cpp       | 65 +++++++++++++++------
 src/tests/drivers/CC3135/test-cc3135.cpp | 73 ++++++++++++++++++------
 2 files changed, 103 insertions(+), 35 deletions(-)

diff --git a/src/shared/radio/CC3135/CC3135.cpp b/src/shared/radio/CC3135/CC3135.cpp
index a6f998568..3e01fe575 100644
--- a/src/shared/radio/CC3135/CC3135.cpp
+++ b/src/shared/radio/CC3135/CC3135.cpp
@@ -189,7 +189,7 @@ void CC3135::readPacket(OpCode opcode, CC3135::Buffer command,
 
         // Locking the interface is not needed
 
-        ResponseHeader header;
+        ResponseHeader header = {};
         readHeader(&header);
 
         if (header.inner.opcode != opcode)
@@ -238,10 +238,10 @@ void CC3135::readHeader(ResponseHeader *header)
                      SYNC_PATTERN_LEN);
     }
 
-    uint8_t buf[4];
+    uint8_t buf[sizeof(ResponseHeader)];
 
     // 2. Read initial data from the device
-    iface->read(&buf[0], 4);
+    iface->read(&buf[0], 8);
 
     /*
     Here the TI driver does some weird stuff.
@@ -251,28 +251,59 @@ void CC3135::readHeader(ResponseHeader *header)
     // 3. Scan for device SYNC
     while (true)
     {
-        // Try and find the SYNC here
-        uint32_t sync;
-        memcpy(&sync, &buf[0], 4);
+        // Reads in SPI are always more than 4 bytes
+
+        bool found = false;
+        int i;
+        for (i = 0; i < 4; i++)
+        {
+            // Try and find the SYNC here
+            uint32_t sync;
+            memcpy(&sync, &buf[i], 4);
+
+            if (sync == 0x0a7b2d01)
+            {
+                // BRUH
+            }
+
+            if (n2hSyncPatternMatch(sync, tx_seq_num))
+            {
+                found = true;
+                break;
+            }
+        }
 
-        if (n2hSyncPatternMatch(sync, tx_seq_num))
+        // Shift everything and read more data
+        if (found)
+        {
+            memmove(&buf[0], &buf[4 + i], 4 - i);
+            iface->read(&buf[4 - i], 4 + i);
             break;
+        }
+        else
+        {
+            memmove(&buf[0], &buf[4], 4);
+            iface->read(&buf[4], 4);
+        }
+    }
 
-        // TODO: The TI driver reads 4 bytes at a time, is this also good?
+    // 4. Scan for double syncs
+    /*uint32_t sync;
+    memcpy(&sync, &buf[0], 4);
+    while(n2hSyncPatternMatch(sync, tx_seq_num)) {
+        memmove(&buf[0], &buf[4], 4);
+        iface->read(&buf[4], 4);
 
-        // Shift everything
-        memmove(&buf[0], &buf[1], 3);
-        iface->read(&buf[3], 1);
-    }
+        memcpy(&sync, &buf[0], 4);
+    }*/
 
     tx_seq_num++;
 
-    // TODO: Is skipping double sync detection good?
-
-    // 4. Read initial header
-    iface->read(reinterpret_cast<uint8_t *>(header), sizeof(ResponseHeader));
+    // 5. Read rest of the header
+    iface->read(&buf[8], sizeof(ResponseHeader) - 8);
+    memcpy(header, &buf[0], sizeof(ResponseHeader));
 
-    // 5. Adjust for bigger response header
+    // 6. Adjust for bigger response header
     header->inner.len -= sizeof(ResponseHeader) - sizeof(GenericHeader);
 }
 
diff --git a/src/tests/drivers/CC3135/test-cc3135.cpp b/src/tests/drivers/CC3135/test-cc3135.cpp
index f41bd3682..a1979a396 100644
--- a/src/tests/drivers/CC3135/test-cc3135.cpp
+++ b/src/tests/drivers/CC3135/test-cc3135.cpp
@@ -45,8 +45,10 @@ using miso = interfaces::spi1::miso;
 using mosi = interfaces::spi1::mosi;
 using cs   = peripherals::cc3135::cs;
 using irq  = peripherals::cc3135::intr;
+using hib  = peripherals::cc3135::hib;
 
 #define CC3135_SPI SPI1
+#define CC3135_HIB
 #elif defined _BOARD_STM32F429ZI_SKYWARD_DEATHST_V3
 #include "interfaces-impl/hwmapping.h"
 using sck  = interfaces::spi6::sck;
@@ -57,6 +59,9 @@ using irq  = sensors::cc3135::intr;
 
 #define CC3135_SPI SPI6
 #else
+
+// Use UART
+#if 0
 using tx  = Gpio<GPIOA_BASE, 2>;
 using rx  = Gpio<GPIOA_BASE, 3>;
 using irq = Gpio<GPIOA_BASE, 4>;
@@ -64,6 +69,18 @@ using hib = Gpio<GPIOA_BASE, 5>;
 
 #define CC3135_UART USART2
 #define CC3135_HIB
+#else
+using sck  = Gpio<GPIOA_BASE, 5>;
+using miso = Gpio<GPIOB_BASE, 4>;
+using mosi = Gpio<GPIOA_BASE, 7>;
+using cs   = Gpio<GPIOD_BASE, 4>;
+using irq  = Gpio<GPIOA_BASE, 4>;
+using hib  = Gpio<GPIOA_BASE, 6>;
+
+#define CC3135_SPI SPI1
+#define CC3135_HIB
+#endif
+
 #endif
 
 CC3135 *cc3135 = nullptr;
@@ -85,7 +102,8 @@ void __attribute__((used)) EXTI4_IRQHandlerImpl()
 
 void initBoard()
 {
-#ifdef CC3135_HIB
+#if (defined CC3135_HIB && !defined _BOARD_STM32F429ZI_SKYWARD_GS && \
+     !defined _BOARD_STM32F429ZI_SKYWARD_DEATHST_V3)
     {
         miosix::FastInterruptDisableLock dLock;
 
@@ -107,6 +125,24 @@ void initBoard()
     }
 #endif
 
+#if (defined CC3135_SPI && !defined _BOARD_STM32F429ZI_SKYWARD_GS && \
+     !defined _BOARD_STM32F429ZI_SKYWARD_DEATHST_V3)
+    {
+        miosix::FastInterruptDisableLock dLock;
+
+        miso::mode(miosix::Mode::ALTERNATE);
+        miso::alternateFunction(5);
+        mosi::mode(miosix::Mode::ALTERNATE);
+        mosi::alternateFunction(5);
+        sck::mode(miosix::Mode::ALTERNATE);
+        sck::alternateFunction(5);
+        irq::mode(miosix::Mode::INPUT);
+        cs::mode(miosix::Mode::OUTPUT);
+    }
+
+    cs::high();
+#endif
+
     auto irq_pin = irq::getPin();
     enableExternalInterrupt(irq_pin.getPort(), irq_pin.getNumber(),
                             InterruptTrigger::RISING_EDGE);
@@ -115,7 +151,7 @@ void initBoard()
 int main()
 {
     // IRQ watcher thread
-    /*std::thread _watcher(
+    std::thread _watcher(
         []()
         {
             size_t last = -1;
@@ -123,35 +159,27 @@ int main()
             {
                 if (last != IRQ_COUNT)
                 {
-                    printf("[cc3135] IRQ: %d\n", IRQ_COUNT);
+                    printf("[cc3135] IRQ: %zu\n", IRQ_COUNT);
                     last = IRQ_COUNT;
                 }
 
                 // Sleep to avoid CPU hogging
                 Thread::sleep(10);
             }
-        });*/
+        });
 
     initBoard();
 
-#ifdef CC3135_HIB
-    // Reset CC3135
-    hib::low();
-    Thread::sleep(10);
-    hib::high();
-
-    // Wait for the device to fully initialize.
-    // The device is very chatty at the beginning,
-    // but it's also in a weird state where the IRQ
-    // pin doesn't trigger properly. Just wait for it to calm down
-    Thread::sleep(2000);
-#endif
-
 #ifdef CC3135_SPI
     SPIBus bus(CC3135_SPI);
     GpioPin cs_pin = cs::getPin();
 
-    std::unique_ptr<ICC3135Iface> iface(new CC3135Spi(bus, cs_pin, {}));
+    SPIBusConfig config = {};
+    config.clockDivider = SPI::ClockDivider::DIV_128;
+    config.mode         = SPI::Mode::MODE_0;
+    config.bitOrder     = SPI::BitOrder::MSB_FIRST;
+
+    std::unique_ptr<ICC3135Iface> iface(new CC3135Spi(bus, cs_pin, config));
 #endif
 
 #ifdef CC3135_UART
@@ -162,6 +190,15 @@ int main()
     cc3135 = new CC3135(std::move(iface));
     printf("[cc3135] Initialization complete!\n");
 
+#ifdef CC3135_HIB
+    // Reset CC3135
+    hib::low();
+    Thread::sleep(10);
+    hib::high();
+#endif
+
+    Thread::sleep(3000);
+
     auto version = cc3135->getVersion();
     printf(
         "[cc3135] Chip Id: %lx\n"
-- 
GitLab