1
0

board.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #include "led.h"
  32. #ifdef __CC_ARM
  33. extern int Image$$RW_IRAM1$$ZI$$Limit;
  34. #define AM_SRAM_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
  35. #elif __ICCARM__
  36. #pragma section="HEAP"
  37. #define AM_SRAM_BEGIN (__segment_end("HEAP"))
  38. #else
  39. extern int __bss_end;
  40. #define NRF_SRAM_BEGIN (&__bss_end)
  41. #endif
  42. #define TICK_RATE_HZ RT_TICK_PER_SECOND
  43. #define SYSTICK_CLOCK_HZ ( 32768UL )
  44. #define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)))
  45. /**
  46. * This is the timer interrupt service routine.
  47. *
  48. */
  49. void am_stimer_cmpr0_isr(void)
  50. {
  51. /* Check the timer interrupt status */
  52. am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);
  53. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  54. if (rt_thread_self() != RT_NULL)
  55. {
  56. /* enter interrupt */
  57. rt_interrupt_enter();
  58. rt_tick_increase();
  59. /* leave interrupt */
  60. rt_interrupt_leave();
  61. }
  62. }
  63. /**
  64. * This is the SysTick Configure.
  65. *
  66. */
  67. void SysTick_Configuration(void)
  68. {
  69. /* Enable compare A interrupt in STIMER */
  70. am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA);
  71. /* Enable the timer interrupt in the NVIC, making sure to use the appropriate priority level */
  72. am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0);
  73. /* Configure the STIMER and run */
  74. am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
  75. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  76. am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ |
  77. AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
  78. }
  79. /**
  80. * This is the low power operation.
  81. * This function enables several power-saving features of the MCU, and
  82. * disables some of the less-frequently used peripherals. It also sets the
  83. * system clock to 24 MHz.
  84. */
  85. void am_low_power_init(void)
  86. {
  87. /* Enable internal buck converters */
  88. am_hal_pwrctrl_bucks_init();
  89. /* Initialize for low power in the power control block */
  90. am_hal_pwrctrl_low_power_init();
  91. /* Turn off the voltage comparator as this is enabled on reset */
  92. am_hal_vcomp_disable();
  93. /* Run the RTC off the LFRC */
  94. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  95. /* Stop the XT and LFRC */
  96. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT);
  97. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC);
  98. /* Disable the RTC */
  99. am_hal_rtc_osc_disable();
  100. }
  101. /**
  102. * This is the deep power save.
  103. *
  104. */
  105. void deep_power_save(void)
  106. {
  107. am_hal_interrupt_master_disable();
  108. am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
  109. am_hal_interrupt_master_enable();
  110. }
  111. /**
  112. * This function will initial APOLLO2 board.
  113. */
  114. void rt_hw_board_init(void)
  115. {
  116. /* Set the system clock to maximum frequency */
  117. am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);
  118. /* Set the default cache configuration */
  119. am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);
  120. /* Configure the board for low power operation */
  121. am_low_power_init();
  122. /* Config SysTick */
  123. SysTick_Configuration();
  124. #ifdef RT_USING_IDLE_HOOK
  125. /* Set sleep deep mode */
  126. rt_thread_idle_sethook(deep_power_save);
  127. #ifndef NO_FPU
  128. /* Enable the floating point module, and configure the core for lazy stacking */
  129. am_hal_sysctrl_fpu_enable();
  130. am_hal_sysctrl_fpu_stacking_enable(true);
  131. #else
  132. am_hal_sysctrl_fpu_disable();
  133. #endif
  134. /* Turn off unused Flash & SRAM */
  135. am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_FLASH512K);
  136. am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_SRAM32K);
  137. #endif
  138. #ifdef RT_USING_CONSOLE
  139. rt_hw_uart_init();
  140. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  141. #endif
  142. #ifdef RT_USING_COMPONENTS_INIT
  143. rt_components_board_init();
  144. #endif
  145. #ifdef RT_USING_HEAP
  146. rt_system_heap_init((void*)AM_SRAM_BEGIN, (void*)AM_SRAM_END);
  147. #endif
  148. }
  149. /*@}*/