Browse Source

Added transceiver files

Thomas Chef 7 tháng trước cách đây
mục cha
commit
9f4b9f3b50
7 tập tin đã thay đổi với 285 bổ sung9 xóa
  1. 1 0
      main/CMakeLists.txt
  2. 7 5
      main/config.h
  3. 18 4
      main/main.c
  4. 120 0
      main/rxTimer.c
  5. 18 0
      main/rxTimer.h
  6. 91 0
      main/transceiver.c
  7. 30 0
      main/transceiver.h

+ 1 - 0
main/CMakeLists.txt

@@ -2,4 +2,5 @@
 set(COMPONENT_SRCDIRS ".")
 set(COMPONENT_ADD_INCLUDEDIRS ".")
 
+
 register_component()

+ 7 - 5
main/config.h

@@ -6,13 +6,15 @@
 #define SW_VERSION "1.0"
 
 // These defines configures which code to generate (to save download time during development)
-#define WIFI_ENABLED
-#define MQTT_ENABLED
-#define ENABLE_DS18B20
 
-//#define MQTT_DEBUG              // Add an extra debug string to the beginning of the topic-string
+#define RX_TIMER_ENABLED
+//#define TRANCEIVER_ENABLED
+
+//#define WIFI_ENABLED
+//#define MQTT_ENABLED
+#define ENABLE_DS18B20
 
-#define KWH_COUNTER_INPUT_IO      GPIO_NUM_16   // Input
+#define MQTT_DEBUG              // Add an extra debug string to the beginning of the topic-string
 
 #define ONE_WIRE_BUS_IO           GPIO_NUM_26   // Temp sensors
 

+ 18 - 4
main/main.c

@@ -5,25 +5,39 @@
 #include "wifi.h"
 #include "mqtt.h"
 #include "readTemps.h"
+#include "rxTimer.h"
 
 
 // Chip info:
 // This is esp32 chip with 2 CPU cores, WiFi/BT/BLE, silicon revision 1, 4MB external flash
 
+void getChipInfo(void) {
+    esp_chip_info_t chip_info;
+    esp_chip_info(&chip_info);
+
+    ESP_LOGI("MAIN", "Model: %s", chip_info.model == CHIP_ESP32 ? "ESP32" : "Unknown");
+    ESP_LOGI("MAIN", "Features: WiFi%s%s, %d cores",
+             (chip_info.features & CHIP_FEATURE_BT) ? "/BT" : "",
+             (chip_info.features & CHIP_FEATURE_BLE) ? "/BLE" : "",
+             chip_info.cores);
+    ESP_LOGI("MAIN", "Revision: %d", chip_info.revision);
+}
+
 
 void app_main(void)
 {
     ESP_LOGI("MAIN", "GarageTransceiver ESP32. Core:%d",xPortGetCoreID());
+    getChipInfo();
 
     // Wait for stable environment
     vTaskDelay(2000.0 / portTICK_PERIOD_MS);
 
-#ifdef ENABLE_DS18B20
-    initTempReadings();
+#ifdef RX_TIMER_ENABLED
+    rxTimerInit();          // First we start the Timer (which samples Rx and handles uS-delays)
 #endif
 
-#ifdef ENABLE_KWH_COUNTER
-    kWhCounter_init();
+#ifdef ENABLE_DS18B20
+    initTempReadings();
 #endif
 
 #ifdef WIFI_ENABLED

+ 120 - 0
main/rxTimer.c

@@ -0,0 +1,120 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "esp_sleep.h"
+#include "esp_log.h"
+#include "driver/uart.h"
+#include "driver/rtc_io.h"
+#include "esp_intr_alloc.h"
+#include "esp_attr.h"
+#include "driver/timer.h"
+#include <stddef.h>
+#include "esp_intr_alloc.h"
+#include "transceiver.h"
+
+static intr_handle_t s_timer_handle;
+
+static bool timerTaskIsDone = false;
+
+static volatile uint32_t tenUsCounter = 0;  
+
+QueueHandle_t rxQueue = NULL;
+
+static uint32_t millisCnt=0;
+
+uint32_t millis() { return millisCnt; }
+
+
+static void timer_tg0_isr(void* arg)
+{
+    static uint32_t subMillisCounter=100;
+
+    if( subMillisCounter-- == 0 ) {
+        millisCnt++;
+        subMillisCounter=100;
+    }
+
+    tenUsCounter++;
+
+    static uint8_t currValue = 0;
+  
+    static uint16_t samples = 0;
+    static uint16_t newSamples = 0;
+    
+    // Sample the pin value
+    uint8_t value = (uint8_t)gpio_get_level(GPIO_RX_DATA);
+
+    if( value == currValue ) {
+        samples++;
+        samples+=newSamples;
+        newSamples=0;
+    }
+    else {
+        newSamples++;
+    }
+    
+    if( newSamples == 6 ) {
+
+        const uint32_t dataToSend = samples * 10;
+
+        if( disableRx == false ) {
+            xQueueSend( rxQueue, &dataToSend, 0 );
+        }
+        
+        samples = newSamples;
+        newSamples = 0;
+        currValue = value;    
+    }
+
+    //Reset irq and set for next time
+    TIMERG1.int_clr_timers.t1 = 1;
+    TIMERG1.hw_timer[1].config.alarm_en = 1;
+}
+
+static void timerSetupTask(void *pvParameter)
+{
+    rxQueue = xQueueCreate( 1000, sizeof( uint32_t ) );
+
+    timer_config_t config = {
+        .alarm_en = true,				//Alarm Enable
+        .counter_en = false,			//If the counter is enabled it will start incrementing / decrementing immediately after calling timer_init()
+        .intr_type = TIMER_INTR_LEVEL,	//Is interrupt is triggered on timer’s alarm (timer_intr_mode_t)
+        .counter_dir = TIMER_COUNT_UP,	//Does counter increment or decrement (timer_count_dir_t)
+        .auto_reload = true,			//If counter should auto_reload a specific initial value on the timer’s alarm, or continue incrementing or decrementing.
+        .divider = 8   				//Divisor of the incoming 160 MHz (12.5nS) APB_CLK clock. E.g. 800 = 10uS per timer tick
+    };
+
+    timer_init(TIMER_GROUP_1, TIMER_1, &config);
+    timer_set_counter_value(TIMER_GROUP_1, TIMER_1, 0);
+    timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 100);     // Here we set the ISR-Value
+    timer_enable_intr(TIMER_GROUP_1, TIMER_1);
+    timer_isr_register(TIMER_GROUP_1, TIMER_1, &timer_tg0_isr, NULL, 0, &s_timer_handle);
+
+    timer_start(TIMER_GROUP_1, TIMER_1);
+
+    
+
+    timerTaskIsDone = true;
+    vTaskDelete(NULL);
+}
+
+void rxTimerInit()
+{
+    xTaskCreatePinnedToCore(&timerSetupTask, "timerSetupTask", 8192, NULL, tskIDLE_PRIORITY + 2, NULL, 1);
+
+    // Wait until task has finished
+    while(timerTaskIsDone == false) vTaskDelay( 50 / portTICK_PERIOD_MS );
+
+    ESP_LOGI("rxTimer", "10uS Timer has been started.");
+}
+
+void delayMicroseconds(uint32_t delay)
+{
+	volatile uint32_t endtime = tenUsCounter + ((uint32_t)delay / 10);
+
+	while( tenUsCounter < endtime ) ;
+}

+ 18 - 0
main/rxTimer.h

@@ -0,0 +1,18 @@
+#ifndef __RX_TIMER__
+#define __RX_TIMER__
+
+#include "freertos/FreeRTOS.h"
+#include "freertos/queue.h"
+#include <stdint.h>
+
+void rxTimerInit();
+
+void delayMicroseconds(uint32_t delay);
+
+extern QueueHandle_t rxQueue;
+
+uint32_t millis();
+
+
+
+#endif

+ 91 - 0
main/transceiver.c

@@ -0,0 +1,91 @@
+#include <stdio.h>
+#include <string.h>
+#include <stdlib.h>
+#include <time.h>
+#include <sys/time.h>
+#include "freertos/FreeRTOS.h"
+#include "freertos/task.h"
+#include "esp_sleep.h"
+#include "esp_log.h"
+#include "driver/uart.h"
+#include "driver/rtc_io.h"
+#include "esp_intr_alloc.h"
+#include "esp_attr.h"
+#include "driver/timer.h"
+#include <stddef.h>
+#include "esp_intr_alloc.h"
+
+#include "transceiver.h"
+#include "rxTimer.h"
+
+
+bool disableRx = true; // Controls if trcv is in RX or TX-mode
+
+#ifdef TRANCEIVER_ENABLED
+
+void trcvSendHighLowPulse(uint32_t high, uint32_t low) {
+
+  if( disableRx == false ) return;
+  
+  gpio_set_level(GPIO_TX_DATA, 1);
+  delayMicroseconds( high ); 
+  gpio_set_level(GPIO_TX_DATA, 0);
+  delayMicroseconds( low );  
+}
+
+void initTransceiver() {
+
+    gpio_set_level(GPIO_ENABLE, 0);
+    gpio_set_level(GPIO_TX_ENABLE, 0);
+    gpio_set_level(GPIO_TX_DATA, 0);
+
+    gpio_pad_select_gpio(GPIO_ENABLE);
+    gpio_pad_select_gpio(GPIO_TX_ENABLE);
+    gpio_pad_select_gpio(GPIO_TX_DATA);
+    gpio_pad_select_gpio(GPIO_RX_DATA);
+
+    gpio_set_direction(GPIO_ENABLE, GPIO_MODE_OUTPUT);
+    gpio_set_direction(GPIO_TX_ENABLE, GPIO_MODE_OUTPUT);
+    gpio_set_direction(GPIO_TX_DATA, GPIO_MODE_OUTPUT);
+    gpio_set_direction(GPIO_RX_DATA, GPIO_MODE_INPUT);
+
+    // ------------------ INIT THE TRANCEIVER -------------------------
+    // From powerdown mode (pin 4-5-6 low), 
+    // 1. Drive high pin 6 (ENABLE)
+    // 2. After 20us drive high pin 5 (RX/TX)200us, hold on 40us 
+    // 3. Drive down 20us pin 6 (ENABLE).  
+    gpio_set_level(GPIO_ENABLE, 1); delayMicroseconds( 20 ); 
+    gpio_set_level(GPIO_TX_ENABLE, 1); delayMicroseconds( 200 ); gpio_set_level(GPIO_TX_ENABLE, 0);
+    delayMicroseconds( 40 ); 
+    gpio_set_level(GPIO_ENABLE, 0); delayMicroseconds( 20 ); gpio_set_level(GPIO_ENABLE, 1);  
+    delayMicroseconds( 200 );
+    // ------------------ INIT THE TRANCEIVER -------------------------  
+            
+    ESP_LOGI("TRCV", "Transceiver has been initialized.");
+}
+
+void trcvSwitch2transmit() {
+
+  disableRx = true;
+  // ------------------ GO TO TRANSMIT STATE -------------------------
+  gpio_set_level(GPIO_TX_ENABLE, 1);
+  delayMicroseconds( 400 );
+  // ------------------ GO TO TRANSMIT STATE -------------------------
+}
+
+void trcvSwitch2receive() {
+  // ------------------ GO TO RECEIVE STATE -------------------------
+  // From Transmission Mode to Receiver Mode follow the procedure:
+  // Drive down pin 5 (RX/TX), after 40us drive down pin 6 (ENABLE) 20us long.
+  //After 200 us the device is ready for reception.
+  gpio_set_level(GPIO_TX_ENABLE, 0); 
+  delayMicroseconds( 40 ); 
+  gpio_set_level(GPIO_ENABLE, 0); 
+  delayMicroseconds( 20 ); 
+  gpio_set_level(GPIO_ENABLE, 1);  
+  delayMicroseconds( 200 );
+  // ------------------ GO TO RECEIVE STATE -------------------------
+  disableRx = false;
+}
+
+#endif

+ 30 - 0
main/transceiver.h

@@ -0,0 +1,30 @@
+#ifndef __TRANSCEIVER__
+#define __TRANSCEIVER__
+
+/****************************************************************************************************
+ * The following I/O-Pins are used in the transceiver app:
+ * 
+ * 19	Output ENABLE
+ * 18	Output TX_ENABLE
+ * 17	Output TX_DATA
+ * 16	Input  RX_DATA
+ ****************************************************************************************************/
+
+ #define GPIO_ENABLE        19
+ #define GPIO_TX_ENABLE     18
+ #define GPIO_TX_DATA       17
+ #define GPIO_RX_DATA       16
+
+
+ extern bool disableRx;
+
+
+void initTransceiver();
+
+void trcvSwitch2transmit();
+void trcvSwitch2receive();
+
+void trcvSendHighLowPulse(uint32_t high, uint32_t low);
+
+
+#endif