/****************************************************************************** * * Copyright 2002 NetBurner ALL RIGHTS RESERVED * Permission is hereby granted to purchasers of NetBurner Hardware * to use or modify this computer program for any use as long as the * resultant program is only executed on NetBurner provided hardware. * * No other rights to use this program or it's derivitives in part or * in whole are granted. * * It may be possible to license this or other NetBurner software for * use on non NetBurner Hardware. * Please contact Licensing@Netburner.com for more infomation. * * NetBurner makes no representation or warranties * with respect to the performance of this computer program, and * specifically disclaims any responsibility for any damages, * special or consequential, connected with the use of this program. * *--------------------------------------------------------------------- * * NetBurner * 5405 morehouse dr #350 * San Diego Ca, 92121 * * information available at: http://www.netburner.com * E-Mail info@netburner.com * * Support is availible: E-Mail support@netburner.com * *****************************************************************************/ /*------------------------------------------------------------------------------ This example program demonstrates how to setup timer2 on the NetBurner 5272 based devices so that it generates interrupts at a specified time interval. Timer2 will be set to a "tick" time of 100us, and generate an interrupt every 1sec that blinks an LED. The input clock source for Timer2 will be set to the system clock divided by 16, so each prescale count will equal 16/62.5MHz = 256ns. Revision History: 1.0 July 24, 2002, Initial Release -------------------------------------------------------------------------------*/ #include "predef.h" #include #include // Uncomment one of the following lines to select either the SB782 or CFV2-66 platform #include <../SB72/system/sim5272.h> /*#include <../CFV2-66/system/sim5272.h>*/ #include #include #include #include #include #include #include #include //----- Function Prototypes ----- extern "C" {void UserMain(void * pd); } int SetupTimer2(BYTE irq_level, WORD prescale, WORD ref_count); /* A variable to hold the number of times the ISR has gone off */ volatile int isr_cnt; /*------------------------------------------------------------------- This is the main routine --------------------------------------------------------------------*/ void UserMain(void * pd) { // Enables a fast one-step compile and flash update of your // NetBurner board with the "make load" command. EnableAutoUpdate(); // Initialize TCP/IP Stack. It is not used in this revision of the // example, but this call will allow you to add your own TCP/IP code. InitializeStack(); // When UserMain() starts, it is a very high priority in the RTOS, // which is good for initialization. Setting the priority to // MAIN_PRIO will put the task just below TCP/IP and HTTP related // tasks once the initialization is complete. OSChangePrio(MAIN_PRIO); // Call function to setup timer2 to use interrupt 4 and a // prescaler of 250 to produce a 64us timer (250 * 256ns = 64us). // The reference count is set to 15624 to produce an interrupt every // second (64us * (15624+1) = 1sec). SetupTimer2(4, 250, 15624); while(1) { OSTimeDly((WORD)(TICKS_PER_SECOND * 2)); iprintf("ISR count = %d Timer tcn = %d\r\n", isr_cnt,sim.timer2.tcn); // display timer2 counter } } /*------------------------------------------------------------------------ Example interrupt routine. Note that the mask value 0f 0x2400 will disable IRQ's of level 4 and higher. Use a different mask if a different interrupt is used. Other mask values are: - IRQ1 = 0x2100 - IRQ2 = 0x2200 - IRQ3 = 0x2300 - IRQ4 = 0x2400 - IRQ5 = 0x2500 - IRQ6 = 0x2600 - IRQ7 = 0x2700 In this example, the LED's on the CFV2-66 increment each time an interrupt occurs. ------------------------------------------------------------------------*/ INTERRUPT(Timer2ISR, 0x2400) { static BYTE nIRQ; sim.timer2.ter &= 0x02; // clear Timer2 event condition putleds(nIRQ++ + 1); // increment & display count isr_cnt++; } /*------------------------------------------------------------------------ Example setup timer2 This function will set up the ColdFire registers related to Timer2. Timer2 is also configured to generate an interrupt every 10,000 ticks (1 second). Parameters: irq_level - Internal interrupt level to use. Valid values are 1 - 7. Note: Only IRQ 1, 4, and 7 are available for EXTERNAL interrupts. prescale - Sets prescale value for timer. The system clock is 40MHz. Each prescale increment divides the system clock by 16. To calculate a prescale value, use: (16 * prescale)/40MHz. For example, a prescaler of 250 gives: (16 * 250)/40MHz = 100us per timer tick. ref_count - Sets Timer Reference Register (TRR). The 16-bit reference value is compared to the free running timer counter as an output compare function. The TRR is set at reset. TRR is matched at TRR+1 intervals, so the count should be one less than the desired total number of counts. Returns: 0 if setup was successful, -1 if unsuccessful ------------------------------------------------------------------------*/ int SetupTimer2(BYTE irq_level, WORD prescale, WORD ref_count) { if ( (irq_level < 1) || (irq_level > 7) ) return -1; // set interrupt vector in vector table //This value of 70 is gotten from the 5272 UM table 7-8 vector_base.table[70] = (long)&Timer2ISR; // Reset timer2. All bits set to reset values. sim.timer2.tmr |= 0x0001; // enable timer sim.timer2.tmr = 0; // reset timer //Setup Timer sim.timer2.tmr |= 0x0004; // Divide system clock by 16 sim.timer2.tmr |= 0x0018; // Set Ref Int Enable and FreeRun/Restart sim.timer2.tmr |= (prescale << 8); // Set prescale sim.timer2.trr = ref_count; // Set ref count sim.timer2.tmr |= 0x0001; // Start timer // Congigure Timer2 interrupt control register to specified level sim.icr1 = 0x00000800 | (irq_level << 8); return 0; }