Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Andrew7
Level 3
Level 3
25 sign-ins 10 questions asked 10 replies posted

I am working with PSoc 5lp at the moment. I understood theoretically what an interrupt does, but why can't we write the work that is to be done with an interrupt in the main function only? Why separate ISR is needed?

Also, why flag variable = 0,1 is used?

0 Likes
1 Solution

Anshuman7,


Can you explain this in a simpler language?

 


I'll give it a try.

In the earliest days of electronic computers the computers were more like sophistic calculators.  It was programmed with one task and it calculated results (outputs) on inputs provide by the user.

An example of early computer use is to calculate trajectories of canon or mortar shells.  The user would provide the distance to land and the shell's gunpowder thrust and the computer would calculate the angle of the canon or mortar gun to achieve the trajectory needed to land where it was desired.

When computer evolved, they wanted to sample input data in real-time.  They did so by polling inputs.  However as they required faster and faster polling, it exceeded the processing power of the CPU.  Therefore an interrupt structure was created in HW.  This HW interrupt signal event forced the main() processing of code to "jump" to a special code section to quickly process the event because it didn't have to wait for the polling scan time.   Once the special code is complete, it returns to the place where the main() was interrupted to continue on.

This was the earliest attempt to perform a form of multitasking.

Why is using a flag in the ISR needed?

Theoretically it isn't necessary to use a flag to pass the event signal to the main() task.

For example, if the end result of processing an ISR is ONLY to turn ON or OFF an LED, then when you process the ISR just control the LED.  Normally the LED control is just changing the state of an GPIO pin.  This should NOT be a blocking function.

If the end result of an ISR needs to execute code that is a blocking function then it is HIGHLY recommended to use a signal flag in the ISR that is passed to main() to then further process the function that blocks.

I good example of a blocking function is sending bytes to a UART (ex: UART_Putxxx()).  These functions can be blocking without you being aware of it.  Executing the UART_Putxxxx() function in the ISR could be a significant problem in the project code causing undesired interrupt processing delays. 

This is why the ISR passing the signal flag to the main() is a preferred code structure.

I hope this helps.

Len
"Engineering is an Art. The Art of Compromise."

View solution in original post

0 Likes
3 Replies
Len_CONSULTRON
Level 9
Level 9
Beta tester 500 solutions authored 1000 replies posted

Anshuman7,

An Interrupt is a HW signal generated when a specific event happens.

Internally on the CPU when this HW signal event occurs, it "jumps" to special code assigned to process the event.  This is called the ISR (Interrupt Service Routine).

The ISR is best used to "get in" and "get out" quickly and not spend a lot of time in the ISR.  Therefore it is encouraged only to perform low-level non-blocking operations in the ISR.  Higher-level potentially blocking operations should be performed at the main() level.

It is quite a common method to pass a flag from the ISR to be used by the main() routine to process the higher-level potentially blocking calls.  The flag is set in the ISR and the flag gets cleared at the main() level.

Does this answer your question?

Len
"Engineering is an Art. The Art of Compromise."
Andrew7
Level 3
Level 3
25 sign-ins 10 questions asked 10 replies posted

""It is quite a common method to pass a flag from the ISR to be used by the main() routine to process the higher-level potentially blocking calls. The flag is set in the ISR and the flag gets cleared at the main() level.""

 

Can you explain this in a simpler language?

 

0 Likes

Anshuman7,


Can you explain this in a simpler language?

 


I'll give it a try.

In the earliest days of electronic computers the computers were more like sophistic calculators.  It was programmed with one task and it calculated results (outputs) on inputs provide by the user.

An example of early computer use is to calculate trajectories of canon or mortar shells.  The user would provide the distance to land and the shell's gunpowder thrust and the computer would calculate the angle of the canon or mortar gun to achieve the trajectory needed to land where it was desired.

When computer evolved, they wanted to sample input data in real-time.  They did so by polling inputs.  However as they required faster and faster polling, it exceeded the processing power of the CPU.  Therefore an interrupt structure was created in HW.  This HW interrupt signal event forced the main() processing of code to "jump" to a special code section to quickly process the event because it didn't have to wait for the polling scan time.   Once the special code is complete, it returns to the place where the main() was interrupted to continue on.

This was the earliest attempt to perform a form of multitasking.

Why is using a flag in the ISR needed?

Theoretically it isn't necessary to use a flag to pass the event signal to the main() task.

For example, if the end result of processing an ISR is ONLY to turn ON or OFF an LED, then when you process the ISR just control the LED.  Normally the LED control is just changing the state of an GPIO pin.  This should NOT be a blocking function.

If the end result of an ISR needs to execute code that is a blocking function then it is HIGHLY recommended to use a signal flag in the ISR that is passed to main() to then further process the function that blocks.

I good example of a blocking function is sending bytes to a UART (ex: UART_Putxxx()).  These functions can be blocking without you being aware of it.  Executing the UART_Putxxxx() function in the ISR could be a significant problem in the project code causing undesired interrupt processing delays. 

This is why the ISR passing the signal flag to the main() is a preferred code structure.

I hope this helps.

Len
"Engineering is an Art. The Art of Compromise."
0 Likes