ref_clock_impl_timergroup.c 1.9 KB

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