interrupt-task-deferred.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #include <stdio.h>
  2. #include "freertos/FreeRTOS.h"
  3. #include "freertos/task.h"
  4. #include "driver/gpio.h"
  5. #define LED_RED GPIO_NUM_5
  6. #define LED_GREEN GPIO_NUM_2
  7. #define PushButton GPIO_NUM_14
  8. #define LED_BLUE GPIO_NUM_4
  9. #define LED_YELLOW GPIO_NUM_22
  10. SemaphoreHandle_t binSem ;
  11. // hardware interrupt ,
  12. static void IRAM_ATTR intrHandlePushButton(void *args){
  13. //vPrintString("inside interrupt service routine");
  14. BaseType_t xHigherPriorityTaskWoken ;
  15. xHigherPriorityTaskWoken = pdFALSE;
  16. gpio_set_level(LED_GREEN,1);
  17. xSemaphoreGiveFromISR(binSem , &xHigherPriorityTaskWoken);
  18. if (xHigherPriorityTaskWoken){
  19. gpio_set_level(LED_GREEN,0);
  20. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  21. }
  22. else{
  23. gpio_set_level(LED_GREEN,0);
  24. }
  25. }
  26. static void IRAM_ATTR intrHandleLEDYELLOW(void *args){
  27. //vPrintString("inside interrupt service routine");
  28. gpio_set_level(LED_YELLOW,1);
  29. gpio_set_level(LED_YELLOW,0);
  30. }
  31. void led_task_Red(void *pvParameter)
  32. {
  33. while (1) {
  34. gpio_set_level(LED_RED, 1);
  35. gpio_set_level(LED_RED, 0);
  36. printf("inside LED routine \n");
  37. vTaskDelay(1);
  38. }
  39. vTaskDelete( NULL );
  40. }
  41. void led_task_Intr(void *pvParameter)
  42. {
  43. while (1) {
  44. xSemaphoreTake(binSem,portMAX_DELAY);
  45. gpio_set_level(LED_BLUE, 1);
  46. gpio_set_level(LED_BLUE, 0);
  47. printf("inside ISR routine\n");
  48. vTaskDelay(1);
  49. }
  50. vTaskDelete( NULL );
  51. }
  52. extern "C" void app_main() {
  53. //printf("\n%d",xPortGetCoreID());
  54. //setup GPIO to input and Output
  55. gpio_set_direction(LED_RED, GPIO_MODE_OUTPUT);
  56. gpio_set_direction(LED_GREEN,GPIO_MODE_OUTPUT);
  57. gpio_set_direction(LED_BLUE,GPIO_MODE_OUTPUT);
  58. gpio_set_direction(LED_YELLOW,GPIO_MODE_OUTPUT);
  59. gpio_set_direction(PushButton , GPIO_MODE_INPUT);
  60. gpio_set_level(PushButton,0);
  61. gpio_set_level(LED_RED,0);
  62. gpio_set_level(LED_GREEN,0);
  63. gpio_set_level(LED_BLUE,0);
  64. gpio_set_level(LED_YELLOW,0);
  65. //attach GPIO to interrupt
  66. gpio_set_intr_type(LED_RED,GPIO_INTR_POSEDGE);
  67. gpio_install_isr_service(0);
  68. gpio_isr_handler_add(LED_RED,intrHandlePushButton,NULL);
  69. gpio_set_intr_type(LED_GREEN,GPIO_INTR_POSEDGE);
  70. gpio_install_isr_service(1);
  71. gpio_isr_handler_add(LED_GREEN,intrHandleLEDYELLOW,NULL);
  72. binSem = xSemaphoreCreateBinary();
  73. if (binSem != NULL){
  74. xTaskCreate(
  75. &led_task_Red, // task function
  76. "red_led_task", // task name
  77. 2048, // stack size in words
  78. NULL, // pointer to parameters
  79. 1, // priority
  80. NULL); // out pointer to task handle
  81. xTaskCreate(
  82. &led_task_Intr, // task function
  83. "Intr_led_task", // task name
  84. 2048, // stack size in words
  85. NULL, // pointer to parameters
  86. 3, // priority
  87. NULL); // out pointer to task handle
  88. }
  89. }