1
0

board.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-09-14 Haley first implementation
  9. */
  10. #include "board.h"
  11. #include <rtthread.h>
  12. #include <rthw.h>
  13. #include "am_mcu_apollo.h"
  14. #include "hal/am_hal_clkgen.h"
  15. #include "hal/am_hal_cachectrl.h"
  16. #include "uart.h"
  17. #ifdef __CC_ARM
  18. extern int Image$$RW_IRAM1$$ZI$$Limit;
  19. #define AM_SRAM_BEGIN (&Image$$RW_IRAM1$$ZI$$Limit)
  20. #elif __ICCARM__
  21. #pragma section="HEAP"
  22. #define AM_SRAM_BEGIN (__segment_end("HEAP"))
  23. #else
  24. extern int __bss_end;
  25. #define AM_SRAM_BEGIN (&__bss_end)
  26. #endif
  27. #define TICK_RATE_HZ RT_TICK_PER_SECOND
  28. #define SYSTICK_CLOCK_HZ ( 32768UL )
  29. #define WAKE_INTERVAL ( (uint32_t) ((SYSTICK_CLOCK_HZ / TICK_RATE_HZ)))
  30. /**
  31. * This is the timer interrupt service routine.
  32. *
  33. */
  34. void am_stimer_cmpr0_isr(void)
  35. {
  36. /* Check the timer interrupt status */
  37. am_hal_stimer_int_clear(AM_HAL_STIMER_INT_COMPAREA);
  38. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  39. if (rt_thread_self() != RT_NULL)
  40. {
  41. /* enter interrupt */
  42. rt_interrupt_enter();
  43. rt_tick_increase();
  44. /* leave interrupt */
  45. rt_interrupt_leave();
  46. }
  47. }
  48. /**
  49. * This is the SysTick Configure.
  50. *
  51. */
  52. void SysTick_Configuration(void)
  53. {
  54. /* Enable compare A interrupt in STIMER */
  55. am_hal_stimer_int_enable(AM_HAL_STIMER_INT_COMPAREA);
  56. /* Enable the timer interrupt in the NVIC, making sure to use the appropriate priority level */
  57. am_hal_interrupt_enable(AM_HAL_INTERRUPT_STIMER_CMPR0);
  58. /* Configure the STIMER and run */
  59. am_hal_stimer_config(AM_HAL_STIMER_CFG_CLEAR | AM_HAL_STIMER_CFG_FREEZE);
  60. am_hal_stimer_compare_delta_set(0, WAKE_INTERVAL);
  61. am_hal_stimer_config(AM_HAL_STIMER_XTAL_32KHZ |
  62. AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
  63. }
  64. /**
  65. * This is the low power operation.
  66. * This function enables several power-saving features of the MCU, and
  67. * disables some of the less-frequently used peripherals. It also sets the
  68. * system clock to 24 MHz.
  69. */
  70. void am_low_power_init(void)
  71. {
  72. /* Enable internal buck converters */
  73. am_hal_pwrctrl_bucks_init();
  74. /* Initialize for low power in the power control block */
  75. am_hal_pwrctrl_low_power_init();
  76. /* Turn off the voltage comparator as this is enabled on reset */
  77. am_hal_vcomp_disable();
  78. #ifndef RT_USING_RTC
  79. /* Run the RTC off the LFRC */
  80. am_hal_rtc_osc_select(AM_HAL_RTC_OSC_LFRC);
  81. /* Stop the XT and LFRC */
  82. am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_XT);
  83. //am_hal_clkgen_osc_stop(AM_HAL_CLKGEN_OSC_LFRC);
  84. /* Disable the RTC */
  85. am_hal_rtc_osc_disable();
  86. #endif
  87. }
  88. /**
  89. * This is the deep power save.
  90. *
  91. */
  92. void deep_power_save(void)
  93. {
  94. am_hal_interrupt_master_disable();
  95. am_hal_sysctrl_sleep(AM_HAL_SYSCTRL_SLEEP_DEEP);
  96. am_hal_interrupt_master_enable();
  97. }
  98. /**
  99. * This function will initial APOLLO2 board.
  100. */
  101. void rt_hw_board_init(void)
  102. {
  103. /* Set the system clock to maximum frequency */
  104. am_hal_clkgen_sysclk_select(AM_HAL_CLKGEN_SYSCLK_MAX);
  105. /* Set the default cache configuration */
  106. am_hal_cachectrl_enable(&am_hal_cachectrl_defaults);
  107. /* Configure the board for low power operation */
  108. am_low_power_init();
  109. /* Config SysTick */
  110. SysTick_Configuration();
  111. #ifdef RT_USING_IDLE_HOOK
  112. /* Set sleep deep mode */
  113. rt_thread_idle_sethook(deep_power_save);
  114. #ifndef NO_FPU
  115. /* Enable the floating point module, and configure the core for lazy stacking */
  116. am_hal_sysctrl_fpu_enable();
  117. am_hal_sysctrl_fpu_stacking_enable(true);
  118. #else
  119. am_hal_sysctrl_fpu_disable();
  120. #endif
  121. /* Turn off unused Flash & SRAM */
  122. am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_FLASH512K);
  123. //am_hal_pwrctrl_memory_enable(AM_HAL_PWRCTRL_MEMEN_SRAM32K);
  124. #endif
  125. #ifdef RT_USING_SERIAL
  126. rt_hw_uart_init();
  127. #endif
  128. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  129. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  130. #endif
  131. #ifdef RT_USING_COMPONENTS_INIT
  132. rt_components_board_init();
  133. #endif
  134. #ifdef RT_USING_HEAP
  135. rt_system_heap_init((void*)AM_SRAM_BEGIN, (void*)AM_SRAM_END);
  136. #endif
  137. }
  138. /*@}*/