Wait for Interrupt on ARM-7 - Page 1
Forum section: electronics / general

author: AussieBruce
author_rank: Regular Contributor
timestamp: June 23, 2022, 11:41:55 am
content: I develop for ARM_7 TDMI, under Crossworks for ARM. The platform documentation doesn’t reference a Wait for Interrupt function, however the ARM developer documentation references WaitForInterrupt(), for platforms after ARM-6. If I use this function I get an undefined function error. How can I include an interrupt wait in my programs? Or is it not worth the effort, ie. is a simple loop forever equally effective?

author: T3sl4co1l
author_rank: Super Contributor
timestamp: June 23, 2022, 12:06:41 pm
content: "Wait for interrupt" sounds like any old WAIT or SLEEP instruction? (Mind, I'm not familiar with ARM, if this is also the case there.)A loop will not work, because it's interrupted, then returned immediately to the loop. You can however set a flag in the interrupt, and loop on the flag condition, breaking the loop when flag is set. Obviously, you need that flag set by every interrupt you want to break that loop.Tim

author: Siwastaja
author_rank: Super Contributor
timestamp: June 23, 2022, 12:29:23 pm
content: Sounds like you have just not included the right header file containing definition for WaitForInterrupt().I'm not familiar with this old ARM architecture but I bet the definition is actually nothing else but__asm__ __volatile__ (("WFI"));or similar.Maybe just define it yourself, or use such piece of inline assembly directly wherever needed.All this instruction does is lower power consumption and stay at this instruction instead of going forward. Any enabled interrupt enables all parts of CPU again and makes the CPU jump to the ISR normally. After the ISR, CPU stays enabled and continues code execution right after the WFI like it never existed. The usual and most correct way is to use WFI in an infinite loop so that you always go back to sleep after returning from any ISR. Also with some architectures it is possible to get spurious wakeups so best to indeed have loop which uses WFI again.Another typical pattern if you don't want to do 100% everything within ISRs is:Code:volatile int flag;void ISR(){// do somethingflag = 1;}void main(){while(1){__WFI();if(flag == 1){flag = 0;// do something}}}

author: abyrvalg
author_rank: Frequent Contributor
timestamp: June 23, 2022, 06:06:21 pm
content: WFI is not available on ARM7TDMI (which is ARMv4T, and WFI was introduced in ARMv6). However, there were chip designer-specific sleep mode implementations controlled via peripheral regs (you set some peripheral bit and the core stops until next event). In general vendor-agnostic case the only way seems to be to wait for a flag set from IRQ/FIQ handlers, as already suggested above. There are just two interrupt entry points on ARM7TDMI, so it’s easy to cover all cases.

author: SiliconWizard
author_rank: Super Contributor
timestamp: June 23, 2022, 06:13:30 pm
content: Edit: I've never used ARM_7 TDMI and confused it with ARM v7. So please take the info below with this in mind. If, as abyrvalg just said, WFI is not supported on it, then you'll have to look up in your particular MCU's docs to see what would be the closest to this. If there is anything. I let the information below just in case that could be useful, but it will apparently do nothing for your specific case.I don't know anything about the specific tools the OP uses, but I know enough ARM assembly to know that there is indeed a WFI instruction.https://developer.arm.com/documentation/ddi0406/cb/Application-Level-Architecture/Instruction-Details/Alphabetical-list-of-instructions/WFIHow to access it on your particular environment, if you don't want to resort to assembly, is a matter of digging that up in the documentation. If you can't find anything, then you can "implement" that yourself following what Siwastaja just posted.Note that if your environment has access to/uses CMSIS, then the function 'void __WFI(void)' is defined as part of the CMSIS-Core functions. (But it's very likely implemented just like Siwastaja suggested, as something like '__asm__ __volatile__ (("WFI"))' ). Now if you use CMSIS, you may as well use the functions that CMSIS defines.

author: abyrvalg
author_rank: Frequent Contributor
timestamp: June 23, 2022, 11:46:11 pm
content: WFI availability from ARMv6K and up:https://developer.arm.com/documentation/dui0473/m/arm-and-thumb-instructions/wfi(see Architectures).ARM7TDMI is ARMv4T:https://developer.arm.com/documentation/dvi0027/b/arm-architecture-v4t/arm7tdmi-processor-core