Thread: returning function from within ISR

  1. #1
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83

    returning function from within ISR

    Hello,

    I am planning an application with an interrupt routine which calls a function then exits back to main. How can I then make this function return to main, since I think that it would return to the ISR which would then have exited, which I suspect wouldn't make my program work very well.

    No code as yet, since I am still in the planning stages of flowcharting.

    --dave

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    It would return back to the ISR.

    But then again, you also need to be pretty careful about what you put in an ISR.
    - you have limited APIs you can call,
    - you have limited memory you can touch without getting involved with lots of semaphores / mutexes
    - you have a limited amount of time in which to do any processing.
    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.

  3. #3
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    I see, didn't realise that it was so complicated, could you point me towards a good resource for me to read on the subject?

    What I want to to in the ISR is to collect ASCII characters from a UART, store them in a char array, identify a certain section in the string to see if its for the particular box the application is running on, then analyse to see which function to call.

    hmm, as I typed that, it feels like a lot to do...

    --dave

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Unless this is a single-tasking embedded OS, you can not know whether your ISR was even called within the context of main or not.

    Theoretically, longjmp (using a setjmp in main) would be able to do this, but be aware that very strange things may (will!) happen to your system if you return to main and the processing before the interrupt was not currently in main itself.

    It really seems like a strange thing to want to do. You may wan to explain further what you actually are trying to achieve, which OS you are using, and such things.

    Edit: based on your description, I would think that the best thing to do would be to let the ISR collect the character data into a buffer (e.g. a circular buffer), and then let the main programs processing check the input buffer at regular intervals (including parsing the data in the buffer and "returning to main").

    --
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I agree with Matsp, you probably want your ISR to do as little as possible. everything after "store them in a char array" should definitely be done as a periodic task from main, and not in the ISR.
    What mirco?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    ok, time for some more meat on the bones...

    main is a continuous loop idle state which checks status on some parts of the hardware and stores in registers.

    The unit is a slave to a host system on the end of an RS485 comms channel, which will send one of three commands - send status (the registers being collected in main), switch on remotely and switch off remotely. As there are a number of units on the bus, i need to check that the message is for the units address and then look in the command for the expression so that I know which function to call to do the processing (was going to do this in a switch block), send the status or carry out the action then send a report.

    After that, return to main loop.

    I am using an STM32 processor, ARM based on the M3 cortex core.

    I have posted an image of my ISR flowchart in case my ramblings above make absolutely no sense.

    --dave

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Generally, you do not want to do that much work inside the ISR. receive a character, store it somewhere, and flag the main process to let it know that you recieved data, then let main sort out what to do with it.

    --
    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.

  8. #8
    Embedded in C...
    Join Date
    Sep 2008
    Location
    Basingstoke, Hampshire
    Posts
    83
    That makes sense - I had pretty much come to that conclusion based on the previous responses.

    How would I then branch main to run another function based on whether the 'data received' flag has been raised or not?

    --dave

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'd probably have two counters, one for "received" and one for "processed". Then the code becomes something like this:
    Code:
    volatile int received_count;
    volatile int processed_count;
    
    ISR()
    {
        ...
        recevied_count ++;
        ...
    }
    
    
    int main()
    {
       ...
       if (received_count != processed_count)
       {
           process();
       }
    }
    
    void process()
    {
        // do processing. 
       ...
       process_count++;
       ....
    }
    You may want to loop inside process() to until process_count == received_count.

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. returning pointers from a function
    By curlious in forum C++ Programming
    Replies: 2
    Last Post: 12-28-2003, 11:37 PM
  5. structure vs class
    By sana in forum C++ Programming
    Replies: 13
    Last Post: 12-02-2002, 07:18 AM