board.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * File : board.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2006 - 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-09-14 Haley first implementation
  23. */
  24. #include "board.h"
  25. #include <rtthread.h>
  26. #include <rthw.h>
  27. #include "am_mcu_apollo.h"
  28. #include "hal/am_hal_clkgen.h"
  29. #include "hal/am_hal_cachectrl.h"
  30. #include "uart.h"
  31. #ifdef __CC_ARM
  32. extern int Image$$RW_IRAM1$$ZI$$Limit;
  33. #define AM_SRAM_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
  34. #elif __ICCARM__
  35. #pragma section="HEAP"
  36. #define AM_SRAM_BEGIN (__segment_end("HEAP"))
  37. #else
  38. extern int __bss_end;
  39. #define AM_SRAM_BEGIN (&__bss_end)
  40. #endif
  41. #define TICK_RATE_HZ RT_TICK_PER_SECOND
  42. #define SYSTICK_CLOCK_HZ ( 32768UL )
  43. #define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)))
  44. /**
  45. * This is the timer interrupt service routine.
  46. *
  47. */
  48. void am_stimer_cmpr0_isr(void)
  49. {
  50. /* Check the timer interrupt status */
  51. am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);
  52. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  53. if (rt_thread_self() != RT_NULL)
  54. {
  55. /* enter interrupt */
  56. rt_interrupt_enter();
  57. rt_tick_increase();
  58. /* leave interrupt */
  59. rt_interrupt_leave();
  60. }
  61. }
  62. /**
  63. * This is the SysTick Configure.
  64. *
  65. */
  66. void SysTick_Configuration(void)
  67. {
  68. /* Enable compare A interrupt in STIMER */
  69. am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA);
  70. /* Enable the timer interrupt in the NVIC, making sure to use the appropriate priority level */
  71. am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0);
  72. /* Configure the STIMER and run */
  73. am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
  74. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  75. am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ |
  76. AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
  77. }
  78. /**
  79. * This is the low power operation.
  80. * This function enables several power-saving features of the MCU, and
  81. * disables some of the less-frequently used peripherals. It also sets the
  82. * system clock to 24 MHz.
  83. */
  84. void am_low_power_init(void)
  85. {
  86. /* Enable internal buck converters */
  87. am_hal_pwrctrl_bucks_init();
  88. /* Initialize for low power in the power control block */
  89. am_hal_pwrctrl_low_power_init();
  90. /* Turn off the voltage comparator as this is enabled on reset */
  91. am_hal_vcomp_disable();
  92. /* Run the RTC off the LFRC */
  93. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  94. /* Stop the XT and LFRC */
  95. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT);
  96. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC);
  97. /* Disable the RTC */
  98. am_hal_rtc_osc_disable();
  99. }
  100. /**
  101. * This is the deep power save.
  102. *
  103. */
  104. void deep_power_save(void)
  105. {
  106. am_hal_interrupt_master_disable();
  107. am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
  108. am_hal_interrupt_master_enable();
  109. }
  110. /**
  111. * This function will initial APOLLO2 board.
  112. */
  113. void rt_hw_board_init(void)
  114. {
  115. /* Set the system clock to maximum frequency */
  116. am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);
  117. /* Set the default cache configuration */
  118. am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);
  119. /* Configure the board for low power operation */
  120. am_low_power_init();
  121. /* Config SysTick */
  122. SysTick_Configuration();
  123. #ifdef RT_USING_IDLE_HOOK
  124. /* Set sleep deep mode */
  125. rt_thread_idle_sethook(deep_power_save);
  126. #ifndef NO_FPU
  127. /* Enable the floating point module, and configure the core for lazy stacking */
  128. am_hal_sysctrl_fpu_enable();
  129. am_hal_sysctrl_fpu_stacking_enable(true);
  130. #else
  131. am_hal_sysctrl_fpu_disable();
  132. #endif
  133. /* Turn off unused Flash & SRAM */
  134. am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_FLASH512K);
  135. am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_SRAM32K);
  136. #endif
  137. #ifdef RT_USING_CONSOLE
  138. rt_hw_uart_init();
  139. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  140. #endif
  141. #ifdef RT_USING_COMPONENTS_INIT
  142. rt_components_board_init();
  143. #endif
  144. #ifdef RT_USING_HEAP
  145. rt_system_heap_init((void*)AM_SRAM_BEGIN, (void*)AM_SRAM_END);
  146. #endif
  147. }
  148. /*@}*/