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

📄 timer_example.c

📁 含t h r e a d x,u c o s 的b s p
💻 C
字号:
/***********************************************************************
 * $Workfile:   timer_example.c  $
 * $Revision:   1.2  $
 * $Author:   WellsK  $
 * $Date:   Oct 01 2003 12:03:58  $
 *
 * Project: Timer driver example
 *
 * Description:
 *     A simple timer driver example.
 *
 * Revision History:
 * $Log:   //smaicnt2/pvcs/VM/sharpmcu/archives/sharpmcu/software/csps/lh7a404/bsps/sdk7a404/examples/timers/timer_example.c-arc  $
 * 
 *    Rev 1.2   Oct 01 2003 12:03:58   WellsK
 * Added logic to get TTB address from register CP15 TTB.
 * 
 *    Rev 1.1   Sep 18 2003 09:25:34   WellsK
 * Updated example for MMU and VIC driver changes.
 * 
 *    Rev 1.0   Jul 01 2003 10:09:50   WellsK
 * Initial revision.
 * 
 *
 ***********************************************************************
 * SHARP MICROELECTRONICS OF THE AMERICAS MAKES NO REPRESENTATION
 * OR WARRANTIES WITH RESPECT TO THE PERFORMANCE OF THIS SOFTWARE,
 * AND SPECIFICALLY DISCLAIMS ANY RESPONSIBILITY FOR ANY DAMAGES, 
 * SPECIAL OR CONSEQUENTIAL, CONNECTED WITH THE USE OF THIS SOFTWARE.
 *
 * SHARP MICROELECTRONICS OF THE AMERICAS PROVIDES THIS SOFTWARE SOLELY 
 * FOR THE PURPOSE OF SOFTWARE DEVELOPMENT INCORPORATING THE USE OF A 
 * SHARP MICROCONTROLLER OR SYSTEM-ON-CHIP PRODUCT. USE OF THIS SOURCE
 * FILE IMPLIES ACCEPTANCE OF THESE CONDITIONS.
 *
 * COPYRIGHT (C) 2001 SHARP MICROELECTRONICS OF THE AMERICAS, INC.
 *     CAMAS, WA
 **********************************************************************/

#include "abl_types.h"
#include "abl_irq_fiq.h"
#include "abl_arm922t_cp15_driver.h"
#include "lh7a404_vic_driver.h"
#include "lh7a404_timer_driver.h"
#include "sdk7a404_cpld_driver.h"

/* Timer device handle */
INT_32 timer2dev;

/* LED state */
BOOL_32 gpio_led;

/* Timer count value */
volatile UNS_32 tcount;

/***********************************************************************
 *
 * Function: timer2_user_interrupt
 *
 * Purpose: Timer 2 interrupt handler
 *
 * Processing:
 *     Clear the timer interrupt. Toggle the state of the LED and
 *     update it.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Nothing
 *
 * Notes: None
 *
 **********************************************************************/
void timer2_user_interrupt(void)
{
    if (gpio_led == TRUE)
    {
        /* Toggle state */
        gpio_led = FALSE;
    }
    else
    {
        gpio_led = TRUE;
    }

    /* Set new state of LED */
    cpld_enable_led(gpio_led);

    /* Clear the interrupt and increment count */
    timer_ioctl(timer2dev, TIMER_INT_CLEAR, 0);
    tcount++;
}

/***********************************************************************
 *
 * Function: c_entry
 *
 * Purpose: Timer driver example
 *
 * Processing:
 *     See function. This example sets up the timers with interrupts
 *     and performs some basic counting.
 *
 * Parameters: None
 *
 * Outputs: None
 *
 * Returns: Always returns 1
 *
 * Notes: None
 *
 **********************************************************************/
int c_entry(void)
{
    /* Start with both LEDs on */
    gpio_led = TRUE;

    /* Disable interrupts in ARM core */
    disable_irq_fiq();

    /* Set virtual address of MMU table (needed for VIC driver
       functions) */
    cp15_set_vmmu_addr((UNS_32 *) cp15_get_ttb());

    /* Initialize interrupt system */
    vic_initialize(0xC0000000);

    /* Install standard IRQ dispatcher at ARM IRQ vector */
    vic_install_arm_handler(IRQ_VEC, (PFV) vic_arm_irq_dispatcher);

    /* Install VIC1 and VIC2 handlers */
    vic_install_arm_handler(VIC1_IRQ_VEC, (PFV) vic1_irq_dispatcher);
    vic_install_arm_handler(VIC2_IRQ_VEC, (PFV) vic2_irq_dispatcher);

    /* Install timers handler in the IRQ dispatcher */
    vic_install_handler(VIC_TC2UINTR, VIC_IRQ,
        (PFV) timer2_user_interrupt);
    
    /* Open timer 2 */
    if ((timer2dev = timer_open(TIMER2, 0)) == 0x00000000)
    {
        return 0;
    }

    /* Setup timer 2 for a 1Hz tick (1000000 microseconds) */
    timer_ioctl(timer2dev, TIMER_SET_USECS, (500 * 1000));

    /* Enable each timer (starts counting) */
    timer_ioctl(timer2dev, TIMER_ENABLE, 1);

    /* Enable timer interrupts in the interrupt controller */
    vic_int_enable(VIC_TC2UINTR, TRUE);

    /* Clear any latched timer interrupts */
    timer_ioctl(timer2dev, TIMER_INT_CLEAR, 0);

    /* Enable IRQ interrupts in the ARM core */
    enable_irq();

    /* Wait for 5 seconds and toggle LEDs */
    tcount = 0;
    while (tcount < 10);

    /* Close timers */
    timer_close(timer2dev);

    /* Disable timer interrupts in the interrupt controller */
    vic_int_enable(VIC_TC2UINTR, FALSE);

    /* Disable ARM core IRQ interrupts */
    disable_irq();

    return 1;
}

#ifndef __GNUC__
/* With ARM and GHS toolsets, the entry point is main() - this will
   allow the linker to generate wrapper code to setup stacks, allocate
   heap area, and initialize and copy code and data segments. For GNU
   toolsets, the entry point is through __start() in the crt0_gnu.asm
   file, and that startup code will setup stacks and data */
int main(void)
{
    return c_entry();
}
#endif

⌨️ 快捷键说明

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