Merge branch "preempt" in "trunk".
[bertos.git] / bertos / cpu / power.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Bernie Innocenti <bernie@codewiz.org>
30  * -->
31  *
32  * \brief CPU power management functions
33  *
34  * \author Bernie Innocenti <bernie@codewiz.org>
35  */
36
37 #ifndef CPU_POWER_H
38 #define CPU_POWER_H
39
40 #include "cfg/cfg_proc.h"
41 #include "cfg/cfg_wdt.h"
42
43 #if CONFIG_KERN
44         #include <kern/proc.h>
45 #endif
46
47 #if CONFIG_WATCHDOG
48         #include <drv/wdt.h>
49 #endif
50
51 /**
52  * Let the CPU rest in tight busy loops
53  *
54  * User code that sits in a busy loop should call cpu_relax() every
55  * once in a while to perform system-dependent idle processing.
56  *
57  * Depending on the system configuration, this might perform different
58  * actions:
59  *
60  *  - yield the CPU to other processes
61  *  - reset the watchdog timer to avoid it from triggering
62  *  - scale the CPU speed down to save power (unimplemented)
63  *  - let the event loop of the emulator process a few events
64  *
65  * \see proc_yield() cpu_pause()
66  */
67 INLINE void cpu_relax(void)
68 {
69 #if CONFIG_KERN
70         if (proc_preemptAllowed())
71                 proc_yield();
72 #endif
73
74 #if CONFIG_WATCHDOG
75         wdt_reset();
76 #endif
77 }
78
79 /**
80  * Stop the processor until the next interrupt occurs.
81  *
82  * Pausing the CPU effectively reduces power usage, and should be used
83  * whenever the program is idle waiting for the next event to occur.
84  *
85  * To avoid deadlocking, the caller should normally check for the
86  * desired condition with interrupts disabled, and enter this function
87  * while interrupts are still disabled:
88  *
89  * \code
90  *     IRQ_DISABLE();
91  *     while (!event_occurred)
92  *         cpu_pause();
93  *     IRQ_ENABLE();
94  * \endcode
95  *
96  * \note Some implementations of cpu_pause() may return before any interrupt
97  *       has occurred.  Calling code should take this possibility into account.
98  *
99  * \note This function is currently unimplemented
100  *
101  * \see cpu_relax() cpu_yield()
102  */
103 INLINE void cpu_pause(void)
104 {
105         //ASSERT_IRQ_DISABLED();
106         //IRQ_ENABLE();
107         cpu_relax();
108         //IRQ_DISABLE();
109 }
110
111 /**
112  * Safely call cpu_pause() until the COND predicate becomes true.
113  */
114 #define CPU_PAUSE_ON(COND) ATOMIC(while (!(COND)) { cpu_pause(); })
115
116 #endif /* CPU_POWER_H */