board.c 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * Copyright (c) 2021-2024 RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-06-02 supperthomas first version
  9. * 2024-12-08 wumingzi support rt_hw_us_delay
  10. */
  11. #include <rtthread.h>
  12. #include "rttypes.h"
  13. #include "hal/systimer_hal.h"
  14. #include "hal/systimer_ll.h"
  15. #include "esp_private/panic_internal.h"
  16. #include "esp_private/systimer.h"
  17. #include "esp_private/periph_ctrl.h"
  18. #include "esp_intr_alloc.h"
  19. #include "esp_attr.h"
  20. #include "esp_timer.h"
  21. #include "driver/gptimer.h"
  22. static systimer_hal_context_t systimer_hal;
  23. IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
  24. {
  25. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_LL_ALARM_OS_TICK_CORE0);
  26. rt_interrupt_enter();
  27. rt_tick_increase();
  28. rt_interrupt_leave();
  29. }
  30. void rt_hw_systick_init(void)
  31. {
  32. esp_intr_alloc(ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1, rt_SysTickIsrHandler, &systimer_hal, NULL);
  33. periph_module_enable(PERIPH_SYSTIMER_MODULE);
  34. systimer_hal_init(&systimer_hal);
  35. systimer_hal_tick_rate_ops_t ops = {
  36. .ticks_to_us = systimer_ticks_to_us,
  37. .us_to_ticks = systimer_us_to_ticks,
  38. };
  39. systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
  40. systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_OS_TICK, 0);
  41. systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_OS_TICK);
  42. systimer_hal_connect_alarm_counter(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, SYSTIMER_LL_COUNTER_OS_TICK);
  43. systimer_hal_set_alarm_period(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, 1000000UL / RT_TICK_PER_SECOND);
  44. systimer_hal_select_alarm_mode(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, SYSTIMER_ALARM_MODE_PERIOD);
  45. systimer_hal_counter_can_stall_by_cpu(&systimer_hal, 1, 0, true);
  46. systimer_hal_enable_alarm_int(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0);
  47. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_LL_COUNTER_OS_TICK);
  48. }
  49. void rt_hw_board_init(void)
  50. {
  51. rt_hw_systick_init();
  52. /* Board underlying hardware initialization */
  53. #ifdef RT_USING_COMPONENTS_INIT
  54. rt_components_board_init();
  55. #endif
  56. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  57. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  58. #endif
  59. }
  60. static gptimer_handle_t gptimer_hw_us = NULL;
  61. static int delay_us_init(void)
  62. {
  63. gptimer_config_t timer_config = {
  64. .clk_src = GPTIMER_CLK_SRC_DEFAULT,
  65. .direction = GPTIMER_COUNT_UP,
  66. .resolution_hz = 1 * 1000 * 1000, /* 1MHz, 1 tick = 1us*/
  67. };
  68. ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_hw_us));
  69. ESP_ERROR_CHECK(gptimer_enable(gptimer_hw_us));
  70. return RT_EOK;
  71. }
  72. INIT_DEVICE_EXPORT(delay_us_init);
  73. void rt_hw_us_delay(rt_uint32_t us)
  74. {
  75. uint64_t count = 0;
  76. ESP_ERROR_CHECK(gptimer_start(gptimer_hw_us));
  77. ESP_ERROR_CHECK(gptimer_set_raw_count(gptimer_hw_us, 0));
  78. /* Retrieve the timestamp at anytime*/
  79. while(count < (uint64_t)us)
  80. {
  81. ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer_hw_us, &count));
  82. }
  83. ESP_ERROR_CHECK(gptimer_stop(gptimer_hw_us));
  84. }