1 // Copyright 2020 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
15 #include "test_utils.h"
16 #include "driver/periph_ctrl.h"
17 #include "soc/periph_defs.h"
18 #include "hal/timer_hal.h"
19
20 #define TIMER_GROUP_ID (1)
21 #define TIMER_ID (0)
22 static timer_hal_context_t timer_hal;
23
ref_clock_init(void)24 void ref_clock_init(void)
25 {
26 periph_module_enable(PERIPH_TIMG1_MODULE);
27 timer_hal_init(&timer_hal, TIMER_GROUP_ID, TIMER_ID);
28 timer_hal_set_use_xtal(&timer_hal, true); // Select XTAL, so ref_clock is independent of the APL clock
29 timer_hal_set_divider(&timer_hal, 40); // Resolution is configured to 1MHz
30 timer_hal_set_counter_increase(&timer_hal, true); // increase mode
31 timer_hal_set_counter_enable(&timer_hal, false); // stop timer from running
32 timer_hal_set_counter_value(&timer_hal, 0); // initial count value to zero
33 timer_hal_intr_disable(&timer_hal); // disable interrupt
34 timer_hal_set_alarm_enable(&timer_hal, false); // we don't need to generate any interrupt
35 timer_hal_set_auto_reload(&timer_hal, false);
36 timer_hal_set_counter_enable(&timer_hal, true); // start counter
37 }
38
ref_clock_deinit(void)39 void ref_clock_deinit(void)
40 {
41 timer_hal_set_counter_enable(&timer_hal, false); // stop timer from running
42 periph_module_disable(PERIPH_TIMG1_MODULE);
43 }
44
ref_clock_get(void)45 uint64_t ref_clock_get(void)
46 {
47 uint64_t count_value = 0;
48 timer_hal_get_counter_value(&timer_hal, &count_value);
49 return count_value;
50 }
51