|
@@ -0,0 +1,121 @@
|
|
|
|
+#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 "config.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 ) ;
|
|
|
|
+}
|