Project-02 Objectives
Explore the use of Free Real Time Operating System (FreeRTOS).
- Create Task 1 – to blink the external LED (GPIO 38)
- Create Task 2 – to blink the built-in RGB LED (GPIO 48)
- Create interrupt – suspend / resume Task 1 by a button press (GPIO 7)
Project Folder
The project folder with source codes are located on DT16.
D:\Users\tfw\Documents\Proj-IOT\04-S3_Projs\02-frtos_tasks\my_tasks
Reference
#4 FreeRtos Tasks – Esp idf – Youtube from Control the Controller
#5 ESP-IDF FreeRTOS Task Example-2 – Youtube from Control the Controller
#6 ESP-IDF FreeRTOS Tasks ISR (interrupt) – Youtube from Control the Controller
ESP32 GPIO Interrupts using ESP-IDF – esp32tutorials.com
Some FreeRTOS functions
- xTaskCreate( )
- xTaskCreatePinnedToCore( )
- vTaskDelay( )
- vTaskSuspend( )
- vTaskResume( )
- vTaskDelete( )
- xTaskGetTickCount( )
- eTaskGetState( )
- pcTaskGetTaskName( )
Some Interrupt functions
gpio_set_intr_type( CONFIG_BUTTON_PIN, GPIO_INTR_NEGEDGE)- enable interrupt on falling edge (from 1 to 0)for button pin
gpio_install_isr_service(ESP_INTR_FLAG_DEFAULT)- install the driver’s GPIO ISR handler service, which allows per-pin GPIO interrupt handlers
- install ISR service with default configuration
gpio_isr_handler_add(CONFIG_BUTTON_PIN, button_isr_handler, NULL)- attach the interrupt service routine
gpio_intr_disable(gpio_num_t gpio_num)- disable the interrupt attached to the pin
gpio_intr_enable(gpio_num_t gpio_num)- enable the interrupt attached to the pin
Steps to Setup GPIO Interrupt
- In app_main( ), prepare the GPIO pin
- setup pinMode, pulldown or pullup, set interrupt type.
gpio_pad_select_gpio(BTN_PIN); gpio_set_direction(BTN_PIN, GPIO_MODE_INPUT); gpio_set_pull_mode(BTN_PIN, GPIO_PULLUP_ONLY); gpio_set_intr_type(BTN_PIN, GPIO_INTR_NEGEDGE);
- Then we will call
gpio_install_isr_service(int intr_alloc_flags)to install the interrupt routine service (IRS) andgpio_isr_handler_add( )to add the interrupt handler –- inside app_main(), add
gpio_install_isr_service(0);,- this function takes in a single parameter which denotes the flags for interrupt allocation. In this case, it is zero.
- After the above, add
gpio_isr_handler_add(INPUT_PIN, gpio_interrupt_handler, void *args);- 1st parameter – the GPIO pin
- 2nd parameter – ISR handler function name
- 3rd parameter – the input parameter for the ISR function. In this case, the ISR has no input parameter. Therefore, NULL is entered.
- inside app_main(), add
- If interrupt is triggered by the button edge of the level change, this helps to eleminate button bouncing.
- Define the interrupt handler function. This function will be called when the interrupt occurs.
void IRAM_ATTR toggleExtLedISR(void *args)
{
gpio_intr_disable(BTN_PIN);
isBtnPressed = true;
} // toggleExtLedISR()
- The ISR first disable the interrupt in order to avoid unnecessary triggering while handling the routine function. Then set the isBtnPressed to true.
- In Task 2 intLed, the while loop monitors the status of isBtnPressed. If true, it gets the status of Task 1 extLed and toggle its state using vTaskSuspend or vTaskResume. At the end of the toggle, it resets the isBtnPressed to false and re-enable the interrupt.
- IRAM_ATTR is to put the function in the instruction ram for better performance. There are some rules for functions in this RAM area. Search other documents for details.
Points to Note
- A task can suspend itself, but apparantly cannot resume itself.
- Use of ‘extern’ to allow a variable or function accessable to other source files.
