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

📄 bsp.c

📁 ucos porting source for samsung sam7x256
💻 C
📖 第 1 页 / 共 2 页
字号:
             }
             break;

        case 1:
             if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED1) {
                 AT91C_BASE_PIOB->PIO_CODR = BSP_LED1;
             } else {
                 AT91C_BASE_PIOB->PIO_SODR = BSP_LED1;
             }
             break;

        case 2:
             if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED2) {
                 AT91C_BASE_PIOB->PIO_CODR = BSP_LED2;
             } else {
                 AT91C_BASE_PIOB->PIO_SODR = BSP_LED2;
             }
             break;

        case 3:
             if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED3) {
                 AT91C_BASE_PIOB->PIO_CODR = BSP_LED3;
             } else {
                 AT91C_BASE_PIOB->PIO_SODR = BSP_LED3;
             }
             break;

        case 4:
             if (AT91C_BASE_PIOB->PIO_ODSR & BSP_LED4) {
                 AT91C_BASE_PIOB->PIO_CODR = BSP_LED4;
             } else {
                 AT91C_BASE_PIOB->PIO_SODR = BSP_LED4;
             }
             break;
    }
}


/*
*********************************************************************************************************
*                                         TICKER INITIALIZATION
*
* Description : This function is called to initialize uC/OS-II's tick source which uses the PIT
*               (typically a timer generating interrupts every 1 to 100 mS).
*
* Arguments   : none
*
* Note(s)     : 1) PIT Interrupt frequency:
*
*                         MCLK        1
*               Freq =    ---- * -----------
*                          16     (PIV + 1)
*
*
*                         MCLK      1
*               PIV  =  ( ---- * ------ ) - 1
*                          16     Freq
*
*               Where:
*                    MCLK = 48 MHz (i.e 48,000,000)
*                    Freq = Desired frequency (i.e. OS_TICKS_PER_SEC)
*********************************************************************************************************
*/

static  void  Tmr_TickInit (void)
{
    CPU_INT32U      counts;
    CPU_INT32U      cpu_frq;

                                                                        /* Determine the number of counts per tick                  */
    cpu_frq                               = BSP_CPU_ClkFreq();

    counts                                = ((cpu_frq * 1000) / 16 / OS_TICKS_PER_SEC) - 1;

                                                                        /* Set the vector address for PIT                           */
    AT91C_BASE_AIC->AIC_SVR[AT91C_ID_SYS] = (CPU_INT32U)Tmr_TickISR_Handler;
    AT91C_BASE_AIC->AIC_SMR[AT91C_ID_SYS] = AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL
                                          | AT91C_AIC_PRIOR_LOWEST;

    AT91C_BASE_AIC->AIC_ICCR              = 1 << AT91C_ID_SYS;
    AT91C_BASE_AIC->AIC_IECR              = 1 << AT91C_ID_SYS;

                                                                        /* Enable the PIT with the correct compare value            */
    AT91C_BASE_PITC->PITC_PIMR            = AT91C_PITC_PITEN | AT91C_PITC_PITIEN | counts;
}


/*
*********************************************************************************************************
*                                           OSView Timer Initialization
*
* Description : This function selects and initializes a timer for use with OSView and the Statistics Task
*
* Arguments   : none
*********************************************************************************************************
*/

#if OS_VIEW_MODULE > 0
void  OSView_TmrInit (void)
{
#if OS_VIEW_TIMER_SEL == 0
    AT91C_BASE_PMC->PMC_PCER             =  (1 << 12);                  /* Enable the peripheral clk                                */
    AT91C_BASE_TCB->TCB_TC0.TC_CCR       =  (1 <<  1);                  /* TC0 timer disabled                                       */
    AT91C_BASE_TCB->TCB_TC0.TC_CMR      &= ~(7 <<  0);                  /* TIMER_CLOCK1 is input clk                                */
    AT91C_BASE_TCB->TCB_TC0.TC_CCR       =  (1 <<  0);                  /* TC0 timer enabled                                        */
    AT91C_BASE_TCB->TCB_TC0.TC_CCR       =  (1 <<  2);                  /* SWTRG to reset and start                                 */
#endif

#if OS_VIEW_TIMER_SEL == 1
    AT91C_BASE_PMC->PMC_PCER             =  (1 << 13);                  /* Enable the peripheral clk                                */
    AT91C_BASE_TCB->TCB_TC1.TC_CCR       =  (1 <<  1);                  /* TC1 timer disabled                                       */
    AT91C_BASE_TCB->TCB_TC1.TC_CMR      &= ~(7 <<  0);                  /* TIMER_CLOCK1 is input clk                                */
    AT91C_BASE_TCB->TCB_TC1.TC_CCR       =  (1 <<  0);                  /* TC1 timer enabled                                        */
    AT91C_BASE_TCB->TCB_TC1.TC_CCR       =  (1 <<  2);                  /* SWTRG to reset and start                                 */
#endif

#if OS_VIEW_TIMER_SEL == 2
    AT91C_BASE_PMC->PMC_PCER             =  (1 << 14);                  /* Enable the peripheral clk                                */
    AT91C_BASE_TCB->TCB_TC2.TC_CCR       =  (1 <<  1);                  /* TC2 timer disabled                                       */
    AT91C_BASE_TCB->TCB_TC2.TC_CMR      &= ~(7 <<  0);                  /* TIMER_CLOCK1 is input clk                                */
    AT91C_BASE_TCB->TCB_TC2.TC_CCR       =  (1 <<  0);                  /* TC2 timer enabled                                        */
    AT91C_BASE_TCB->TCB_TC2.TC_CCR       =  (1 <<  2);                  /* SWTRG to reset and start                                 */
#endif

}
#endif


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

#if OS_VIEW_MODULE > 0
CPU_INT32U  OSView_TmrRd (void)
{
    CPU_INT32U      cnts;


#if OS_VIEW_TIMER_SEL == 0
    cnts = (CPU_INT32U)(AT91C_BASE_TCB->TCB_TC0.TC_CV & 0x0000FFFF);    /* Read timer 0                                             */
#endif

#if OS_VIEW_TIMER_SEL == 1
    cnts = (CPU_INT32U)(AT91C_BASE_TCB->TCB_TC1.TC_CV & 0x0000FFFF);    /* Read timer 1                                             */
#endif

#if OS_VIEW_TIMER_SEL == 2
    cnts = (CPU_INT32U)(AT91C_BASE_TCB->TCB_TC2.TC_CV & 0x0000FFFF);    /* Read timer 2                                             */
#endif

    return (cnts);
}
#endif


/*
*********************************************************************************************************
*                                           PIT IRQ HANDLER
*
* Description : This function handles the PIT interrupt that is used to generate TICKs for uC/OS-II.
*
* Arguments   : none
*********************************************************************************************************
*/

static  void  Tmr_TickISR_Handler (void)
{
    CPU_INT32U      status;


    AT91C_BASE_AIC->AIC_IVR   = 0;                                      /* Write the IVR, as required in Protection Mode            */

    status                    = AT91C_BASE_PITC->PITC_PIVR;
    (void)status;                                                       /* Prevent compiler warning about status set & not used     */

    OSTimeTick();                                                       /* Tell uC/OS-II about clock tick                           */
                                                                        /* Clear  timer #0 interrupt                                */
    AT91C_BASE_AIC->AIC_ICCR  = 1 << AT91C_ID_SYS;
    AT91C_BASE_AIC->AIC_EOICR = 0;                                      /* Signal end of interrupt                                  */
}


/*
*********************************************************************************************************
*                                                   Ser_Init
*
* Description :   Initializes a debug serial port.
*                 This function initializes a serial port for communication purposes.
*
* Arguments   :   None.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_Init (CPU_INT32U baud)
{
    CPU_INT32U     peripheral_clk_frq;


    peripheral_clk_frq         = BSP_CPU_ClkFreq();                     /* Get the UART input clock frequency                       */

    AT91C_BASE_PIOA->PIO_PDR   = (1 << 28) | (1 << 27);                 /* Set GPIOA pins 9 and 10 as DBGU UART pins                */
    AT91C_BASE_PIOA->PIO_ASR   = (1 << 28) | (1 << 27);                 /* Select GPIOA attached peripheral (DBGU)                  */

    AT91C_BASE_DBGU->DBGU_IDR  = AT91C_US_RXRDY         |               /* Disable Rx interrupts                                    */
                                 AT91C_US_TXRDY;                        /* Disable Tx interrupt                                     */

    AT91C_BASE_DBGU->DBGU_CR   = AT91C_US_RXEN          |               /* Enable the receiver                                      */
                                 AT91C_US_TXEN;                         /* Enable the transmitter                                   */

    AT91C_BASE_DBGU->DBGU_MR   = AT91C_US_USMODE_NORMAL |               /* Normal mode selected                                     */
                                 AT91C_US_PAR_NONE;                     /* No parity bit selected                                   */

                                                                        /* Set the DBGU baud rate to 38,400                         */
    AT91C_BASE_DBGU->DBGU_BRGR = (CPU_INT16U)((peripheral_clk_frq * 1000) / baud / 16);

    AT91C_BASE_PMC->PMC_PCER   = (1 << AT91C_ID_SYS);                   /* Enable the DBGU peripheral clock                         */
}


/*
*********************************************************************************************************
*                                                Ser_WrByte
*
* Description :   Writes a byte to a debug serial port.
*                 This function writes a single byte to a serial port. Note, this function
*                 also blocks until room is available in the UART for the byte to be sent.
*
* Arguments   :   A byte containing the value of the charcater to output.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_WrByte (CPU_INT08U b)
{
    while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_TXRDY) == 0){          /*  Wait for room in the transmit register.                 */
        ;
    }
    AT91C_BASE_DBGU->DBGU_THR     = b;
}


/*
*********************************************************************************************************
*                                                Ser_WrStr
*
* Description :   Writes a string to a debug serial port.
*                 This funcion writes a character string to a serial port.
*
* Arguments   :   A string of characters.
*
* Returns     :   None.
*********************************************************************************************************
*/

void  Ser_WrStr (CPU_INT08U *s)
{
    while (*s) {
        Ser_WrByte(*s++);
    }
}


/*
*********************************************************************************************************
*                                                Ser_RdByte
*
* Description :   Reads a byte from a debug serial port.
*                 This funcion reads a byte from a serial port. This call blocks until a
*                 character appears at the port. Echo of the byte is also sent to the serial port.
*
* Arguments   :   None.
*
* Returns     :   A byte containing the value of the received charcater.
*********************************************************************************************************
*/

CPU_INT08U Ser_RdByte (void)
{
    CPU_INT08U      b;

    while ((AT91C_BASE_DBGU->DBGU_CSR & AT91C_US_RXRDY) == 0) {         /*  Wait for a byte to show up.                             */
        OSTimeDly(2);
    }
    b = (CPU_INT08U)(AT91C_BASE_DBGU->DBGU_RHR & 0x00FF);               /* Read the character.                                      */
    return (b);
}


/*
*********************************************************************************************************
*                                                Ser_RdStr
*
* Description :   Reads a string from a debug 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   :   The address of a character string to store the received string.
*
* Returns     :   A string with the bytes received.
*********************************************************************************************************
*/

void  Ser_RdStr (CPU_INT08U *s, CPU_INT16U numBytes)
{
    CPU_INT08U  b;
    CPU_INT08U  i;


    i = 0;
    b = 0;
    while ( b != 13 || i == numBytes) {
        b = Ser_RdByte();
        b = b & 0x000000FF;
        if (b == 8) {
            i--;
            s[i] = ' ';
            Ser_Printf("%c %c",b,b);
        } else {
            s[i] = b;
            Ser_Printf("%c",s[i]);
            i++;
        }
    }
    s[i] = '\0';
    Ser_Printf("\n");
}


/*
*********************************************************************************************************
*                                                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 (CPU_CHAR *format, ...)
{
   static  CPU_INT08U   buffer[80 + 1];
           va_list      vArgs;


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

   Ser_WrStr((CPU_CHAR*) buffer);
}

⌨️ 快捷键说明

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