-1

I’m working on firmware level code that is constantly checking for user input:

while (1) {
    if (user_input()) {
        Handle_user_input()
    }
}

Currently this loop causes CPU usage to be 100%. What I am hoping for is a way to implement a sleep() function that will cause the CPU to sleep or go into some lower power mode for a specified amount of time to reduce CPU usage to something more pleasant (close to 0%).

while (1) {
    if (user_input()) {
        Handle_user_input()
    }
    sleep(ONE_MILLISECOND);
}

Please send me your implementation of the sleep() function that you think would work.

Thank you.

P.S. If you wish to use PowerPC assembly language, please use 'asm volatile("your code");'.

user1766438
  • 492
  • 4
  • 13

1 Answers1

0

I'm not sure what sort of PowerPC chip you're aiming at, but you can look at some open-source firmware and OS code for inspiration.

  • you can look at Skiboot code - core/cpu.c has functions like cpu_relax and cpu idle code that can show you how to do this on modern Power CPUs.

  • Linux - arch/powerpc/include/asm/processor.h if you're looking at a 64-bit chip, see the spin_until_cond macro.

Both demonstrate changing thread priorities. If you want to go further, you can put a CPU to sleep and use a timer interrupt to wake it up again - skiboot has examples.

dja
  • 1,453
  • 14
  • 20
  • So you probably want to look at sleep states and timer interrupts. You should check out the processor manual (e.g. https://www.nxp.com/docs/en/reference-manual/MPC7410UM.pdf or similar, section 10.2). `arch/powerpc/platforms/powermac/sleep.S` in the linux kernel provides some very low-level asm and there's a lot of higher-level code written around it. I don't know what firmware stack you're dealing with or what other utility functions you have access to, but this is a pretty substantial chunk of work. – dja Dec 09 '19 at 04:57