board.c 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /*
  2. * Copyright (c) 2021-2022, 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. */
  10. #include <rtthread.h>
  11. #include "hal/systimer_hal.h"
  12. #include "hal/systimer_ll.h"
  13. #include "esp_private/panic_internal.h"
  14. #include "esp_private/systimer.h"
  15. #include "esp_private/periph_ctrl.h"
  16. #include "esp_intr_alloc.h"
  17. #include "esp_attr.h"
  18. static systimer_hal_context_t systimer_hal;
  19. IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
  20. {
  21. systimer_ll_clear_alarm_int(systimer_hal.dev, SYSTIMER_LL_ALARM_OS_TICK_CORE0);
  22. rt_interrupt_enter();
  23. rt_tick_increase();
  24. rt_interrupt_leave();
  25. }
  26. void rt_hw_systick_init(void)
  27. {
  28. esp_intr_alloc(ETS_SYSTIMER_TARGET0_EDGE_INTR_SOURCE, ESP_INTR_FLAG_IRAM | ESP_INTR_FLAG_LEVEL1, rt_SysTickIsrHandler, &systimer_hal, NULL);
  29. periph_module_enable(PERIPH_SYSTIMER_MODULE);
  30. systimer_hal_init(&systimer_hal);
  31. systimer_hal_tick_rate_ops_t ops = {
  32. .ticks_to_us = systimer_ticks_to_us,
  33. .us_to_ticks = systimer_us_to_ticks,
  34. };
  35. systimer_hal_set_tick_rate_ops(&systimer_hal, &ops);
  36. systimer_ll_set_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_OS_TICK, 0);
  37. systimer_ll_apply_counter_value(systimer_hal.dev, SYSTIMER_LL_COUNTER_OS_TICK);
  38. systimer_hal_connect_alarm_counter(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, SYSTIMER_LL_COUNTER_OS_TICK);
  39. systimer_hal_set_alarm_period(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, 1000000UL / RT_TICK_PER_SECOND);
  40. systimer_hal_select_alarm_mode(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0, SYSTIMER_ALARM_MODE_PERIOD);
  41. systimer_hal_counter_can_stall_by_cpu(&systimer_hal, 1, 0, true);
  42. systimer_hal_enable_alarm_int(&systimer_hal, SYSTIMER_LL_ALARM_OS_TICK_CORE0);
  43. systimer_hal_enable_counter(&systimer_hal, SYSTIMER_LL_COUNTER_OS_TICK);
  44. }
  45. void rt_hw_board_init(void)
  46. {
  47. rt_hw_systick_init();
  48. /* Board underlying hardware initialization */
  49. #ifdef RT_USING_COMPONENTS_INIT
  50. rt_components_board_init();
  51. #endif
  52. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  53. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  54. #endif
  55. }