[Resolved] How to use ESP32 timer in ESP-IDF?

minetest2048
Posts: 2
Joined: Thu Oct 31, 2019 7:56 am

[Resolved] How to use ESP32 timer in ESP-IDF?

Postby minetest2048 » Thu Oct 31, 2019 8:08 am

I'm using DOIT ESP32 dev1 board with PlatformIO IDE and ESP-IDF framework, and I'm trying to blink the on board LED connected to GPIO pin 2 using timer interrupts, but for some reason the LED is stuck on and won't blink. Here is the code:
  1.  
  2. #include <stdio.h>
  3. #include "freertos/FreeRTOS.h"
  4. #include "freertos/task.h"
  5. #include "driver/gpio.h"
  6. #include "sdkconfig.h"
  7. #include "driver/periph_ctrl.h"
  8. #include "driver/timer.h"
  9.  
  10. volatile uint8_t spkr_pin =  1;
  11.  
  12. // Forward declarations
  13.  
  14. void pinSetup(void);
  15. void audiotest(void *pvParameter);
  16. void timer_group0_isr (void *param);
  17. void timer_config(void);
  18.  
  19.  
  20.  
  21.  
  22. void app_main(){
  23.      pinSetup();
  24.      timer_config();
  25.      xTaskCreate(&audiotest, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
  26. }
  27.  
  28.  
  29.  
  30.  
  31.  
  32. void audiotest(void *pvParameter){
  33.     TickType_t xLastWakeTime = xTaskGetTickCount();
  34.     const TickType_t xFrequency = 10;
  35.     uint8_t pin =  0;
  36.  
  37.     while (1){
  38.         vTaskDelayUntil( &xLastWakeTime, xFrequency );
  39.         gpio_set_level(GPIO_NUM_22, pin);
  40.         pin = !pin;
  41.     }
  42. }
  43.  
  44.  
  45. void IRAM_ATTR timer_group0_isr (void *param){
  46.     TIMERG0.int_clr_timers.t0 = 1; //clear interrupt bit
  47.     gpio_set_level(GPIO_NUM_2, *(int *)param);
  48.     *(int *)param = !*(int *)param ;
  49. }
  50.  
  51. void timer_config(void){
  52.     timer_config_t audio;
  53.     audio.divider = 80; //Set prescaler for 1 MHz clock
  54.     audio.counter_dir = TIMER_COUNT_UP;
  55.     audio.alarm_en = 1;
  56.     audio.intr_type = TIMER_INTR_LEVEL;
  57.     audio.auto_reload = TIMER_AUTORELOAD_EN; // Reset timer to 0 when end condition is triggered
  58.     audio.counter_en = TIMER_PAUSE;
  59.     timer_init(TIMER_GROUP_0,0,&audio); //start timer 0 at group 0
  60.     timer_set_counter_value(TIMER_GROUP_0,0,0); //set timer for 0
  61.     timer_isr_register(TIMER_GROUP_0,0,&timer_group0_isr,&spkr_pin,ESP_INTR_FLAG_IRAM,NULL);
  62.     timer_set_alarm_value(TIMER_GROUP_0,0,5000000);
  63.     timer_enable_intr(TIMER_GROUP_0,0);
  64.  
  65.     timer_start(TIMER_GROUP_0,0);
  66. }
  67.  
  68. void pinSetup (void) {
  69.     gpio_pad_select_gpio(GPIO_NUM_22);
  70.     gpio_set_direction(GPIO_NUM_22,GPIO_MODE_DEF_OUTPUT);
  71.     gpio_pad_select_gpio(GPIO_NUM_2);
  72.     gpio_set_direction(GPIO_NUM_2,GPIO_MODE_DEF_OUTPUT);
  73. }
  74.  
I've been following https://docs.espressif.com/projects/esp ... timer.html , and the on board LED turns on 5 second after boot, which means that the interrupt handler got triggered, but then 5 second later the LED should turn off, but it doesn't turn off. Is there a code that is missing?

I also tried to follow the example code at https://github.com/espressif/esp-idf/bl ... ple_main.c , but there are some functions like timer_group_intr_clr_in_isr that doesn't exist in documentation or in my IDE

EDIT: after some googling I found out that you need to reenable the alarm after it is triggered, you can do that by using

Code: Select all

 TIMERG0.hw_timer[0].config.alarm_en = 1;
, my question is that is this equal to

Code: Select all

timer_set_alarm
?

Who is online

Users browsing this forum: Baidu [Spider] and 115 guests