crosscore_int.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 <string.h>
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "esp_intr.h"
  18. #include "esp_intr_alloc.h"
  19. #include "esp_panic.h"
  20. #include "rom/ets_sys.h"
  21. #include "rom/uart.h"
  22. #include "soc/cpu.h"
  23. #include "soc/dport_reg.h"
  24. #include "soc/io_mux_reg.h"
  25. #include "soc/rtc_cntl_reg.h"
  26. #include "freertos/FreeRTOS.h"
  27. #include "freertos/task.h"
  28. #include "freertos/semphr.h"
  29. #include "freertos/queue.h"
  30. #include "freertos/portmacro.h"
  31. #define REASON_YIELD BIT(0)
  32. #define REASON_FREQ_SWITCH BIT(1)
  33. #define REASON_PRINT_BACKTRACE BIT(2)
  34. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  35. static volatile uint32_t reason[ portNUM_PROCESSORS ];
  36. /*
  37. ToDo: There is a small chance the CPU already has yielded when this ISR is serviced. In that case, it's running the intended task but
  38. the ISR will cause it to switch _away_ from it. portYIELD_FROM_ISR will probably just schedule the task again, but have to check that.
  39. */
  40. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield()
  41. {
  42. portYIELD_FROM_ISR();
  43. }
  44. static void IRAM_ATTR esp_crosscore_isr(void *arg) {
  45. uint32_t my_reason_val;
  46. //A pointer to the correct reason array item is passed to this ISR.
  47. volatile uint32_t *my_reason=arg;
  48. //Clear the interrupt first.
  49. if (xPortGetCoreID()==0) {
  50. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  51. } else {
  52. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
  53. }
  54. //Grab the reason and clear it.
  55. portENTER_CRITICAL_ISR(&reason_spinlock);
  56. my_reason_val=*my_reason;
  57. *my_reason=0;
  58. portEXIT_CRITICAL_ISR(&reason_spinlock);
  59. //Check what we need to do.
  60. if (my_reason_val & REASON_YIELD) {
  61. esp_crosscore_isr_handle_yield();
  62. }
  63. if (my_reason_val & REASON_FREQ_SWITCH) {
  64. /* Nothing to do here; the frequency switch event was already
  65. * handled by a hook in xtensa_vectors.S. Could be used in the future
  66. * to allow DFS features without the extra latency of the ISR hook.
  67. */
  68. }
  69. if (my_reason_val & REASON_PRINT_BACKTRACE) {
  70. esp_backtrace_print(100);
  71. }
  72. }
  73. //Initialize the crosscore interrupt on this core. Call this once
  74. //on each active core.
  75. void esp_crosscore_int_init() {
  76. portENTER_CRITICAL(&reason_spinlock);
  77. reason[xPortGetCoreID()]=0;
  78. portEXIT_CRITICAL(&reason_spinlock);
  79. esp_err_t err;
  80. if (xPortGetCoreID()==0) {
  81. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
  82. } else {
  83. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
  84. }
  85. assert(err == ESP_OK);
  86. }
  87. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
  88. assert(core_id<portNUM_PROCESSORS);
  89. //Mark the reason we interrupt the other CPU
  90. portENTER_CRITICAL_ISR(&reason_spinlock);
  91. reason[core_id] |= reason_mask;
  92. portEXIT_CRITICAL_ISR(&reason_spinlock);
  93. //Poke the other CPU.
  94. if (core_id==0) {
  95. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  96. } else {
  97. DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  98. }
  99. }
  100. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  101. {
  102. esp_crosscore_int_send(core_id, REASON_YIELD);
  103. }
  104. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  105. {
  106. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  107. }
  108. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  109. {
  110. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  111. }