board.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /***************************************************************************//**
  2. * @file board.c
  3. * @brief Board support 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
  10. * LICENSE in this 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. * 2011-07-12 onelife Add SWO output enable function
  17. * 2011-12-08 onelife Add giant gecko development kit support
  18. * 2011-12-09 onelife Add giant gecko support
  19. * 2011-12-09 onelife Add LEUART module support
  20. * 2011-12-14 onelife Add LFXO enabling routine in driver initialization
  21. * function
  22. * 2011-12-15 onelife Add MicroSD initialization routine in driver
  23. * initialization function
  24. * 2011-12-29 onelife Add keys and joystick initialization routine in
  25. * driver initialization function
  26. * 2012-02-15 onelife Modify SWO setup function to support giant gecko
  27. ******************************************************************************/
  28. /***************************************************************************//**
  29. * @addtogroup efm32
  30. * @{
  31. ******************************************************************************/
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "board.h"
  34. /* Private typedef -----------------------------------------------------------*/
  35. /* Private define ------------------------------------------------------------*/
  36. #define IS_NVIC_VECTTAB(VECTTAB) (((VECTTAB) == RAM_MEM_BASE) || \
  37. ((VECTTAB) == FLASH_MEM_BASE))
  38. #define IS_NVIC_OFFSET(OFFSET) ((OFFSET) < 0x000FFFFF)
  39. /***************************************************************************//**
  40. * @addtogroup SysTick_clock_source
  41. * @{
  42. ******************************************************************************/
  43. #define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB)
  44. #define SysTick_CLKSource_HCLK ((uint32_t)0x00000004)
  45. #define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \
  46. ((SOURCE) == SysTick_CLKSource_HCLK_Div8))
  47. /***************************************************************************//**
  48. * @}
  49. ******************************************************************************/
  50. /* Private macro -------------------------------------------------------------*/
  51. /* Private variables ---------------------------------------------------------*/
  52. /* Private function prototypes -----------------------------------------------*/
  53. /* Private functions ---------------------------------------------------------*/
  54. /***************************************************************************//**
  55. * @brief
  56. * Set the allocation and offset of the vector table
  57. *
  58. * @details
  59. *
  60. * @note
  61. *
  62. * @param[in] NVIC_VectTab
  63. * Indicate the vector table is allocated in RAM or ROM
  64. *
  65. * @param[in] Offset
  66. * The vector table offset
  67. ******************************************************************************/
  68. static void NVIC_SetVectorTable(
  69. rt_uint32_t NVIC_VectTab,
  70. rt_uint32_t Offset)
  71. {
  72. /* Check the parameters */
  73. RT_ASSERT(IS_NVIC_VECTTAB(NVIC_VectTab));
  74. RT_ASSERT(IS_NVIC_OFFSET(Offset));
  75. SCB->VTOR = NVIC_VectTab | (Offset & (rt_uint32_t)0x1FFFFF80);
  76. }
  77. /***************************************************************************//**
  78. * @brief
  79. * Configure the address of vector table
  80. *
  81. * @details
  82. *
  83. * @note
  84. *
  85. ******************************************************************************/
  86. static void NVIC_Configuration(void)
  87. {
  88. #ifdef VECT_TAB_RAM
  89. /* Set the vector table allocated at 0x20000000 */
  90. NVIC_SetVectorTable(RAM_MEM_BASE, 0x0);
  91. #else /* VECT_TAB_FLASH */
  92. /* Set the vector table allocated at 0x00000000 */
  93. NVIC_SetVectorTable(FLASH_MEM_BASE, 0x0);
  94. #endif
  95. /* Set NVIC Preemption Priority Bits: 0 bit for pre-emption, 4 bits for
  96. subpriority */
  97. NVIC_SetPriorityGrouping(0x7UL);
  98. /* Set Base Priority Mask Register */
  99. __set_BASEPRI(EFM32_BASE_PRI_DEFAULT);
  100. }
  101. /***************************************************************************//**
  102. * @brief
  103. * Enable high frequency crystal oscillator (HFXO), and set HFCLK domain to
  104. * use HFXO as source.
  105. *
  106. * @details
  107. *
  108. * @note
  109. *
  110. ******************************************************************************/
  111. static void efm_hfxo_switch(void)
  112. {
  113. CMU_TypeDef *cmu = CMU;
  114. /* Turning on HFXO to increase frequency accuracy. */
  115. /* Waiting until oscillator is stable */
  116. cmu->OSCENCMD = CMU_OSCENCMD_HFXOEN;
  117. while (!(cmu->STATUS && CMU_STATUS_HFXORDY)) ;
  118. /* Switching the CPU clock source to HFXO */
  119. cmu->CMD = CMU_CMD_HFCLKSEL_HFXO;
  120. /* Turning off the high frequency RC Oscillator (HFRCO) */
  121. /* GENERATL WARNING! Make sure not to disable the current
  122. * source of the HFCLK. */
  123. cmu->OSCENCMD = CMU_OSCENCMD_HFRCODIS;
  124. }
  125. /***************************************************************************//**
  126. * @brief
  127. * Configure the SysTick clock source
  128. *
  129. * @details
  130. *
  131. * @note
  132. *
  133. * @param[in] SysTick_CLKSource
  134. * Specifies the SysTick clock source.
  135. *
  136. * @arg SysTick_CLKSource_HCLK_Div8
  137. * AHB clock divided by 8 selected as SysTick clock source.
  138. *
  139. * @arg SysTick_CLKSource_HCLK
  140. * AHB clock selected as SysTick clock source.
  141. ******************************************************************************/
  142. static void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
  143. {
  144. /* Check the parameters */
  145. RT_ASSERT(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
  146. if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
  147. {
  148. SysTick->CTRL |= SysTick_CLKSource_HCLK;
  149. }
  150. else
  151. {
  152. SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
  153. }
  154. }
  155. /***************************************************************************//**
  156. * @brief
  157. * Configure the SysTick for OS tick.
  158. *
  159. * @details
  160. *
  161. * @note
  162. *
  163. ******************************************************************************/
  164. static void SysTick_Configuration(void)
  165. {
  166. rt_uint32_t core_clock;
  167. rt_uint32_t cnts;
  168. efm_hfxo_switch();
  169. core_clock = SystemCoreClockGet();
  170. cnts = core_clock / RT_TICK_PER_SECOND;
  171. SysTick_Config(cnts);
  172. SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
  173. }
  174. /***************************************************************************//**
  175. * @brief
  176. * Enable SWO.
  177. *
  178. * @details
  179. *
  180. * @note
  181. *
  182. ******************************************************************************/
  183. void efm_swo_setup(void)
  184. {
  185. rt_uint32_t *dwt_ctrl = (rt_uint32_t *) 0xE0001000;
  186. rt_uint32_t *tpiu_prescaler = (rt_uint32_t *) 0xE0040010;
  187. rt_uint32_t *tpiu_protocol = (rt_uint32_t *) 0xE00400F0;
  188. CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
  189. /* Enable Serial wire output pin */
  190. GPIO->ROUTE |= GPIO_ROUTE_SWOPEN;
  191. #if defined(_EFM32_GIANT_FAMILY)
  192. /* Set location 0 */
  193. GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC0;
  194. /* Enable output on pin - GPIO Port F, Pin 2 */
  195. GPIO->P[5].MODEL &= ~(_GPIO_P_MODEL_MODE2_MASK);
  196. GPIO->P[5].MODEL |= GPIO_P_MODEL_MODE2_PUSHPULL;
  197. #else
  198. /* Set location 1 */
  199. GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC1;
  200. /* Enable output on pin */
  201. GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK);
  202. GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL;
  203. #endif
  204. /* Enable debug clock AUXHFRCO */
  205. CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN;
  206. while(!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY));
  207. /* Enable trace in core debug */
  208. CoreDebug->DHCSR |= 1;
  209. CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
  210. /* Enable PC and IRQ sampling output */
  211. *dwt_ctrl = 0x400113FF;
  212. /* Set TPIU prescaler to 16. */
  213. *tpiu_prescaler = 0xf;
  214. /* Set protocol to NRZ */
  215. *tpiu_protocol = 2;
  216. /* Unlock ITM and output data */
  217. ITM->LAR = 0xC5ACCE55;
  218. ITM->TCR = 0x10009;
  219. }
  220. /***************************************************************************//**
  221. * @brief
  222. * Initialize the board.
  223. *
  224. * @details
  225. *
  226. * @note
  227. *
  228. ******************************************************************************/
  229. void rt_hw_board_init(void)
  230. {
  231. /* Chip errata */
  232. CHIP_Init();
  233. /* Initialize DVK board register access */
  234. #if defined(EFM32_GXXX_DK)
  235. DVK_init();
  236. #elif defined(EFM32GG_DK3750)
  237. DVK_init(DVK_Init_EBI);
  238. /* Disable all DVK interrupts */
  239. DVK_disableInterrupt(BC_INTEN_MASK);
  240. DVK_clearInterruptFlags(BC_INTFLAG_MASK);
  241. #endif
  242. /* NVIC Configuration */
  243. NVIC_Configuration();
  244. /* Configure external oscillator */
  245. SystemHFXOClockSet(EFM32_HFXO_FREQUENCY);
  246. /* Configure the SysTick */
  247. SysTick_Configuration();
  248. }
  249. /***************************************************************************//**
  250. * @brief
  251. * Initialize the hardware drivers.
  252. *
  253. * @details
  254. *
  255. * @note
  256. *
  257. ******************************************************************************/
  258. void rt_hw_driver_init(void)
  259. {
  260. CMU_ClockEnable(cmuClock_HFPER, true);
  261. /* Enable GPIO */
  262. CMU_ClockEnable(cmuClock_GPIO, true);
  263. /* Enabling clock to the interface of the low energy modules */
  264. CMU_ClockEnable(cmuClock_CORELE, true);
  265. /* Starting LFXO and waiting until it is stable */
  266. #if defined(EFM32_USING_LFXO)
  267. CMU_OscillatorEnable(cmuOsc_LFXO, true, true);
  268. /* Select LFXO for specified module (and wait for it to stabilize) */
  269. #if (defined(RT_USING_LEUART0) || defined(RT_USING_LEUART1))
  270. CMU_ClockSelectSet(cmuClock_LFB, cmuSelect_LFXO);
  271. #endif
  272. #if defined(RT_USING_RTC)
  273. CMU_ClockSelectSet(cmuClock_LFA,cmuSelect_LFXO);
  274. #endif
  275. #endif
  276. /* Enable SWO */
  277. #if defined(EFM32_SWO_ENABLE)
  278. efm_swo_setup();
  279. #endif
  280. /* Initialize DMA */
  281. rt_hw_dma_init();
  282. /* Initialize USART */
  283. #if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || \
  284. defined(RT_USING_USART2) || defined(RT_USING_UART0) || \
  285. defined(RT_USING_UART1))
  286. rt_hw_usart_init();
  287. #endif
  288. /* Initialize LEUART */
  289. #if (defined(RT_USING_LEUART0) || defined(RT_USING_LEUART1))
  290. rt_hw_leuart_init();
  291. #endif
  292. /* Setup Console */
  293. #if defined(EFM32_GXXX_DK)
  294. DVK_enablePeripheral(DVK_RS232A);
  295. DVK_enablePeripheral(DVK_SPI);
  296. #elif defined(EFM32GG_DK3750)
  297. #if (RT_CONSOLE_DEVICE == EFM_UART1)
  298. DVK_enablePeripheral(DVK_RS232_UART);
  299. #elif (RT_CONSOLE_DEVICE == EFM_LEUART1)
  300. DVK_enablePeripheral(DVK_RS232_LEUART);
  301. #endif
  302. #endif
  303. rt_console_set_device(CONSOLE_DEVICE);
  304. /* Initialize Timer */
  305. #if (defined(RT_USING_TIMER0) || defined(RT_USING_TIMER1) || defined(RT_USING_TIMER2))
  306. rt_hw_timer_init();
  307. #endif
  308. /* Initialize ADC */
  309. #if defined(RT_USING_ADC0)
  310. rt_hw_adc_init();
  311. #endif
  312. /* Initialize ACMP */
  313. #if (defined(RT_USING_ACMP0) || defined(RT_USING_ACMP1))
  314. rt_hw_acmp_init();
  315. #endif
  316. /* Initialize IIC */
  317. #if (defined(RT_USING_IIC0) || defined(RT_USING_IIC1))
  318. rt_hw_iic_init();
  319. #endif
  320. /* Initialize RTC */
  321. #if defined(RT_USING_RTC)
  322. rt_hw_rtc_init();
  323. #endif
  324. /* Enable SPI access to MicroSD card */
  325. #if defined(EFM32_USING_SPISD)
  326. #if defined(EFM32_GXXX_DK)
  327. DVK_writeRegister(BC_SPI_CFG, 1);
  328. #elif defined(EFM32GG_DK3750)
  329. DVK_enablePeripheral(DVK_MICROSD);
  330. #endif
  331. #endif
  332. /* Enable SPI access to Ethernet */
  333. #if defined(EFM32_USING_ETHERNET)
  334. DVK_enablePeripheral(DVK_ETH);
  335. #endif
  336. /* Initialize LCD */
  337. #if defined(EFM32_USING_LCD)
  338. efm32_spiLcd_init();
  339. #endif
  340. /* Initialize Keys */
  341. #if defined(EFM32_USING_KEYS)
  342. #if defined(EFM32GG_DK3750)
  343. efm32_hw_keys_init();
  344. #endif
  345. #endif
  346. }
  347. /***************************************************************************//**
  348. * @}
  349. ******************************************************************************/