How to implement ISRs in C ?

This is a discussion on How to implement ISRs in C ? within the C Programming forums, part of the General Programming Boards category; Hi, Can anybody tell how to implement ISRs in C?...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    12

    How to implement ISRs in C ?

    Hi,

    Can anybody tell how to implement ISRs in C?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Your question is short and sweet, but unfortunately not quite so for the answer. Unfortunately, you also haven't given enough information to actually give a complete answer.

    The handling of ISR (Interrupt Service Requests) is dependant on the architecture of the processor. Most processors have a vector table to indicate which IRQ go to what interrupt handler. In all architectures I'm aware of, you can't [without support from the compiler] write interrupt handlers directly in C, but you can write a small "wrapper" that does the necessary steps to get a sane context for the C code to run in, and after the C-code has been run, restore the original context of the system.

    Most of that wrapper deals with saving and restoring registers that the ABI (C environment) "lets" the C functions destroy. For example, in x86, eax, ecx and edx are commonly "scratchpad" registers - so a function call is allowed to "destroy" those values. But an ISR happens at "any" time in the code, and of course, these registers may hold some useful data that is needed after the ISR returns.

    On some processors, there are more things to deal with. 29K for example, the ISR setup is a matter of a dozen or so instructions just to allow C code to run.

    On some processors, the vector table isn't a "table of adresses", but rather a set of fixed addresses that the processor jumps to, at for example 16 byte offsets, so you have some short sequence of instructions that jump off to somewhere else.

    In summary, a lot of what you need to do in an ISR can be done in C, but you need some amount of "setup" and "teardown" code to make the C code run correctly and correctly restore the state of "before the ISR".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    Yes. The question is very short. But this was the question which i faced in one of the interviews i attended. I didn't understand how to explain.

    Your explaination gave me the idea of how interrupts are serviced.

    But i dint understand from here
    "you can write a small "wrapper" that does the necessary steps to get a sane context for the C code to run in, and after the C-code has been run, restore the original context of the system"
    since i dont know what wrapper is n how it works.

    Can you explain with a small example if its easy to do so?

    Regards.
    Ambika

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the example would be dependant on the architecture of the processor.

    In x86, the wrapping code would look something like this:
    Code:
    wrap_irq:
                   push eax
                   push ecx
                   push edx
                   mov  eax, [esp+16]    // Get irq number
                   push eax
                   call   isrtable[eax * 4]
                   add  esp, 4
                   pop  edx
                   pop  ecx
                   pop  eax
                   iret
    This probably doesn't tell you much more than before, but it's quite hard to explain these things without a LOT of writing and a good amount of feedback from the other party.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    ok thank you.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    You're also strongly constrained by what the OS will allow you to do.

    Handling interrupts in your average consumer OS is a privileged task handled by drivers, not normal user code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    you mean to say in normal C coding ( application developers) will not use this ISR concept while coding?

  8. #8
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    If you are writing code in a normal application in Windows or Linux, you will never EVER write code that even knows about interrupts in a direct way. The programmer will possibly know that when reading the disk, network packets are sent or received, or as time passes, interrupts are generated by the hardware - but the correlation between those interrupts and the actual code in the application using files or network services, will be so remote that it's just like knowing that a spark-plug fires in the engine when you are driving a car - it is sort of useful to understand that this happens, but you don't NEED to know it to understand what's going on.

    If, on the other hand, you are working in an embedded system, or working on drivers in Windows or Linux, you may well deal with interrupts. If a potential employer is asking this sort of question, it's likely that they ask because it's relevant to the task you are to do. On the other hand, I was asked "How much water flows through the tames at London bridge" on an interview, and no, it wasn't for a job with the "Thames River Control".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  9. #9
    Registered User
    Join Date
    Nov 2007
    Posts
    12
    :-).
    I am a normal C applicaton developer n have never come accross these kind of concepts in the course of development. Was jus curious to know more on this quesion which was asked long before.
    To continue with this thread can you tell me what is the difference between C and Embedded C?
    Does our before discussion comes under Embedded C?

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    [rant]I personally HATE the term "Embedded C" - it means nothing, but it's a good term for someone who also knows little, to make the difference between C programming in an embedded world [this means something] and C programming in a desktop or server environment [for example].

    We don't say "Server C" or "Desktop C", right?
    [/rant]

    Embedded programming often involves lower level programming, such as device control functionality and direct access to hardware components. But the language C is no different here than it is in Windows - after all, Windows also allows you to access devices at the driver level - it may not be entirely trivial to create a driver, but that's a slightly different matter. Of course, Windows [and Linux] also takes control of Interrupts, DMA and any other low-level functionality "for you" - so even driver programmers don't need to worry about wrapper code and such things - you just tell Windows that "When the interrupt for my device happens, please call the function My_ISR()" when you register for an interrupt.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,832
    Embedded C covers all sorts of things from mobile phones, microwaves, DVD players, cameras, spacecraft etc etc.

    If it has an OS at all, it will likely be an RTOS.

    More often than not, you'll be dealing with physical addresses rather than virtual memory, so if it doesn't fit, there's no swap file to bail you out. There's usually a lot less of it about as well, maybe only a few MB at most.

    The processors are slower than your modern desktop machines by at least an order of magnitude. Some care is therefore required in choosing the best approach to do something.

    Debugging an embedded target can be a real challenge as it is easy to trash the entire machine and have no clue whatsoever as to what happened. Even the most severe bugs on your desktop user programs are easily trapped by either the debugger or the OS.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    lfs addicted
    Join Date
    Nov 2007
    Posts
    49
    this tutorial about kernel programming covers also managing ISRs and it is quite easy:
    http://www.osdever.net/bkerndev/Docs/title.htm

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 12-20-2007, 07:55 PM
  2. how do i implement IOleCommandTarget
    By hanhao in forum Windows Programming
    Replies: 1
    Last Post: 06-24-2007, 11:33 PM
  3. Implement "Whats This" using Win32
    By hemanth.balaji in forum Windows Programming
    Replies: 1
    Last Post: 05-29-2005, 07:03 AM
  4. How to implement custom window message
    By naruto in forum Windows Programming
    Replies: 1
    Last Post: 08-27-2004, 09:19 PM
  5. Can't Implement Tabbing
    By Invincible in forum Windows Programming
    Replies: 10
    Last Post: 02-27-2002, 05:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21