Reading via interrupts

In our last program, we were checking if the button is pressed or not after each second. The button must remain pressed at the time of console printing to get it printed pressed. If you press the button quickly in between two printings, it will still print released. So in this situation, one button press event is lost. Consider another scenario. It is possible that nobody pressed the button for a long time. But still the program will wake up every 1 second and check for pin values. This is called polling. It wastes lots of CPU time and system resources on repetitive checking. This is not good programming practice. Polling prevents the CPU from going to sleep and saves power. This is not good for embedded systems where the battery power is crucial. Also, you cannot respond well to high frequency input signals using polling.

To solve this problem, some I/O devices/components are allowed to notify the CPU when a specific event occurs. These events are like a touch being sensed on a touchscreen or data copying being done or a device being attached to the USB port or the button is pressed. Some physical connection lines are reserved for this. I/O devices send notification signals on these lines to the CPU. This whole mechanism is called interrupt. With this mechanism in place, there is no need for the CPU to check the device for event information again and again. The CPU can do other work relying on the component/device and interrupt controller for notification of an event. The saved CPU time can be used to do better multitasking or going into sleep mode to save power. We often use interrupt a service without being aware of using it, for example, when the power button is pressed, an interrupt signal is sent to the processor, which gets handled by the OS to perform an orderly shutdown.

BeagleBone has a dedicated interrupt controller tightly coupled with the CPU, which handles interrupts. No event is lost because the interrupt controller makes sure every notification goes till CPU. When we want a button press event to be notified to the CPU via interrupt, there is a need to interrupt support on the GPIO pin to which the button is connected. When the direction is set to input, all GPIO pins on BeagleBone can generate interrupts. This is supported in the Linux kernel as well as at software level. BeagleBone supports interrupt generation when the input signal changes from LOW to HIGH (RISING) or from HIGH to LOW (FALLING) or in both cases (CHANGE). The input signal is RISING at the time of pushing button and FALLING at the time of the pressed button getting released. We can register our interrupt handler function for any of these events RISING / FAILING / CHANGE. Later when events occur, OS will be notified and our interrupt handler function will be called. This way, we will never miss any quick button presses and unnecessary wake ups & checks will be avoided.

BoneScript provides the function attachInterrupt() and detachInterrupt() which make use of the GPIO interrupt mechanism internally. The function attachInterrupt() maps specified events/modes on the specified pin to interrupt handler function. The handler function gets called when that event occurs on that pin. The function detachInterrupt() unmaps this binding. Here are prototypes of these functions:

  • attachInterrupt(pin, handler, mode, callback)

    The parameters are as follows:

    • pin: The BeagleBone pin identifier string.
    • handler: Expression evaluating to true or false. Based on evaluation result, interrupt is handled or ignored. We will write true at this place in our program so that every interrupt generated associated with attached interrupt is handled with our handler.
    • mode: Event to consider when generating interrupt. Mode may be RISING or FALLING or CHANGE. You need to specify RISING if you want to be notified when button is pressed and specify FALLING if you want button-release notification. Specify mode as CHANGE if you want to consider both button pressed and released to be considered.
    • callback: This is the name of the interrupt handler function. This function will be called automatically when interrupt is generated for specified mode.
  • detachInterrupt(pin, callback)

    The parameters are as follows:

    • pin - BeagleBone pin identifier string.
    • callback – Name of the function to be called upon completion of this function. Please note that this is an optional parameter. We are going to use detachInterrupt() without this parameter.

Before writing the program, we need to set up the circuit for the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.17.76.175