/****************************************************************************** * * Example program for NetBurner CFV2-40 * * Copyright 2000 NetBurner ALL RIGHTS RESERVED * * 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. * ********************************************************************************/ /*------------------------------------------------------------------------------ This example program demonstrates how to setup timer2 on the NetBurner CFV2-40 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/40MHz = 400ns. Revision History: 1.0 July 8, 1999, Initial Release 1.1 March 29, 2000 - Modified SetupTimer2() to use command line parameters - Added comments for other IRQ's -------------------------------------------------------------------------------*/ #include "predef.h" #include #include #include #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); /*------------------------------------------------------------------- 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 100us timer (250 * 400ns = 100us). // The reference count is set to 9999 to produce an interrupt every // second (100us * (9999+1) = 1sec). SetupTimer2(4, 250, 9999); while(1) { OSTimeDly((WORD)(TICKS_PER_SECOND * 2.5)); printf("Count = %d\r\n", 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-40 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 } /*------------------------------------------------------------------------ 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 + 1))/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 vector_base.table[24+irq_level] = (long)&Timer2ISR; // Congigure Timer2 interrupt control register to specified level // with priority 0 and autovector enabled. sim.icr10 = 0x80 | (irq_level << 2); sim.imr &= ~0x0400; // clear IRQ mask bit for Timer2 // 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 return 0; }