//used to initialize all of the timer at once uint32_t intervalTimer_initAll() { for (int i=0; i<=2; i++) { intervalTimer_init(i);// calls each of the timers } }
uint32_t intervalTimer_reset_timer(uint32_t baseAddress, uint32_t timerNumber) { // Write a 0 into the TLR0 and TLR1 registers Xil_Out32(baseAddress + INTERVALTIMER_TLR0_ADDRESS , INTERVALTIMER_INITIALIZE); Xil_Out32(baseAddress + INTERVALTIMER_TLR1_ADDRESS , INTERVALTIMER_INITIALIZE); // Write a 1 into the LOAD0 and LOAD1 bits of the TCSR0 and TCSR1 register uint32_t startZeroA = Xil_In32(baseAddress + INTERVALTIMER_TCSR0_ADDRESS); uint32_t startZeroB = Xil_In32(baseAddress + INTERVALTIMER_TCSR1_ADDRESS); startZeroA = startZeroA | INTERVALTIMER_LOAD_BIT; startZeroB = startZeroB | INTERVALTIMER_LOAD_BIT; Xil_Out32(baseAddress + INTERVALTIMER_TCSR0_ADDRESS , startZeroA); Xil_Out32(baseAddress + INTERVALTIMER_TCSR1_ADDRESS , startZeroB); // Write a 0 back into the LOAD0 and LOAD1 registers so they stop loading 0 consistently uint32_t startOneA = Xil_In32(baseAddress + INTERVALTIMER_TCSR0_ADDRESS); uint32_t startOneB = Xil_In32(baseAddress + INTERVALTIMER_TCSR1_ADDRESS); startOneA = startOneA | INTERVALTIMER_LOAD_CLEAR_BIT; startOneB = startZeroB | INTERVALTIMER_LOAD_CLEAR_BIT; Xil_Out32(baseAddress + INTERVALTIMER_TCSR0_ADDRESS , startOneA); Xil_Out32(baseAddress + INTERVALTIMER_TCSR1_ADDRESS , startOneB); intervalTimer_init(timerNumber); return 0; }
// Just calls intervalTimer_init on all timers. u32 intervalTimer_initAll() { int i; for (i=0; i<INTERVAL_TIMER_MAX_TIMER_INDEX; i++) { intervalTimer_init(i); } return 0; }
// Makes sure the timer is off and then reset both 32-bit counters by loading a 0 into them. u32 intervalTimer_reset(u32 timerNumber) { // Load 0 in to the counter 0 load register. intervalTimer_writeRegister(timerNumber, INTERVAL_TIMER_TLR0_REG_OFFSET, 0); // Load 0 in to the counter 1 load register. intervalTimer_writeRegister(timerNumber, INTERVAL_TIMER_TLR1_REG_OFFSET, 0); // Load the contents of load register 0 into counter 0. intervalTimer_writeRegister(timerNumber, INTERVAL_TIMER_TCSRO_REG_OFFSET, INTERVAL_TIMER_TSCR0_LOAD0_BIT_MASK); // Load the contents of load register 1 into counter 1. intervalTimer_writeRegister(timerNumber, INTERVAL_TIMER_TCSR1_REG_OFFSET, INTERVAL_TIMER_TSCR1_LOAD1_BIT_MASK); // Reinitialize the timer. intervalTimer_init(timerNumber); return 0; }
uint32_t intervalTimer_runTest(uint32_t timerNumber) { intervalTimer_init(timerNumber); intervalTimer_reset(timerNumber); // Show that the timer is reset. printf("timer %ld TCR0 should be 0 at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); printf("timer %ld TCR1 should be 0 at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); // Statements returns false if TCR0 and TCR1 aren't 0 if (readTimerRegister(INTERVALTIMER_TCR0_ADDRESS , timerNumber) !=INTERVALTIMER_INITIALIZE) { return INTERVALTIMER_VALID_TIMER_FALSE; } if (readTimerRegister(INTERVALTIMER_TCR1_ADDRESS , timerNumber) !=INTERVALTIMER_INITIALIZE) { return INTERVALTIMER_VALID_TIMER_FALSE; } intervalTimer_start(timerNumber); // Show that the timer is running. // Test points for comparison u32 TP_One; u32 TP_Two; printf("The following register values should be changing while reading them.\n\r"); printf("timer %ld TCR0 should be changing at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); TP_One = readTimerRegister(INTERVALTIMER_TCR0_ADDRESS , timerNumber); printf("timer %ld TCR0 should be changing at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); printf("timer %ld TCR0 should be changing at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); printf("timer %ld TCR0 should be changing at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); TP_Two = readTimerRegister(INTERVALTIMER_TCR0_ADDRESS , timerNumber); printf("timer %ld TCR0 should be changing at this point:%ld\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); // Statement returns false if TCR0 isn't changing if (TP_One == TP_Two) { return INTERVALTIMER_VALID_TIMER_FALSE; } // Wait about 2 minutes so that you roll over to TCR1. // If you don't see a '1' in TCR1 after this long wait you probably haven't programmed the timer correctly. waitALongTime(); printf("timer %ld TCR0 value after wait:%lx\n\r", timerNumber , readTimerRegister(INTERVALTIMER_TCR0_ADDRESS, timerNumber)); printf("timer %ld TCR1 should have changed at this point:%ld\n\r", timerNumber ,readTimerRegister(INTERVALTIMER_TCR1_ADDRESS, timerNumber)); // Statement returns false if TCR1 didn't change if (readTimerRegister(INTERVALTIMER_TCR1_ADDRESS,timerNumber) == INTERVALTIMER_INITIALIZE) { return INTERVALTIMER_VALID_TIMER_FALSE; } return INTERVALTIMER_VALID_TIMER_TRUE; }
//used to reset a chosen timer uint32_t intervalTimer_reset(uint32_t timerNumber) { if(timerNumber == 0) { // to store a 0 into counter 0 (TCSR0), do the following: Xil_Out32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TLR0_OFFSET, 0x0000);//write a 0 into the TLR0 register. uint32_t read0 = Xil_In32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TCSR0_OFFSET);//read value stored at TCSR0 uint32_t newread0 = read0 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD0) a 1 Xil_Out32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TCSR0_OFFSET, newread0);//write a 1 into the LOAD0 bit in the TCSR0. //To store a 0 into counter 1 (TCSR1), do the following: Xil_Out32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TLR1_OFFSET, 0x0000);//write a 0 into the TLR1 register. uint32_t read1 = Xil_In32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TCSR1_OFFSET);//read value stored at TCSR1 uint32_t newread1 = read1 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD1) a 1 Xil_Out32(INTERVALTIMER_TIMER_0_BASEADDR + INTERVALTIMER_TCSR1_OFFSET, newread1);//write a 1 into the LOAD1 bit in the TCSR1 } if(timerNumber == 1) { // to store a 0 into counter 0 (TCSR0), do the following: Xil_Out32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TLR0_OFFSET, 0x0000);//write a 0 into the TLR0 register. uint32_t read0 = Xil_In32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TCSR0_OFFSET);//read value stored at TCSR0 uint32_t newread0 = read0 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD0) a 1 Xil_Out32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TCSR0_OFFSET, newread0);//write a 1 into the LOAD0 bit in the TCSR0. //To store a 0 into counter 1 (TCSR1), do the following: Xil_Out32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TLR1_OFFSET, 0x0000);//write a 0 into the TLR1 register. uint32_t read1 = Xil_In32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TCSR1_OFFSET);//read value stored at TCSR1 uint32_t newread1 = read1 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD1) a 1 Xil_Out32(INTERVALTIMER_TIMER_1_BASEADDR + INTERVALTIMER_TCSR1_OFFSET, newread1);//write a 1 into the LOAD1 bit in the TCSR1 } if(timerNumber == 2) { // to store a 0 into counter 0 (TCSR0), do the following: Xil_Out32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TLR0_OFFSET, 0x0000);//write a 0 into the TLR0 register. uint32_t read0 = Xil_In32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TCSR0_OFFSET);//read value stored at TCSR0 uint32_t newread0 = read0 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD0) a 1 Xil_Out32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TCSR0_OFFSET, newread0);//write a 1 into the LOAD0 bit in the TCSR0. //To store a 0 into counter 1 (TCSR1), do the following: Xil_Out32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TLR1_OFFSET, 0x0000);//write a 0 into the TLR1 register. uint32_t read1 = Xil_In32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TCSR1_OFFSET);//read value stored at TCSR1 uint32_t newread1 = read1 | INTERVALTIMER_FIFTH_ON;// make the fifth bit (LOAD1) a 1 Xil_Out32(INTERVALTIMER_TIMER_2_BASEADDR + INTERVALTIMER_TCSR1_OFFSET, newread1);//write a 1 into the LOAD1 bit in the TCSR1 } intervalTimer_init(timerNumber); // call the initialize function which clears TCSR0 and TCSR1 (especially to clear LOAD0 and LOAD1 bits) }
// runs a test to see if timers are operating correctly uint32_t intervalTimer_runTest(uint32_t timerNumber) { intervalTimer_init(timerNumber); //initialize the timer intervalTimer_reset(timerNumber); //reset the timer printf("timer_0 TCR0 should be 0 at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); // Show that the timer is reset. printf("timer_0 TCR1 should be 0 at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR1_OFFSET)); //reset the TCR1 as well as TCR0 intervalTimer_start(timerNumber); // Show that the timer is running. printf("The following register values should be changing while reading them.\n\r"); //print statement to the screen printf("timer_0 TCR0 should be changing at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); //these print the current values of the timer printf("timer_0 TCR0 should be changing at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); //repeat multiple times to show that change is continuous as time passes printf("timer_0 TCR0 should be changing at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); printf("timer_0 TCR0 should be changing at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); printf("timer_0 TCR0 should be changing at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET)); waitALongTime(); // Wait about 2 minutes so that you roll over to TCR1. printf("timer_0 TCR0 value after wait:%lx\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR0_OFFSET));// If you don't see a '1' in TCR1 after this long wait you probably haven't programmed the timer correctly. printf("timer_0 TCR1 should have changed at this point:%ld\n\r", readTimerRegister(timerNumber, INTERVALTIMER_TCR1_OFFSET));//this shows the time that has passed }
// // This will configure the counter, start it, see if it runs, stop it, see if it stays stopped. // Pretty good way to test for the existence of the counter hardware. u32 intervalTimer_test(u32 timerNumber) { u32 errorValue = 0; // See if the hardware appears to be present by writing a value to the load register and reading it back. u32 loadValue = 0x55AA; intervalTimer_writeRegister(timerNumber, INTERVAL_TIMER_TLR0_REG_OFFSET, loadValue); if (loadValue != intervalTimer_readRegister(timerNumber, INTERVAL_TIMER_TLR0_REG_OFFSET)) { printf("intervalTimer_test: hardware for timer: %lu does not appear to be present.\n\r", timerNumber); } intervalTimer_init(timerNumber); // Init the timer. intervalTimer_reset(timerNumber); // Reset the timer to 0. u32 counter0Value = intervalTimer_readRegister(timerNumber, INTERVAL_TIMER_TCR0_REG_OFFSET); // See if the timer is reset. Timer should be zero at this point. if (counter0Value) { printf("intervalTimer_test: counter 0 should be zero after initialization. Value is: %lu.\n", counter0Value); errorValue = 1; } else { // OK, see if the timer will run. intervalTimer_start(timerNumber); volatile int i; // Wait for a bit. for (i=0; i<1000; i++); counter0Value = intervalTimer_readRegister(timerNumber, INTERVAL_TIMER_TCR0_REG_OFFSET); if (!counter0Value) { printf("intervalTimer_test: counter 0 does not seem to be running. Value is: %lu.\n\r", counter0Value); errorValue = 1; } else { // OK. See if the timer will stop. intervalTimer_stop(timerNumber); counter0Value = intervalTimer_readRegister(timerNumber, INTERVAL_TIMER_TCR0_REG_OFFSET); int i; // Wait for a bit. for (i=0; i<1000; i++); u32 secondCounter0Value = intervalTimer_readRegister(timerNumber, INTERVAL_TIMER_TCR0_REG_OFFSET); if (counter0Value != secondCounter0Value) { printf("intervalTimer_test: counter 0 does not seem to be stopped.\n\r"); errorValue = 1; } } } return errorValue; }
uint32_t intervalTimer_initAll() { // Returns 1 if timer0 and timer1 and timer2 are all initialized. Returns 0 if one isn't initialized return intervalTimer_init(INTERVALTIMER_2_ID) & intervalTimer_init(INTERVALTIMER_1_ID) & intervalTimer_init(INTERVALTIMER_0_ID); }