/** * * This function does a minimal test on the TmrCtr device and driver as a * design example. The purpose of this function is to illustrate * how to use the XTmrCtr component. * * * @param DeviceId is the XPAR_<TMRCTR_instance>_DEVICE_ID value from * xparameters.h * @param TmrCtrNumber is the timer counter of the device to operate on. * Each device may contain multiple timer counters. * The timer number is a zero based number with a range of * 0 - (XTC_DEVICE_TIMER_COUNT - 1). * * @return XST_SUCCESS if successful, XST_FAILURE if unsuccessful * * @note None * ****************************************************************************/ int TmrCtrSelfTestExample(u16 DeviceId, u8 TmrCtrNumber) { int Status; XTmrCtr *TmrCtrInstancePtr = &TimerCounter; /* * Initialize the TmrCtr driver so that it iss ready to use */ Status = XTmrCtr_Initialize(TmrCtrInstancePtr, DeviceId); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Perform a self-test to ensure that the hardware was built * correctly, use the 1st timer in the device (0) */ Status = XTmrCtr_SelfTest(TmrCtrInstancePtr, TmrCtrNumber); if (Status != XST_SUCCESS) { return XST_FAILURE; } return XST_SUCCESS; }
/** * This function does a minimal test on the timer counter device and driver as a * design example. The purpose of this function is to illustrate how to use the * XTmrCtr component. It initializes a timer counter and then sets it up in * compare mode with auto reload such that a periodic interrupt is generated. * * This function uses interrupt driven mode of the timer counter. * * @param IntcInstancePtr is a pointer to the Interrupt Controller * driver Instance * @param TmrCtrInstancePtr is a pointer to the XTmrCtr driver Instance * @param DeviceId is the XPAR_<TmrCtr_instance>_DEVICE_ID value from * xparameters.h * @param IntrId is XPAR_<INTC_instance>_<TmrCtr_instance>_VEC_ID * value from xparameters.h * @param TmrCtrNumber is the number of the timer to which this * handler is associated with. * * @return * - XST_SUCCESS if the Test is successful * - XST_FAILURE if the Test is not successful * * @note This function contains an infinite loop such that if interrupts * are not working it may never return. * *****************************************************************************/ int TmrCtrFastIntrExample(XIntc* IntcInstancePtr, XTmrCtr* TmrCtrInstancePtr, u16 DeviceId, u16 IntrId, u8 TmrCtrNumber) { int Status; int LastTimerExpired = 0; /* * Initialize the timer counter so that it's ready to use, * specify the device ID that is generated in xparameters.h */ Status = XTmrCtr_Initialize(TmrCtrInstancePtr, DeviceId); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Perform a self-test to ensure that the hardware was built * correctly, use the 1st timer in the device (0) */ Status = XTmrCtr_SelfTest(TmrCtrInstancePtr, TmrCtrNumber); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Connect the timer counter to the interrupt subsystem such that * interrupts can occur. This function is application specific. */ Status = TmrCtrSetupIntrSystem(IntcInstancePtr, TmrCtrInstancePtr, DeviceId, IntrId, TmrCtrNumber); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Setup the handler for the timer counter that will be called from the * interrupt context when the timer expires, specify a pointer to the * timer counter driver instance as the callback reference so the * handler is able to access the instance data */ XTmrCtr_SetHandler(TmrCtrInstancePtr, TimerCounterHandler, TmrCtrInstancePtr); /* * Enable the interrupt of the timer counter so interrupts will occur * and use auto reload mode such that the timer counter will reload * itself automatically and continue repeatedly, without this option * it would expire once only */ XTmrCtr_SetOptions(TmrCtrInstancePtr, TmrCtrNumber, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION); /* * Set a reset value for the timer counter such that it will expire * eariler than letting it roll over from 0, the reset value is loaded * into the timer counter when it is started */ XTmrCtr_SetResetValue(TmrCtrInstancePtr, TmrCtrNumber, RESET_VALUE); /* * Start the timer counter such that it's incrementing by default, * then wait for it to timeout a number of times */ XTmrCtr_Start(TmrCtrInstancePtr, TmrCtrNumber); while (1) { /* * Wait for the first timer counter to expire as indicated by * the shared variable which the handler will increment */ while (TimerExpired == LastTimerExpired) { } LastTimerExpired = TimerExpired; /* * If it has expired a number of times, then stop the timer * counter and stop this example */ if (TimerExpired == 3) { XTmrCtr_Stop(TmrCtrInstancePtr, TmrCtrNumber); break; } } TmrCtrDisableIntr(IntcInstancePtr, DeviceId); return XST_SUCCESS; }
/**************************************************************************** * * FUNCTION: * * main * * DESCRIPTION: * * This is the entry point for the example. The embedded system automatically * calls main. * * ARGUMENTS: * * None. * * RETURN VALUE: * * None. * * NOTES: * * None. * ****************************************************************************/ int main() { XStatus Status; int PreviousCount = 0; /* Using printf with the UART Lite assumes that the layer 0 device * driver was selected for the UART Lite in the XPS and the standard * I/O peripheral in XPS was set to the UART Lite */printf("\n\rStarting the Application\n\r"); /*************************** GPIO Setup *******************************/ /* The second GPIO example uses the higher level (layer 1) driver to * blink the LEDs, First initialize the GPIO component */ Status = XGpio_Initialize(&Gpio, XPAR_AXI_GPIO_0_DEVICE_ID); if (Status != XST_SUCCESS) { printf("GPIO initialization error\n\r\r"); } /* Set the direction for all signals to be inputs except the LED * outputs */ XGpio_SetDataDirection(&Gpio, 1, ~LED); /************************* Timer Setup ********************************/ /* Initialize the timer counter so that it's ready to use, * specify the device ID that is generated in xparameters.h */ Status = XTmrCtr_Initialize(&TimerCounter, XPAR_AXI_TIMER_0_DEVICE_ID); if (Status != XST_SUCCESS) { printf("Timer counter initialization error\n\r\r"); } /* Perform a self-test to ensure that the hardware was built * correctly, use the 1st timer in the device (0) */ Status = XTmrCtr_SelfTest(&TimerCounter, TIMER_COUNTER_0); if (Status != XST_SUCCESS) { printf("Timer counter self-test error\n\r"); } /* Setup the handler for the timer counter that will be called from the * interrupt context when the timer expires, specify a pointer to the * timer counter driver instance as the callback reference so the handler * is able to access the instance data */ XTmrCtr_SetHandler(&TimerCounter, TimerCounterHandler, &TimerCounter); /* Enable the interrupt of the timer counter so interrupts will occur * and use auto reload mode such that the timer counter will reload * itself automatically and continue repeatedly, without this option * it would expire once only */ XTmrCtr_SetOptions(&TimerCounter, TIMER_COUNTER_0, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION); /* Set a reset value for the timer counter such that it will expire * earlier than letting it roll over from 0, the reset value is loaded * into the timer counter when it is started */ XTmrCtr_SetResetValue(&TimerCounter, TIMER_COUNTER_0, resetTime /*RESET_VALUE*/); /* Start the timer counter such that it's incrementing */ XTmrCtr_Start(&TimerCounter, TIMER_COUNTER_0); /********************** Interrupt Controller Setup *********************/ /* * Initialize the interrupt controller driver so that it's ready to use, * using the device ID that is generated in xparameters.h */ Status = XIntc_Initialize(&InterruptController, XPAR_AXI_INTC_0_DEVICE_ID); if (Status != XST_SUCCESS) { printf("Interrupt controller initialization error\n\r"); } /* * Connect the device driver handler that will be called when an interrupt * for the device occurs, the device driver handler performs the specific * interrupt processing for the device */ Status = XIntc_Connect(&InterruptController, XPAR_AXI_INTC_0_AXI_TIMER_0_INTERRUPT_INTR, // we don't know what this is, but it's probably 0 (XInterruptHandler) XTmrCtr_InterruptHandler, &TimerCounter); if (Status != XST_SUCCESS) { printf("Interrupt controller connect error\n\r"); } /* * Start the interrupt controller so interrupts are enabled for all * devices that cause interrupts. Specify real mode so that the timer * counter can cause interrupts through the interrupt controller. */ Status = XIntc_Start(&InterruptController, XIN_REAL_MODE); if (Status != XST_SUCCESS) { printf("Interrupt controller start error\n\r"); } /* Enable the interrupt for the timer counter and enable interrupts in * the microblaze processor */ XIntc_Enable(&InterruptController, XPAR_AXI_INTC_0_AXI_TIMER_0_INTERRUPT_INTR); microblaze_enable_interrupts(); /********************** Application Processing *********************/ /* Insert foreground processing here, interrupts will handle * processing in the background */ while (1) { char get = (char)*(volatile int *)(XPAR_RS232_UART_1_BASEADDR); if (get >= ' ' && get <= '~') break; } // stop the timer microblaze_disable_interrupts(); // break out of the while look /* The application should not ever execute the following code, but it is * present to indicate if the application does exit because of an error */printf("Exiting the Application\n\r"); }