board.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /******************************************************************//**
  2. * @file board.c
  3. * @brief USART driver of RT-Thread RTOS for EFM32
  4. * COPYRIGHT (C) 2011, RT-Thread Development Team
  5. * @author onelife
  6. * @version 0.4 beta
  7. **********************************************************************
  8. * @section License
  9. * The license and distribution terms for this file may be found in the file LICENSE in this
  10. * distribution or at http://www.rt-thread.org/license/LICENSE
  11. **********************************************************************
  12. * @section Change Logs
  13. * Date Author Notes
  14. * 2010-12-21 onelife Initial creation for EFM32
  15. *********************************************************************/
  16. /******************************************************************//**
  17. * @addtogroup efm32
  18. * @{
  19. *********************************************************************/
  20. /* Includes -------------------------------------------------------------------*/
  21. #include "board.h"
  22. /* Private typedef -------------------------------------------------------------*/
  23. /* Private define --------------------------------------------------------------*/
  24. #define IS_NVIC_VECTTAB(VECTTAB) (((VECTTAB) == RAM_MEM_BASE) || \
  25. ((VECTTAB) == FLASH_MEM_BASE))
  26. #define IS_NVIC_OFFSET(OFFSET) ((OFFSET) < 0x000FFFFF)
  27. /******************************************************************//**
  28. * @addtogroup SysTick_clock_source
  29. * @{
  30. *********************************************************************/
  31. #define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB)
  32. #define SysTick_CLKSource_HCLK ((uint32_t)0x00000004)
  33. #define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \
  34. ((SOURCE) == SysTick_CLKSource_HCLK_Div8))
  35. /******************************************************************//**
  36. * @}
  37. *********************************************************************/
  38. /* Private macro --------------------------------------------------------------*/
  39. /* Private variables ------------------------------------------------------------*/
  40. /* Private function prototypes ---------------------------------------------------*/
  41. /* Private functions ------------------------------------------------------------*/
  42. /******************************************************************//**
  43. * @brief
  44. * Set the allocation and offset of the vector table
  45. *
  46. * @details
  47. *
  48. * @note
  49. *
  50. * @param[in] NVIC_VectTab
  51. * Indicate the vector table is allocated in RAM or ROM
  52. *
  53. * @param[in] Offset
  54. * The vector table offset
  55. *********************************************************************/
  56. static void NVIC_SetVectorTable(rt_uint32_t NVIC_VectTab, rt_uint32_t Offset)
  57. {
  58. /* Check the parameters */
  59. RT_ASSERT(IS_NVIC_VECTTAB(NVIC_VectTab));
  60. RT_ASSERT(IS_NVIC_OFFSET(Offset));
  61. SCB->VTOR = NVIC_VectTab | (Offset & (rt_uint32_t)0x1FFFFF80);
  62. }
  63. /******************************************************************//**
  64. * @brief
  65. * Configure the address of vector table
  66. *
  67. * @details
  68. *
  69. * @note
  70. *
  71. *********************************************************************/
  72. static void NVIC_Configuration(void)
  73. {
  74. #ifdef VECT_TAB_RAM
  75. /* Set the vector table allocated at 0x20000000 */
  76. NVIC_SetVectorTable(RAM_MEM_BASE, 0x0);
  77. #else /* VECT_TAB_FLASH */
  78. /* Set the vector table allocated at 0x00000000 */
  79. NVIC_SetVectorTable(FLASH_MEM_BASE, 0x0);
  80. #endif
  81. /* Set NVIC Preemption Priority Bits: 0 bit for pre-emption, 4 bits for subpriority */
  82. NVIC_SetPriorityGrouping(0x7UL);
  83. /* Set Base Priority Mask Register */
  84. __set_BASEPRI(EFM32_BASE_PRI_DEFAULT);
  85. }
  86. /******************************************************************//**
  87. * @brief
  88. * Enable high frequency crystal oscillator (HFXO), and set HFCLK domain to use HFXO as
  89. * source.
  90. *
  91. * @details
  92. *
  93. * @note
  94. *
  95. *********************************************************************/
  96. static void switchToHFXO(void)
  97. {
  98. CMU_TypeDef *cmu = CMU;
  99. /* Turning on HFXO to increase frequency accuracy. */
  100. /* Waiting until oscillator is stable */
  101. cmu->OSCENCMD = CMU_OSCENCMD_HFXOEN;
  102. while (!(cmu->STATUS && CMU_STATUS_HFXORDY)) ;
  103. /* Switching the CPU clock source to HFXO */
  104. cmu->CMD = CMU_CMD_HFCLKSEL_HFXO;
  105. /* Turning off the high frequency RC Oscillator (HFRCO) */
  106. /* GENERATL WARNING! Make sure not to disable the current
  107. * source of the HFCLK. */
  108. cmu->OSCENCMD = CMU_OSCENCMD_HFRCODIS;
  109. }
  110. /******************************************************************//**
  111. * @brief
  112. * Configure the SysTick clock source
  113. *
  114. * @details
  115. *
  116. * @note
  117. *
  118. * @param[in] SysTick_CLKSource
  119. * Specifies the SysTick clock source.
  120. *
  121. * @arg SysTick_CLKSource_HCLK_Div8
  122. * AHB clock divided by 8 selected as SysTick clock source.
  123. *
  124. * @arg SysTick_CLKSource_HCLK
  125. * AHB clock selected as SysTick clock source.
  126. *********************************************************************/
  127. static void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  128. {
  129. /* Check the parameters */
  130. RT_ASSERT(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  131. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  132. {
  133. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  134. }
  135. else
  136. {
  137. SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  138. }
  139. }
  140. /******************************************************************//**
  141. * @brief
  142. * Configure the SysTick for OS tick.
  143. *
  144. * @details
  145. *
  146. * @note
  147. *
  148. *********************************************************************/
  149. static void SysTick_Configuration(void)
  150. {
  151. rt_uint32_t core_clock;
  152. rt_uint32_t cnts;
  153. switchToHFXO();
  154. core_clock = SystemCoreClockGet();
  155. cnts = core_clock / RT_TICK_PER_SECOND;
  156. SysTick_Config(cnts);
  157. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  158. }
  159. /******************************************************************//**
  160. * @brief
  161. * Initialize the board.
  162. *
  163. * @details
  164. *
  165. * @note
  166. *
  167. *********************************************************************/
  168. void rt_hw_board_init(void)
  169. {
  170. /* Chip errata */
  171. CHIP_Init();
  172. /* NVIC Configuration */
  173. NVIC_Configuration();
  174. /* Configure external oscillator */
  175. SystemHFXOClockSet(EFM32_HFXO_FREQUENCY);
  176. /* Configure the SysTick */
  177. SysTick_Configuration();
  178. }
  179. /******************************************************************//**
  180. * @brief
  181. * Initialize the hardware drivers.
  182. *
  183. * @details
  184. *
  185. * @note
  186. *
  187. *********************************************************************/
  188. void rt_hw_driver_init(void)
  189. {
  190. CMU_ClockEnable(cmuClock_HFPER, true);
  191. /* Enable GPIO */
  192. CMU_ClockEnable(cmuClock_GPIO, true);
  193. /* Enabling clock to the interface of the low energy modules */
  194. CMU_ClockEnable(cmuClock_CORELE, true);
  195. /* Initialize DMA */
  196. rt_hw_dma_init();
  197. /* Initialize USART */
  198. rt_hw_usart_init();
  199. /* Initialize Timer */
  200. rt_hw_timer_init();
  201. /* Initialize ADC */
  202. rt_hw_adc_init();
  203. /* Initialize ACMP */
  204. rt_hw_acmp_init();
  205. /* Initialize IIC */
  206. rt_hw_iic_init();
  207. rt_console_set_device(CONSOLE_DEVICE);
  208. }
  209. /******************************************************************//**
  210. * @}
  211. *********************************************************************/