2016-10-26 09:09:55 -04:00
|
|
|
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "esp_attr.h"
|
|
|
|
#include "esp_err.h"
|
2019-03-26 04:30:43 -04:00
|
|
|
#include "esp_intr_alloc.h"
|
2020-03-27 05:58:12 -04:00
|
|
|
#include "esp_debug_helpers.h"
|
2016-10-26 09:09:55 -04:00
|
|
|
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "esp32/rom/ets_sys.h"
|
|
|
|
#include "esp32/rom/uart.h"
|
2016-10-26 09:09:55 -04:00
|
|
|
|
|
|
|
#include "soc/cpu.h"
|
|
|
|
#include "soc/dport_reg.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/gpio_periph.h"
|
|
|
|
#include "soc/rtc_periph.h"
|
2016-10-26 09:09:55 -04:00
|
|
|
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "freertos/queue.h"
|
|
|
|
#include "freertos/portmacro.h"
|
|
|
|
|
|
|
|
|
2017-08-21 10:29:08 -04:00
|
|
|
#define REASON_YIELD BIT(0)
|
|
|
|
#define REASON_FREQ_SWITCH BIT(1)
|
2020-03-27 05:58:12 -04:00
|
|
|
#define REASON_PRINT_BACKTRACE BIT(2)
|
2016-10-26 09:09:55 -04:00
|
|
|
|
2017-08-21 10:29:08 -04:00
|
|
|
static portMUX_TYPE reason_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
2016-10-26 09:09:55 -04:00
|
|
|
static volatile uint32_t reason[ portNUM_PROCESSORS ];
|
|
|
|
|
|
|
|
/*
|
|
|
|
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
|
|
|
|
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.
|
|
|
|
*/
|
2019-07-16 05:33:30 -04:00
|
|
|
static inline void IRAM_ATTR esp_crosscore_isr_handle_yield(void)
|
2017-08-21 10:29:08 -04:00
|
|
|
{
|
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
}
|
|
|
|
|
2017-01-10 11:52:45 -05:00
|
|
|
static void IRAM_ATTR esp_crosscore_isr(void *arg) {
|
2017-08-21 10:29:08 -04:00
|
|
|
uint32_t my_reason_val;
|
2016-10-27 04:07:47 -04:00
|
|
|
//A pointer to the correct reason array item is passed to this ISR.
|
2017-08-21 10:29:08 -04:00
|
|
|
volatile uint32_t *my_reason=arg;
|
2016-11-10 04:59:46 -05:00
|
|
|
|
2016-10-27 04:07:47 -04:00
|
|
|
//Clear the interrupt first.
|
|
|
|
if (xPortGetCoreID()==0) {
|
2017-05-08 08:03:04 -04:00
|
|
|
DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, 0);
|
2016-10-27 04:07:47 -04:00
|
|
|
} else {
|
2017-05-08 08:03:04 -04:00
|
|
|
DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, 0);
|
2016-10-27 04:07:47 -04:00
|
|
|
}
|
|
|
|
//Grab the reason and clear it.
|
2018-01-29 08:40:38 -05:00
|
|
|
portENTER_CRITICAL_ISR(&reason_spinlock);
|
2017-08-21 10:29:08 -04:00
|
|
|
my_reason_val=*my_reason;
|
|
|
|
*my_reason=0;
|
2018-01-29 08:40:38 -05:00
|
|
|
portEXIT_CRITICAL_ISR(&reason_spinlock);
|
2016-10-26 09:09:55 -04:00
|
|
|
|
2016-10-27 04:07:47 -04:00
|
|
|
//Check what we need to do.
|
2017-08-21 10:29:08 -04:00
|
|
|
if (my_reason_val & REASON_YIELD) {
|
|
|
|
esp_crosscore_isr_handle_yield();
|
|
|
|
}
|
|
|
|
if (my_reason_val & REASON_FREQ_SWITCH) {
|
|
|
|
/* Nothing to do here; the frequency switch event was already
|
|
|
|
* handled by a hook in xtensa_vectors.S. Could be used in the future
|
|
|
|
* to allow DFS features without the extra latency of the ISR hook.
|
|
|
|
*/
|
2016-10-27 04:07:47 -04:00
|
|
|
}
|
2020-03-27 05:58:12 -04:00
|
|
|
if (my_reason_val & REASON_PRINT_BACKTRACE) {
|
|
|
|
esp_backtrace_print(100);
|
|
|
|
}
|
2016-10-26 09:09:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
//Initialize the crosscore interrupt on this core. Call this once
|
|
|
|
//on each active core.
|
2019-07-16 05:33:30 -04:00
|
|
|
void esp_crosscore_int_init(void) {
|
2017-08-21 10:29:08 -04:00
|
|
|
portENTER_CRITICAL(&reason_spinlock);
|
2016-10-27 04:07:47 -04:00
|
|
|
reason[xPortGetCoreID()]=0;
|
2017-08-21 10:29:08 -04:00
|
|
|
portEXIT_CRITICAL(&reason_spinlock);
|
2017-01-10 12:14:18 -05:00
|
|
|
esp_err_t err;
|
2016-10-27 04:07:47 -04:00
|
|
|
if (xPortGetCoreID()==0) {
|
2017-08-21 10:29:08 -04:00
|
|
|
err = esp_intr_alloc(ETS_FROM_CPU_INTR0_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[0], NULL);
|
2016-10-27 04:07:47 -04:00
|
|
|
} else {
|
2017-08-21 10:29:08 -04:00
|
|
|
err = esp_intr_alloc(ETS_FROM_CPU_INTR1_SOURCE, ESP_INTR_FLAG_IRAM, esp_crosscore_isr, (void*)&reason[1], NULL);
|
2016-10-27 04:07:47 -04:00
|
|
|
}
|
2017-01-10 12:14:18 -05:00
|
|
|
assert(err == ESP_OK);
|
2016-10-26 09:09:55 -04:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:29:08 -04:00
|
|
|
static void IRAM_ATTR esp_crosscore_int_send(int core_id, uint32_t reason_mask) {
|
|
|
|
assert(core_id<portNUM_PROCESSORS);
|
2016-10-27 04:07:47 -04:00
|
|
|
//Mark the reason we interrupt the other CPU
|
2019-03-25 06:39:21 -04:00
|
|
|
portENTER_CRITICAL_ISR(&reason_spinlock);
|
2017-08-21 10:29:08 -04:00
|
|
|
reason[core_id] |= reason_mask;
|
2019-03-25 06:39:21 -04:00
|
|
|
portEXIT_CRITICAL_ISR(&reason_spinlock);
|
2016-10-27 04:07:47 -04:00
|
|
|
//Poke the other CPU.
|
2017-08-21 10:29:08 -04:00
|
|
|
if (core_id==0) {
|
2017-05-08 08:03:04 -04:00
|
|
|
DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_0_REG, DPORT_CPU_INTR_FROM_CPU_0);
|
2016-10-27 04:07:47 -04:00
|
|
|
} else {
|
2017-05-08 08:03:04 -04:00
|
|
|
DPORT_WRITE_PERI_REG(DPORT_CPU_INTR_FROM_CPU_1_REG, DPORT_CPU_INTR_FROM_CPU_1);
|
2016-10-27 04:07:47 -04:00
|
|
|
}
|
2016-10-26 09:09:55 -04:00
|
|
|
}
|
|
|
|
|
2017-08-21 10:29:08 -04:00
|
|
|
void IRAM_ATTR esp_crosscore_int_send_yield(int core_id)
|
|
|
|
{
|
|
|
|
esp_crosscore_int_send(core_id, REASON_YIELD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRAM_ATTR esp_crosscore_int_send_freq_switch(int core_id)
|
|
|
|
{
|
|
|
|
esp_crosscore_int_send(core_id, REASON_FREQ_SWITCH);
|
|
|
|
}
|
|
|
|
|
2020-03-27 05:58:12 -04:00
|
|
|
void IRAM_ATTR esp_crosscore_int_send_print_backtrace(int core_id)
|
|
|
|
{
|
|
|
|
esp_crosscore_int_send(core_id, REASON_PRINT_BACKTRACE);
|
|
|
|
}
|