⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bsp.c

📁 嵌入式的tcpip协议栈
💻 C
📖 第 1 页 / 共 4 页
字号:
* Return(s)   : The 32bit counts of the timer.
*********************************************************************************************************
*/

#if (uC_PROBE_COM_MODULE == DEF_ENABLED) && (OS_PROBE_HOOKS_EN == 1)
CPU_INT32U  OSProbe_TmrRd (void)
{
    return ((CPU_INT32U)T1TC);
}
#endif


/*
*********************************************************************************************************
*********************************************************************************************************
**                                     uC/OS-II TIMER FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            Tmr_TickInit()
*
* Description : Initialize uC/OS-II's tick source.
*
* Argument(s) : none.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  Tmr_TickInit (void)
{
    CPU_INT32U  pclk_freq;
    CPU_INT32U  rld_cnts;

                                                                /* VIC timer #0 init.                                   */
    VICIntSelect     &= ~(1 << VIC_TIMER0);                     /* Configure the timer interrupt as an IRQ source.      */
    VICVectAddr4      =  (CPU_INT32U)Tmr_TickISR_Handler;       /* Set the vector address.                              */
    VICVectPriority4  = 15;                                     /* Set the vector priority.                             */
    VICIntEnable      =  (1 << VIC_TIMER0);                     /* Enable the timer interrupt source.                   */

    pclk_freq         =   BSP_CPU_PclkFreq(PCLK_TIMER0);        /* Get the peripheral clock frequency.                  */

    rld_cnts          =   pclk_freq / OS_TICKS_PER_SEC;         /* Calculate the nbr of cnts necessary for the OS tick. */

    T0TCR             =  (1 << 1);                              /* Disable and reset ctr 0 and the prescale ctr 0.      */
    T0TCR             =   0;                                    /* Clear the reset bit.                                 */
    T0PC              =   0;                                    /* Prescaler is set to no division.                     */

    T0MR0             =   rld_cnts;
    T0MCR             =   3;                                    /* Interrupt on MR0 (reset TC), stop TC.                */

    T0CCR             =   0;                                    /* Capture is disabled.                                 */
    T0EMR             =   0;                                    /* No external match output.                            */
    T0TCR             =   1;                                    /* Enable timer 0.                                      */
}

/*
*********************************************************************************************************
*                                       Tmr_TickISR_Handler()
*
* Description : Handle the timer interrupt that is used to generate TICKs for uC/OS-II.
*
* Argument(s) : none.
*
* Return(s)   : none.
*********************************************************************************************************
*/

void  Tmr_TickISR_Handler (void)
{
    T0IR  = 0xFF;                                               /* Clear timer #0 interrupt.                            */
    OSTimeTick();                                               /* Call uC/OS-II's OSTimeTick().                        */
}


/*
*********************************************************************************************************
*********************************************************************************************************
**                                          LOCAL FUNCTIONS
*********************************************************************************************************
*********************************************************************************************************
*/

/*
*********************************************************************************************************
*                                            PLL_Init()
*
* Description : Set up and activate the PLL.
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Note(s)     : (1) The PLL output frequency is calculated by:
*
*                           Fcco = 2 * Fin * m / n
*
*                   where
*
*                           Fin is the PLL input clock (here, the main oscillator)
*                           M   is the PLL clock multiplier. The value (M - 1) is programmed in PLLCFG.
*                           N   is the PLL clock divider.    The value (N - 1) is programmed in PLLCFG.
*
*               (2) Fcco must be between 250 and 550 MHz. The ARM Core clock must never exceed 72 MHz.
*                   Set clk_div to divide Fcco accordingly.
*
*               (3) When using the USB device, you must choose Fcco as a multiple of 96 MHz, and then
*                   set clk_div_usb to divide Fcco to exactly 48 MHz.
*
*               (4) In this example
*
*                         Fin         = 12MHz,
*                         M           = 12,
*                         N           =  1,
*                         clk_div     =  6, and
*                         clk_div_usb =  6.
*
*                 Therefore, Fcco        = 2 * Fin * M / N      = (2 * 12 * 12 / 1) = 288MHz.
*                 The processor clock    = (Fcco / clk_div)     = (288MHz / 6)      =  48MHz.
*                 Finally, the USB clock = (Fcco / clk_div_usb) = (288MHz / 6)      =  48MHz.
*
*               (5) A PLL errata on early revisions of the part prevent Fcco from being greater than 288MHz.
*
*               (6) For later revisions, M = 20, clk_div = 8, and clk_div_usb = 10 will yield 60MHz for
*                   the processor clock and 48MHz for the USB clock.
*********************************************************************************************************
*/

static  void  PLL_Init (void)
{
#if CPU_CFG_CRITICAL_METHOD == CPU_CRITICAL_METHOD_STATUS_LOCAL
    CPU_SR  cpu_sr = 0;
#endif

    CPU_INT32U  m;
    CPU_INT32U  n;
    CPU_INT32U  clk_div;
    CPU_INT32U  clk_div_usb;


    m           = 11;                                           /* PLL Multiplier = 20, MSEL bits = 12 - 1 = 11.        */
    n           =  0;                                           /* PLL Divider    =  1, NSEL bits =  1 - 1 =  0.        */
    clk_div     =  5;                                           /* Cfg the  ARM Core clock div to 6. CCLKSEL =  6 - 1.  */
    clk_div_usb =  5;                                           /* Cfg the USB clock divider to 6, USBSEL  = 6 - 1.     */

    if ((PLLSTAT & DEF_BIT_25) > 0) {                           /* If the PLL is already running.                       */
        CPU_CRITICAL_ENTER();
        PLLCON  &= ~DEF_BIT_01;                                 /* Disconnect the PLL.                                  */
        PLLFEED  =  0xAA;                                       /* PLL register update sequence, 0xAA, 0x55.            */
        PLLFEED  =  0x55;
        CPU_CRITICAL_EXIT();
    }

    CPU_CRITICAL_ENTER();
    PLLCON   &= ~DEF_BIT_00;                                    /* Disable the PLL.                                     */
    PLLFEED   =  0xAA;                                          /* PLL register update sequence, 0xAA, 0x55.            */
    PLLFEED   =  0x55;
    CPU_CRITICAL_EXIT();

    SCS      &= ~DEF_BIT_04;                                    /* OSCRANGE = 0, Main OSC is between 1 and 20 Mhz.      */
    SCS      |=  DEF_BIT_05;                                    /* OSCEN = 1, Enable the main oscillator.               */

    while ((SCS &  DEF_BIT_06) == 0) {                          /* Wait until OSCSTAT is set (Main OSC rdy to be used). */
        ;
    }

    CLKSRCSEL = DEF_BIT_00;                                     /* Select main OSC, 12MHz, as the PLL clock source.     */

    CPU_CRITICAL_ENTER();
    PLLCFG    = (m << 0) | (n << 16);                           /* Configure the PLL multiplier and divider.            */
    PLLFEED   = 0xAA;                                           /* PLL register update sequence, 0xAA, 0x55.            */
    PLLFEED   = 0x55;
    CPU_CRITICAL_EXIT();

    CPU_CRITICAL_ENTER();
    PLLCON   |= DEF_BIT_00;                                     /* Enable the PLL.                                      */
    PLLFEED   = 0xAA;                                           /* PLL register update sequence, 0xAA, 0x55.            */
    PLLFEED   = 0x55;
    CPU_CRITICAL_EXIT();

    CCLKCFG   = clk_div;                                        /* Configure the ARM Core Processor clock divider.      */
    USBCLKCFG = clk_div_usb;                                    /* Configure the USB clock divider.                     */

    while ((PLLSTAT & DEF_BIT_26) == 0) {                       /* Wait for PLOCK to become set.                        */
        ;
    }

    PCLKSEL0  = 0xAAAAAAAA;                                     /* Set peripheral clocks to be half of main clock.      */
    PCLKSEL1  = 0x22AAA8AA;

    CPU_CRITICAL_ENTER();
    PLLCON   |= DEF_BIT_01;                                     /* Connect the PLL. The PLL is now the active clk src.  */
    PLLFEED   = 0xAA;                                           /* PLL register update sequence, 0xAA, 0x55.            */
    PLLFEED   = 0x55;
    CPU_CRITICAL_EXIT();

    while ((PLLSTAT & DEF_BIT_25) == 0) {                       /* Wait PLLC, the PLL connect status bit to become set. */
        ;
    }
}


/*
*********************************************************************************************************
*                                       MAM_Init()
*
* Description : Initialize the Memory Acceleration Module.
*
* Argument(s) : none.
*
* Return(s)   : none.
*********************************************************************************************************
*/

static  void  MAM_Init (void)
{
    CPU_INT32U  clk_freq;


    clk_freq = BSP_CPU_ClkFreq();                               /* Get the current core clock frequency.                */

    MAMCR    = 0;                                               /* Disable MAM functionality.                           */

    if (clk_freq < 20000000) {                                  /* Compare current clock frequency with MAM modes.      */
        MAMTIM = 1;                                             /* Set MAM fetch cycles to 1 processor clk in duration. */
    }

    if (clk_freq < 40000000) {
        MAMTIM = 2;                                             /* Set MAM fetch cycles to 2 processor clk in duration. */
    }

    if (clk_freq >= 40000000) {
        MAMTIM = 3;                                             /* Set MAM fetch cycles to 3 processor clk in duration. */
    }

    MAMCR = 1;                                                  /* Enable partially-enabled MAM functionality                            */
}

/*
*********************************************************************************************************
*                                          GPIO_Init()
*
* Description : Initializes the GPIO pins.  All the I/O pins are initialized in this function
*               so you don't have to look at multiple places for I/O initialization.
*
* Argument(s) : none.
*
* Return(s)   : none.
*
* Note(s)     : (1) Refer to the LPC2378 User Manual, Chapter 9 for a detailed Pin Assignment
*********************************************************************************************************
*/

static  void  GPIO_Init (void)
  {
    CPU_INT32U  pinsel;


    IO0DIR   = 0;
    IO1DIR   = 0;
    FIO0DIR  = 0;
    FIO1DIR  = 0;
    FIO2DIR  = 0;
    FIO3DIR  = 0;
    FIO4DIR  = 0;

    FIO0MASK = 0;
    FIO1MASK = 0;
    FIO2MASK = 0;
    FIO3MASK = 0;
    FIO4MASK = 0;

    PINSEL0  = 0;
    PINSEL1  = 0;
    PINSEL2  = 0;
    PINSEL3  = 0;
    PINSEL4  = 0;
    PINSEL5  = 0;
    PINSEL6  = 0;
    PINSEL7  = 0;
    PINSEL8  = 0;
    PINSEL9  = 0;
    PINSEL10 = 0;

                                                                /* Configure P0.28 & P0.27 for I2C0.                    */
    pinsel   = PINSEL1;
    pinsel  &= 0xFC3FFFFF;
    pinsel  |= 0x01400000;
    PINSEL1  = pinsel;

                                                                /* Configure P2.10 for GPIO.                            */
    pinsel   = PINSEL4;
    pinsel  &= 0xFFCFFFFF;
    PINSEL4  = pinsel;
    FIO2DIR &= ~DEF_BIT_10;


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -