140 likes | 178 Views
Learn how to utilize callback functions and setup Interrupt Service Routines to efficiently handle interrupt events. Integrating peripherals API for controlling LEDs and buttons.
E N D
Callback v.s. Polling • Polling • Using a loop to continuously check object. • Callback • Setup an ISR (Interrupt Service Routine). When an interrupt raises, the ISR will be called automatically to handle follow-up actions.
Callback v.s. Polling • Lab1 • Lab2 • vAHI_TickTimerInit(vTickTimerISR); while (TRUE) { …… if (bBlink_Flag) { led_on(LED0); led_off(LED1); bBlink_Flag = FALSE; } …… }
Sample • Install and Test
Overview • Through Integrated Peripherals API, we can initialize the Button and LED devices. • Through FontalBSP, we can control the LEDs and monitor the button status • Through callback mechanism provided by Integrated Peripherals API, we can catch the interrupt event.
Integrated peripherals API • The functions are defined in AppHardwareApi.h • u32AHI_init(void); • This should be called after every reset or wake-up and before any other AHI calls are made. • We can see that in sample codes about Lab1 and Lab2.
BoardAPI • Allow rapid software development by providing a function library for interfacing to the evaluation board components • LED control • Button control • Sensor control • The FT 6250 device provides the FontalBSP.
Callback function • Interrupt Handling • Interrupts from peripheral devices are handled by a set of device-specific callbacks. • callback registration PRIVATE void vHwDeviceIntCallback(uint32 u32DeviceId, uint32 u32ItemBitmap) • u32DeviceID is an enumerated value indicating the peripheral that generated the interrupt. • u32ItemBitmap is set for each individual interrupt source within the peripheral.
vTickTimerISR PUBLIC void AppColdStart(void) { vAHI_TickTimerInit(vTickTimerISR); } PRIVATE void vTickTimerISR(uint32 u32DeviceId, uint32 u32ItemBitmap) { if (bBlink_Flag) { led_on(LED0); led_off(LED1); bBlink_Flag = FALSE; } else { led_on(LED1); led_off(LED0); bBlink_Flag = TRUE; } }
Callback function • callback registration
Lab2 • What should you do in this lab • Using the Callback function to handle the buttons interrupt events and control LEDs. • Bonus • Battery limitation • Using another ticktimer to save energy. If no one press the buttons for a long time, automatically turn them off. • Once each of buttons are pressed, you are required to let LED return the last status.
Lab2 pseudo code • u32AHI_Init(); • u32AppApiInit(NULL, NULL, NULL, NULL, NULL, NULL, NULL); • led_init(); • btn_init(); • vAHI_SysCtrlRegisterCallback(vSystemISR);
Callback function PRIVATE void vSystemISR(uint32 u32DeviceId, uint32 u32ItemBitmap) { switch (u32DeviceId) { case E_AHI_DEVICE_SYSCTRL: if (btn_pressed(BUTTON0)) …… break; default: break; } }