Jay Don Scott
Jay Don Scott

Reputation: 3

How to get data from an interrupt into a task-based system?

I am preparing for an embedded systems interview and was given this question as one of the questions to prepare with.

From my research, I learned that interrupts are called by a piece of hardware when there is an important issue that the OS should take care of and because of this data cannot be returned by an interrupt.

However, I didn't find any definitive information about how interrupts work with a task-based system. Is this a trick question about interrupts or is there a method to get data from them?

Upvotes: 0

Views: 850

Answers (3)

Clifford
Clifford

Reputation: 93466

It is true that an interrupt cannot "return" data to a caller, because there is no caller. Interrupts are triggered by asynchronous events independent of normal program flow.

However it is possible for an interrupt pass data to a thread/task context (or even other interrupts) via shared memory or inter-process communication (IPC) such as a message queue, pipe or mailbox. Such data exchange must be non-blocking. So for example, if a message queue is full, the ISR may not wait on the queue to become available - it must baulk and discard the data.

interrupts are called [...] when there is an important issue that the OS should take care of [...]

It is not about "importance" it is about timliness, determinusm, meeting real-time deadlines, and dealing with data before buffers or FIFOs are overrun for example. There need not even be an OS, ant interrupts are generally application specific and not an issue for the OS at all.

I didn't find any definitive information about how interrupts work with a task-based system.

Perhaps you need to hone your research technique (or Google Fu). https://www.google.com/search?q=rtos+interrupt+communication

Upvotes: 2

ThongDT
ThongDT

Reputation: 189

There are several ways to get data from interrupt:

  • Using queue or similar approach (implement in freeRTOS). NOTE: there is special API for ISR.
  • Using global variable. For this way you need to care about consistency of data because interrupt can happen anytime. ...

Upvotes: 0

Armandas
Armandas

Reputation: 2396

It's not a trick question. The problem is the same whether you have a "task-based system" (RTOS) or not:

  • Interrupts will happen at random times, relative to your main program. I.e. you could be calculating something like a = b + c and the interrupt could happen at a time where b is loaded into a register, but before c is.
  • Interrupts execute in a different context from your main program. Depending on the architecture, you could have a separate stack, for example.

Given the above, the interrupt service routines cannot take any parameters and cannot return any values. The way to pass data in and out of an ISR is to use shared memory (e.g. a global variable).

Upvotes: 0

Related Questions