rxTimer.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5. #include <sys/time.h>
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "esp_sleep.h"
  9. #include "esp_log.h"
  10. #include "driver/uart.h"
  11. #include "driver/rtc_io.h"
  12. #include "esp_intr_alloc.h"
  13. #include "esp_attr.h"
  14. #include "driver/timer.h"
  15. #include <stddef.h>
  16. #include "esp_intr_alloc.h"
  17. #include "config.h"
  18. #ifdef RX_TIMER
  19. static intr_handle_t s_timer_handle;
  20. static bool timerTaskIsDone = false;
  21. static volatile uint32_t tenUsCounter = 0;
  22. QueueHandle_t rxQueue = NULL;
  23. static uint32_t millisCnt=0;
  24. uint32_t millis() { return millisCnt; }
  25. static void timer_tg0_isr(void* arg)
  26. {
  27. static uint32_t subMillisCounter=100;
  28. if( subMillisCounter-- == 0 ) {
  29. millisCnt++;
  30. subMillisCounter=100;
  31. }
  32. tenUsCounter++;
  33. static uint8_t currValue = 0;
  34. static uint16_t samples = 0;
  35. static uint16_t newSamples = 0;
  36. // Sample the pin value
  37. uint8_t value = (uint8_t)gpio_get_level(GPIO_IR_RX_DATA);
  38. if( value == currValue ) {
  39. samples++;
  40. samples+=newSamples;
  41. newSamples=0;
  42. }
  43. else {
  44. newSamples++;
  45. }
  46. if( newSamples == 6 ) {
  47. const uint32_t dataToSend = samples * 10;
  48. //if( disableRx == false ) {
  49. xQueueSend( rxQueue, &dataToSend, 0 );
  50. //}
  51. samples = newSamples;
  52. newSamples = 0;
  53. currValue = value;
  54. }
  55. //Reset irq and set for next time
  56. TIMERG1.int_clr_timers.t1 = 1;
  57. TIMERG1.hw_timer[1].config.alarm_en = 1;
  58. }
  59. static void timerSetupTask(void *pvParameter)
  60. {
  61. rxQueue = xQueueCreate( 1000, sizeof( uint32_t ) );
  62. timer_config_t config = {
  63. .alarm_en = true, //Alarm Enable
  64. .counter_en = false, //If the counter is enabled it will start incrementing / decrementing immediately after calling timer_init()
  65. .intr_type = TIMER_INTR_LEVEL, //Is interrupt is triggered on timer’s alarm (timer_intr_mode_t)
  66. .counter_dir = TIMER_COUNT_UP, //Does counter increment or decrement (timer_count_dir_t)
  67. .auto_reload = true, //If counter should auto_reload a specific initial value on the timer’s alarm, or continue incrementing or decrementing.
  68. .divider = 8 //Divisor of the incoming 160 MHz (12.5nS) APB_CLK clock. E.g. 800 = 10uS per timer tick
  69. };
  70. timer_init(TIMER_GROUP_1, TIMER_1, &config);
  71. timer_set_counter_value(TIMER_GROUP_1, TIMER_1, 0);
  72. timer_set_alarm_value(TIMER_GROUP_1, TIMER_1, 100); // Here we set the ISR-Value
  73. timer_enable_intr(TIMER_GROUP_1, TIMER_1);
  74. timer_isr_register(TIMER_GROUP_1, TIMER_1, &timer_tg0_isr, NULL, 0, &s_timer_handle);
  75. timer_start(TIMER_GROUP_1, TIMER_1);
  76. timerTaskIsDone = true;
  77. vTaskDelete(NULL);
  78. }
  79. void rxTimerInit()
  80. {
  81. xTaskCreatePinnedToCore(&timerSetupTask, "timerSetupTask", 8192, NULL, tskIDLE_PRIORITY + 2, NULL, 1);
  82. // Wait until task has finished
  83. while(timerTaskIsDone == false) vTaskDelay( 50 / portTICK_PERIOD_MS );
  84. ESP_LOGI("rxTimer", "10uS Timer has been started.");
  85. }
  86. void delayMicroseconds(uint32_t delay)
  87. {
  88. volatile uint32_t endtime = tenUsCounter + ((uint32_t)delay / 10);
  89. while( tenUsCounter < endtime ) ;
  90. }
  91. #endif