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

📄 bsp.c

📁 lpc2478+ucosII
💻 C
📖 第 1 页 / 共 4 页
字号:
* Description :   Receive a single byte using UART0
*
* Arguments   :   None.
*
* Returns     :   The received byte
*********************************************************************************************************
*/

CPU_INT08U  Ser_RdByte (void)
{
    CPU_INT08U  rx_byte;


#if SER_COMM_SEL == SER_UART_0
    while ((U0LSR & DEF_BIT_00) == 0) {
        OSTimeDly(1);
    }

    rx_byte = (CPU_INT08U)(U0RBR);                              /* Remove the data from the holding register                */
    return (rx_byte);
#endif

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

    rx_byte = (CPU_INT08U)(U1RBR);                              /* Remove the data from the holding register                */
    return (rx_byte);
#endif
}

/*
*********************************************************************************************************
*                                                Ser_RdStr
*
* Description :   This function reads a string using Channel 0 of the UART.
*
* Arguments   :   s      A pointer to a buffer at which the string can be stored
*                 len    The size of the string that will be read
*
* Returns     :   None
*********************************************************************************************************
*/

void  Ser_RdStr (CPU_CHAR    *rx_str,
                 CPU_INT32U   len)
{
    CPU_CHAR  input;
    CPU_CHAR  input_ix;


    input_ix  = 0;
    rx_str[0] = 0;

    while (1)
    {
        input = Ser_RdByte();

        if ((input == '\r') ||
            (input == '\n')) {
            Ser_Printf("\n");
            rx_str[input_ix] = 0;
            break;
        }

        if (input == '\b') {
            if (input_ix > 0) {
                Ser_Printf("\b \b");
                input_ix--;
                rx_str[input_ix] = 0;
            }
        }

        if (Str_IsPrint(input)) {
            Ser_Printf("%c", input);
            rx_str[input_ix] = input;
            input_ix++;
            if (input_ix >= len) {
               input_ix = len;
            }
        }
    }
}

/*
*********************************************************************************************************
*                                                Ser_Printf
*
* Description :   Formatted outout to the serial port.
*                 This funcion reads a string from a serial port. This call blocks until a
*                 character appears at the port and the last character is a Carriage
*                 Return (0x0D).
*
* Arguments   :   Format string follwing the C format convention.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_Printf (const  CPU_CHAR *format, ...)
{
    static  CPU_CHAR  buffer[80 + 1];
            va_list   vArgs;


    va_start(vArgs, format);
    vsprintf((char *)buffer, (char const *)format, vArgs);
    va_end(vArgs);

    Ser_WrStr((CPU_CHAR*) buffer);
}

/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                         DCC Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                         DCC INITIALIZATION
*
* Description : This function initializes the interrupts for DCC receive/transmit.
*
* Arguments   : none
*
* Returns     ; none
*********************************************************************************************************
*/

#if OS_CPU_ARM_DCC_EN > 0
static  void  DCC_Init (void)
{
                                                                /* VIC DEBUG RX Initialization                              */
    VICIntEnClear       =  (1 << VIC_DEBUGRX);                  /* Enable the timer interrupt source                        */
    VICIntSelect       &= ~(1 << VIC_DEBUGRX);                  /* Configure the DCC Rx function as an IRQ source           */
    VICVectAddr2        =  (CPU_INT32U)DCC_RxISR_Handler;       /* Set the vector address                                   */
    VICIntEnable        =  (1 << VIC_DEBUGRX);                  /* Enable the timer interrupt source                        */
}
#endif

/*
*********************************************************************************************************
*                                         DCC ISR HANDLERS
*
* Description : These functions are the handlers for DCC receive and transmit.
*
* Arguments   : none
*
* Returns     ; none
*********************************************************************************************************
*/

#if OS_CPU_ARM_DCC_EN > 0
void  DCC_RxISR_Handler (void)
{
    OSDCC_Handler();
}

void  DCC_TxISR_Handler (void)
{
    OSDCC_Handler();
}
#endif

/*
******************************************************************************************************************************
******************************************************************************************************************************
**                                Static Board Support Initialization Functions
******************************************************************************************************************************
******************************************************************************************************************************
*/

/*
*********************************************************************************************************
*                                      Set the CPU Clock Frequency
*
* Description: This function sets up and activates the PLL
*
* Arguements  : None
*
* Returns     : None
*
* Notes      : 1) The PLL output frequency is calculated by the following formula:
*                     Fcco = 2 * Fin * m / n, where Fin is the PLL input clock. In
*                     this particular case, Fin is set to the Main Oscillator
*                     whose frequency is #define'd in bsp.h. M is the PLL
*                     clock multiplier. M must be written to the PLLCFG register
*                     as the desired multiplier - 1. N is the PLL clock divider
*                     and must be written to PLLCFG as the desired divider - 1.
*
*              2) Fcco must be between 250 and 550 MHz. The ARM Core clock
*                 must never exceed 72 MHz. Use cClkDiv to divide Fcco accordingly.
*
*              3) When using the USB device, you must choose Fcco as a multiple
*                 of 96 MHz, and then use usbClkDiv to divide Fcco to exactly
*                 48 MHz.
*
*              4) In this example, Fin = 12MHz, M = 12, N = 1, cClkDiv = 6 and usbClkDiv = 6.
*                 Therefore, Fcco = 2 * Fin * M / N = (2 * 12 * 12 / 1) = 288MHz.
*                 The processor clock = (Fcco / cClkDiv) = (288MHz / 6) =  48MHz.
*                 Finally, the USB clock = (Fcco / usbClkDib) = (288MHz / 6) = 48MHz.
*
*              5) Early revisions of the part have a PLL errata preventing Fcco from
*                 being greater than 288MHz.
*
*              6) For later revisions, M = 20, cCLKDiv = 8, and usbClkDiv = 10 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 /* Allocate storage for CPU status register                 */
    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;                                     /* Configure the  ARM Core clock div to 6. CCLKSEL =  6 - 1 */
    clk_div_usb =        5;                                     /* Configure the USB clock divider to 6, USBSEL  = 6 - 1    */

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

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

    SCS        &= ~(1 << 4);                                    /* OSCRANGE = 0, Main OSC is between 1 and 20 Mhz           */
	SCS        |=  (1 << 5);                                    /* OSCEN = 1, Enable the main oscillator                    */

    while ((SCS &  (1 << 6)) == 0) {                            /* Wait until OSCSTAT is set (Main OSC ready to be used)    */
        ;
    }

    CLKSRCSEL   =  (1 << 0);		                            /* 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     |=  (1 << 0);                                    /* 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 & (1 << 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     |=  (1 << 1);                                    /* Connect the PLL. The PLL is now the active clock source  */
    PLLFEED     =     0xAA;                                     /* PLL register update sequence, 0xAA, 0x55                 */
    PLLFEED     =     0x55;
    CPU_CRITICAL_EXIT();

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

/*
*********************************************************************************************************
*                                     MAM_Init()
*
* Description : This function initializes the Memory Acceleration Module
*
* Arguements  : None
*
* Returns     : 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 clock in duration    */
    }

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

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

    MAMCR       =  2;                                           /* Enable full MAM functionality                            */
}

/*
*********************************************************************************************************
*                                          INITIALIZE I/Os
*
* Description : This function consolidates the I/O initialization for various modules.
*
* Arguements  : None
*

⌨️ 快捷键说明

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