crosscore_int.c 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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 "rom/ets_sys.h"
  20. #include "rom/uart.h"
  21. #include "soc/cpu.h"
  22. #include "soc/dport_reg.h"
  23. #include "soc/io_mux_reg.h"
  24. #include "soc/rtc_cntl_reg.h"
  25. #include "freertos/FreeRTOS.h"
  26. #include "freertos/task.h"
  27. #include "freertos/semphr.h"
  28. #include "freertos/queue.h"
  29. #include "freertos/portmacro.h"
  30. #define REASON_YIELD (1<<0)
  31. static portMUX_TYPE reasonSpinlock = portMUX_INITIALIZER_UNLOCKED;
  32. static volatile uint32_t reason[ portNUM_PROCESSORS ];
  33. /*
  34. 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
  35. 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.
  36. */
  37. static void IRAM_ATTR esp_crosscore_isr(void *arg) {
  38. uint32_t myReasonVal;
  39. //A pointer to the correct reason array item is passed to this ISR.
  40. volatile uint32_t *myReason=arg;
  41. //Clear the interrupt first.
  42. if (xPortGetCoreID()==0) {
  43. WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
  44. } else {
  45. WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
  46. }
  47. //Grab the reason and clear it.
  48. portENTER_CRITICAL(&reasonSpinlock);
  49. myReasonVal=*myReason;
  50. *myReason=0;
  51. portEXIT_CRITICAL(&reasonSpinlock);
  52. //Check what we need to do.
  53. if (myReasonVal&REASON_YIELD) {
  54. portYIELD_FROM_ISR();
  55. }
  56. }
  57. //Initialize the crosscore interrupt on this core. Call this once
  58. //on each active core.
  59. void esp_crosscore_int_init() {
  60. portENTER_CRITICAL(&reasonSpinlock);
  61. reason[xPortGetCoreID()]=0;
  62. portEXIT_CRITICAL(&reasonSpinlock);
  63. esp_err_t err;
  64. if (xPortGetCoreID()==0) {
  65. err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[xPortGetCoreID()], NULL);
  66. } else {
  67. err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[xPortGetCoreID()], NULL);
  68. }
  69. assert(err == ESP_OK);
  70. }
  71. void IRAM_ATTR esp_crosscore_int_send_yield(int coreId) {
  72. assert(coreId<portNUM_PROCESSORS);
  73. //Mark the reason we interrupt the other CPU
  74. portENTER_CRITICAL(&reasonSpinlock);
  75. reason[coreId]|=REASON_YIELD;
  76. portEXIT_CRITICAL(&reasonSpinlock);
  77. //Poke the other CPU.
  78. if (coreId==0) {
  79. WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
  80. } else {
  81. WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
  82. }
  83. }