rxTimer.c 3.2 KB

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