/** * * Starts the specified timer counter of the device such that it starts running. * The timer counter is reset before it is started and the reset value is * loaded into the timer counter. * * If interrupt mode is specified in the options, it is necessary for the caller * to connect the interrupt handler of the timer/counter to the interrupt source, * typically an interrupt controller, and enable the interrupt within the * interrupt controller. * * @param InstancePtr is a pointer to the XTmrCtr instance. * @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 None. * * @note None. * ******************************************************************************/ void XTmrCtr_Start(XTmrCtr * InstancePtr, u8 TmrCtrNumber) { u32 ControlStatusReg; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Read the current register contents such that only the necessary bits * of the register are modified in the following operations */ ControlStatusReg = XTimerCtr_ReadReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); /* * Reset the timer counter such that it reloads from the compare * register and the interrupt is cleared simultaneously, the interrupt * can only be cleared after reset such that the interrupt condition is * cleared */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, XTC_CSR_LOAD_MASK); /* * Remove the reset condition such that the timer counter starts running * with the value loaded from the compare register */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, ControlStatusReg | XTC_CSR_ENABLE_TMR_MASK); }
/** * * Returns the timer counter value that was captured the last time the external * capture input was asserted. * * @param InstancePtr is a pointer to the XTmrCtr instance. * @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 The current capture value for the indicated timer counter. * * @note None. * *******************************************************************************/ u32 XTmrCtr_GetCaptureValue(XTmrCtr * InstancePtr, u8 TmrCtrNumber) { Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); return XTimerCtr_ReadReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TLR_OFFSET); }
void timer_handler() { Xuint32 ControlStatusReg; ControlStatusReg = XTimerCtr_ReadReg(Timer_tmrctr.BaseAddress, 0, XTC_TCSR_OFFSET); count++; onLED(count); XTmrCtr_WriteReg(Timer_tmrctr.BaseAddress, 0, XTC_TCSR_OFFSET, ControlStatusReg |XTC_CSR_INT_OCCURED_MASK); }
/** * * Checks if the specified timer counter of the device has expired. In capture * mode, expired is defined as a capture occurred. In compare mode, expired is * defined as the timer counter rolled over/under for up/down counting. * * When interrupts are enabled, the expiration causes an interrupt. This function * is typically used to poll a timer counter to determine when it has expired. * * @param InstancePtr is a pointer to the XTmrCtr instance. * @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 TRUE if the timer has expired, and FALSE otherwise. * * @note None. * ******************************************************************************/ int XTmrCtr_IsExpired(XTmrCtr * InstancePtr, u8 TmrCtrNumber) { u32 CounterControlReg; Xil_AssertNonvoid(InstancePtr != NULL); Xil_AssertNonvoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT); Xil_AssertNonvoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Check if timer is expired */ CounterControlReg = XTimerCtr_ReadReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); return ((CounterControlReg & XTC_CSR_INT_OCCURED_MASK) == XTC_CSR_INT_OCCURED_MASK); }
/** * * Resets the specified timer counter of the device. A reset causes the timer * counter to set it's value to the reset value. * * @param InstancePtr is a pointer to the XTmrCtr instance. * @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 None. * * @note None. * ******************************************************************************/ void XTmrCtr_Reset(XTmrCtr * InstancePtr, u8 TmrCtrNumber) { u32 CounterControlReg; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Read current contents of the register so it won't be destroyed */ CounterControlReg = XTimerCtr_ReadReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); /* * Reset the timer by toggling the reset bit in the register */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, CounterControlReg | XTC_CSR_LOAD_MASK); XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, CounterControlReg); }
/** * * Stops the timer counter by disabling it. * * It is the callers' responsibility to disconnect the interrupt handler of the * timer_counter from the interrupt source, typically an interrupt controller, * and disable the interrupt within the interrupt controller. * * @param InstancePtr is a pointer to the XTmrCtr instance. * @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 None. * * @note None. * ******************************************************************************/ void XTmrCtr_Stop(XTmrCtr * InstancePtr, u8 TmrCtrNumber) { u32 ControlStatusReg; Xil_AssertVoid(InstancePtr != NULL); Xil_AssertVoid(TmrCtrNumber < XTC_DEVICE_TIMER_COUNT); Xil_AssertVoid(InstancePtr->IsReady == XIL_COMPONENT_IS_READY); /* * Read the current register contents */ ControlStatusReg = XTimerCtr_ReadReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); /* * Disable the timer counter such that it's not running */ ControlStatusReg &= ~(XTC_CSR_ENABLE_TMR_MASK); /* * Write out the updated value to the actual register. */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, ControlStatusReg); }
int Get_Timer() { return XTimerCtr_ReadReg(XPAR_AXI_TIMER_0_BASEADDR,XPAR_AXI_TIMER_0_DEVICE_ID, XTC_TCR_OFFSET); }
int Stop_Timer() { XTmrCtr_Disable(XPAR_AXI_TIMER_0_BASEADDR,XPAR_AXI_TIMER_0_DEVICE_ID); return XTimerCtr_ReadReg(XPAR_AXI_TIMER_0_BASEADDR,XPAR_AXI_TIMER_0_DEVICE_ID, XTC_TCR_OFFSET); }
/** * * Interrupt Service Routine (ISR) for the driver. This function only performs * processing for the device and does not save and restore the interrupt context. * * @param InstancePtr contains a pointer to the timer/counter instance for * the interrupt. * * @return None. * * @note None. * ******************************************************************************/ void XTmrCtr_InterruptHandler(void *InstancePtr) { XTmrCtr *TmrCtrPtr = NULL; u8 TmrCtrNumber; u32 ControlStatusReg; /* * Verify that each of the inputs are valid. */ Xil_AssertVoid(InstancePtr != NULL); /* * Convert the non-typed pointer to an timer/counter instance pointer * such that there is access to the timer/counter */ TmrCtrPtr = (XTmrCtr *) InstancePtr; /* * Loop thru each timer counter in the device and call the callback * function for each timer which has caused an interrupt */ for (TmrCtrNumber = 0; TmrCtrNumber < XTC_DEVICE_TIMER_COUNT; TmrCtrNumber++) { /* * Check if timer is expired */ ControlStatusReg = XTimerCtr_ReadReg(TmrCtrPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); if (ControlStatusReg & XTC_CSR_INT_OCCURED_MASK) { /* * Increment statistics for the number of interrupts and * call the callback to handle any application specific * processing */ TmrCtrPtr->Stats.Interrupts++; TmrCtrPtr->Handler(TmrCtrPtr->CallBackRef, TmrCtrNumber); /* * Read the new Control/Status Register content. */ ControlStatusReg = XTimerCtr_ReadReg(TmrCtrPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); /* * If in compare mode and a single shot rather than auto * reload mode then disable the timer and reset it such * so that the interrupt can be acknowledged, this * should be only temporary till the hardware is fixed */ if (((ControlStatusReg & XTC_CSR_AUTO_RELOAD_MASK) == 0) && ((ControlStatusReg & XTC_CSR_CAPTURE_MODE_MASK) == 0)) { /* * Disable the timer counter and reset it such * that the timer counter is loaded with the * reset value allowing the interrupt to be * acknowledged */ ControlStatusReg &= ~XTC_CSR_ENABLE_TMR_MASK; XTmrCtr_WriteReg(TmrCtrPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, ControlStatusReg | XTC_CSR_LOAD_MASK); /* * Clear the reset condition, the reset bit must * be manually cleared by a 2nd write to the * register */ XTmrCtr_WriteReg(TmrCtrPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, ControlStatusReg); } /* * Acknowledge the interrupt by clearing the interrupt * bit in the timer control status register, this is * done after calling the handler so the application * could call IsExpired, the interrupt * is cleared by writing a 1 to the interrupt bit of the * register without changing any of the other bits */ XTmrCtr_WriteReg(TmrCtrPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, ControlStatusReg | XTC_CSR_INT_OCCURED_MASK); } } }
/** * * Initializes a specific timer/counter instance/driver. Initialize fields of * the XTmrCtr structure, then reset the timer/counter * * @param InstancePtr is a pointer to the XTmrCtr instance. * @param DeviceId is the unique id of the device controlled by this * XTmrCtr component. Passing in a device id associates the * generic XTmrCtr component to a specific device, as chosen by * the caller or application developer. * * @return * - XST_SUCCESS if initialization was successful * - XST_DEVICE_IS_STARTED if the device has already been started * - XST_DEVICE_NOT_FOUND if the device doesn't exist * * @note None. * ******************************************************************************/ int XTmrCtr_Initialize(XTmrCtr * InstancePtr, u16 DeviceId) { XTmrCtr_Config *TmrCtrConfigPtr; int TmrCtrNumber; u32 StatusReg; Xil_AssertNonvoid(InstancePtr != NULL); /* * Lookup the device configuration in the temporary CROM table. Use this * configuration info down below when initializing this component. */ TmrCtrConfigPtr = XTmrCtr_LookupConfig(DeviceId); if (TmrCtrConfigPtr == (XTmrCtr_Config *) NULL) { return XST_DEVICE_NOT_FOUND; } /* * Check each of the timer counters of the device, if any are already * running, then the device should not be initialized. This allows the * user to stop the device and reinitialize, but prevents a user from * inadvertently initializing. */ for (TmrCtrNumber = 0; TmrCtrNumber < XTC_DEVICE_TIMER_COUNT; TmrCtrNumber++) { /* * Read the current register contents and check if the timer * counter is started and running, note that the register read * is not using the base address in the instance so this is not * destructive if the timer counter is already started */ StatusReg = XTimerCtr_ReadReg(TmrCtrConfigPtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET); if (StatusReg & XTC_CSR_ENABLE_TMR_MASK) { return XST_DEVICE_IS_STARTED; } } /* * Set some default values, including setting the callback * handlers to stubs. */ InstancePtr->BaseAddress = TmrCtrConfigPtr->BaseAddress; InstancePtr->Handler = NULL; InstancePtr->CallBackRef = NULL; /* * Clear the statistics for this driver */ InstancePtr->Stats.Interrupts = 0; /* Initialize the registers of each timer/counter in the device */ for (TmrCtrNumber = 0; TmrCtrNumber < XTC_DEVICE_TIMER_COUNT; TmrCtrNumber++) { /* * Set the Compare register to 0 */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TLR_OFFSET, 0); /* * Reset the timer and the interrupt, the reset bit will need to * be cleared after this */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, XTC_CSR_INT_OCCURED_MASK | XTC_CSR_LOAD_MASK); /* * Set the control/status register to complete initialization by * clearing the reset bit which was just set */ XTmrCtr_WriteReg(InstancePtr->BaseAddress, TmrCtrNumber, XTC_TCSR_OFFSET, 0); } /* * Indicate the instance is ready to use, successfully initialized */ InstancePtr->IsReady = XIL_COMPONENT_IS_READY; return XST_SUCCESS; }