board.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2009 RT-Thread Develop Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2017-09-14 Haley first implementation
  13. */
  14. #include "board.h"
  15. #include <rtthread.h>
  16. #include <rthw.h>
  17. #include "am_mcu_apollo.h"
  18. #include "hal/am_hal_clkgen.h"
  19. #include "hal/am_hal_cachectrl.h"
  20. #include "hw_uart.h"
  21. #define TICK_RATE_HZ RT_TICK_PER_SECOND
  22. #define SYSTICK_CLOCK_HZ ( 32768UL )
  23. #define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)) )
  24. /**
  25. * This is the timer interrupt service routine.
  26. *
  27. */
  28. void am_stimer_cmpr0_isr(void)
  29. {
  30. /* Check the timer interrupt status */
  31. am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);
  32. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  33. if (rt_thread_self() != RT_NULL)
  34. {
  35. /* enter interrupt */
  36. rt_interrupt_enter();
  37. rt_tick_increase();
  38. /* leave interrupt */
  39. rt_interrupt_leave();
  40. }
  41. }
  42. /**
  43. * This is the SysTick Configure.
  44. *
  45. */
  46. void SysTick_Configuration(void)
  47. {
  48. /* Set the main clk */
  49. am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);
  50. /* Enable compare A interrupt in STIMER */
  51. am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA);
  52. /* Enable the timer interrupt in the NVIC */
  53. am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0);
  54. /* Configure the STIMER and run */
  55. am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
  56. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  57. am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ |
  58. AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
  59. }
  60. /**
  61. * This is the CacheCtrl Enable.
  62. *
  63. */
  64. void CacheCtrl_Enable(void)
  65. {
  66. am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);
  67. }
  68. /**
  69. * This is the low power operation.
  70. * This function enables several power-saving features of the MCU, and
  71. * disables some of the less-frequently used peripherals. It also sets the
  72. * system clock to 24 MHz.
  73. */
  74. void am_low_power_init(void)
  75. {
  76. /* Enable internal buck converters */
  77. am_hal_pwrctrl_bucks_init();
  78. /* Initialize for low power in the power control block */
  79. am_hal_pwrctrl_low_power_init();
  80. /* Turn off the voltage comparator as this is enabled on reset */
  81. am_hal_vcomp_disable();
  82. /* Run the RTC off the LFRC */
  83. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  84. /* Stop the XT and LFRC */
  85. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT);
  86. // am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC);
  87. /* Disable the RTC */
  88. am_hal_rtc_osc_disable();
  89. }
  90. /**
  91. * This is the deep power save.
  92. *
  93. */
  94. void deep_power_save(void)
  95. {
  96. am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
  97. }
  98. /**
  99. * This function will initial APOLLO2 board.
  100. */
  101. void rt_hw_board_init(void)
  102. {
  103. /* Set the clock frequency */
  104. SysTick_Configuration();
  105. /* Set the default cache configuration */
  106. CacheCtrl_Enable();
  107. /* Configure the board for low power operation */
  108. //am_low_power_init();
  109. #ifdef RT_USING_IDLE_HOOK
  110. rt_thread_idle_sethook(deep_power_save);
  111. #endif
  112. #ifdef RT_USING_CONSOLE
  113. rt_hw_uart_init();
  114. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  115. #endif
  116. #ifdef RT_USING_COMPONENTS_INIT
  117. rt_components_board_init();
  118. #endif
  119. }
  120. /*@}*/