1 // Copyright 2020-2021 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <stdint.h>
17 #include <stdbool.h>
18 #include "soc/systimer_struct.h"
19 #include "hal/assert.h"
20 
21 #define SYSTIMER_LL_COUNTER_CLOCK (0) // Counter used for "wallclock" time
22 #define SYSTIMER_LL_ALARM_CLOCK   (2) // Alarm used for "wallclock" time
23 
24 #define SYSTIMER_LL_TICKS_PER_US  (80) // 80 systimer ticks == 1us
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 // All these functions get invoked either from ISR or HAL that linked to IRAM.
31 // Always inline these functions even no gcc optimization is applied.
32 
33 /******************* Clock *************************/
34 
systimer_ll_enable_clock(systimer_dev_t * dev,bool en)35 __attribute__((always_inline)) static inline void systimer_ll_enable_clock(systimer_dev_t *dev, bool en)
36 {
37     dev->conf.clk_en = en;
38 }
39 
40 /******************* Counter *************************/
41 
systimer_ll_enable_counter(systimer_dev_t * dev,uint32_t counter_id,bool en)42 __attribute__((always_inline)) static inline void systimer_ll_enable_counter(systimer_dev_t *dev, uint32_t counter_id, bool en)
43 {
44     // ESP32-S2 only has one counter in systimer group
45     (void)dev;
46     (void)counter_id;
47 }
48 
systimer_ll_counter_can_stall_by_cpu(systimer_dev_t * dev,uint32_t counter_id,uint32_t cpu_id,bool can)49 __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)
50 {
51     (void)dev;
52     (void)counter_id;
53     (void)cpu_id;
54     (void)can;
55 }
56 
systimer_ll_counter_snapshot(systimer_dev_t * dev,uint32_t counter_id)57 __attribute__((always_inline)) static inline void systimer_ll_counter_snapshot(systimer_dev_t *dev, uint32_t counter_id)
58 {
59     (void)counter_id;
60     dev->update.timer_update = 1;
61 }
62 
systimer_ll_is_counter_value_valid(systimer_dev_t * dev,uint32_t counter_id)63 __attribute__((always_inline)) static inline bool systimer_ll_is_counter_value_valid(systimer_dev_t *dev, uint32_t counter_id)
64 {
65     (void)counter_id;
66     return dev->update.timer_value_valid;
67 }
68 
systimer_ll_set_counter_value(systimer_dev_t * dev,uint32_t counter_id,uint64_t value)69 __attribute__((always_inline)) static inline void systimer_ll_set_counter_value(systimer_dev_t *dev, uint32_t counter_id, uint64_t value)
70 {
71     (void)counter_id;
72     dev->load_hi.timer_load_hi = value >> 32;
73     dev->load_lo.timer_load_lo = value;
74 }
75 
systimer_ll_get_counter_value_low(systimer_dev_t * dev,uint32_t counter_id)76 __attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_value_low(systimer_dev_t *dev, uint32_t counter_id)
77 {
78     return dev->value_lo.timer_value_lo;
79 }
80 
systimer_ll_get_counter_value_high(systimer_dev_t * dev,uint32_t counter_id)81 __attribute__((always_inline)) static inline uint32_t systimer_ll_get_counter_value_high(systimer_dev_t *dev, uint32_t counter_id)
82 {
83     return dev->value_hi.timer_value_hi;
84 }
85 
systimer_ll_apply_counter_value(systimer_dev_t * dev,uint32_t counter_id)86 __attribute__((always_inline)) static inline void systimer_ll_apply_counter_value(systimer_dev_t *dev, uint32_t counter_id)
87 {
88     dev->load.timer_load = 1;
89 }
90 
systimer_ll_set_step_for_pll(systimer_dev_t * dev,uint32_t step)91 __attribute__((always_inline)) static inline void systimer_ll_set_step_for_pll(systimer_dev_t *dev, uint32_t step)
92 {
93     dev->step.timer_pll_step = step;
94 }
95 
systimer_ll_set_step_for_xtal(systimer_dev_t * dev,uint32_t step)96 __attribute__((always_inline)) static inline void systimer_ll_set_step_for_xtal(systimer_dev_t *dev, uint32_t step)
97 {
98     dev->step.timer_xtal_step = step;
99 }
100 
101 /******************* Alarm *************************/
102 
systimer_ll_set_alarm_target(systimer_dev_t * dev,uint32_t alarm_id,uint64_t value)103 __attribute__((always_inline)) static inline void systimer_ll_set_alarm_target(systimer_dev_t *dev, uint32_t alarm_id, uint64_t value)
104 {
105     dev->target_val[alarm_id].hi.timer_target_hi = value >> 32;
106     dev->target_val[alarm_id].lo.timer_target_lo = value;
107 }
108 
systimer_ll_get_alarm_target(systimer_dev_t * dev,uint32_t alarm_id)109 __attribute__((always_inline)) static inline uint64_t systimer_ll_get_alarm_target(systimer_dev_t *dev, uint32_t alarm_id)
110 {
111     return ((uint64_t)(dev->target_val[alarm_id].hi.timer_target_hi) << 32) | dev->target_val[alarm_id].lo.timer_target_lo;
112 }
113 
systimer_ll_connect_alarm_counter(systimer_dev_t * dev,uint32_t alarm_id,uint32_t counter_id)114 __attribute__((always_inline)) static inline void systimer_ll_connect_alarm_counter(systimer_dev_t *dev, uint32_t alarm_id, uint32_t counter_id)
115 {
116     // On esp32-s2, counter int the systimer is fixed connectred to other three alarm comparators
117     (void)dev;
118     (void)alarm_id;
119     (void)counter_id;
120 }
121 
systimer_ll_enable_alarm_oneshot(systimer_dev_t * dev,uint32_t alarm_id)122 __attribute__((always_inline)) static inline void systimer_ll_enable_alarm_oneshot(systimer_dev_t *dev, uint32_t alarm_id)
123 {
124     dev->target_conf[alarm_id].target_period_mode = 0;
125 }
126 
systimer_ll_enable_alarm_period(systimer_dev_t * dev,uint32_t alarm_id)127 __attribute__((always_inline)) static inline void systimer_ll_enable_alarm_period(systimer_dev_t *dev, uint32_t alarm_id)
128 {
129     dev->target_conf[alarm_id].target_period_mode = 1;
130 }
131 
systimer_ll_set_alarm_period(systimer_dev_t * dev,uint32_t alarm_id,uint32_t period)132 __attribute__((always_inline)) static inline void systimer_ll_set_alarm_period(systimer_dev_t *dev, uint32_t alarm_id, uint32_t period)
133 {
134     HAL_ASSERT(period < (1 << 30));
135     dev->target_conf[alarm_id].target_period = period;
136 }
137 
systimer_ll_get_alarm_period(systimer_dev_t * dev,uint32_t alarm_id)138 __attribute__((always_inline)) static inline uint32_t systimer_ll_get_alarm_period(systimer_dev_t *dev, uint32_t alarm_id)
139 {
140     return dev->target_conf[alarm_id].target_period;
141 }
142 
systimer_ll_apply_alarm_value(systimer_dev_t * dev,uint32_t alarm_id)143 __attribute__((always_inline)) static inline void systimer_ll_apply_alarm_value(systimer_dev_t *dev, uint32_t alarm_id)
144 {
145     (void)dev;
146     (void)alarm_id;
147 }
148 
systimer_ll_enable_alarm(systimer_dev_t * dev,uint32_t alarm_id,bool en)149 __attribute__((always_inline)) static inline void systimer_ll_enable_alarm(systimer_dev_t *dev, uint32_t alarm_id, bool en)
150 {
151     dev->target_conf[alarm_id].target_work_en = en;
152 }
153 
154 /******************* Interrupt *************************/
155 
systimer_ll_enable_alarm_int(systimer_dev_t * dev,uint32_t alarm_id,bool en)156 __attribute__((always_inline)) static inline void systimer_ll_enable_alarm_int(systimer_dev_t *dev, uint32_t alarm_id, bool en)
157 {
158     if (en) {
159         dev->int_ena.val |= 1 << alarm_id;
160     } else {
161         dev->int_ena.val &= ~(1 << alarm_id);
162     }
163 }
164 
systimer_ll_is_alarm_int_fired(systimer_dev_t * dev,uint32_t alarm_id)165 __attribute__((always_inline)) static inline bool systimer_ll_is_alarm_int_fired(systimer_dev_t *dev, uint32_t alarm_id)
166 {
167     return dev->int_raw.val & (1 << alarm_id);
168 }
169 
systimer_ll_clear_alarm_int(systimer_dev_t * dev,uint32_t alarm_id)170 __attribute__((always_inline)) static inline void systimer_ll_clear_alarm_int(systimer_dev_t *dev, uint32_t alarm_id)
171 {
172     dev->int_clr.val |= 1 << alarm_id;
173 }
174 
175 #ifdef __cplusplus
176 }
177 #endif
178