int wl_timer_initialize(){ int status = 0; XTmrCtr *TmrCtrInstancePtr = &TimerCounter; XTmrCtr_Config *TmrCtrConfigPtr; //Initialize the timer counter status = XTmrCtr_Initialize(TmrCtrInstancePtr, TMRCTR_DEVICE_ID); if (status == XST_DEVICE_IS_STARTED) { xil_printf("Timer was already running; clear/init manually\n"); TmrCtrConfigPtr = XTmrCtr_LookupConfig(TMRCTR_DEVICE_ID); TmrCtrInstancePtr->BaseAddress = TmrCtrConfigPtr->BaseAddress; TmrCtrInstancePtr->IsReady = XIL_COMPONENT_IS_READY; XTmrCtr_Stop(TmrCtrInstancePtr, 0); XTmrCtr_Reset(TmrCtrInstancePtr, 0); status = XTmrCtr_Initialize(TmrCtrInstancePtr, TMRCTR_DEVICE_ID); } if (status != XST_SUCCESS) { xil_printf("w3_node_init: Error in XtmrCtr_Initialize (%d)\n", status); } // Set timer 0 to into a "count down" mode XTmrCtr_SetOptions(TmrCtrInstancePtr, 0, (XTC_DOWN_COUNT_OPTION)); ///Timer Setup/// XTmrCtr_SetResetValue(TmrCtrInstancePtr,1,0); //Sets it so issuing a "start" command begins at counter=0 ///////////////// return status; }
/** * Initializes a specific timer/counter instance/driver. Initialize fields of * the XTmrCtr structure, then reset the timer/counter.If a timer is already * running then it is not initialized. * * * @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 *ConfigPtr; Xil_AssertNonvoid(InstancePtr != NULL); /* In case all timer counters are already started, don't proceed with * re-initialization. */ if ((InstancePtr->IsStartedTmrCtr0 == XIL_COMPONENT_IS_STARTED) && (InstancePtr->IsStartedTmrCtr1 == XIL_COMPONENT_IS_STARTED)) { return XST_DEVICE_IS_STARTED; } /* Retrieve configuration of timer counter core with matching ID. */ ConfigPtr = XTmrCtr_LookupConfig(DeviceId); if (!ConfigPtr) { return XST_DEVICE_NOT_FOUND; } XTmrCtr_CfgInitialize(InstancePtr, ConfigPtr, ConfigPtr->BaseAddress); return XTmrCtr_InitHw(InstancePtr); }
/** * * Initializes a specific timer/counter instance/driver. Initialize fields of * the XTmrCtr structure, then reset the timer/counter.If a timer is already * running then it is not initialized. * * * @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; int TmrCtrLowIndex = 0; int TmrCtrHighIndex = XTC_DEVICE_TIMER_COUNT; Xil_AssertNonvoid(InstancePtr != NULL); /* * If both the timers have already started, disallow the initialize and * return a status indicating it is started. This allows the user to stop * the device and reinitialize, but prevents a user from inadvertently * initializing. * In case one of the timers has not started then that particular timer * will be initialized */ if ((InstancePtr->IsStartedTmrCtr0 == XIL_COMPONENT_IS_STARTED) && (InstancePtr->IsStartedTmrCtr1 == XIL_COMPONENT_IS_STARTED)) { return XST_DEVICE_IS_STARTED; } /* * Ensure that only the timer which is NOT started can be initialized */ if ((InstancePtr->IsStartedTmrCtr0 == XIL_COMPONENT_IS_STARTED)) { TmrCtrLowIndex = 1; } else if ((InstancePtr->IsStartedTmrCtr1 == XIL_COMPONENT_IS_STARTED)) { TmrCtrHighIndex = 1; } else { InstancePtr->IsStartedTmrCtr0 = 0; InstancePtr->IsStartedTmrCtr1 = 0; } /* * 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; } /* * 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 = TmrCtrLowIndex; TmrCtrNumber < TmrCtrHighIndex; 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; }
/** * * 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 = XTmrCtr_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; }