int Init_ScuTimer(void) { int Status = XST_SUCCESS; XScuTimer_Config *ConfigPtr; int TimerLoadValue = 0; ConfigPtr = XScuTimer_LookupConfig(TIMER_DEVICE_ID); Status = XScuTimer_CfgInitialize(&TimerInstance, ConfigPtr, ConfigPtr->BaseAddr); if (Status != XST_SUCCESS) { xil_printf("In %s: Scutimer Cfg initialization failed...\r\n", __func__); return XST_FAILURE; } Status = XScuTimer_SelfTest(&TimerInstance); if (Status != XST_SUCCESS) { xil_printf("In %s: Scutimer Self test failed...\r\n", __func__); return XST_FAILURE; } XScuTimer_EnableAutoReload(&TimerInstance); /* * Set for 250 milli seconds timeout. */ TimerLoadValue = XPAR_CPU_CORTEXA9_0_CPU_CLK_FREQ_HZ / 8; XScuTimer_LoadTimer(&TimerInstance, TimerLoadValue); return XST_SUCCESS; }
/* * The application must provide a function that configures a peripheral to * create the FreeRTOS tick interrupt, then define configSETUP_TICK_INTERRUPT() * in FreeRTOSConfig.h to call the function. This file contains a function * that is suitable for use on the Zynq SoC. */ void vConfigureTickInterrupt( void ) { static XScuGic xInterruptController; /* Interrupt controller instance */ BaseType_t xStatus; extern void FreeRTOS_Tick_Handler( void ); XScuTimer_Config *pxTimerConfig; XScuGic_Config *pxGICConfig; const uint8_t ucRisingEdge = 3; /* This function is called with the IRQ interrupt disabled, and the IRQ interrupt should be left disabled. It is enabled automatically when the scheduler is started. */ /* Ensure XScuGic_CfgInitialize() has been called. In this demo it has already been called from prvSetupHardware() in main(). */ pxGICConfig = XScuGic_LookupConfig( XPAR_SCUGIC_SINGLE_DEVICE_ID ); xStatus = XScuGic_CfgInitialize( &xInterruptController, pxGICConfig, pxGICConfig->CpuBaseAddress ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */ /* The priority must be the lowest possible. */ XScuGic_SetPriorityTriggerType( &xInterruptController, XPAR_SCUTIMER_INTR, portLOWEST_USABLE_INTERRUPT_PRIORITY << portPRIORITY_SHIFT, ucRisingEdge ); /* Install the FreeRTOS tick handler. */ xStatus = XScuGic_Connect( &xInterruptController, XPAR_SCUTIMER_INTR, (Xil_ExceptionHandler) FreeRTOS_Tick_Handler, ( void * ) &xTimer ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */ /* Initialise the timer. */ pxTimerConfig = XScuTimer_LookupConfig( XPAR_SCUTIMER_DEVICE_ID ); xStatus = XScuTimer_CfgInitialize( &xTimer, pxTimerConfig, pxTimerConfig->BaseAddr ); configASSERT( xStatus == XST_SUCCESS ); ( void ) xStatus; /* Remove compiler warning if configASSERT() is not defined. */ /* Enable Auto reload mode. */ XScuTimer_EnableAutoReload( &xTimer ); /* Ensure there is no prescale. */ XScuTimer_SetPrescaler( &xTimer, 0 ); /* Load the timer counter register. */ XScuTimer_LoadTimer( &xTimer, XSCUTIMER_CLOCK_HZ / configTICK_RATE_HZ ); /* Start the timer counter and then wait for it to timeout a number of times. */ XScuTimer_Start( &xTimer ); /* Enable the interrupt for the xTimer in the interrupt controller. */ XScuGic_Enable( &xInterruptController, XPAR_SCUTIMER_INTR ); /* Enable the interrupt in the xTimer itself. */ vClearTickInterrupt(); XScuTimer_EnableInterrupt( &xTimer ); }
int main(void) { XGpio dip, push; XScuTimer Timer; /* Cortex A9 SCU Private Timer Instance */ XScuTimer_Config *ConfigPtr; int value, skip, psb_check, dip_check, status, timerCounter, time1, time2; VectorArray AInst; VectorArray BTinst; VectorArray PInst; xil_printf("-- Start of the Program --\r\n"); xil_printf("Enter choice: 1 (SW->Leds), 2 (Timer->Leds), 3 (Matrix), 4 (Exit) \r\n"); XGpio_Initialize(&dip, XPAR_SW_8BIT_DEVICE_ID); XGpio_SetDataDirection(&dip, 1, 0xffffffff); XGpio_Initialize(&push, XPAR_BTNS_5BIT_DEVICE_ID); XGpio_SetDataDirection(&push, 1, 0xffffffff); ConfigPtr = XScuTimer_LookupConfig (XPAR_PS7_SCUTIMER_0_DEVICE_ID); status = XScuTimer_CfgInitialize (&Timer, ConfigPtr, ConfigPtr->BaseAddr); if(status != XST_SUCCESS){ xil_printf("Timer init() failed\r\n"); return XST_FAILURE; } // Load timer with delay XScuTimer_LoadTimer(&Timer, ONE_SECOND); // Set AutoLoad mode XScuTimer_EnableAutoReload(&Timer); while (1) { xil_printf("CMD:> "); // Read an input value from the console. value = inbyte(); skip = inbyte(); //CR skip = inbyte(); //LF switch (value) { case '1': while(!XGpio_DiscreteRead(&push, 1)) { dip_check = XGpio_DiscreteRead(&dip, 1); LED_IP_mWriteReg(XPAR_LED_IP_S_AXI_BASEADDR, 0, dip_check); for (skip = 0; skip < 9999999; skip++); } break; case '2': timerCounter = 0; XScuTimer_Start(&Timer); while(!XGpio_DiscreteRead(&push, 1)) { if(XScuTimer_IsExpired(&Timer)) { XScuTimer_ClearInterruptStatus(&Timer); timerCounter = (timerCounter + 1) % 256; LED_IP_mWriteReg(XPAR_LED_IP_S_AXI_BASEADDR, 0, timerCounter); } } break; case '3': setInputMatrices(AInst, BTinst); displayMatrix(AInst); displayMatrix(BTinst); XScuTimer_Start(&Timer); // Software matrix time1 = XScuTimer_GetCounterValue(&Timer); multiMatrixSoft(AInst, BTinst, PInst); time2 = XScuTimer_GetCounterValue(&Timer); xil_printf("SW time: %d\n\n", time1-time2); displayMatrix(PInst); // Hardware matrix time1 = XScuTimer_GetCounterValue(&Timer); multiMatrixHard(AInst, BTinst, PInst); time2 = XScuTimer_GetCounterValue(&Timer); XScuTimer_Stop(&Timer); xil_printf("HW time: %d\n\n", time1-time2); displayMatrix(PInst); break; case '4': // Exit return XST_SUCCESS; break; default : break; } } }
int main (void) { XGpio dip, push; int psb_check, dip_check, dip_check_prev, count, Status; // PS Timer related definitions XScuTimer_Config *ConfigPtr; XScuTimer *TimerInstancePtr = &Timer; xil_printf("-- Start of the Program --\r\n"); XGpio_Initialize(&dip, XPAR_SW_4BIT_DEVICE_ID); XGpio_SetDataDirection(&dip, 1, 0xffffffff); XGpio_Initialize(&push, XPAR_BTNS_4BIT_DEVICE_ID); XGpio_SetDataDirection(&push, 1, 0xffffffff); count = 0; // Initialize the timer ConfigPtr = XScuTimer_LookupConfig(XPAR_PS7_SCUTIMER_0_DEVICE_ID); Status = XScuTimer_CfgInitialize(TimerInstancePtr, ConfigPtr, ConfigPtr->BaseAddr); if(Status != XST_SUCCESS){ return XST_FAILURE; } // Read dip switch values dip_check_prev = XGpio_DiscreteRead(&dip, 1); // Load timer with delay in multiple of ONE_SECOND XScuTimer_LoadTimer(TimerInstancePtr, ONE_SECOND*dip_check_prev); // Set AutoLoad mode XScuTimer_EnableAutoReload(TimerInstancePtr); // Start the timer XScuTimer_Start(TimerInstancePtr); while (1) { // Read push buttons and break the loop if Center button pressed psb_check = XGpio_DiscreteRead(&push, 1); if(psb_check & 0x1) { XScuTimer_Stop(TimerInstancePtr); break; } dip_check = XGpio_DiscreteRead(&dip, 1); if (dip_check != dip_check_prev) { xil_printf("DIP Switch Status %x, %x\r\n", dip_check_prev, dip_check); dip_check_prev = dip_check; // load timer with the new switch settings XScuTimer_LoadTimer(TimerInstancePtr, ONE_SECOND*dip_check_prev); count = 0; } if(XScuTimer_IsExpired(TimerInstancePtr)) { // clear status bit XScuTimer_ClearInterruptStatus(TimerInstancePtr); // output the count to LED and increment the count LED_IP_mWriteReg(XPAR_LED_IP_S_AXI_BASEADDR, 0, count); count++; } } return 0; }
/* * Setup the A9 internal timer to generate the tick interrupts at the * required frequency. */ static void prvSetupTimerInterrupt( void ) { extern void vTickISR (void); int Status; XScuTimer_Config *ScuConfig; prvSetupInterruptController(); Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, &InterruptController); /* * Connect to the interrupt controller */ Status = XScuGic_Connect(&InterruptController, XPAR_SCUTIMER_INTR, (Xil_ExceptionHandler)vTickISR, (void *)&Timer); if (Status != XST_SUCCESS) { return; } /* Timer Setup */ /* * Initialize the A9Timer driver. */ ScuConfig = XScuTimer_LookupConfig(XPAR_SCUTIMER_DEVICE_ID); Status = XScuTimer_CfgInitialize(&Timer, ScuConfig, ScuConfig->BaseAddr); if (Status != XST_SUCCESS) { return; } /* * Enable Auto reload mode. */ XScuTimer_EnableAutoReload(&Timer); /* * Load the timer counter register. */ XScuTimer_LoadTimer(&Timer, XSCUTIMER_CLOCK_HZ / configTICK_RATE_HZ); /* * Start the timer counter and then wait for it * to timeout a number of times. */ XScuTimer_Start(&Timer); /* * Enable the interrupt for the Timer in the interrupt controller */ XScuGic_Enable(&InterruptController, XPAR_SCUTIMER_INTR); /* * Enable the timer interrupts for timer mode. */ XScuTimer_EnableInterrupt(&Timer); /* * Do NOT enable interrupts in the ARM processor here. * This happens when the scheduler is started. */ }
int main() { init_platform(); XScuTimer Timer; XGpio ce, axis_data, axis_sw, btn, rst, sw; //setup of the timer XScuTimer_Config *TimerConfigPtr; XScuTimer *TimerInstancePtr = &Timer; TimerConfigPtr = XScuTimer_LookupConfig(TIMER_DEVICE_ID); int status, readFlag, switchVal, i = 0; int16_t rawData = 0; //the raw binary from the ACL chip float res = 0.003921568627; //resolution of each bit in the raw binary double gData, angle, sumAngle = 0; /* Initialize all the GPIOs used */ status = XGpio_Initialize(&btn, BTN_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } status = XGpio_Initialize(&sw, SW_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } status = XGpio_Initialize(&ce, CE_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } status = XGpio_Initialize(&axis_data, AXIS_DATA_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } status = XGpio_Initialize(&axis_sw, AXIS_SW_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } status = XGpio_Initialize(&rst, RST_ID); if (status != XST_SUCCESS) { return XST_FAILURE; } /* Private Timer Initialization */ status = XScuTimer_CfgInitialize(&Timer, TimerConfigPtr, TimerConfigPtr->BaseAddr); if (status != XST_SUCCESS) { return XST_FAILURE; } XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE); //loaded with 65 XScuTimer_EnableAutoReload(TimerInstancePtr); XScuTimer_Start(TimerInstancePtr); while(1){ /* Constantly read the switches */ switchVal = XGpio_DiscreteRead(&sw, 1); /* Constantly looking for when the reset button (btn0) is pressed */ XGpio_DiscreteWrite(&rst, 1, XGpio_DiscreteRead(&btn, 1)); /* Tells the PmodACL HW model what switches are active */ XGpio_DiscreteWrite(&axis_sw, 1, switchVal); /* Used to see if the axis data gpio has been read yet */ readFlag = 0; if (XGpio_DiscreteRead(&ce, 1) == 0){ XScuTimer_LoadTimer(TimerInstancePtr, TIMER_LOAD_VALUE); /* This keeps us in here until it's done sending/receiving */ while (XGpio_DiscreteRead(&ce, 1) == 0){ if (XScuTimer_IsExpired(TimerInstancePtr)){ rawData = XGpio_DiscreteRead(&axis_data, 1); readFlag = 1; //you've read! XScuTimer_ClearInterruptStatus(TimerInstancePtr); } } } if (readFlag == 1){ /* This state is used to see if the number received is negative or not (rawData > 512 is negative) */ if (rawData < 512){ gData = rawData * res; //res is the resolution of each bit received }else{ gData = (512 - rawData) * res; //subtracting rawData from 512 is used to get the actual negative number } //not the binary unsigned /* This switch is used to see what axis you're using and what to display * NOTE: The Z-axis is setup to do angle measurement! */ switch(switchVal){ case 0: printf("x-axis:%fg\r\n", gData); break; case 1: printf("y-axis:%fg\r\n", gData); break; case 2: gData += .0991; //add the offset gData = (gData>1)? 1 : gData; //if gData is greater than 1, gData = 1 (because you can't take the acos angle = acos(gData); //of a number greater than 1) angle *= (180 / 3.14159); //acos gives you the angle in radians, this converts it to degrees sumAngle += angle; //used for finding the average i++; /* After 20 samples it prints the average value out */ if (i == 20){ printf("z-axis:%f degrees\r\n", (sumAngle / 20) ); i = 0; sumAngle = 0; } break; default: printf("x-axis:%fg\r\n", gData); //chooses x-axis by default break; } readFlag = 0; } } cleanup_platform(); return 0; }