Search This Blog

Wednesday, February 3, 2016

The communication gateway of an MCU




  THE COMMUNICATION GATEWAY OF AN MCU


This is my first tech blog, on understanding communication principles between a micro controller unit (MCU) and it's peripherals. It explains the basic facts and principles and does not delve deep into system level details, but would provide a foundation which would aid the reader to move ahead with MCU programming using interrupts.






Introduction

So you want the MCU to talk  right??. So your MCU has to respond to other devices, and share their (device's) feelings, and understand it's requests. So you have to set a communication link between the MCU and all its subordinates , i.e other devices in total. We can use two techniques to setup the communication. They are

1. Polling
2. Interrupt

1. Polling: Polling is a technique of communication between the MCU, and it's peripherals, wherein the MCU keeps checking / asking / requesting it's peripherals if the latter ( peripherals ) needs any kind of service . This technique is considered to be very inefficient since it wastes MCU's time, and hence the MCU gets tied down checking by not doing anything useful.
To give a real time example, if I imagine a teacher as a MCU, and students in a class like the peripherals, the teacher asking every moment if the students do have any queries / questions, and hence the teacher gets tied down with this without proceeding ahead with the class.

2. Interrupts:

An interrupt is a method of communication in which, the peripherals send a signal to tell the MCU (microcontroller / processor /CPU) that it needs some kind of service. Following the message received from the peripheral, the MCU stops whatever activity it is doing, and attends / services the interrupt request. This method is efficient in practice, since the MCU is not tied down checking if any peripheral needs any service or not. It is performing it's job, and it responds to the peripheral only after receiving a request signal from the peripheral.
Unlike the earlier example, the teacher will not ask any students if they have any queries or not. He/ she keeps doing their job. They will respond to students, only if they receive any questions from the students. A question here in this case is compared  to an  interrupt signal.
If there are multiple interrupts which are received by the processor at a time, then it prioritizes the interrupts based on the level of importance of the interrupt.


Working principles of polling and interrupts

Polling: As mentioned earlier, polling is a system of communication in which the MCU repeatedly checks for the occurrence of an event.

Working principle: The working principle is explained in brief, as to how polling can be generated thru a piece of software code. Usually we create a polling mechanism using a simple loop, wherein the MCU keeps checking inside the loop, for the occurrence of an event.
Consider an example of a timer. As we know, a timer times out ( overflows ) after it reaches it's maximum count. MCU monitors a overflow flag inside the loop, to check if the flag is raised or not. Raise of the flag indicates that the timer has timed out, else it keeps polling the flag, until the flag is set high (raised ).
A crude structure of polling in 'C' can be written as given below. In the loop given below, the MCU checks the flag until the flag is raised high.

Code format of a polling mechanism in 'C'

while(1)               // Infinite loop
{
     while( overflow_flag==0);   // Polling
      overflow_flag=0;                //  Disable the overflow flag
}

As we see the code above, inside an infinite loop, we poll the overflow flag inside a time delay loop. Here the MCU keeps polling the overflow flag as long as it is equal to zero. Once the timer overflows, the flag is set high. We have to clear (make it zero) the flag otherwise, we cannot poll again for another timer overflow. As we see here in this case, MCU keeps polling the overflow flag and hence gets tied down, only for checking.

Interrupts: Interrupts are defined as external signals which are communicated to the MCU, by other peripheral devices. They indicate occurrence of an event.

Understanding basic operation of an interrupt mechanism

Working principle: Execution of an interrupt in an MCU is done by a separate unit called as an 'Interrupt controller'. Once an interrupt signal is received by the interrupt controller, the MCU ( processor ) stops / suspends its current activity and jumps / transfers to a separate program / sub routine. This routine / program ( separate program ) is called as an interrupt service routine (ISR), also called as an interrupt handler. These routines are stored separately in a different address location / table called as interrupt vector table (IVT). Once an interrupt is generated, the MCU jumps to the sub routine by knowing it's address which is stored inside the IVT. The process of switching the  MCU from its current activity to the sub routine is called as ' context switching'.
The MCU services/ responds back to the  interrupt in the sub-routine and after completion, the MCU transfers it's control back to it's current activity which it was performing earlier (before occurrence of the interrupt) and continues with its work.
Interrupt unlike polling would not keep checking continuously for the occurrence of an event / status of a flag. This saves the processor time, since the processor would be working on other functions. The processor would respond once it receives interrupt signals.

Code format of an interrupt mechanism in 'C'

main( )
{
    init code //    initialization routines
    while(1)   //  Infinite loop:
    {              //  Be here until interrupt signal is received

    }

void ISR_routine( )
{
    if (overflow_flag)  // check if flag is set
    {
         overflow_flag=0;
         // process the function required, if flag is set
    }

}

Conclusion

This article provides a basic understanding of communication techniques between the MCU and peripherals and outlines some fundamental working principles of polling and interrupts.








No comments:

Post a Comment