crosscore_int.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2015-2020 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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <stdint.h>
  15. #include "esp_attr.h"
  16. #include "esp_err.h"
  17. #include "esp_intr_alloc.h"
  18. #include "soc/periph_defs.h"
  19. #include "soc/system_reg.h"
  20. #include "hal/cpu_hal.h"
  21. #include "freertos/FreeRTOS.h"
  22. #include "freertos/portmacro.h"
  23. #define REASON_YIELD BIT(0)
  24. #define REASON_FREQ_SWITCH BIT(1)
  25. #define REASON_PRINT_BACKTRACE BIT(2)
  26. static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
  27. static volatile uint32_t reason[portNUM_PROCESSORS];
  28. // TODO ESP32-C3 IDF-2449
  29. static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
  30. {
  31. portYIELD_FROM_ISR();
  32. }
  33. static void IRAM_ATTR esp_crosscore_isr(void *arg)
  34. {
  35. uint32_t my_reason_val;
  36. //A pointer to the correct reason array item is passed to this ISR.
  37. volatile uint32_t *my_reason = arg;
  38. //Clear the interrupt first.
  39. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, 0);
  40. //Grab the reason and clear it.
  41. portENTER_CRITICAL_ISR(&reason_spinlock);
  42. my_reason_val = *my_reason;
  43. *my_reason = 0;
  44. portEXIT_CRITICAL_ISR(&reason_spinlock);
  45. //Check what we need to do.
  46. if (my_reason_val & REASON_YIELD) {
  47. esp_crosscore_isr_handle_yield();
  48. }
  49. if (my_reason_val & REASON_FREQ_SWITCH) {
  50. /* Nothing to do here; the frequency switch event was already
  51. * handled by a hook in xtensa_vectors.S. Could be used in the future
  52. * to allow DFS features without the extra latency of the ISR hook.
  53. */
  54. }
  55. }
  56. // Initialize the crosscore interrupt on this core.
  57. void esp_crosscore_int_init(void)
  58. {
  59. portENTER_CRITICAL(&reason_spinlock);
  60. reason[cpu_hal_get_core_id()] = 0;
  61. portEXIT_CRITICAL(&reason_spinlock);
  62. esp_err_t err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void *)&reason[0], NULL);
  63. assert(err == ESP_OK);
  64. }
  65. static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask)
  66. {
  67. assert(core_id < portNUM_PROCESSORS);
  68. //Mark the reason we interrupt the other CPU
  69. portENTER_CRITICAL(&reason_spinlock);
  70. reason[core_id] |= reason_mask;
  71. portEXIT_CRITICAL(&reason_spinlock);
  72. //Poke the other CPU.
  73. WRITE_PERI_REG(SYSTEM_CPU_INTR_FROM_CPU_0_REG, SYSTEM_CPU_INTR_FROM_CPU_0);
  74. }
  75. void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
  76. {
  77. esp_crosscore_int_send(core_id, REASON_YIELD);
  78. }
  79. void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
  80. {
  81. esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
  82. }
  83. void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
  84. {
  85. esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
  86. }