freertos_hooks.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include "esp_attr.h"
  17. #include "esp_freertos_hooks.h"
  18. //We use just a static array here because it's not expected many components will need
  19. //an idle or tick hook.
  20. #define MAX_HOOKS 8
  21. static esp_freertos_idle_cb_t idle_cb[MAX_HOOKS]={0};
  22. static esp_freertos_tick_cb_t tick_cb[MAX_HOOKS]={0};
  23. void IRAM_ATTR esp_vApplicationTickHook()
  24. {
  25. int n;
  26. for (n=0; n<MAX_HOOKS; n++) {
  27. if (tick_cb[n]!=NULL) {
  28. tick_cb[n]();
  29. }
  30. }
  31. }
  32. void esp_vApplicationIdleHook()
  33. {
  34. bool doWait=true;
  35. bool r;
  36. int n;
  37. for (n=0; n<MAX_HOOKS; n++) {
  38. if (idle_cb[n]!=NULL) {
  39. r=idle_cb[n]();
  40. if (!r) doWait=false;
  41. }
  42. }
  43. if (doWait) {
  44. //Wait for whatever interrupt comes next... this should save some power.
  45. asm("waiti 0");
  46. }
  47. }
  48. esp_err_t esp_register_freertos_idle_hook(esp_freertos_idle_cb_t new_idle_cb)
  49. {
  50. int n;
  51. for (n=0; n<MAX_HOOKS; n++) {
  52. if (idle_cb[n]==NULL) {
  53. idle_cb[n]=new_idle_cb;
  54. return ESP_OK;
  55. }
  56. }
  57. return ESP_ERR_NO_MEM;
  58. }
  59. esp_err_t esp_register_freertos_tick_hook(esp_freertos_tick_cb_t new_tick_cb)
  60. {
  61. int n;
  62. for (n=0; n<MAX_HOOKS; n++) {
  63. if (tick_cb[n]==NULL) {
  64. tick_cb[n]=new_tick_cb;
  65. return ESP_OK;
  66. }
  67. }
  68. return ESP_ERR_NO_MEM;
  69. }
  70. void esp_deregister_freertos_idle_hook(esp_freertos_idle_cb_t old_idle_cb)
  71. {
  72. int n;
  73. for (n=0; n<MAX_HOOKS; n++) {
  74. if (idle_cb[n]==old_idle_cb) idle_cb[n]=NULL;
  75. }
  76. }
  77. void esp_deregister_freertos_tick_hook(esp_freertos_tick_cb_t old_tick_cb)
  78. {
  79. int n;
  80. for (n=0; n<MAX_HOOKS; n++) {
  81. if (tick_cb[n]==old_tick_cb) tick_cb[n]=NULL;
  82. }
  83. }