int main() { init_platform(); XIOModule_Initialize(&gpo, XPAR_IOMODULE_0_DEVICE_ID); // Initialize the GPO module microblaze_register_handler(XIOModule_DeviceInterruptHandler, XPAR_IOMODULE_0_DEVICE_ID); // register the interrupt handler XIOModule_Start(&gpo); // start the GPO module XIOModule_Connect(&gpo, XIN_IOMODULE_FIT_1_INTERRUPT_INTR, timerTick, NULL); // register timerTick() as our interrupt handler XIOModule_Enable(&gpo, XIN_IOMODULE_FIT_1_INTERRUPT_INTR); // enable the interrupt microblaze_enable_interrupts(); // enable global interrupts u8 leds = 0; while (1){ // write the LED value to port 1 (you can have up to 4 ports) XIOModule_DiscreteWrite(&gpo, 1, leds++); xil_printf("%d", leds); xil_printf(","); delay(500); // delay one half second } return 0; }
/** * * This function connects the interrupt handler of the IO Module to the * processor. This function is separate to allow it to be customized for each * application. Each processor or RTOS may require unique processing to connect * the interrupt handler. * * @param None. * * @return None. * * @note None. * ****************************************************************************/ XStatus SetUpInterruptSystem(XIOModule *XIOModuleInstancePtr) { XStatus Status; /* * Connect a 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 = XIOModule_Connect(XIOModuleInstancePtr, IOMODULE_DEVICE_ID, (XInterruptHandler) DeviceDriverHandler, (void *)0); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Start the IO Module such that interrupts are enabled for all devices * that cause interrupts. */ Status = XIOModule_Start(XIOModuleInstancePtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Enable interrupts for the device and then cause interrupts so the * handlers will be called. */ XIOModule_Enable(XIOModuleInstancePtr, IOMODULE_DEVICE_ID); /* * Initialize the exception table. */ Xil_ExceptionInit(); /* * Register the IO module interrupt handler with the exception table. */ Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XIOModule_DeviceInterruptHandler, (void*) 0); /* * Enable exceptions. */ Xil_ExceptionEnable(); return XST_SUCCESS; }
/** * This function does a minimal test on the IO Module device and driver as a * design example. The purpose of this function is to illustrate how to use the * IO Module component. It initializes the Programmable Interval Timers 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 IO Module. * * @param IOModuleInstancePtr is a pointer to the IO Module driver * Instance * @param DeviceId is the XPAR_<IOModule_instance>_DEVICE_ID value from * xparameters.h * * @return XST_SUCCESS if the Test is successful, otherwise XST_FAILURE * * @note This function contains an infinite loop such that if interrupts * are not working it may never return. * *****************************************************************************/ XStatus IOModuleIntrExample(XIOModule *IOModuleInstancePtr, u16 DeviceId) { int Status; u8 Timer; XIOModule_Config *CfgPtr = IOModuleInstancePtr->CfgPtr; /* * Initialize the IO Module so that it's ready to use, specify the device * ID that is generated in xparameters.h */ Status = XIOModule_Initialize(IOModuleInstancePtr, DeviceId); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Perform a self-test to ensure that the hardware was built correctly. */ Status = XIOModule_SelfTest(IOModuleInstancePtr); if (Status != XST_SUCCESS) { return XST_FAILURE; } /* * Initialize and enable interrupts in the processor. */ IOModuleSetupIntrSystem(IOModuleInstancePtr); /* * Setup the handler for the IO Module handler that will be called from * the interrupt context when an interrupt occurs, specify a pointer to * the IO Module driver instance as the callback reference so the * handler is able to access the instance data. */ XIOModule_SetHandler(IOModuleInstancePtr, IOModuleHandler, IOModuleInstancePtr); for (Timer = 0; Timer < XTC_DEVICE_TIMER_COUNT; Timer++) { /* * Skip unused timers,timers with prescaler (since they may * have very long expiration times), timers without readable * counters, and timers with small size (since the counter * may not change when sampled). */ if (! (CfgPtr->PitUsed[Timer] && CfgPtr->PitPrescaler[Timer] == XTC_PRESCALER_NONE && CfgPtr->PitReadable[Timer] && CfgPtr->PitSize[Timer] > MIN_TIMER_BITS)) { TimerExpired[Timer] = MAX_INTR_COUNT; continue; } /* * Use auto reload mode such that the Programmable Interval Timers will * reload automatically and continue repeatedly, without this option * they would expire once only */ XIOModule_Timer_SetOptions(IOModuleInstancePtr, Timer, XTC_INT_MODE_OPTION | XTC_AUTO_RELOAD_OPTION); /* * Set a reset value for the Programmable Interval Timers such that * they will expire earlier than letting them roll over from 0, the * reset value is loaded into the Programmable Interval Timers when * they are started. */ XIOModule_SetResetValue(IOModuleInstancePtr, Timer, RESET_VALUE); /* * Enable the interrupt for the Programmable Interval Timers. */ XIOModule_Enable(IOModuleInstancePtr, Timer + XIN_IOMODULE_PIT_1_INTERRUPT_INTR); /* * Start the Programmable Interval Timers such that they are * decrementing by default, then wait for them to timeout a number of * times. */ XIOModule_Timer_Start(IOModuleInstancePtr, Timer); } while (1) { int TotalExpiredCount = 0; /* * Wait for the Programmable Interval Timers to expire as indicated by * the shared variable which the handler will increment, and stop each * timer when it has reached the expected number of times. */ for (Timer = 0; Timer < XTC_DEVICE_TIMER_COUNT; Timer++) { if (TimerExpired[Timer] >= MAX_INTR_COUNT) XIOModule_Timer_Stop(IOModuleInstancePtr, Timer); TotalExpiredCount += TimerExpired[Timer]; } /* * If all timers have expired the expected number of times, then stop * this example. */ if (TotalExpiredCount == MAX_INTR_COUNT * XTC_DEVICE_TIMER_COUNT) { break; } } IOModuleDisableIntr(IOModuleInstancePtr); return XST_SUCCESS; }