2020-11-26 00:06:21 -05:00
|
|
|
// Copyright 2020 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.
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "soc/systimer_struct.h"
|
2021-05-18 22:53:21 -04:00
|
|
|
#include "hal/assert.h"
|
2021-04-02 00:41:21 -04:00
|
|
|
|
|
|
|
#define SYSTIMER_LL_COUNTER_CLOCK (0) // Counter used for "wallclock" time
|
|
|
|
#define SYSTIMER_LL_COUNTER_OS_TICK (1) // Counter used for OS tick
|
|
|
|
#define SYSTIMER_LL_ALARM_OS_TICK_CORE0 (0) // Alarm used for OS tick of CPU core 0
|
|
|
|
#define SYSTIMER_LL_ALARM_CLOCK (2) // Alarm used for "wallclock" time
|
|
|
|
|
2021-04-25 07:38:01 -04:00
|
|
|
#define SYSTIMER_LL_TICKS_PER_US (16) // 16 systimer ticks == 1us
|
|
|
|
|
2020-11-26 00:06:21 -05:00
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// All these functions get invoked either from ISR or HAL that linked to IRAM.
|
|
|
|
// Always inline these functions even no gcc optimization is applied.
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
/******************* Clock *************************/
|
2020-11-26 00:06:21 -05:00
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_clock(systimer_dev_t *dev, bool en)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->conf.clk_en = en;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
/******************* Counter *************************/
|
|
|
|
|
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_counter(systimer_dev_t *dev, uint32_t counter_id, bool en)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
if (en) {
|
|
|
|
dev->conf.val |= 1 << (30 - counter_id);
|
|
|
|
} else {
|
|
|
|
dev->conf.val &= ~(1 << (30 - counter_id));
|
|
|
|
}
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_counter_can_stall_by_cpu(systimer_dev_t *dev, uint32_t counter_id, uint32_t cpu_id, bool can)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
|
|
|
if (can) {
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->conf.val |= 1 << ((28 - counter_id * 2) - cpu_id);
|
2020-11-26 00:06:21 -05:00
|
|
|
} else {
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->conf.val &= ~(1 << ((28 - counter_id * 2) - cpu_id));
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_counter_snapshot(systimer_dev_t *dev, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->unit_op[counter_id].timer_unit_update = 1;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline bool systimer_ll_is_counter_value_valid(systimer_dev_t *dev, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
return dev->unit_op[counter_id].timer_unit_value_valid;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_set_counter_value(systimer_dev_t *dev, uint32_t counter_id, uint64_t value)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->unit_load_val[counter_id].hi.timer_unit_load_hi = value >> 32;
|
|
|
|
dev->unit_load_val[counter_id].lo.timer_unit_load_lo = value & 0xFFFFFFFF;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_value_low(systimer_dev_t *dev, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
return dev->unit_val[counter_id].lo.timer_unit_value_lo;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_value_high(systimer_dev_t *dev, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
return dev->unit_val[counter_id].hi.timer_unit_value_hi;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_apply_counter_value(systimer_dev_t *dev, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->unit_load[counter_id].val = 0x01;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
/******************* Alarm *************************/
|
2020-11-26 00:06:21 -05:00
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_set_alarm_target(systimer_dev_t *dev, uint32_t alarm_id, uint64_t value)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->target_val[alarm_id].hi.timer_target_hi = value >> 32;
|
|
|
|
dev->target_val[alarm_id].lo.timer_target_lo = value & 0xFFFFFFFF;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline uint64_t systimer_ll_get_alarm_target(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
return ((uint64_t)(dev->target_val[alarm_id].hi.timer_target_hi) << 32) | dev->target_val[alarm_id].lo.timer_target_lo;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_connect_alarm_counter(systimer_dev_t *dev, uint32_t alarm_id, uint32_t counter_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->target_conf[alarm_id].target_timer_unit_sel = counter_id;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_oneshot(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->target_conf[alarm_id].target_period_mode = 0;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_period(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->target_conf[alarm_id].target_period_mode = 1;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_set_alarm_period(systimer_dev_t *dev, uint32_t alarm_id, uint32_t period)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-05-18 22:53:21 -04:00
|
|
|
HAL_ASSERT(period < (1 << 26));
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->target_conf[alarm_id].target_period = period;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-08-04 08:33:44 -04:00
|
|
|
__attribute__((always_inline)) static inline uint32_t systimer_ll_get_alarm_period(systimer_dev_t *dev, uint32_t alarm_id)
|
|
|
|
{
|
|
|
|
return dev->target_conf[alarm_id].target_period;
|
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_apply_alarm_value(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->comp_load[alarm_id].val = 0x01;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_alarm(systimer_dev_t *dev, uint32_t alarm_id, bool en)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
if (en) {
|
|
|
|
dev->conf.val |= 1 << (24 - alarm_id);
|
|
|
|
} else {
|
|
|
|
dev->conf.val &= ~(1 << (24 - alarm_id));
|
|
|
|
}
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
/******************* Interrupt *************************/
|
2020-11-26 00:06:21 -05:00
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_enable_alarm_int(systimer_dev_t *dev, uint32_t alarm_id, bool en)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
if (en) {
|
|
|
|
dev->int_ena.val |= 1 << alarm_id;
|
|
|
|
} else {
|
|
|
|
dev->int_ena.val &= ~(1 << alarm_id);
|
|
|
|
}
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline bool systimer_ll_is_alarm_int_fired(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
return dev->int_st.val & (1 << alarm_id);
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
2021-04-02 00:41:21 -04:00
|
|
|
__attribute__((always_inline)) static inline void systimer_ll_clear_alarm_int(systimer_dev_t *dev, uint32_t alarm_id)
|
2020-11-26 00:06:21 -05:00
|
|
|
{
|
2021-04-02 00:41:21 -04:00
|
|
|
dev->int_clr.val |= 1 << alarm_id;
|
2020-11-26 00:06:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|