2021-08-04 08:33:44 -04:00
|
|
|
/*
|
2023-07-18 04:21:15 -04:00
|
|
|
* SPDX-FileCopyrightText: 2017-2023 Espressif Systems (Shanghai) CO LTD
|
2021-08-04 08:33:44 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include <stdint.h>
|
2021-08-04 08:33:44 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include "FreeRTOS.h"
|
|
|
|
#include "task.h"
|
2023-08-28 09:55:27 -04:00
|
|
|
#if ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) )
|
|
|
|
/* Required for xTaskIncrementTickOtherCores() */
|
|
|
|
#include "esp_private/freertos_idf_additions_priv.h"
|
|
|
|
#endif /* ( !CONFIG_FREERTOS_SMP && ( configNUM_CORES > 1 ) ) */
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
#if CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
|
|
|
|
#if CONFIG_FREERTOS_CORETIMER_0
|
|
|
|
#define SYSTICK_INTR_ID (ETS_INTERNAL_TIMER0_INTR_SOURCE + ETS_INTERNAL_INTR_SOURCE_OFF)
|
|
|
|
#else /* CONFIG_FREERTOS_CORETIMER_1 */
|
|
|
|
#define SYSTICK_INTR_ID (ETS_INTERNAL_TIMER1_INTR_SOURCE + ETS_INTERNAL_INTR_SOURCE_OFF)
|
|
|
|
#endif
|
|
|
|
#else /* CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER */
|
|
|
|
#define SYSTICK_INTR_ID (ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE)
|
|
|
|
#endif /* CONFIG_FREERTOS_SYSTICK_USES_CCOUNT */
|
2022-02-07 04:44:33 -05:00
|
|
|
|
2021-08-04 08:33:44 -04:00
|
|
|
BaseType_t xPortSysTickHandler(void);
|
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
/* --------------------------------------------- SYSTIMER Implementation -----------------------------------------------
|
|
|
|
* Implementation of a tick interrupt using the SYSTIMER perpiheral
|
|
|
|
* Todo: Abstract setup and operation in a separate SYSTIMER driver (IDF-6976)
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
#if CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
#include "soc/periph_defs.h"
|
|
|
|
#include "hal/systimer_hal.h"
|
|
|
|
#include "hal/systimer_ll.h"
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
#include "esp_private/systimer.h"
|
|
|
|
#include "esp_private/periph_ctrl.h"
|
|
|
|
#ifdef CONFIG_PM_TRACE
|
|
|
|
#include "esp_private/pm_trace.h"
|
|
|
|
#endif //CONFIG_PM_TRACE
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2021-08-11 00:38:09 -04:00
|
|
|
_Static_assert(SOC_CPU_CORES_NUM <= SOC_SYSTIMER_ALARM_NUM - 1, "the number of cores must match the number of core alarms in SYSTIMER");
|
2021-08-04 08:33:44 -04:00
|
|
|
|
|
|
|
void SysTickIsrHandler(void *arg);
|
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
static uint32_t s_handled_systicks[configNUM_CORES] = { 0 };
|
2021-08-04 08:33:44 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Set up the systimer peripheral to generate the tick interrupt
|
|
|
|
*
|
|
|
|
* Both timer alarms are configured in periodic mode.
|
|
|
|
* It is done at the same time so SysTicks for both CPUs occur at the same time or very close.
|
|
|
|
* Shifts a time of triggering interrupts for core 0 and core 1.
|
|
|
|
*/
|
2023-04-24 04:08:51 -04:00
|
|
|
void vSystimerSetup(void)
|
2021-08-04 08:33:44 -04:00
|
|
|
{
|
|
|
|
unsigned cpuid = xPortGetCoreID();
|
|
|
|
#ifdef CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL3
|
|
|
|
const unsigned level = ESP_INTR_FLAG_LEVEL3;
|
|
|
|
#else
|
|
|
|
const unsigned level = ESP_INTR_FLAG_LEVEL1;
|
|
|
|
#endif
|
|
|
|
/* Systimer HAL layer object */
|
|
|
|
static systimer_hal_context_t systimer_hal;
|
|
|
|
/* set system timer interrupt vector */
|
2023-07-18 04:21:15 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO: IDF-7487
|
|
|
|
* ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE is renamed to ETS_SYSTIMER_TARGET0_INTR_SOURCE.
|
|
|
|
* It's said that this interrupt is never an edge type, for previous all chips. You may need to check this and unify the name.
|
|
|
|
*/
|
|
|
|
#if !CONFIG_IDF_TARGET_ESP32P4
|
2021-08-04 08:33:44 -04:00
|
|
|
ESP_ERROR_CHECK(esp_intr_alloc(ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE + cpuid, ESP_INTR_FLAG_IRAM | level, SysTickIsrHandler, &systimer_hal, NULL));
|
2023-07-18 04:21:15 -04:00
|
|
|
#else
|
|
|
|
ESP_ERROR_CHECK(esp_intr_alloc(ETS_SYSTIMER_TARGET0_INTR_SOURCE + cpuid, ESP_INTR_FLAG_IRAM | level, SysTickIsrHandler, &systimer_hal, NULL));
|
|
|
|
#endif
|
2021-08-04 08:33:44 -04:00
|
|
|
|
|
|
|
if (cpuid == 0) {
|
2022-07-21 07:05:21 -04:00
|
|
|
periph_module_enable(PERIPH_SYSTIMER_MODULE);
|
2021-08-04 08:33:44 -04:00
|
|
|
systimer_hal_init(&systimer_hal);
|
2022-07-21 07:05:21 -04:00
|
|
|
systimer_hal_tick_rate_ops_t ops = {
|
|
|
|
.ticks_to_us = systimer_ticks_to_us,
|
|
|
|
.us_to_ticks = systimer_us_to_ticks,
|
|
|
|
};
|
|
|
|
systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_OS_TICK, 0);
|
|
|
|
systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_COUNTER_OS_TICK);
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2021-08-11 00:38:09 -04:00
|
|
|
for (cpuid = 0; cpuid < SOC_CPU_CORES_NUM; cpuid++) {
|
2023-06-19 09:57:02 -04:00
|
|
|
// Set stall option and alarm mode to default state. Below they will be set to a required state.
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_hal_counter_can_stall_by_cpu(&systimer_hal, SYSTIMER_COUNTER_OS_TICK, cpuid, false);
|
2023-06-19 09:57:02 -04:00
|
|
|
uint32_t alarm_id = SYSTIMER_ALARM_OS_TICK_CORE0 + cpuid;
|
|
|
|
systimer_hal_select_alarm_mode(&systimer_hal, alarm_id, SYSTIMER_ALARM_MODE_ONESHOT);
|
2021-08-11 00:38:09 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for (cpuid = 0; cpuid < portNUM_PROCESSORS; ++cpuid) {
|
2023-01-10 02:13:19 -05:00
|
|
|
uint32_t alarm_id = SYSTIMER_ALARM_OS_TICK_CORE0 + cpuid;
|
2021-08-04 08:33:44 -04:00
|
|
|
|
|
|
|
/* configure the timer */
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_hal_connect_alarm_counter(&systimer_hal, alarm_id, SYSTIMER_COUNTER_OS_TICK);
|
2021-08-04 08:33:44 -04:00
|
|
|
systimer_hal_set_alarm_period(&systimer_hal, alarm_id, 1000000UL / CONFIG_FREERTOS_HZ);
|
|
|
|
systimer_hal_select_alarm_mode(&systimer_hal, alarm_id, SYSTIMER_ALARM_MODE_PERIOD);
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_hal_counter_can_stall_by_cpu(&systimer_hal, SYSTIMER_COUNTER_OS_TICK, cpuid, true);
|
2021-08-04 08:33:44 -04:00
|
|
|
if (cpuid == 0) {
|
|
|
|
systimer_hal_enable_alarm_int(&systimer_hal, alarm_id);
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_hal_enable_counter(&systimer_hal, SYSTIMER_COUNTER_OS_TICK);
|
2021-08-11 00:38:09 -04:00
|
|
|
#ifndef CONFIG_FREERTOS_UNICORE
|
2021-08-04 08:33:44 -04:00
|
|
|
// SysTick of core 0 and core 1 are shifted by half of period
|
2023-01-10 02:13:19 -05:00
|
|
|
systimer_hal_counter_value_advance(&systimer_hal, SYSTIMER_COUNTER_OS_TICK, 1000000UL / CONFIG_FREERTOS_HZ / 2);
|
2021-08-11 00:38:09 -04:00
|
|
|
#endif
|
2021-08-04 08:33:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2023-01-10 02:13:19 -05:00
|
|
|
uint32_t alarm_id = SYSTIMER_ALARM_OS_TICK_CORE0 + cpuid;
|
2021-08-04 08:33:44 -04:00
|
|
|
systimer_hal_enable_alarm_int(&systimer_hal, alarm_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Systimer interrupt handler.
|
|
|
|
*
|
|
|
|
* The Systimer interrupt for SysTick works in periodic mode no need to calc the next alarm.
|
|
|
|
* If a timer interrupt is ever serviced more than one tick late, it is necessary to process multiple ticks.
|
|
|
|
*/
|
2023-04-24 04:08:51 -04:00
|
|
|
void SysTickIsrHandler(void *arg)
|
2021-08-04 08:33:44 -04:00
|
|
|
{
|
|
|
|
uint32_t cpuid = xPortGetCoreID();
|
|
|
|
systimer_hal_context_t *systimer_hal = (systimer_hal_context_t *)arg;
|
|
|
|
#ifdef CONFIG_PM_TRACE
|
|
|
|
ESP_PM_TRACE_ENTER(TICK, cpuid);
|
|
|
|
#endif
|
|
|
|
|
2023-01-10 02:13:19 -05:00
|
|
|
uint32_t alarm_id = SYSTIMER_ALARM_OS_TICK_CORE0 + cpuid;
|
2021-08-04 08:33:44 -04:00
|
|
|
do {
|
|
|
|
systimer_ll_clear_alarm_int(systimer_hal->dev, alarm_id);
|
|
|
|
|
2023-01-10 02:13:19 -05:00
|
|
|
uint32_t diff = systimer_hal_get_counter_value(systimer_hal, SYSTIMER_COUNTER_OS_TICK) / systimer_ll_get_alarm_period(systimer_hal->dev, alarm_id) - s_handled_systicks[cpuid];
|
2021-08-04 08:33:44 -04:00
|
|
|
if (diff > 0) {
|
|
|
|
if (s_handled_systicks[cpuid] == 0) {
|
|
|
|
s_handled_systicks[cpuid] = diff;
|
|
|
|
diff = 1;
|
|
|
|
} else {
|
|
|
|
s_handled_systicks[cpuid] += diff;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
xPortSysTickHandler();
|
|
|
|
} while (--diff);
|
|
|
|
}
|
|
|
|
} while (systimer_ll_is_alarm_int_fired(systimer_hal->dev, alarm_id));
|
|
|
|
|
|
|
|
#ifdef CONFIG_PM_TRACE
|
|
|
|
ESP_PM_TRACE_EXIT(TICK, cpuid);
|
|
|
|
#endif
|
|
|
|
}
|
2023-04-24 04:08:51 -04:00
|
|
|
#endif /* CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER */
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
/* ------------------------------------------------ Common Port Tick ---------------------------------------------------
|
|
|
|
* Tick related functions common across all ports
|
|
|
|
* ------------------------------------------------------------------------------------------------------------------ */
|
2021-08-04 08:33:44 -04:00
|
|
|
|
2023-04-24 04:08:51 -04:00
|
|
|
/**
|
|
|
|
* @brief Initialize the tick interrupt timer
|
|
|
|
*
|
|
|
|
* - CCOUNT timer is used if CONFIG_FREERTOS_SYSTICK_USES_CCOUNT is set
|
|
|
|
* - SYSTIMER is used if CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER is set
|
|
|
|
*/
|
|
|
|
void vPortSetupTimer(void)
|
|
|
|
{
|
|
|
|
#if CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
|
|
|
|
extern void _frxt_tick_timer_init(void);
|
|
|
|
extern void _xt_tick_divisor_init(void);
|
|
|
|
/* Init the tick divisor value */
|
|
|
|
_xt_tick_divisor_init();
|
|
|
|
_frxt_tick_timer_init();
|
|
|
|
#else /* CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER */
|
|
|
|
vSystimerSetup();
|
|
|
|
#endif /* CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER */
|
|
|
|
}
|
2022-07-09 06:47:14 -04:00
|
|
|
|
2021-08-04 08:33:44 -04:00
|
|
|
/**
|
|
|
|
* @brief Handler of SysTick
|
|
|
|
*
|
|
|
|
* The function is called from:
|
|
|
|
* - _frxt_timer_int for xtensa with CONFIG_FREERTOS_SYSTICK_USES_CCOUNT
|
|
|
|
* - SysTickIsrHandler for xtensa with CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER
|
|
|
|
* - SysTickIsrHandler for riscv
|
|
|
|
*/
|
|
|
|
BaseType_t xPortSysTickHandler(void)
|
|
|
|
{
|
2022-01-29 03:49:56 -05:00
|
|
|
#if configBENCHMARK
|
2021-08-04 08:33:44 -04:00
|
|
|
portbenchmarkIntLatency();
|
2022-01-29 03:49:56 -05:00
|
|
|
#endif //configBENCHMARK
|
2021-08-04 08:33:44 -04:00
|
|
|
traceISR_ENTER(SYSTICK_INTR_ID);
|
2022-07-09 06:47:14 -04:00
|
|
|
|
|
|
|
// Call IDF Tick Hook
|
2023-04-24 04:08:51 -04:00
|
|
|
extern void esp_vApplicationTickHook(void);
|
2022-07-09 06:47:14 -04:00
|
|
|
esp_vApplicationTickHook();
|
|
|
|
|
|
|
|
// Call FreeRTOS Increment tick function
|
|
|
|
BaseType_t xSwitchRequired;
|
2023-04-24 04:08:51 -04:00
|
|
|
#if CONFIG_FREERTOS_SMP
|
|
|
|
// Amazon SMP FreeRTOS requires that only core 0 calls xTaskIncrementTick()
|
|
|
|
#if ( configNUM_CORES > 1 )
|
|
|
|
if (portGET_CORE_ID() == 0) {
|
|
|
|
xSwitchRequired = xTaskIncrementTick();
|
|
|
|
} else {
|
|
|
|
xSwitchRequired = pdFALSE;
|
|
|
|
}
|
|
|
|
#else /* configNUM_CORES > 1 */
|
|
|
|
xSwitchRequired = xTaskIncrementTick();
|
|
|
|
#endif /* configNUM_CORES > 1 */
|
|
|
|
#else /* !CONFIG_FREERTOS_SMP */
|
|
|
|
#if ( configNUM_CORES > 1 )
|
|
|
|
/*
|
|
|
|
Multi-core IDF FreeRTOS requires that...
|
|
|
|
- core 0 calls xTaskIncrementTick()
|
|
|
|
- core 1 calls xTaskIncrementTickOtherCores()
|
|
|
|
*/
|
|
|
|
if (xPortGetCoreID() == 0) {
|
|
|
|
xSwitchRequired = xTaskIncrementTick();
|
|
|
|
} else {
|
|
|
|
xSwitchRequired = xTaskIncrementTickOtherCores();
|
|
|
|
}
|
|
|
|
#else /* configNUM_CORES > 1 */
|
|
|
|
/*
|
|
|
|
Vanilla (single core) FreeRTOS expects that xTaskIncrementTick() cannot be interrupted (i.e., no nested
|
|
|
|
interrupts). Thus we have to disable interrupts before calling it.
|
|
|
|
*/
|
|
|
|
UBaseType_t uxSavedInterruptStatus = portSET_INTERRUPT_MASK_FROM_ISR();
|
|
|
|
xSwitchRequired = xTaskIncrementTick();
|
|
|
|
portCLEAR_INTERRUPT_MASK_FROM_ISR(uxSavedInterruptStatus);
|
|
|
|
#endif /* configNUM_CORES > 1 */
|
|
|
|
#endif /* !CONFIG_FREERTOS_SMP */
|
2022-07-09 06:47:14 -04:00
|
|
|
|
|
|
|
// Check if yield is required
|
|
|
|
if (xSwitchRequired != pdFALSE) {
|
2021-08-04 08:33:44 -04:00
|
|
|
portYIELD_FROM_ISR();
|
|
|
|
} else {
|
|
|
|
traceISR_EXIT();
|
|
|
|
}
|
2022-07-09 06:47:14 -04:00
|
|
|
return xSwitchRequired;
|
2021-08-04 08:33:44 -04:00
|
|
|
}
|