board.c 7.4 KB

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