sleep_mac_bb.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stddef.h>
  7. #include <string.h>
  8. #include <sys/lock.h>
  9. #include <sys/param.h>
  10. #include "esp_attr.h"
  11. #include "esp_sleep.h"
  12. #include "soc/soc_caps.h"
  13. #include "esp_private/sleep_mac_bb.h"
  14. #include "sdkconfig.h"
  15. #if CONFIG_MAC_BB_PD
  16. #define MAC_BB_POWER_DOWN_CB_NO (2)
  17. #define MAC_BB_POWER_UP_CB_NO (2)
  18. static DRAM_ATTR mac_bb_power_down_cb_t s_mac_bb_power_down_cb[MAC_BB_POWER_DOWN_CB_NO];
  19. static DRAM_ATTR mac_bb_power_up_cb_t s_mac_bb_power_up_cb[MAC_BB_POWER_UP_CB_NO];
  20. esp_err_t esp_register_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  21. {
  22. int index = MAC_BB_POWER_DOWN_CB_NO;
  23. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  24. if (s_mac_bb_power_down_cb[i] == cb) {
  25. return ESP_OK;
  26. }
  27. if (s_mac_bb_power_down_cb[i] == NULL) {
  28. index = i;
  29. }
  30. }
  31. if (index < MAC_BB_POWER_DOWN_CB_NO) {
  32. s_mac_bb_power_down_cb[index] = cb;
  33. return ESP_OK;
  34. }
  35. return ESP_ERR_NO_MEM;
  36. }
  37. esp_err_t esp_unregister_mac_bb_pd_callback(mac_bb_power_down_cb_t cb)
  38. {
  39. for (int i = MAC_BB_POWER_DOWN_CB_NO - 1; i >= 0; i--) {
  40. if (s_mac_bb_power_down_cb[i] == cb) {
  41. s_mac_bb_power_down_cb[i] = NULL;
  42. return ESP_OK;
  43. }
  44. }
  45. return ESP_ERR_INVALID_STATE;
  46. }
  47. void IRAM_ATTR mac_bb_power_down_cb_execute(void)
  48. {
  49. for (int i = 0; i < MAC_BB_POWER_DOWN_CB_NO; i++) {
  50. if (s_mac_bb_power_down_cb[i]) {
  51. s_mac_bb_power_down_cb[i]();
  52. }
  53. }
  54. }
  55. esp_err_t esp_register_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  56. {
  57. int index = MAC_BB_POWER_UP_CB_NO;
  58. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  59. if (s_mac_bb_power_up_cb[i] == cb) {
  60. return ESP_OK;
  61. }
  62. if (s_mac_bb_power_up_cb[i] == NULL) {
  63. index = i;
  64. }
  65. }
  66. if (index < MAC_BB_POWER_UP_CB_NO) {
  67. s_mac_bb_power_up_cb[index] = cb;
  68. return ESP_OK;
  69. }
  70. return ESP_ERR_NO_MEM;
  71. }
  72. esp_err_t esp_unregister_mac_bb_pu_callback(mac_bb_power_up_cb_t cb)
  73. {
  74. for (int i = MAC_BB_POWER_UP_CB_NO - 1; i >= 0; i--) {
  75. if (s_mac_bb_power_up_cb[i] == cb) {
  76. s_mac_bb_power_up_cb[i] = NULL;
  77. return ESP_OK;
  78. }
  79. }
  80. return ESP_ERR_INVALID_STATE;
  81. }
  82. void IRAM_ATTR mac_bb_power_up_cb_execute(void)
  83. {
  84. for (int i = 0; i < MAC_BB_POWER_UP_CB_NO; i++) {
  85. if (s_mac_bb_power_up_cb[i]) {
  86. s_mac_bb_power_up_cb[i]();
  87. }
  88. }
  89. }
  90. #endif ///CONFIG_MAC_BB_PD