|
1 | 1 | /* |
2 | | - * SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD |
| 2 | + * SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD |
3 | 3 | * |
4 | 4 | * SPDX-License-Identifier: Apache-2.0 |
5 | 5 | */ |
6 | 6 |
|
| 7 | +#include "sdkconfig.h" |
| 8 | +#include "esp_attr.h" |
| 9 | +#include "freertos/FreeRTOS.h" |
7 | 10 | #include "esp_private/io_mux.h" |
| 11 | +#include "hal/rtc_io_ll.h" |
| 12 | + |
| 13 | +#define RTCIO_RCC_ATOMIC() \ |
| 14 | + for (int _rc_cnt = 1; \ |
| 15 | + _rc_cnt ? (portENTER_CRITICAL(&rtc_spinlock), 1) : 0; \ |
| 16 | + portEXIT_CRITICAL(&rtc_spinlock), _rc_cnt--) |
8 | 17 |
|
9 | 18 | esp_err_t io_mux_set_clock_source(soc_module_clk_t clk_src) |
10 | 19 | { |
11 | 20 | // IO MUX clock source is not selectable |
12 | 21 | return ESP_OK; |
13 | 22 | } |
| 23 | + |
| 24 | +extern portMUX_TYPE rtc_spinlock; |
| 25 | +static portMUX_TYPE s_io_mux_spinlock = portMUX_INITIALIZER_UNLOCKED; |
| 26 | + |
| 27 | +static rtc_io_status_t s_rtc_io_status = { |
| 28 | + .rtc_io_enabled_cnt = { 0 }, |
| 29 | + .rtc_io_using_mask = 0 |
| 30 | +}; |
| 31 | + |
| 32 | +void io_mux_enable_lp_io_clock(gpio_num_t gpio_num, bool enable) |
| 33 | +{ |
| 34 | + portENTER_CRITICAL(&s_io_mux_spinlock); |
| 35 | + if (enable) { |
| 36 | + if (s_rtc_io_status.rtc_io_enabled_cnt[gpio_num] == 0) { |
| 37 | + s_rtc_io_status.rtc_io_using_mask |= (1ULL << gpio_num); |
| 38 | + } |
| 39 | + s_rtc_io_status.rtc_io_enabled_cnt[gpio_num]++; |
| 40 | + } else if (!enable && (s_rtc_io_status.rtc_io_enabled_cnt[gpio_num] > 0)) { |
| 41 | + s_rtc_io_status.rtc_io_enabled_cnt[gpio_num]--; |
| 42 | + if (s_rtc_io_status.rtc_io_enabled_cnt[gpio_num] == 0) { |
| 43 | + s_rtc_io_status.rtc_io_using_mask &= ~(1ULL << gpio_num); |
| 44 | + } |
| 45 | + } |
| 46 | + RTCIO_RCC_ATOMIC() { |
| 47 | + if (s_rtc_io_status.rtc_io_using_mask == 0) { |
| 48 | + rtcio_ll_enable_io_clock(false); |
| 49 | + } else { |
| 50 | + rtcio_ll_enable_io_clock(true); |
| 51 | + } |
| 52 | + } |
| 53 | + portEXIT_CRITICAL(&s_io_mux_spinlock); |
| 54 | +} |
| 55 | + |
| 56 | +void io_mux_force_disable_lp_io_clock(gpio_num_t gpio_num) |
| 57 | +{ |
| 58 | + portENTER_CRITICAL(&s_io_mux_spinlock); |
| 59 | + s_rtc_io_status.rtc_io_enabled_cnt[gpio_num] = 0; |
| 60 | + s_rtc_io_status.rtc_io_using_mask &= ~(1ULL << gpio_num); |
| 61 | + if (s_rtc_io_status.rtc_io_using_mask == 0) { |
| 62 | + RTCIO_RCC_ATOMIC() { |
| 63 | + rtcio_ll_enable_io_clock(false); |
| 64 | + } |
| 65 | + } |
| 66 | + portEXIT_CRITICAL(&s_io_mux_spinlock); |
| 67 | +} |
0 commit comments