From 1715f67c620f4d320891ddcd527b2c7827a52707 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= <ondra@ondrovo.com>
Date: Tue, 22 May 2018 14:26:23 +0200
Subject: [PATCH] now it works

---
 Inc/main.h        |  5 +++
 Inc/nrf.h         |  7 +++--
 Src/gex_gateway.c | 12 ++++---
 Src/gpio.c        | 80 ++++++++++++++++++++++++++---------------------
 Src/main.c        | 25 ++++++++++++---
 Src/nrf.c         |  7 +++--
 6 files changed, 86 insertions(+), 50 deletions(-)

diff --git a/Inc/main.h b/Inc/main.h
index f6a2c98..da9d4bd 100644
--- a/Inc/main.h
+++ b/Inc/main.h
@@ -71,6 +71,9 @@
 /* USER CODE BEGIN Includes */
 
 /* USER CODE END Includes */
+extern uint32_t led_tx_countdown;
+extern uint32_t led_rx_countdown;
+#define DATA_FLASH_TIME 5000
 
 /* Private define ------------------------------------------------------------*/
 
@@ -87,6 +90,8 @@
 #define NRF_NSS_GPIO_Port GPIOA
 #define RENUM_Pin LL_GPIO_PIN_0
 #define RENUM_GPIO_Port GPIOA
+#define NRF_RESET_Pin LL_GPIO_PIN_9
+#define NRF_RESET_GPIO_Port GPIOB
 
 /* ########################## Assert Selection ############################## */
 /**
diff --git a/Inc/nrf.h b/Inc/nrf.h
index 27c5b54..34de77a 100644
--- a/Inc/nrf.h
+++ b/Inc/nrf.h
@@ -6,7 +6,7 @@
 #define GEX_NRF_NRF_H
 
 /*
- * nordic.h
+ * nordic.h - adapted from http://barefootelectronics.com/NRF24L01.aspx
  *
  * Created:12/16/2013 3:36:04 PM
  *  Author: Tom
@@ -21,8 +21,11 @@
 
 #include "main.h"
 
+#define NRF_CHANNEL 76
+
+
 #define dbg_nrf(...) do{}while(0)
-//#define dbg_nrf(...) dbg(##__VA_ARGS__)
+//#define dbg_nrf dbg
 
 // Initialize SPI and the Nordic
 
diff --git a/Src/gex_gateway.c b/Src/gex_gateway.c
index d32bd57..6f3390e 100644
--- a/Src/gex_gateway.c
+++ b/Src/gex_gateway.c
@@ -135,7 +135,6 @@ void handle_txframe_chunk(const uint8_t *buffer, uint16_t size)
             }
         }
 
-        LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
         cmd_state = CMD_STATE_IDLE;
     }
 }
@@ -173,6 +172,7 @@ void gw_handle_usb_out(uint8_t *buffer)
                 }
 
                 LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDTX_Pin);
+                led_tx_countdown = DATA_FLASH_TIME;
                 start_slave_cmd(slave_addr, frame_len, cksum);
                 dbg_nrf("Collecting frame for slave %02x: %d bytes", (int)slave_addr, (int)frame_len);
                 cmd_state = CMD_STATE_TXMSG;
@@ -188,7 +188,7 @@ void gw_handle_usb_out(uint8_t *buffer)
         }
     }
     else if (cmd_state == CMD_STATE_TXMSG) {
-        LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDTX_Pin);
+        led_tx_countdown = DATA_FLASH_TIME;
         handle_txframe_chunk(buffer, 64);
     }
 }
@@ -276,10 +276,9 @@ void gw_setup_radio(void)
     NRF_ModeRX(); // base state is RX
 }
 
-void EXTI2_IRQHandler(void)
+void EXTI1_IRQHandler(void)
 {
-    LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_2);
-    LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDRX_Pin);
+    LL_EXTI_ClearFlag_0_31(LL_EXTI_LINE_1);
 
     struct msg_data m;
     m.msg_type = MSG_TYPE_DATA;
@@ -290,6 +289,9 @@ void EXTI2_IRQHandler(void)
         dbg("IRQ but no msg!");
     }
     else {
+        LL_GPIO_SetOutputPin(LED_GPIO_Port, LEDRX_Pin);
+        led_rx_countdown = DATA_FLASH_TIME;
+
         dbg_nrf("Msg RXd from nordic!");
 
         m.dev_addr = NRF_PipeNum2Addr(pipenum);
diff --git a/Src/gpio.c b/Src/gpio.c
index 990f17d..cecb3f6 100644
--- a/Src/gpio.c
+++ b/Src/gpio.c
@@ -70,54 +70,62 @@
 void MX_GPIO_Init(void)
 {
 
-  LL_EXTI_InitTypeDef EXTI_InitStruct;
-  LL_GPIO_InitTypeDef GPIO_InitStruct;
+    LL_EXTI_InitTypeDef EXTI_InitStruct;
+    LL_GPIO_InitTypeDef GPIO_InitStruct;
 
-  /* GPIO Ports Clock Enable */
-  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
-  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
-  LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
+    /* GPIO Ports Clock Enable */
+    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
+    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
+    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
+    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
 
-  /**/
-  LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED1_Pin);
-  LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
-  LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
-  LL_GPIO_SetOutputPin(RENUM_GPIO_Port, RENUM_Pin);
+    /**/
 
-  /**/
+    /**/
 //  LL_GPIO_ResetOutputPin(GPIOA, NRF_CE_Pin|NRF_NSS_Pin);
 
-  /**/
-  GPIO_InitStruct.Pin = LED1_Pin|LEDRX_Pin|LEDTX_Pin;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
-
-  /**/
-  GPIO_InitStruct.Pin = NRF_CE_Pin|NRF_NSS_Pin|RENUM_Pin;
-  GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
-  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
-  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
-  LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+    /**/
+    GPIO_InitStruct.Pin = LED1_Pin | LEDRX_Pin | LEDTX_Pin;
+    GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
+    GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
+    GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+    LL_GPIO_Init(LED_GPIO_Port, &GPIO_InitStruct);
+
+    /**/
+    GPIO_InitStruct.Pin = NRF_CE_Pin | NRF_NSS_Pin | RENUM_Pin;
+    GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
+    GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
+    GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+    LL_GPIO_Init(GPIOA, &GPIO_InitStruct);
+
+    GPIO_InitStruct.Pin = NRF_RESET_Pin;
+    GPIO_InitStruct.Mode = LL_GPIO_MODE_OUTPUT;
+    GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
+    GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
+    LL_GPIO_Init(NRF_RESET_GPIO_Port, &GPIO_InitStruct);
 
 #if 1
-  /**/
-  LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE1); // IRQ on PA1
+    /**/
+    LL_GPIO_AF_SetEXTISource(LL_GPIO_AF_EXTI_PORTA, LL_GPIO_AF_EXTI_LINE1); // IRQ on PA1
 
-  /**/
-  EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_1;
-  EXTI_InitStruct.LineCommand = ENABLE;
-  EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
-  EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
-  LL_EXTI_Init(&EXTI_InitStruct);
+    /**/
+    EXTI_InitStruct.Line_0_31 = LL_EXTI_LINE_1;
+    EXTI_InitStruct.LineCommand = ENABLE;
+    EXTI_InitStruct.Mode = LL_EXTI_MODE_IT;
+    EXTI_InitStruct.Trigger = LL_EXTI_TRIGGER_FALLING;
+    LL_EXTI_Init(&EXTI_InitStruct);
 
-  NVIC_EnableIRQ(EXTI2_IRQn);
+    NVIC_EnableIRQ(EXTI1_IRQn);
 
-  /**/
-  LL_GPIO_SetPinMode(NRF_IRQ_GPIO_Port, NRF_IRQ_Pin, LL_GPIO_MODE_FLOATING);
+    /**/
+    LL_GPIO_SetPinMode(NRF_IRQ_GPIO_Port, NRF_IRQ_Pin, LL_GPIO_MODE_FLOATING);
 #endif
 
+    LL_GPIO_ResetOutputPin(LED_GPIO_Port, LED1_Pin);
+    LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
+    LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
+    LL_GPIO_SetOutputPin(RENUM_GPIO_Port, RENUM_Pin);
+    LL_GPIO_SetOutputPin(NRF_RESET_GPIO_Port, NRF_RESET_Pin);
 }
 
 /* USER CODE BEGIN 2 */
diff --git a/Src/main.c b/Src/main.c
index 59f127c..613ea65 100644
--- a/Src/main.c
+++ b/Src/main.c
@@ -61,6 +61,9 @@
 
 /* USER CODE BEGIN Includes */
 
+uint32_t led_tx_countdown = 0;
+uint32_t led_rx_countdown = 0;
+
 /* USER CODE END Includes */
 
 /* Private variables ---------------------------------------------------------*/
@@ -82,7 +85,7 @@ void SystemClock_Config(void);
 
 /* USER CODE END 0 */
 
-extern void EXTI2_IRQHandler(void);
+extern void EXTI1_IRQHandler(void);
 
 /**
   * @brief  The application entry point.
@@ -113,6 +116,7 @@ int main(void)
 
     LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOC);
     LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOD);
+    LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOB);
     LL_APB2_GRP1_EnableClock(LL_APB2_GRP1_PERIPH_GPIOA);
 
     /* Initialize all configured peripherals */
@@ -123,6 +127,8 @@ int main(void)
     MX_USART1_UART_Init();
     /* USER CODE BEGIN 2 */
 
+    // give juice to the nrf module
+    LL_GPIO_ResetOutputPin(NRF_RESET_GPIO_Port, NRF_RESET_Pin);
     /* USER CODE END 2 */
 
     gw_setup_radio();
@@ -136,7 +142,7 @@ int main(void)
     dbg("Main loop starts.");
     /* Infinite loop */
     /* USER CODE BEGIN WHILE */
-    int cnt = 0;
+    uint32_t cnt = 0;
     uint8_t buff[MQ_SLOT_LEN];
 
     while (1) {
@@ -149,13 +155,22 @@ int main(void)
             cnt = 0;
         }
 
+        if (led_tx_countdown > 0) {
+            if (--led_tx_countdown == 0) {
+                LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDTX_Pin);
+            }
+        }
+
+        if (led_rx_countdown > 0) {
+            if (--led_rx_countdown == 0) {
+                LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
+            }
+        }
+
         if (mq_can_read(&usb_inq)) {
             if (!usb_tx_busy) {
                 mq_read(&usb_inq, buff);
                 CDC_Transmit_FS(buff, MQ_SLOT_LEN);
-
-                // blinking - maybe this will work .. not sure
-                LL_GPIO_ResetOutputPin(LED_GPIO_Port, LEDRX_Pin);
             }
         }
     }
diff --git a/Src/nrf.c b/Src/nrf.c
index 829e032..26b167d 100644
--- a/Src/nrf.c
+++ b/Src/nrf.c
@@ -492,6 +492,7 @@ void NRF_Init(uint8_t pSpeed)
     NSS(1);
     CE(0);
 
+    dbg_nrf("Waiting for module start...");
     LL_mDelay(200);
 
     for (int i = 0; i < 6; i++) {
@@ -499,11 +500,12 @@ void NRF_Init(uint8_t pSpeed)
         nrf_pipe_enabled[i] = 0;
     }
 
+    dbg_nrf("init regs");
     // clear flags etc
     NRF_PowerDown();
     NRF_FlushRx();
     NRF_FlushTx();
-    NRF_WriteRegister(RG_STATUS, 0x70);
+    NRF_WriteRegister(RG_STATUS, 0x70); // this will fail to verify, that's OK
 
     NRF_WriteRegister(RG_CONFIG, ModeBits);
     NRF_WriteRegister(RG_SETUP_AW, 0b11);             // 5 byte addresses
@@ -511,7 +513,7 @@ void NRF_Init(uint8_t pSpeed)
     NRF_WriteRegister(RG_EN_RXADDR, 0x01); // disable all except 1 which we'll assign later
 
     NRF_WriteRegister(RG_SETUP_RETR, 0x18);        // 8 retries, 500 ms
-    NRF_WriteRegister(RG_RF_CH, 76);                // channel
+    NRF_WriteRegister(RG_RF_CH, NRF_CHANNEL);                // channel
 
     NRF_WriteRegister(RG_RF_SETUP, pSpeed);
 
@@ -521,4 +523,5 @@ void NRF_Init(uint8_t pSpeed)
 //    for (int i = 0; i < 6; i++) {
 //        NRF_WriteRegister(RG_RX_PW_P0+i, 32);      // Receive 32 byte packets - XXX this is probably not needed with dynamic length
 //    }
+    dbg_nrf("nrf init finished");
 }