Polling Method in Embedded Programming
Polling operation in embedded programming refers to actively sampling the status of an external device. Polling is most often used in terms of input/output (I/O), and it is the process where the embedded system waits for an external device to check for its readiness or status. For example, we have a simple microcontroller device with one push button and one LED. When we press the button, LED will be lit. If we press the button again, then the LED will be off. The pseudocode code would be like:
if button pressed, toggle LED
In this program you can see that the microcontroller always checks the status of the button and then decides the LED condition.
A real-life example about polling method is you are working on homework in your room, and waiting for UPS to deliver an important package. The polling method is exactly like you often (may be every 5 minutes) go to the front door and open the door to check whether your package has come or not.
Polling basically means that the embedded system will check the all the status for each device at set intervals, which is called polling cycle. For example, check the switch state every 5 ms, detect the obstacle every 20 ms, and read the room temperature every 5 sec. The optimal polling cycle will vary according to several factors, including the desired speed of response and the overhead (e.g. processor time and bandwidth) of the polling.
Implementation of Polling Method in a Single Task
For a simple embedded system, it is usually designed to execute multiple jobs in a single task, such as in the main() function. There are two types of polling methods can be used in the code, as shown below. Assume that the latency time on each job can be ignored.
Same Polling Cycle
Figure 1: Multi-Jobs with Same Polling Cycle
It is easy to implement If all the jobs have the same polling cycle. The following code uses a simple delay function for polling interval.
delay for 20 ms
Or, the following code uses SysTick to get more accurate time interval:
get the SysTick value to time1
loop until ( time1 - SysTick) greater then or equal to 20 ms