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

📄 timer_example.c

📁 dsp tms320c6486的csl例程
💻 C
📖 第 1 页 / 共 2 页
字号:
/* =============================================================================
 * Copyright (c) Texas Instruments Inc 2002, 2003, 2004, 2005, 2006
 *
 * Use of this software is controlled by the terms and conditions found in the
 * license agreement under which this software has been supplied.
 * ==========================================================================*/

/** ===========================================================================
 * @file Timer_example.c
 *
 * @path $(CSLPATH)\example\c6486\timer\src
 *
 * @desc Example for the usage of General purpose timer CSL APIs
 * =============================================================================
 * @n <b> Test Description </b>
 * @n The example illustrates the usage of CSL APIs for using timer.
 * 1. This example configures the first instance of timer
 *    as interrupt or event to be sourced normally by the timer.
 * 2. This enables the timer in one shot mode (for watchdog mode timer is
 *    enabled in continuous mode).
 * 3. The  interrupt handler announces occurrence of each interrupt
 *    to the application.
 * 4. The above said procedure is followed for all the modes of the timer
 *    (i.e. 64 bit timer, dual 32 bit timer( chained and unchained)
 *    and watchdog).
 **************************************************************************
 */

/** ============================================================================
 *  Revision History
 *  ===============
 * 07-Jul-2006 TRR Updated for Tomahawk 
 * 29-Jul-2005 PSK updated changes according to revised timer spec. the number
 *                 convention TIM12, TIM34 are changed to TIMLO and TIMHI.
 * 21-Apr-2005 PSK File Created.
 * =============================================================================
 */

#include <csl_tmr.h>
#include <stdio.h>
#include <csl_intc.h>
#include <soc.h>
#include <csl_intcAux.h>

/** Handle for the TMR instance */
CSL_TmrHandle         hTmr;

/** change the Instance of the timer and test */
Uint8 IntcInstance = CSL_TMR_0;


/* Locals & Forwards */
void tmrIntcPrdLoLoadDemo(void);
void tmrIntcPrdHiLoadDemo(void);
void tmrIntcChainedDemo(void);
void tmrIntcGptDemo(void);
void tmrWdtModeDemo(void); 

/*
 *****************************************************************************
 *                    INTC related code
 *****************************************************************************
 */

/* Intc Declarations */
CSL_IntcObj                  tmrIntcObj, tmrIntcObj1;
CSL_IntcHandle               tmrIntcHandle, tmrIntcHandle1;
CSL_IntcContext              context, context1;

CSL_IntcEventHandlerRecord   eventHandler[30];
CSL_IntcEventHandlerRecord   record[1];

/* count for ISR */
volatile int intrCnt = 0;

/*
 * ===========================================================================
 * @func   TimerInterruptHandler
 *
 * @desc
 *    Handler for Timer Interrupt
 *
 * @arg    event - interrupt event id
 * ============================================================================
 */
void TimerInterruptHandler (
    CSL_IntcEventId * event
)
{
    intrCnt++;
    CSL_intcEventClear(*event);
}



/** ===========================================================================
 *  @n@b   main
 *
 *  @b Description
 *  @n This is the main function for the file.This initializes the CSL for
 *     INTC, installs interrupt handler for first instance of general purpose
 *     timer and invokes the routine which demonstrates the usage of CSL APis
 *     for General purpose timer
 *
 *  @b Arguments
 *  @verbatim
            None
    @endverbatim
 *
 * <b> Return Value </b>  void
 * ============================================================================
 */
void main (
    void
)
{
    CSL_IntcGlobalEnableState state;

    /* Initialize INTC */
    context.numEvtEntries = 1;
    context.eventhandlerRecord = record;

    CSL_intcInit(&context);

    /*Enable NMIS */
    CSL_intcGlobalNmiEnable();

    /* Enable all interrupts */
    CSL_intcGlobalEnable(&state);

    tmrIntcPrdLoLoadDemo();
    tmrIntcPrdHiLoadDemo();
    tmrIntcChainedDemo();
    tmrIntcGptDemo();
    /* tmrWdtModeDemo(); */

    printf(">>>>>> Examples for all the modes of timer are"
    		"completed and PASSED <<<<<<<< \n");
}

/*
 * ===========================================================================
 * @func   tmrIntcPrdLoLoadDemo
 *
 * @desc
 *    This function is example for the Period register loading with value
 *
 * @arg    None
 * ============================================================================
 */
void tmrIntcPrdLoLoadDemo (
    void
)
{
    CSL_TmrObj TmrObj;
    CSL_Status intStat, status;
    CSL_TmrHwSetup hwSetup = CSL_TMR_HWSETUP_DEFAULTS;
    CSL_IntcEventHandlerRecord EventRecord;
    CSL_IntcParam vectId;

    Uint32 LoadValue = 100;

    CSL_TmrEnamode TimeCountMode = CSL_TMR_ENAMODE_ENABLE;

    /* Clear local data structures */
    memset(&TmrObj, 0, sizeof(CSL_TmrObj));
    printf("Running Gp Timer Interrupt DUAL UNCHAINED Low....\n");

    /**************************************************************
    * INTC related code      						  *
    **************************************************************/

    /* Open INTC */
    vectId = CSL_INTC_VECTID_12;

    tmrIntcHandle = CSL_intcOpen(&tmrIntcObj, CSL_INTC_EVENTID_TINTLO_LOCAL, &vectId,
                                 &intStat);

    /* Bind ISR to Interrupt */
    EventRecord.handler = (CSL_IntcEventHandler)&TimerInterruptHandler;
    EventRecord.arg = (void *)tmrIntcHandle;
    CSL_intcPlugEventHandler(tmrIntcHandle, &EventRecord);

    /* Event Enable */
    CSL_intcHwControl(tmrIntcHandle, CSL_INTC_CMD_EVTENABLE, NULL);

    /**************************************************************/

    /* Initialize GP timer CSL module */
    CSL_tmrInit(NULL);

    hTmr =  CSL_tmrOpen(&TmrObj, IntcInstance, NULL, &status);

    CSL_tmrHwSetup(hTmr, &hwSetup);

    /* Stop the GP Timer */
    status = CSL_tmrHwControl(hTmr, CSL_TMR_CMD_RESET_TIMLO, NULL);

    /* set the timer mode to unchained dual mode */
    hwSetup.tmrTimerMode = CSL_TMR_TIMMODE_DUAL_UNCHAINED;

    CSL_tmrHwSetup(hTmr, &hwSetup);

    /* Load the period register */
    CSL_tmrHwControl(hTmr, CSL_TMR_CMD_LOAD_PRDLO, (void *)&LoadValue);

    /* Start the timer in one shot mode */
    CSL_tmrHwControl(hTmr, CSL_TMR_CMD_START_TIMLO, (void *)&TimeCountMode);

    /* INTC related code */
    while (1) {
        if (intrCnt == 1)
	        break;
    }

    /**************************************************************/

    CSL_intcHwControl(tmrIntcHandle, CSL_INTC_CMD_EVTDISABLE, NULL);

    printf("INTR: The Total number of Events occurred are: 0x%d\n", intrCnt);

    intrCnt = 0;

    /* Stop the GP Timer */
    CSL_tmrHwControl(hTmr, CSL_TMR_CMD_RESET_TIMLO, NULL);

    printf("Example for TIMLO completed\n");

    /* Close the Tmr instance */
    CSL_tmrClose(hTmr);
    CSL_intcClose(tmrIntcHandle);
}

/*
 * =============================================================================
 *   @func   tmrIntcPrdHiLoadDemo
 *
 *   @desc
 *      This function tests the Period register High loading with value
 *
 *   @arg    None
 * =============================================================================
 */
void tmrIntcPrdHiLoadDemo (
    void
)
{
    CSL_TmrObj TmrObj;
    CSL_Status status;
    CSL_TmrHwSetup hwSetup = CSL_TMR_HWSETUP_DEFAULTS;
    CSL_IntcEventHandlerRecord EventRecord;
    CSL_IntcParam vectId;

    Uint32 LoadValue = 100;

    CSL_TmrEnamode TimeCountMode = CSL_TMR_ENAMODE_ENABLE;

    /* Clear local data structures */
    memset(&TmrObj, 0, sizeof(CSL_TmrObj));
    printf("\n\n\n\n\nRunning Gp Timer Interrupt DUAL UNCHAINED TIMHI....\n");

    /**************************************************************
    * INTC related code 						  *
    **************************************************************/

    /* Open INTC	*/
    vectId = CSL_INTC_VECTID_13;
    tmrIntcHandle = CSL_intcOpen(&tmrIntcObj, CSL_INTC_EVENTID_TINTHI_LOCAL, &vectId,
                                 NULL);

    /* Bind ISR to Interrupt */
    EventRecord.handler = (CSL_IntcEventHandler)&TimerInterruptHandler;
    EventRecord.arg = (void *)tmrIntcHandle;
    CSL_intcPlugEventHandler(tmrIntcHandle, &EventRecord);

    /* Event Enable */
    CSL_intcHwControl(tmrIntcHandle, CSL_INTC_CMD_EVTENABLE, NULL);

    /**************************************************************/

    /* Initialize GP timer CSL module */
    CSL_tmrInit(NULL);
    hTmr =  CSL_tmrOpen(&TmrObj, IntcInstance, NULL, &status);
    CSL_tmrHwSetup(hTmr, &hwSetup);

    /* Stop the GP Timer */
    CSL_tmrHwControl(hTmr, CSL_TMR_CMD_RESET_TIMHI, NULL);

    /* set the timer mode to unchained dual mode */
    hwSetup.tmrTimerMode = CSL_TMR_TIMMODE_DUAL_UNCHAINED;

    CSL_tmrHwSetup(hTmr, &hwSetup);

    /* Load the period register */
    status = CSL_tmrHwControl(hTmr, CSL_TMR_CMD_LOAD_PRDHI, (void *)&LoadValue);

    /* Start the timer in one shot mode*/
    CSL_tmrHwControl(hTmr, CSL_TMR_CMD_START_TIMHI, (void *)&TimeCountMode);

    /* INTC related code */
    while (1) {
        if (intrCnt == 1)
	        break;
    }

    /**************************************************************/

    CSL_intcHwControl(tmrIntcHandle, CSL_INTC_CMD_EVTDISABLE, NULL);

    printf("INTR: The Total number of Events occurred are: 0x%d\n", intrCnt);

    intrCnt = 0;

    /* Stop the GP Timer */
    status = CSL_tmrHwControl(hTmr, CSL_TMR_CMD_RESET_TIMHI, NULL);
    printf("Example for TIM34 completed\n");

    /* Close the Tmr instance */
    CSL_tmrClose(hTmr);
    CSL_intcClose(tmrIntcHandle);
}

/*
 * ============================================================================

⌨️ 快捷键说明

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