2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @file board.c
|
|
|
|
* @brief Board support of RT-Thread RTOS for EFM32
|
2011-02-28 12:47:50 +08:00
|
|
|
* COPYRIGHT (C) 2011, RT-Thread Development Team
|
2011-02-17 11:33:15 +08:00
|
|
|
* @author onelife
|
2011-07-29 10:47:37 +08:00
|
|
|
* @version 0.4 beta
|
|
|
|
*******************************************************************************
|
2011-02-17 11:33:15 +08:00
|
|
|
* @section License
|
2011-07-29 10:47:37 +08:00
|
|
|
* The license and distribution terms for this file may be found in the file
|
|
|
|
* LICENSE in this distribution or at http://www.rt-thread.org/license/LICENSE
|
|
|
|
*******************************************************************************
|
2011-02-17 11:33:15 +08:00
|
|
|
* @section Change Logs
|
|
|
|
* Date Author Notes
|
|
|
|
* 2010-12-21 onelife Initial creation for EFM32
|
2011-05-12 15:19:37 +08:00
|
|
|
* 2011-05-06 onelife Add EFM32 development kit and SPI Flash support
|
2011-07-29 10:47:37 +08:00
|
|
|
* 2011-07-12 onelife Add SWO output enable function
|
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @addtogroup efm32
|
|
|
|
* @{
|
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Includes ------------------------------------------------------------------*/
|
2011-02-17 11:33:15 +08:00
|
|
|
#include "board.h"
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Private typedef -----------------------------------------------------------*/
|
|
|
|
/* Private define ------------------------------------------------------------*/
|
2011-02-17 11:33:15 +08:00
|
|
|
#define IS_NVIC_VECTTAB(VECTTAB) (((VECTTAB) == RAM_MEM_BASE) || \
|
|
|
|
((VECTTAB) == FLASH_MEM_BASE))
|
|
|
|
#define IS_NVIC_OFFSET(OFFSET) ((OFFSET) < 0x000FFFFF)
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
|
|
|
* @addtogroup SysTick_clock_source
|
|
|
|
* @{
|
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
#define SysTick_CLKSource_HCLK_Div8 ((uint32_t)0xFFFFFFFB)
|
|
|
|
#define SysTick_CLKSource_HCLK ((uint32_t)0x00000004)
|
|
|
|
#define IS_SYSTICK_CLK_SOURCE(SOURCE) (((SOURCE) == SysTick_CLKSource_HCLK) || \
|
|
|
|
((SOURCE) == SysTick_CLKSource_HCLK_Div8))
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @}
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Private macro -------------------------------------------------------------*/
|
|
|
|
/* Private variables ---------------------------------------------------------*/
|
|
|
|
/* Private function prototypes -----------------------------------------------*/
|
|
|
|
/* Private functions ---------------------------------------------------------*/
|
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Set the allocation and offset of the vector table
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] NVIC_VectTab
|
|
|
|
* Indicate the vector table is allocated in RAM or ROM
|
|
|
|
*
|
|
|
|
* @param[in] Offset
|
2011-07-29 10:47:37 +08:00
|
|
|
* The vector table offset
|
|
|
|
******************************************************************************/
|
|
|
|
static void NVIC_SetVectorTable(
|
|
|
|
rt_uint32_t NVIC_VectTab,
|
|
|
|
rt_uint32_t Offset)
|
2011-02-17 11:33:15 +08:00
|
|
|
{
|
|
|
|
/* Check the parameters */
|
|
|
|
RT_ASSERT(IS_NVIC_VECTTAB(NVIC_VectTab));
|
|
|
|
RT_ASSERT(IS_NVIC_OFFSET(Offset));
|
|
|
|
|
|
|
|
SCB->VTOR = NVIC_VectTab | (Offset & (rt_uint32_t)0x1FFFFF80);
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Configure the address of vector table
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static void NVIC_Configuration(void)
|
|
|
|
{
|
|
|
|
#ifdef VECT_TAB_RAM
|
|
|
|
/* Set the vector table allocated at 0x20000000 */
|
|
|
|
NVIC_SetVectorTable(RAM_MEM_BASE, 0x0);
|
|
|
|
#else /* VECT_TAB_FLASH */
|
|
|
|
/* Set the vector table allocated at 0x00000000 */
|
|
|
|
NVIC_SetVectorTable(FLASH_MEM_BASE, 0x0);
|
|
|
|
#endif
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/* Set NVIC Preemption Priority Bits: 0 bit for pre-emption, 4 bits for
|
|
|
|
subpriority */
|
2011-02-17 11:33:15 +08:00
|
|
|
NVIC_SetPriorityGrouping(0x7UL);
|
|
|
|
|
|
|
|
/* Set Base Priority Mask Register */
|
|
|
|
__set_BASEPRI(EFM32_BASE_PRI_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Enable high frequency crystal oscillator (HFXO), and set HFCLK domain to
|
|
|
|
* use HFXO as source.
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static void switchToHFXO(void)
|
|
|
|
{
|
|
|
|
CMU_TypeDef *cmu = CMU;
|
|
|
|
|
|
|
|
/* Turning on HFXO to increase frequency accuracy. */
|
|
|
|
/* Waiting until oscillator is stable */
|
|
|
|
cmu->OSCENCMD = CMU_OSCENCMD_HFXOEN;
|
|
|
|
while (!(cmu->STATUS && CMU_STATUS_HFXORDY)) ;
|
|
|
|
|
|
|
|
/* Switching the CPU clock source to HFXO */
|
|
|
|
cmu->CMD = CMU_CMD_HFCLKSEL_HFXO;
|
|
|
|
|
|
|
|
/* Turning off the high frequency RC Oscillator (HFRCO) */
|
|
|
|
/* GENERATL WARNING! Make sure not to disable the current
|
|
|
|
* source of the HFCLK. */
|
|
|
|
cmu->OSCENCMD = CMU_OSCENCMD_HFRCODIS;
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Configure the SysTick clock source
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
* @param[in] SysTick_CLKSource
|
|
|
|
* Specifies the SysTick clock source.
|
|
|
|
*
|
|
|
|
* @arg SysTick_CLKSource_HCLK_Div8
|
|
|
|
* AHB clock divided by 8 selected as SysTick clock source.
|
|
|
|
*
|
|
|
|
* @arg SysTick_CLKSource_HCLK
|
|
|
|
* AHB clock selected as SysTick clock source.
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static void SysTick_CLKSourceConfig(uint32_t SysTick_CLKSource)
|
|
|
|
{
|
|
|
|
/* Check the parameters */
|
|
|
|
RT_ASSERT(IS_SYSTICK_CLK_SOURCE(SysTick_CLKSource));
|
|
|
|
|
|
|
|
if (SysTick_CLKSource == SysTick_CLKSource_HCLK)
|
|
|
|
{
|
|
|
|
SysTick->CTRL |= SysTick_CLKSource_HCLK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SysTick->CTRL &= SysTick_CLKSource_HCLK_Div8;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Configure the SysTick for OS tick.
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
static void SysTick_Configuration(void)
|
|
|
|
{
|
|
|
|
rt_uint32_t core_clock;
|
|
|
|
rt_uint32_t cnts;
|
|
|
|
|
|
|
|
switchToHFXO();
|
|
|
|
core_clock = SystemCoreClockGet();
|
|
|
|
cnts = core_clock / RT_TICK_PER_SECOND;
|
|
|
|
|
|
|
|
SysTick_Config(cnts);
|
|
|
|
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK);
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Enable SWO.
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
|
|
|
void setupSWO(void)
|
|
|
|
{
|
|
|
|
rt_uint32_t *dwt_ctrl = (rt_uint32_t *) 0xE0001000;
|
|
|
|
rt_uint32_t *tpiu_prescaler = (rt_uint32_t *) 0xE0040010;
|
|
|
|
rt_uint32_t *tpiu_protocol = (rt_uint32_t *) 0xE00400F0;
|
|
|
|
|
|
|
|
CMU->HFPERCLKEN0 |= CMU_HFPERCLKEN0_GPIO;
|
|
|
|
/* Enable Serial wire output pin */
|
|
|
|
GPIO->ROUTE |= GPIO_ROUTE_SWOPEN;
|
|
|
|
/* Set location 1 */
|
|
|
|
GPIO->ROUTE = (GPIO->ROUTE & ~(_GPIO_ROUTE_SWLOCATION_MASK)) | GPIO_ROUTE_SWLOCATION_LOC1;
|
|
|
|
/* Enable output on pin */
|
|
|
|
GPIO->P[2].MODEH &= ~(_GPIO_P_MODEH_MODE15_MASK);
|
|
|
|
GPIO->P[2].MODEH |= GPIO_P_MODEH_MODE15_PUSHPULL;
|
|
|
|
/* Enable debug clock AUXHFRCO */
|
|
|
|
CMU->OSCENCMD = CMU_OSCENCMD_AUXHFRCOEN;
|
|
|
|
|
|
|
|
while(!(CMU->STATUS & CMU_STATUS_AUXHFRCORDY));
|
|
|
|
|
|
|
|
/* Enable trace in core debug */
|
|
|
|
CoreDebug->DHCSR |= 1;
|
|
|
|
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
|
|
|
|
|
|
|
|
/* Enable PC and IRQ sampling output */
|
|
|
|
*dwt_ctrl = 0x400113FF;
|
|
|
|
/* Set TPIU prescaler to 16. */
|
|
|
|
*tpiu_prescaler = 0xf;
|
|
|
|
/* Set protocol to NRZ */
|
|
|
|
*tpiu_protocol = 2;
|
|
|
|
/* Unlock ITM and output data */
|
|
|
|
ITM->LAR = 0xC5ACCE55;
|
|
|
|
ITM->TCR = 0x10009;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***************************************************************************//**
|
|
|
|
* @brief
|
|
|
|
* Initialize the board.
|
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
void rt_hw_board_init(void)
|
|
|
|
{
|
|
|
|
/* Chip errata */
|
|
|
|
CHIP_Init();
|
|
|
|
|
2011-05-12 15:19:37 +08:00
|
|
|
#if defined(EFM32_G290_DK)
|
|
|
|
/* Initialize DVK board register access */
|
|
|
|
DVK_init();
|
|
|
|
#endif
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* NVIC Configuration */
|
|
|
|
NVIC_Configuration();
|
|
|
|
|
2011-02-28 12:47:50 +08:00
|
|
|
/* Configure external oscillator */
|
|
|
|
SystemHFXOClockSet(EFM32_HFXO_FREQUENCY);
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* Configure the SysTick */
|
|
|
|
SysTick_Configuration();
|
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @brief
|
2011-07-29 10:47:37 +08:00
|
|
|
* Initialize the hardware drivers.
|
2011-02-17 11:33:15 +08:00
|
|
|
*
|
|
|
|
* @details
|
|
|
|
*
|
|
|
|
* @note
|
|
|
|
*
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|
2011-02-17 11:33:15 +08:00
|
|
|
void rt_hw_driver_init(void)
|
|
|
|
{
|
|
|
|
CMU_ClockEnable(cmuClock_HFPER, true);
|
|
|
|
|
|
|
|
/* Enable GPIO */
|
|
|
|
CMU_ClockEnable(cmuClock_GPIO, true);
|
|
|
|
|
|
|
|
/* Enabling clock to the interface of the low energy modules */
|
|
|
|
CMU_ClockEnable(cmuClock_CORELE, true);
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
#ifdef EFM32_SWO_ENABLE
|
|
|
|
/* Enable SWO */
|
|
|
|
setupSWO();
|
|
|
|
#endif
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* Initialize DMA */
|
|
|
|
rt_hw_dma_init();
|
|
|
|
|
2011-02-28 12:47:50 +08:00
|
|
|
/* Initialize USART */
|
2011-05-12 15:19:37 +08:00
|
|
|
#if defined(EFM32_G290_DK)
|
|
|
|
DVK_enablePeripheral(DVK_RS232A);
|
|
|
|
DVK_enablePeripheral(DVK_SPI);
|
|
|
|
#endif
|
2011-06-20 09:56:28 +08:00
|
|
|
#if (defined(RT_USING_USART0) || defined(RT_USING_USART1) || defined(RT_USING_USART2))
|
2011-02-28 12:47:50 +08:00
|
|
|
rt_hw_usart_init();
|
2011-06-20 09:56:28 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* Setup console */
|
|
|
|
rt_console_set_device(CONSOLE_DEVICE);
|
2011-02-28 12:47:50 +08:00
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* Initialize Timer */
|
2011-06-20 09:56:28 +08:00
|
|
|
#if (defined(RT_USING_TIMER0) || defined(RT_USING_TIMER1) || defined(RT_USING_TIMER2))
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_hw_timer_init();
|
2011-06-20 09:56:28 +08:00
|
|
|
#endif
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-02-28 12:47:50 +08:00
|
|
|
/* Initialize ADC */
|
2011-06-20 09:56:28 +08:00
|
|
|
#if defined(RT_USING_ADC0)
|
2011-02-28 12:47:50 +08:00
|
|
|
rt_hw_adc_init();
|
2011-06-20 09:56:28 +08:00
|
|
|
#endif
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-02-28 12:47:50 +08:00
|
|
|
/* Initialize ACMP */
|
2011-06-20 09:56:28 +08:00
|
|
|
#if (defined(RT_USING_ACMP0) || defined(RT_USING_ACMP1))
|
2011-02-28 12:47:50 +08:00
|
|
|
rt_hw_acmp_init();
|
2011-06-20 09:56:28 +08:00
|
|
|
#endif
|
|
|
|
|
2011-02-17 11:33:15 +08:00
|
|
|
/* Initialize IIC */
|
2011-06-20 09:56:28 +08:00
|
|
|
#if (defined(RT_USING_IIC0) || defined(RT_USING_IIC1))
|
2011-02-17 11:33:15 +08:00
|
|
|
rt_hw_iic_init();
|
2011-06-20 09:56:28 +08:00
|
|
|
#endif
|
2011-02-17 11:33:15 +08:00
|
|
|
|
2011-06-20 09:56:28 +08:00
|
|
|
/* Initialize RTC */
|
|
|
|
#if defined(RT_USING_RTC)
|
|
|
|
rt_hw_rtc_init();
|
|
|
|
#endif
|
2011-02-17 11:33:15 +08:00
|
|
|
}
|
|
|
|
|
2011-07-29 10:47:37 +08:00
|
|
|
/***************************************************************************//**
|
2011-02-17 11:33:15 +08:00
|
|
|
* @}
|
2011-07-29 10:47:37 +08:00
|
|
|
******************************************************************************/
|