2021-05-23 20:09:38 -04:00
|
|
|
/*
|
2024-01-01 22:16:55 -05:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Espressif Systems (Shanghai) CO LTD
|
2021-05-23 20:09:38 -04:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2016-12-07 01:18:10 -05:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <ctype.h>
|
2020-07-21 01:07:34 -04:00
|
|
|
#include "sdkconfig.h"
|
|
|
|
#include "esp_types.h"
|
2016-12-07 01:18:10 -05:00
|
|
|
#include "esp_log.h"
|
2019-05-13 06:02:45 -04:00
|
|
|
#include "soc/rtc_periph.h"
|
2018-06-14 04:33:46 -04:00
|
|
|
#include "soc/rtc.h"
|
2019-06-05 22:57:29 -04:00
|
|
|
#include "soc/periph_defs.h"
|
2016-12-07 01:18:10 -05:00
|
|
|
#include "freertos/FreeRTOS.h"
|
2017-03-02 03:13:30 -05:00
|
|
|
#include "freertos/semphr.h"
|
2017-07-18 07:57:28 -04:00
|
|
|
#include "freertos/timers.h"
|
2017-06-02 05:47:23 -04:00
|
|
|
#include "esp_intr_alloc.h"
|
|
|
|
#include "sys/lock.h"
|
2022-04-29 01:40:23 -04:00
|
|
|
#include "esp_private/rtc_ctrl.h"
|
2022-05-30 04:47:14 -04:00
|
|
|
#include "esp_attr.h"
|
2017-06-02 05:47:23 -04:00
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Enable built-in checks in queue.h in debug builds
|
|
|
|
#define INVARIANTS
|
|
|
|
#endif
|
2019-03-14 05:29:32 -04:00
|
|
|
#include "sys/queue.h"
|
2017-06-02 05:47:23 -04:00
|
|
|
|
2024-01-01 22:16:55 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4 || CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-07-12 07:46:23 -04:00
|
|
|
static const char *TAG = "rtc_module";
|
|
|
|
#endif
|
|
|
|
|
2023-08-01 04:04:29 -04:00
|
|
|
// rtc_spinlock is used by other peripheral drivers
|
|
|
|
portMUX_TYPE rtc_spinlock = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
|
2024-01-01 22:16:55 -05:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 && !CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-07-12 07:46:23 -04:00
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
#define NOT_REGISTERED (-1)
|
|
|
|
|
|
|
|
// Disable the interrupt which cannot work without cache.
|
|
|
|
static DRAM_ATTR uint32_t rtc_intr_cache;
|
|
|
|
static DRAM_ATTR uint32_t rtc_intr_enabled;
|
|
|
|
static DRAM_ATTR int rtc_isr_cpu = NOT_REGISTERED; // Unused number
|
|
|
|
static void s_rtc_isr_noniram_hook(uint32_t rtc_intr_mask);
|
|
|
|
static void s_rtc_isr_noniram_hook_relieve(uint32_t rtc_intr_mask);
|
2019-07-25 11:11:31 -04:00
|
|
|
|
2017-06-02 05:47:23 -04:00
|
|
|
/*---------------------------------------------------------------
|
|
|
|
INTERRUPT HANDLER
|
|
|
|
---------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
|
|
typedef struct rtc_isr_handler_ {
|
|
|
|
uint32_t mask;
|
|
|
|
intr_handler_t handler;
|
|
|
|
void* handler_arg;
|
2022-05-30 04:47:14 -04:00
|
|
|
uint32_t flags;
|
2017-06-02 05:47:23 -04:00
|
|
|
SLIST_ENTRY(rtc_isr_handler_) next;
|
|
|
|
} rtc_isr_handler_t;
|
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
static DRAM_ATTR SLIST_HEAD(rtc_isr_handler_list_, rtc_isr_handler_) s_rtc_isr_handler_list =
|
2017-06-02 05:47:23 -04:00
|
|
|
SLIST_HEAD_INITIALIZER(s_rtc_isr_handler_list);
|
2022-05-30 04:47:14 -04:00
|
|
|
static DRAM_ATTR portMUX_TYPE s_rtc_isr_handler_list_lock = portMUX_INITIALIZER_UNLOCKED;
|
2017-06-02 05:47:23 -04:00
|
|
|
static intr_handle_t s_rtc_isr_handle;
|
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
IRAM_ATTR static void rtc_isr(void* arg)
|
2017-06-02 05:47:23 -04:00
|
|
|
{
|
|
|
|
uint32_t status = REG_READ(RTC_CNTL_INT_ST_REG);
|
|
|
|
rtc_isr_handler_t* it;
|
2019-03-25 06:37:04 -04:00
|
|
|
portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
|
2017-06-02 05:47:23 -04:00
|
|
|
SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
|
|
|
|
if (it->mask & status) {
|
2019-03-25 06:37:04 -04:00
|
|
|
portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
|
2017-06-02 05:47:23 -04:00
|
|
|
(*it->handler)(it->handler_arg);
|
2019-03-25 06:37:04 -04:00
|
|
|
portENTER_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
|
2017-06-02 05:47:23 -04:00
|
|
|
}
|
|
|
|
}
|
2019-03-25 06:37:04 -04:00
|
|
|
portEXIT_CRITICAL_ISR(&s_rtc_isr_handler_list_lock);
|
2017-06-02 05:47:23 -04:00
|
|
|
REG_WRITE(RTC_CNTL_INT_CLR_REG, status);
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
static esp_err_t rtc_isr_ensure_installed(void)
|
2017-06-02 05:47:23 -04:00
|
|
|
{
|
|
|
|
esp_err_t err = ESP_OK;
|
|
|
|
portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
|
|
|
|
if (s_rtc_isr_handle) {
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
|
|
|
REG_WRITE(RTC_CNTL_INT_ENA_REG, 0);
|
|
|
|
REG_WRITE(RTC_CNTL_INT_CLR_REG, UINT32_MAX);
|
2022-05-30 04:47:14 -04:00
|
|
|
err = esp_intr_alloc(ETS_RTC_CORE_INTR_SOURCE, ESP_INTR_FLAG_IRAM, &rtc_isr, NULL, &s_rtc_isr_handle);
|
2017-06-02 05:47:23 -04:00
|
|
|
if (err != ESP_OK) {
|
|
|
|
goto out;
|
|
|
|
}
|
2022-05-30 04:47:14 -04:00
|
|
|
rtc_isr_cpu = esp_intr_get_cpu(s_rtc_isr_handle);
|
2017-06-02 05:47:23 -04:00
|
|
|
out:
|
|
|
|
portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
|
|
|
|
return err;
|
|
|
|
}
|
2023-08-08 02:42:05 -04:00
|
|
|
#endif // !CONFIG_IDF_TARGET_ESP32C6 TODO: IDF-8008
|
2017-06-02 05:47:23 -04:00
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
esp_err_t rtc_isr_register(intr_handler_t handler, void* handler_arg, uint32_t rtc_intr_mask, uint32_t flags)
|
2017-06-02 05:47:23 -04:00
|
|
|
{
|
2024-01-01 22:16:55 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4 || CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-11-18 04:59:05 -05:00
|
|
|
ESP_EARLY_LOGW(TAG, "rtc_isr_register() has not been implemented yet");
|
2022-07-12 07:46:23 -04:00
|
|
|
return ESP_OK;
|
|
|
|
#else
|
2017-06-02 05:47:23 -04:00
|
|
|
esp_err_t err = rtc_isr_ensure_installed();
|
|
|
|
if (err != ESP_OK) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
rtc_isr_handler_t* item = heap_caps_malloc(sizeof(*item), MALLOC_CAP_INTERNAL);
|
2017-06-02 05:47:23 -04:00
|
|
|
if (item == NULL) {
|
|
|
|
return ESP_ERR_NO_MEM;
|
|
|
|
}
|
|
|
|
item->handler = handler;
|
|
|
|
item->handler_arg = handler_arg;
|
|
|
|
item->mask = rtc_intr_mask;
|
2022-05-30 04:47:14 -04:00
|
|
|
item->flags = flags;
|
2017-06-02 05:47:23 -04:00
|
|
|
portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
|
2022-05-30 04:47:14 -04:00
|
|
|
if (flags & RTC_INTR_FLAG_IRAM) {
|
|
|
|
s_rtc_isr_noniram_hook(rtc_intr_mask);
|
|
|
|
} else {
|
|
|
|
s_rtc_isr_noniram_hook_relieve(rtc_intr_mask);
|
|
|
|
}
|
2017-06-02 05:47:23 -04:00
|
|
|
SLIST_INSERT_HEAD(&s_rtc_isr_handler_list, item, next);
|
|
|
|
portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
|
|
|
|
return ESP_OK;
|
2022-07-12 07:46:23 -04:00
|
|
|
#endif
|
2017-06-02 05:47:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
esp_err_t rtc_isr_deregister(intr_handler_t handler, void* handler_arg)
|
|
|
|
{
|
2024-01-01 22:16:55 -05:00
|
|
|
#if CONFIG_IDF_TARGET_ESP32C6 || CONFIG_IDF_TARGET_ESP32H2 || CONFIG_IDF_TARGET_ESP32P4 || CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-11-18 04:59:05 -05:00
|
|
|
ESP_EARLY_LOGW(TAG, "rtc_isr_deregister() has not been implemented yet");
|
2022-07-12 07:46:23 -04:00
|
|
|
return ESP_OK;
|
|
|
|
#else
|
2017-06-02 05:47:23 -04:00
|
|
|
rtc_isr_handler_t* it;
|
|
|
|
rtc_isr_handler_t* prev = NULL;
|
|
|
|
bool found = false;
|
|
|
|
portENTER_CRITICAL(&s_rtc_isr_handler_list_lock);
|
|
|
|
SLIST_FOREACH(it, &s_rtc_isr_handler_list, next) {
|
|
|
|
if (it->handler == handler && it->handler_arg == handler_arg) {
|
|
|
|
if (it == SLIST_FIRST(&s_rtc_isr_handler_list)) {
|
|
|
|
SLIST_REMOVE_HEAD(&s_rtc_isr_handler_list, next);
|
|
|
|
} else {
|
|
|
|
SLIST_REMOVE_AFTER(prev, next);
|
|
|
|
}
|
|
|
|
found = true;
|
2022-05-30 04:47:14 -04:00
|
|
|
if (it->flags & RTC_INTR_FLAG_IRAM) {
|
|
|
|
s_rtc_isr_noniram_hook_relieve(it->mask);
|
|
|
|
}
|
2018-07-11 09:31:23 -04:00
|
|
|
free(it);
|
2017-06-02 05:47:23 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
prev = it;
|
|
|
|
}
|
|
|
|
portEXIT_CRITICAL(&s_rtc_isr_handler_list_lock);
|
|
|
|
return found ? ESP_OK : ESP_ERR_INVALID_STATE;
|
2022-07-12 07:46:23 -04:00
|
|
|
#endif
|
2017-06-02 05:47:23 -04:00
|
|
|
}
|
2022-05-30 04:47:14 -04:00
|
|
|
|
2024-01-01 22:16:55 -05:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 && !CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-05-30 04:47:14 -04:00
|
|
|
/**
|
|
|
|
* @brief This helper function can be used to avoid the interrupt to be triggered with cache disabled.
|
|
|
|
* There are lots of different signals on RTC module (i.e. sleep_wakeup, wdt, brownout_detect, etc.)
|
|
|
|
* We might want some of them can be triggered with cache disabled, some are not. Therefore, this function
|
|
|
|
* is created to avoid those which do not want to be triggered with cache disabled.
|
|
|
|
*
|
|
|
|
* @param rtc_intr_mask the mask of the rtc interrupt.
|
|
|
|
*/
|
|
|
|
static void s_rtc_isr_noniram_hook(uint32_t rtc_intr_mask)
|
|
|
|
{
|
|
|
|
rtc_intr_cache |= rtc_intr_mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void s_rtc_isr_noniram_hook_relieve(uint32_t rtc_intr_mask)
|
|
|
|
{
|
|
|
|
rtc_intr_cache &= ~rtc_intr_mask;
|
|
|
|
}
|
2022-07-12 07:46:23 -04:00
|
|
|
#endif
|
|
|
|
|
2022-05-30 04:47:14 -04:00
|
|
|
|
|
|
|
IRAM_ATTR void rtc_isr_noniram_disable(uint32_t cpu)
|
|
|
|
{
|
2024-01-01 22:16:55 -05:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 && !CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-05-30 04:47:14 -04:00
|
|
|
if (rtc_isr_cpu == cpu) {
|
|
|
|
rtc_intr_enabled |= RTCCNTL.int_ena.val;
|
|
|
|
RTCCNTL.int_ena.val &= rtc_intr_cache;
|
|
|
|
}
|
2022-07-12 07:46:23 -04:00
|
|
|
#endif
|
2022-05-30 04:47:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
IRAM_ATTR void rtc_isr_noniram_enable(uint32_t cpu)
|
|
|
|
{
|
2024-01-01 22:16:55 -05:00
|
|
|
#if !CONFIG_IDF_TARGET_ESP32C6 && !CONFIG_IDF_TARGET_ESP32H2 && !CONFIG_IDF_TARGET_ESP32P4 && !CONFIG_IDF_TARGET_ESP32C5 // TODO: IDF-8008
|
2022-05-30 04:47:14 -04:00
|
|
|
if (rtc_isr_cpu == cpu) {
|
|
|
|
RTCCNTL.int_ena.val = rtc_intr_enabled;
|
|
|
|
rtc_intr_enabled = 0;
|
|
|
|
}
|
2022-07-12 07:46:23 -04:00
|
|
|
#endif
|
2022-05-30 04:47:14 -04:00
|
|
|
}
|