# FIQ help needed

**URL:** <https://forums.freertos.org/t/fiq-help-needed/955>\
**Category:** Kernel\
**Created:** [June 21, 2007, 8:13pm UTC](https://forums.freertos.org/t/fiq-help-needed/955 "2007-06-21T20:13:34Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 21, 2007, 8:13pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/1 "2007-06-21T20:13:34Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Thursday, June 21, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da):

Processor:&nbsp; STM750 (ARM7)  
Compiler:&nbsp;&nbsp; IAR

I’m programming firmware for a stepper motor.&nbsp; Currently, the time between steps is controlled by a timer that sends a pulse to the stepper motor every time it expires.&nbsp; An IRQ interrupt allows the timer to be reloaded and other things to be done every step.

The problem is that when FreeRTOS switches tasks, it disables interrupts for as long as 28 microseconds.&nbsp; This causes the motor interrupt to be delayed sometimes until interrupts are reenabled.&nbsp; This interrupt MUST NOT be delayed for any length of time!

I thought that the answer to this problem would be to set up the timer to trigger a fast interrupt (FIQ), since they have a higher priority than IRQs.&nbsp; So I tried setting up Timer 2 to generate an FIQ.&nbsp; But when I run the program, the FIQ is always active because the FIQ Pending bit won’t clear (interrupt signal always on?).&nbsp; An oscilloscope shows no activity on the GPIO line defined as the output from the timer.

Can anyone tell me how to simply set up a timer to generate an FIQ?&nbsp; I can’t find any documentation anywhere on this subject, and ST’s documentation doesn’t tell an ARM newbie like me what to do.

Thanks!

---

<div class="post-metadata">

**Author:** ![rtel](https://d3iykfbgfh8zhg.cloudfront.net/user_avatar/forums.freertos.org/rtel/32/31_2.png) [@rtel](https://forums.freertos.org/u/rtel)\
**Post date:** [June 22, 2007, 7:49am UTC](https://forums.freertos.org/t/fiq-help-needed/955/2 "2007-06-22T07:49:35Z")

</div>

**[rtel](https://sourceforge.net/u/rtel/)** wrote on [Friday, June 22, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/293b):

For ST microcontroller specific settings I would suggest posting a question on the ST forum (somewhere on [mcu.st.com](http://mcu.st.com)).

As a [FreeRTOS.org](http://FreeRTOS.org) note, I have successfully used the FIQ to run high priority interrupts in the manner you describe.&nbsp; Note however that you may need to change the definitions of vPortEnterCritical() and vPortExitCritical().&nbsp; In the ST750/IAR port these use the intrinsics \_\_diable\_interrupt() and \_\_enable\_interrupt(), and I’m not sure if these disable/enable both IRQ and FIQ, or just IRQ.&nbsp; In your case you want only the IRQ to be disabled.&nbsp; Note also that FIQ interrupts cannot use [FreeRTOS.org](http://FreeRTOS.org) API functions with such a setup.

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [June 22, 2007, 1:02pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/3 "2007-06-22T13:02:26Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Friday, June 22, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/293b/c7a0):

I can help you with this one.&nbsp; \_\_disable\_interrupt() disables both FIQ and IRQ interrupts.&nbsp; Here’s the disassembled code for the IAR functions called by the intrinsic (comments are my own).&nbsp; Note that there are two entry points in the disable function, one for Thumb mode and one for ARM mode, but for some reason there’s no Thumb mode entry for the enable function.

Disable interrupts:

??DiI\_t:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; This function is in Thumb mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; pc&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Jump to the ARM function  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nop&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Alignment instruction  
??DiI\_a:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; This function is in ARM mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mrs&nbsp;&nbsp;&nbsp;&nbsp; r12, cpsr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Get the status flags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; orr&nbsp;&nbsp;&nbsp;&nbsp; r12, r12, #0xC0&nbsp; ; Disable both IRQ and FIQ  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msr&nbsp;&nbsp;&nbsp;&nbsp; cpsr\_c, r12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Store the updated status flags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mrs&nbsp;&nbsp;&nbsp;&nbsp; r12, cpsr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Get the updated status flags again  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ands&nbsp;&nbsp;&nbsp; r12, r12, #0xC0&nbsp; ; Were the flags set okay?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; beq&nbsp;&nbsp;&nbsp;&nbsp; ??DiI\_a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; No, go back and try again  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Return to caller

Enable interrupts:

??EiI\_a:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; This function is in ARM mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mrs&nbsp;&nbsp;&nbsp;&nbsp; r12, cpsr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Get the status flags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bic&nbsp;&nbsp;&nbsp;&nbsp; r12, r12, #0xC0&nbsp; ; Enable both IRQ and FIQ  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; msr&nbsp;&nbsp;&nbsp;&nbsp; cpsr\_c, r12&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Store the updated status flags  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; mrs&nbsp;&nbsp;&nbsp;&nbsp; r12, cpsr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Get the updated status flags again  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ands&nbsp;&nbsp;&nbsp; r12, r12, #0xC0&nbsp; ; Were the flags cleared okay?  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bne&nbsp;&nbsp;&nbsp;&nbsp; ??EiI\_a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; No, go back and try again  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bx&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; lr&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ; Return to caller

In order to keep from disabling FIQ, the two 0xC0’s needs to be changed to 0x80.

I added custom assembler functions with the same names that affect only the IRQ.&nbsp; Using the same names causes my function to be used instead of IAR’s, so I don’t have to change any FreeRTOS code.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 10, 2007, 12:08pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/4 "2007-07-10T12:08:10Z")

</div>

**[frankandersen](https://sourceforge.net/u/frankandersen/)** wrote on [Tuesday, July 10, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/9b99):

Hi Ricky,

Where did you put the Enable and Disable functions, in my attempt the intrinsic functions are still called.

Best regards, Frank Andersen

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 11, 2007, 12:16pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/5 "2007-07-11T12:16:35Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Wednesday, July 11, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/9b99/b9a2):

I put them at the end of the assembler file containing the interrupt vectors.&nbsp; Make sure the names are exactly ??DiI\_t and ??EnI\_t for the Thumb mode entry points, and ??DiI\_a and ??EnI\_a for the ARM mode.&nbsp; Also, make sure you declare those labels public so that the linker will use them instead of the library versions.

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 12, 2007, 5:20am UTC](https://forums.freertos.org/t/fiq-help-needed/955/6 "2007-07-12T05:20:41Z")

</div>

**[frankandersen](https://sourceforge.net/u/frankandersen/)** wrote on [Thursday, July 12, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/8f94):

Hi Ricky,

I get two errors:

Error[403]: Illegal register, ‘PC’ is not allowed here C:\FreeRTOSV431\FreeRTOS\Demo\ARM7\_LPC2378\_STK\_IAR\SrcIAR\cstartup.s79 154

??DiI\_t: ; This function is in Thumb mode  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; bx pc ; Jump to the ARM function this is the error  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; nop ; Alignment instruction

Then I tried to replace the bx pc with nop, then the following error came.

Error[e27]: Entry "??DiI\_a" in module ?RESET ( C:\FreeRTOSV431\FreeRTOS\Demo\ARM7\_LPC2378\_STK\_IAR\Flash\_Debug\Obj\cstartup.r79 ) redefined in module ?DiI\_t ( C:\Program Files\

Where did you find the assembler code for the functions?

Best regards, Frank Andersen

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 12, 2007, 12:39pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/7 "2007-07-12T12:39:49Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Thursday, July 12, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/8f94/733c):

I used the IAR j-link debugger to step through the code.&nbsp; The original code set the interrupt disable bits and then looped back to check if they were still set.&nbsp; I got rid of the loop in my version.

Perhaps you can try an end run around the compiler by using “bx r15”?&nbsp; If that doesn’t work, try  
\_\_\_\_\_\_\_\_ldr\_\_\_\_\_r0, =??DiI\_t  
\_\_\_\_\_\_\_\_bx\_\_\_\_\_\_r0

As for error e27, it appears that you may be including the library file endisint.s79 in your project.&nbsp; If you do that you’ll get the “redefined” error.&nbsp; You can only replace functions that appear in libraries, so you’ll need to remove the file from your project list and just use the C runtime library (either Normal or Full, go to Options -\> C/C++ Compiler -\> Library Configuration to set it).

Here’s the exact code I have for the IAR EWARM assembler version 4.41:

\_\_\_\_\_\_\_\_rseg\_\_\_\_CODE:CODE(2)  
\_\_\_\_\_\_\_\_code16

??DiI\_t:  
\_\_\_\_\_\_\_\_bx\_\_\_\_\_\_pc\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Switch to arm mode  
\_\_\_\_\_\_\_\_nop\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Alignment instruction

\_\_\_\_\_\_\_\_code32

??DiI\_a:  
\_\_\_\_\_\_\_\_mrs\_\_\_\_\_r0, cpsr\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Get the status flags  
\_\_\_\_\_\_\_\_orr\_\_\_\_\_r0, r0, #0x80\_\_\_\_\_\_\_\_\_\_\_// Disable IRQ interrupts  
\_\_\_\_\_\_\_\_msr\_\_\_\_\_cpsr\_c, r0\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Store the updated status flags  
\_\_\_\_\_\_\_\_bx\_\_\_\_\_\_lr\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Return

\_\_\_\_\_\_\_\_code16

??EnI\_t:  
\_\_\_\_\_\_\_\_bx\_\_\_\_\_\_pc\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Switch to arm mode  
\_\_\_\_\_\_\_\_nop\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Alignment instruction

\_\_\_\_\_\_\_\_code32

??EnI\_a:  
\_\_\_\_\_\_\_\_mrs\_\_\_\_\_r0, cpsr\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Get the status flags  
\_\_\_\_\_\_\_\_bic\_\_\_\_\_r0, r0, #0x80\_\_\_\_\_\_\_\_\_\_\_// Enable IRQ interrupts  
\_\_\_\_\_\_\_\_msr\_\_\_\_\_cpsr\_c, r0\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Store the updated status flags  
\_\_\_\_\_\_\_\_bx\_\_\_\_\_\_lr\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_// Return

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 13, 2007, 6:57am UTC](https://forums.freertos.org/t/fiq-help-needed/955/8 "2007-07-13T06:57:52Z")

</div>

**[frankandersen](https://sourceforge.net/u/frankandersen/)** wrote on [Friday, July 13, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/7f09):

Hi Ricky,

Thank you very much, I was not aware of the RSEG and CODExx things.

Do you know how to the functions \_\_enable\_interrupt and \_\_disable\_interrupt is defined, I would like to make two functions for enabling and disabling the FIQ interrupt. I have made a duplicate of the ??DiI\_t, ??DiI\_a, ??EnI\_t and ??EnI\_a and change the #0x80 to #0x40.

I would like to have two functions \_\_enable\_fiq and \_\_disable\_fiq

But depending on if the ARM is running thumb or arm, I need to call:

??DiF\_t  
??DiF\_a

Does I need to check T bit in CPSR and then branch to the t or a function?

Regarding the loop that checks if the I or F bit is set, I have seen and app not from NXP that states that it is neccesary to check if the bit is set. But IAR does not do that.

Best regards, Frank Andersen

---

<div class="post-metadata">

**Author:** ![system](https://d2v6vdsk2p900z.cloudfront.net/original/1X/7fc8fdb729c645aab5b20282bbe34e57cb407be6.jpeg) [@system](https://forums.freertos.org/u/system)\
**Post date:** [July 13, 2007, 1:33pm UTC](https://forums.freertos.org/t/fiq-help-needed/955/9 "2007-07-13T13:33:45Z")

</div>

**[jorick23](https://sourceforge.net/u/jorick23/)** wrote on [Friday, July 13, 2007](https://sourceforge.net/p/freertos/discussion/382005/thread/2821a7bf/#f1da/7f09/fe4e):

If you’re calling the functions from C, you could write the routines in ARM mode only and let the compiler worry about generating the proper code for the call.&nbsp; Just make sure you use the \_\_arm keyword in the C function prototypes so that the compiler can generate the correct code.

\_\_arm void \_\_disable\_fiq (void);  
\_\_arm void \_\_enable\_fiq (void);

You don’t have to worry about using the ??DiF\_t naming convention since there’s no library version of that function.

If you’re calling them from assembler, write the routines in Thumb and ARM mode just like I did and then use the bl instruction to branch to the proper label depending on the mode you’re in.&nbsp; You don’t need to check the T bit because you’ll already know which mode you’re in by whether the code is written in Thumb or ARM instructions.

There may be some processors that need you to check if the bit is set.&nbsp; But I’m using the STR750 and STR912 processors which don’t need the check so I left it out.&nbsp; IAR included it just in case one of the processors that required the check was being used.
