- #include <stdio.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "driver/gpio.h"
- #include "sdkconfig.h"
- #include "driver/periph_ctrl.h"
- #include "driver/timer.h"
- volatile uint8_t spkr_pin = 1;
- // Forward declarations
- void pinSetup(void);
- void audiotest(void *pvParameter);
- void timer_group0_isr (void *param);
- void timer_config(void);
- void app_main(){
- pinSetup();
- timer_config();
- xTaskCreate(&audiotest, "blink_task", configMINIMAL_STACK_SIZE, NULL, 5, NULL);
- }
- void audiotest(void *pvParameter){
- TickType_t xLastWakeTime = xTaskGetTickCount();
- const TickType_t xFrequency = 10;
- uint8_t pin = 0;
- while (1){
- vTaskDelayUntil( &xLastWakeTime, xFrequency );
- gpio_set_level(GPIO_NUM_22, pin);
- pin = !pin;
- }
- }
- void IRAM_ATTR timer_group0_isr (void *param){
- TIMERG0.int_clr_timers.t0 = 1; //clear interrupt bit
- gpio_set_level(GPIO_NUM_2, *(int *)param);
- *(int *)param = !*(int *)param ;
- }
- void timer_config(void){
- timer_config_t audio;
- audio.divider = 80; //Set prescaler for 1 MHz clock
- audio.counter_dir = TIMER_COUNT_UP;
- audio.alarm_en = 1;
- audio.intr_type = TIMER_INTR_LEVEL;
- audio.auto_reload = TIMER_AUTORELOAD_EN; // Reset timer to 0 when end condition is triggered
- audio.counter_en = TIMER_PAUSE;
- timer_init(TIMER_GROUP_0,0,&audio); //start timer 0 at group 0
- timer_set_counter_value(TIMER_GROUP_0,0,0); //set timer for 0
- timer_isr_register(TIMER_GROUP_0,0,&timer_group0_isr,&spkr_pin,ESP_INTR_FLAG_IRAM,NULL);
- timer_set_alarm_value(TIMER_GROUP_0,0,5000000);
- timer_enable_intr(TIMER_GROUP_0,0);
- timer_start(TIMER_GROUP_0,0);
- }
- void pinSetup (void) {
- gpio_pad_select_gpio(GPIO_NUM_22);
- gpio_set_direction(GPIO_NUM_22,GPIO_MODE_DEF_OUTPUT);
- gpio_pad_select_gpio(GPIO_NUM_2);
- gpio_set_direction(GPIO_NUM_2,GPIO_MODE_DEF_OUTPUT);
- }
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;
Code: Select all
timer_set_alarm