freertos_hooks.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 "freertos/FreeRTOS.h"
  17. #include "esp_attr.h"
  18. #include "esp_freertos_hooks.h"
  19. #include "sdkconfig.h"
  20. #include "esp_pm.h"
  21. #include "esp_private/pm_impl.h"
  22. //We use just a static array here because it's not expected many components will need
  23. //an idle or tick hook.
  24. #define MAX_HOOKS 8
  25. static portMUX_TYPE hooks_spinlock = portMUX_INITIALIZER_UNLOCKED;
  26. static esp_freertos_idle_cb_t idle_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
  27. static esp_freertos_tick_cb_t tick_cb[portNUM_PROCESSORS][MAX_HOOKS]={0};
  28. void IRAM_ATTR esp_vApplicationTickHook(void)
  29. {
  30. int n;
  31. int core = xPortGetCoreID();
  32. for (n=0; n<MAX_HOOKS; n++) {
  33. if (tick_cb[core][n]!=NULL) {
  34. tick_cb[core][n]();
  35. }
  36. }
  37. }
  38. void esp_vApplicationIdleHook(void)
  39. {
  40. bool can_go_idle=true;
  41. int core = xPortGetCoreID();
  42. for (int n = 0; n < MAX_HOOKS; n++) {
  43. if (idle_cb[core][n] != NULL && !idle_cb[core][n]()) {
  44. can_go_idle = false;
  45. }
  46. }
  47. if (!can_go_idle) {
  48. return;
  49. }
  50. #ifdef CONFIG_PM_ENABLE
  51. esp_pm_impl_idle_hook();
  52. #endif
  53. esp_pm_impl_waiti();
  54. }
  55. esp_err_t esp_register_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t new_idle_cb, UBaseType_t cpuid)
  56. {
  57. if(cpuid >= portNUM_PROCESSORS){
  58. return ESP_ERR_INVALID_ARG;
  59. }
  60. portENTER_CRITICAL(&hooks_spinlock);
  61. for(int n = 0; n < MAX_HOOKS; n++){
  62. if (idle_cb[cpuid][n]==NULL) {
  63. idle_cb[cpuid][n]=new_idle_cb;
  64. portEXIT_CRITICAL(&hooks_spinlock);
  65. return ESP_OK;
  66. }
  67. }
  68. portEXIT_CRITICAL(&hooks_spinlock);
  69. return ESP_ERR_NO_MEM;
  70. }
  71. esp_err_t esp_register_freertos_idle_hook(esp_freertos_idle_cb_t new_idle_cb)
  72. {
  73. return esp_register_freertos_idle_hook_for_cpu(new_idle_cb, xPortGetCoreID());
  74. }
  75. esp_err_t esp_register_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t new_tick_cb, UBaseType_t cpuid)
  76. {
  77. if(cpuid >= portNUM_PROCESSORS){
  78. return ESP_ERR_INVALID_ARG;
  79. }
  80. portENTER_CRITICAL(&hooks_spinlock);
  81. for(int n = 0; n < MAX_HOOKS; n++){
  82. if (tick_cb[cpuid][n]==NULL) {
  83. tick_cb[cpuid][n]=new_tick_cb;
  84. portEXIT_CRITICAL(&hooks_spinlock);
  85. return ESP_OK;
  86. }
  87. }
  88. portEXIT_CRITICAL(&hooks_spinlock);
  89. return ESP_ERR_NO_MEM;
  90. }
  91. esp_err_t esp_register_freertos_tick_hook(esp_freertos_tick_cb_t new_tick_cb)
  92. {
  93. return esp_register_freertos_tick_hook_for_cpu(new_tick_cb, xPortGetCoreID());
  94. }
  95. void esp_deregister_freertos_idle_hook_for_cpu(esp_freertos_idle_cb_t old_idle_cb, UBaseType_t cpuid)
  96. {
  97. if(cpuid >= portNUM_PROCESSORS){
  98. return;
  99. }
  100. portENTER_CRITICAL(&hooks_spinlock);
  101. for(int n = 0; n < MAX_HOOKS; n++){
  102. if(idle_cb[cpuid][n] == old_idle_cb) idle_cb[cpuid][n] = NULL;
  103. }
  104. portEXIT_CRITICAL(&hooks_spinlock);
  105. }
  106. void esp_deregister_freertos_idle_hook(esp_freertos_idle_cb_t old_idle_cb)
  107. {
  108. portENTER_CRITICAL(&hooks_spinlock);
  109. for(int m = 0; m < portNUM_PROCESSORS; m++) {
  110. esp_deregister_freertos_idle_hook_for_cpu(old_idle_cb, m);
  111. }
  112. portEXIT_CRITICAL(&hooks_spinlock);
  113. }
  114. void esp_deregister_freertos_tick_hook_for_cpu(esp_freertos_tick_cb_t old_tick_cb, UBaseType_t cpuid)
  115. {
  116. if(cpuid >= portNUM_PROCESSORS){
  117. return;
  118. }
  119. portENTER_CRITICAL(&hooks_spinlock);
  120. for(int n = 0; n < MAX_HOOKS; n++){
  121. if(tick_cb[cpuid][n] == old_tick_cb) tick_cb[cpuid][n] = NULL;
  122. }
  123. portEXIT_CRITICAL(&hooks_spinlock);
  124. }
  125. void esp_deregister_freertos_tick_hook(esp_freertos_tick_cb_t old_tick_cb)
  126. {
  127. portENTER_CRITICAL(&hooks_spinlock);
  128. for(int m = 0; m < portNUM_PROCESSORS; m++){
  129. esp_deregister_freertos_tick_hook_for_cpu(old_tick_cb, m);
  130. }
  131. portEXIT_CRITICAL(&hooks_spinlock);
  132. }