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

📄 bsp.c

📁 lpc2478+ucosII
💻 C
📖 第 1 页 / 共 4 页
字号:
        case PCLK_PCB:
        case PCLK_I2C1:
        case PCLK_SSP0:
        case PCLK_TIMER2:
        case PCLK_TIMER3:
        case PCLK_UART2:
        case PCLK_UART3:
        case PCLK_I2C2:
        case PCLK_MCI:
        case PCLK_SYSCON:
             selection = ((PCLKSEL1 >> ((pclk - 16) * 2)) & 0x03);
             if (selection == 0) {
                 return (clk_freq / 4);
             } else if (selection == 1) {
                 return (clk_freq);
             } else if (selection == 2) {
                 return (clk_freq / 2);
             } else {
                 return (clk_freq / 8);
             }

        default:
             return (0);
    }
}

/*
*********************************************************************************************************
*                                     DISABLE ALL INTERRUPTS
*
* Description : This function disables all interrupts from the interrupt controller.
*
* Arguments   : none
*
* Returns     : None
*********************************************************************************************************
*/

void  BSP_IntDisAll (void)
{
    VICIntEnClear = 0xFFFFFFFFL;                                        /* Disable ALL interrupts                                   */
}

/*
*********************************************************************************************************
*                                          EXCEPTION HANDLER
*
* Description : This function should be used to handle any exceptions.  It is called by
*               OS_CPU_ARM_EXCEPT_HANDLER(), which is declared in os_cpu_a.s
*
* Arguments   : ID, an identifier used to indicate what type of ARM exception has been triggered
*               Possible ID values are shown below.
*                  OS_CPU_ARM_EXCEPT_RESET             0x00
*                  OS_CPU_ARM_EXCEPT_UNDEF_INSTR       0x01
*                  OS_CPU_ARM_EXCEPT_SWI               0x02
*                  OS_CPU_ARM_EXCEPT_PREFETCH_ABORT    0x03
*                  OS_CPU_ARM_EXCEPT_DATA_ABORT        0x04
*                  OS_CPU_ARM_EXCEPT_ADDR_ABORT        0x05
*                  OS_CPU_ARM_EXCEPT_IRQ               0x06
*                  OS_CPU_ARM_EXCEPT_FIQ               0x07
*********************************************************************************************************
*/

void  OS_CPU_ExceptHndlr (CPU_DATA ID)
{
    BSP_FNCT_PTR  pfnct;

                                                                        /* If this exception is either an IRQ or FIQ                */
    if ((ID == OS_CPU_ARM_EXCEPT_IRQ) || (ID == OS_CPU_ARM_EXCEPT_FIQ)) {
        pfnct = (BSP_FNCT_PTR)VICAddress;                               /* Read the interrupt vector from the VIC                   */
        if (pfnct != (BSP_FNCT_PTR)0) {                                 /* Make sure we don't have a NULL pointer                   */
          (*pfnct)();                                                   /* Execute the ISR for the interrupting device              */
            VICAddress  =  1;                                           /* Acknowlege the VIC interrupt                             */
        }
    }
}

/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                          OS-View Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                     INITIALIZE TIMER FOR uC/OS-View
*
* Description : This function is called to by uC/OS-View to initialize the free running timer that is
*               used to make time measurements.
*
* Arguments   : none
*
* Returns     ; none
*********************************************************************************************************
*/

#if OS_VIEW_MODULE > 0
void  OSView_TmrInit (void)
{
    T1PR  = 0;
    T1TCR = 0x00000001;                                         /* Enable the timer                                         */

}
#endif

/*
*********************************************************************************************************
*                                     READ TIMER FOR uC/OS-View
*
* Description : This function is called to read the current counts of a 32 bit free running timer.
*
* Arguments   : none
*
* Returns     ; The 32 bit counts of the timer assuming the timer (MUST be an UP counter).
*********************************************************************************************************
*/

#if OS_VIEW_MODULE > 0
CPU_INT32U  OSView_TmrRd (void)
{
    if (OSRunning == DEF_TRUE) {
        return ((CPU_INT32U)T1TC);
    } else {
        return (0);
    }
}
#endif


/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                         uC/OS-II Timer Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                       TICKER INITIALIZATION
*
* Description : This function is called to initialize uC/OS-II's tick source (typically a timer generating
*               interrupts every 1 to 100 mS).
*
* Arguments   : none
*
* Returns     ; none
*********************************************************************************************************
*/

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

                                                                /* VIC timer #0 Initialization                              */
    VICIntSelect       &= ~(1 << VIC_TIMER0);                   /* Configure the timer interrupt as an IRQ source           */
    VICVectAddr4        =  (CPU_INT32U)Tmr_TickISR_Handler;     /* Set the vector address                                   */
    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 # of counts necessary for the OS ticker    */

    T0TCR               =  (1 << 1);                            /* Disable and reset counter 0 and the prescale counter 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                                           */
}


/*
*********************************************************************************************************
*                                         TIMER #0 IRQ HANDLER
*
* Description : This function handles the timer interrupt that is used to generate TICKs for uC/OS-II.
*
* Arguments   : none
*
* Returns     ; none
*********************************************************************************************************
*/

void  Tmr_TickISR_Handler (void)
{
    T0IR        = 0xFF;                                         /* Clear timer #0 interrupt                                 */

    OSTimeTick();                                               /* Call uC/OS-II's OSTimeTick()                             */
}
/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                       Serial Port Communications
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                                   Ser_Init
*
* Description :   This function prepares UART0.
*
* Arguments   :   None.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_Init (void)
{
    CPU_INT16U  div;                                            /* Baud rate divisor                                        */
    CPU_INT08U  divlo;
    CPU_INT08U  divhi;
    CPU_INT08U  lcr;										    /* Line Control Register                                    */
    CPU_INT32U  pclk_freq;
    CPU_INT32U  pinsel;


#if SER_COMM_SEL == SER_UART_0
                                                                /* Compute divisor for desired baud rate                    */
    pclk_freq   =  BSP_CPU_PclkFreq(PCLK_UART0);                /* Get the CPU clock frequency                              */

    div         = (CPU_INT16U)(((2 * pclk_freq / 16 / 115200) + 1) / 2);
    divlo       =  div & 0x00FF;                                /* Split divisor into LOW and HIGH bytes                    */
    divhi       = (div >> 8) & 0x00FF;
    lcr         =  DEF_BIT_00 | DEF_BIT_01;                     /* 8 Bits, 1 Stop, No Parity                                */

                                                                /* Configure P0.2 & P0.3 for UART0                          */
    pinsel      = PINSEL0;
    pinsel     &= 0xFFFFFF0F;
    pinsel     |= 0x00000050;
    PINSEL0     = pinsel;

    U0LCR       = DEF_BIT_07;                                   /* Set divisor access bit                                   */
    U0DLL       = divlo;                                        /* Load divisor                                             */
    U0DLM       = divhi;
    U0LCR       = lcr;                                          /* Set line control register (Bit 8 is 0)                   */
    U0IER       = 0x00;                                         /* Disable both Rx and Tx interrupts                        */
    U0FCR       = DEF_BIT_00 | DEF_BIT_01 | DEF_BIT_02;         /* Enable FIFO, flush Rx & Tx                               */
#endif

#if SER_COMM_SEL == SER_UART_1
                                                                /* Compute divisor for desired baud rate                    */
    pclk_freq   =  BSP_CPU_PclkFreq(PCLK_UART1);                /* Get the CPU clock frequency                              */

    div         = (CPU_INT16U)(((2 * pclk_freq / 16 / 115200) + 1) / 2);
    divlo       =  div & 0x00FF;                                /* Split divisor into LOW and HIGH bytes                    */
    divhi       = (div >> 8) & 0x00FF;
    lcr         =  DEF_BIT_00 | DEF_BIT_01;                     /* 8 Bits, 1 Stop, No Parity                                */

                                                                /* Configure P3.16 & P3.17 for UART1                        */
    pinsel      = PINSEL7;
    pinsel     &= 0xFFFFFFF0;
    pinsel     |= 0x0000000F;
    PINSEL7     = pinsel;

    U1LCR       = DEF_BIT_07;                                   /* Set divisor access bit                                   */
    U1DLL       = divlo;                                        /* Load divisor                                             */
    U1DLM       = divhi;
    U1LCR       = lcr;                                          /* Set line control register (Bit 8 is 0)                   */
    U1IER       = 0x00;                                         /* Disable both Rx and Tx interrupts                        */
    U1FCR       = DEF_BIT_00 | DEF_BIT_01 | DEF_BIT_02;         /* Enable FIFO, flush Rx & Tx                               */
#endif
}

/*
*********************************************************************************************************
*                                                Ser_WrByte
*
* Description :   Transmit a single byte using UART0
*
* Arguments   :   The byte that should be transmitted.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_WrByte (CPU_INT08U tx_byte)
{
#if SER_COMM_SEL == SER_UART_0
    U0THR       = tx_byte;
#endif

#if SER_COMM_SEL == SER_UART_1
    U1THR       = tx_byte;
#endif
}

/*
*********************************************************************************************************
*                                                Ser_WrStr
*
* Description :   Transmits a string using UART0
*
* Arguments   :   The string that will be transmitted.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_WrStr (const CPU_CHAR * tx_str)
{
    while ((*tx_str) != 0) {
        Ser_WrByte(*tx_str++);
#if SER_COMM_SEL == SER_UART_0
        while ((U0LSR & DEF_BIT_05) == 0) {
            OSTimeDly(1);
        }
#endif

#if SER_COMM_SEL == SER_UART_1
        while ((U1LSR & DEF_BIT_05) == 0) {
            OSTimeDly(1);
        }
#endif
    }
}

/*
*********************************************************************************************************
*                                                Ser_RdByte
*

⌨️ 快捷键说明

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