📄 bsp.c
字号:
/*
*********************************************************************************************************
* Atmel AT91 SAM7
* Board Support Package
*
* (c) Copyright 2004, Micrium, Inc., Weston, FL
* All Rights Reserved
*
*
* File : BSP.C
* By : Jean J. Labrosse
*********************************************************************************************************
*/
#include <includes.h>
/*
*********************************************************************************************************
* CONSTANTS
*********************************************************************************************************
*/
#define BSP_XTAL_FREQ 18432L /* onboard crystal frequency (khz) */
#define BSP_RAM_REMAP_TEST_BYTE (*(CPU_INT08U *)0x00000030L)
/*
*********************************************************************************************************
* DATA TYPES
*********************************************************************************************************
*/
/*
*********************************************************************************************************
* PROTOTYPES
*********************************************************************************************************
*/
static void BSP_DummyISR_Handler(void);
static void BSP_IntCtrlInit(void);
static void PLL_Init(void);
static void Tmr_TickInit(void);
static void Tmr_TickISR_Handler(void);
/*
*********************************************************************************************************
* DUMMY IRQ HANDLER
*
* Description : This function is called to handle invalid IRQs
*
* Arguments : none
*********************************************************************************************************
*/
static void BSP_DummyISR_Handler (void)
{
AT91C_BASE_AIC->AIC_IVR = 0; /* Debug variant of vector read (protect mode is used) */
}
/*
*********************************************************************************************************
* BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
* functions found in this module.
*
* Arguments : none
*********************************************************************************************************
*/
void BSP_Init (void)
{
AT91C_BASE_WDTC->WDTC_WDMR = AT91C_WDTC_WDDIS; /* Disable the Watchdog Timer */
/* Enable the hardware reset button. This is done by */
/* writting the Mode Register KEY, and setting bit 0 */
AT91C_BASE_RSTC->RSTC_RMR |= (INT32U)(0xA5 << 24) | AT91C_RSTC_URSTEN;
LED_Init(); /* Initialize the I/Os for the LEDs */
PLL_Init(); /* Initialize the PLL and select it for MCLK */
BSP_IntCtrlInit(); /* Initialize the Interrupt Controller */
Tmr_TickInit(); /* Initialize uC/OS-II's Tick Rate */
}
/*
*********************************************************************************************************
* PLL INITIALIZATION
*
* Description : This function initializes the AT91SAM7X256 PLL thereby setting the frequency of MCK
* which is used by PIT to generate the OS tick interrupt.
*
* Arguments : none
*********************************************************************************************************
*/
static void PLL_Init (void)
{
AT91C_BASE_WDTC->WDTC_WDMR |= (1 << 15); /* Disable the watchdog timer */
/* Set the number of flash wait states to 2 */
AT91C_BASE_MC->MC_FMR |= (1 << 8); /* Since we are running > 30mhz out of flash */
/* Enable the Main Oscillator */
AT91C_BASE_CKGR->CKGR_MOR = AT91C_CKGR_OSCOUNT | AT91C_CKGR_MOSCEN;
/* Wait for the Main Oscillator to start up */
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MOSCS) == 0) {
;
}
AT91C_BASE_CKGR->CKGR_PLLR = 0x01033F64; /* Enable and set PLL to 48MHZ MCK, 48MHZ USB */
/* USBDiv = 0, PLL Clock Output */
/* Mult = 259 (plus 1) = 260 */
/* PLLCount (startup time = Maximum, 4096) */
/* Divider = 100 */
/* Sumary: MCLK=USBClk= (18432000 * 260 / 100) */
/* Wait for the PLL to start up */
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_LOCK) == 0) {
;
}
/* Master clock select = PLL w/ no prescaler */
AT91C_BASE_PMC->PMC_MCKR = AT91C_PMC_PRES_CLK | AT91C_PMC_CSS_PLL_CLK;
/* Wait for the Master Clock (MCK) to start up */
while ((AT91C_BASE_PMC->PMC_SR & AT91C_PMC_MCKRDY) == 0) {
;
}
}
/*
*********************************************************************************************************
* DETERMINE AND RETURN CPU OPERATING FREQUENCY
*
* Description : This function is called by OSView_c.c : OSView_GetCPUName().
*
* Arguments : none
* Returns : INT32U, CPU frequency in khz.
*********************************************************************************************************
*/
CPU_INT32U BSP_CPU_ClkFreq (void)
{
CPU_INT32U pll_mult;
CPU_INT32U pll_div;
CPU_INT08U mclk_div;
CPU_INT32U cpu_freq;
pll_mult = (AT91C_BASE_CKGR->CKGR_PLLR & 0x07FF0000) >> 16;
pll_div = (AT91C_BASE_CKGR->CKGR_PLLR & 0x000000FF) >> 0;
if (pll_div == 0) {
return (BSP_XTAL_FREQ); /* If the PLL mult is 0, then the PLL is disabled */
}
/* Read the Master Clock divider */
mclk_div = (AT91C_BASE_PMC->PMC_MCKR >> 2) & 0x07;
mclk_div = 1 << mclk_div; /* Convert 0-7 into 1, 2, 4, 8, 16, 32, or 64 */
if (mclk_div >= 128) { /* Divider pattern for 128 is reserved */
return (BSP_XTAL_FREQ);
}
cpu_freq = ((BSP_XTAL_FREQ * (pll_mult + 1)) / pll_div) / mclk_div;
return (cpu_freq);
}
/*
*********************************************************************************************************
* INITIALIZE THE INTERRUPT CONTROLLER
*
* Description : This function is called to disable ALL interrupts.
*
* Arguments : none
*********************************************************************************************************
*/
static void BSP_IntCtrlInit (void)
{
CPU_INT16U i;
BSP_RAM_REMAP_TEST_BYTE = 0xAA; /* Write a byte to RAM */
if (BSP_RAM_REMAP_TEST_BYTE == 0xAA) { /* Check if the write to RAM worked v */
AT91C_BASE_MC->MC_RCR = 1; /* If so, toggle REMAP register */
}
#ifdef RAM_REMAPPED /* --------- BEGIN CODE RUNNING OUT OF RAM ---------------- */
BSP_InitExceptVect();
#endif /* --------- END CODE RUNNING OUT OF RAM ---------------- */
AT91C_BASE_AIC->AIC_EOICR = 0x00000000; /* End-of-interrupt command */
for (i = 0; i < 32; i++) { /* Disable all ISRs */
AT91C_BASE_AIC->AIC_SVR[i] = (CPU_INT32U)BSP_DummyISR_Handler;
AT91C_BASE_AIC->AIC_SMR[i] = 0;
}
}
/*
*********************************************************************************************************
* DISABLE ALL INTERRUPTS
*
* Description : This function is called to disable ALL interrupts.
*
* Arguments : none
*********************************************************************************************************
*/
void BSP_IntDisAll (void)
{
AT91C_BASE_AIC->AIC_IDCR = 0xFFFFFFFF; /* Disable all interrupts */
}
/*
*********************************************************************************************************
* BSP INITIALIZATION
*
* Description : This function should be called by your application code before you make use of any of the
* functions found in this module.
*
* Arguments : none
*********************************************************************************************************
*/
void LED_Init (void)
{
AT91C_BASE_PIOB->PIO_PER = BSP_LED4 | BSP_LED3 | BSP_LED2 | BSP_LED1; /* Enable register */
AT91C_BASE_PIOB->PIO_OER = BSP_LED4 | BSP_LED3 | BSP_LED2 | BSP_LED1; /* Output enable */
AT91C_BASE_PIOB->PIO_IDR = BSP_LED4 | BSP_LED3 | BSP_LED2 | BSP_LED1; /* Disable LED pin interrupt */
LED_Off(BSP_LED_ALL); /* Turn OFF all the LEDs */
}
/*
*********************************************************************************************************
* LED ON
*
* Description : This function is used to control any or all the LEDs on the board.
*
* Arguments : led is the number of the LED to control
* 0 indicates that you want ALL the LEDs to be ON
* 1 turns ON LED1 on the board
* .
* .
* 4 turns ON LED4 on the board
*********************************************************************************************************
*/
void LED_On (CPU_INT08U led)
{
switch (led) {
case 0:
AT91C_BASE_PIOB->PIO_CODR = BSP_LED4 | BSP_LED3 | BSP_LED2 | BSP_LED1;
break;
case 1:
AT91C_BASE_PIOB->PIO_CODR = BSP_LED1;
break;
case 2:
AT91C_BASE_PIOB->PIO_CODR = BSP_LED2;
break;
case 3:
AT91C_BASE_PIOB->PIO_CODR = BSP_LED3;
break;
case 4:
AT91C_BASE_PIOB->PIO_CODR = BSP_LED4;
break;
}
}
/*
*********************************************************************************************************
* LED OFF
*
* Description : This function is used to control any or all the LEDs on the board.
*
* Arguments : led is the number of the LED to turn OFF
* 0 indicates that you want ALL the LEDs to be OFF
* 1 turns OFF LED1 on the board
* .
* .
* 4 turns OFF LED4 on the board
*********************************************************************************************************
*/
void LED_Off (CPU_INT08U led)
{
switch (led) {
case 0:
AT91C_BASE_PIOB->PIO_SODR = BSP_LED4 | BSP_LED3 | BSP_LED2 | BSP_LED1;
break;
case 1:
AT91C_BASE_PIOB->PIO_SODR = BSP_LED1;
break;
case 2:
AT91C_BASE_PIOB->PIO_SODR = BSP_LED2;
break;
case 3:
AT91C_BASE_PIOB->PIO_SODR = BSP_LED3;
break;
case 4:
AT91C_BASE_PIOB->PIO_SODR = BSP_LED4;
break;
}
}
/*
*********************************************************************************************************
* LED TOGGLE
*
* Description : This function is used to toggle any or all the LEDs on the board.
*
* Arguments : led is the number of the LED to toggle
* 0 indicates that you want ALL the LEDs to be toggle
* 1 toggles LED1 on the board
* .
* .
* 4 toggles LED4 on the board
*********************************************************************************************************
*/
void LED_Toggle (CPU_INT08U led)
{
switch (led) {
case 0:
if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED1) {
AT91C_BASE_PIOB->PIO_CODR = BSP_LED1;
} else {
AT91C_BASE_PIOB->PIO_SODR = BSP_LED1;
}
if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED2) {
AT91C_BASE_PIOB->PIO_CODR = BSP_LED2;
} else {
AT91C_BASE_PIOB->PIO_SODR = BSP_LED2;
}
if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED3) {
AT91C_BASE_PIOB->PIO_CODR = BSP_LED3;
} else {
AT91C_BASE_PIOB->PIO_SODR = BSP_LED3;
}
if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED4) {
AT91C_BASE_PIOB->PIO_CODR = BSP_LED4;
} else {
AT91C_BASE_PIOB->PIO_SODR = BSP_LED4;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -