Merged revisions 4004-4036,4039-4048,4050-4095,4097-4100 via svnmerge from
[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 #include <cfg/compiler.h>
44
45 #if CONFIG_KERN
46         #include <kern/proc.h>
47 #endif
48
49 #if CONFIG_WATCHDOG
50         #include <drv/wdt.h>
51 #endif
52
53 /**
54  * Let the CPU rest in tight busy loops
55  *
56  * User code that sits in a busy loop should call cpu_relax() every
57  * once in a while to perform system-dependent idle processing.
58  *
59  * Depending on the system configuration, this might perform different
60  * actions:
61  *
62  *  - yield the CPU to other processes
63  *  - reset the watchdog timer to avoid it from triggering
64  *  - scale the CPU speed down to save power (unimplemented)
65  *  - let the event loop of the emulator process a few events
66  *
67  * \see proc_yield() cpu_pause()
68  */
69 INLINE void cpu_relax(void)
70 {
71 #if CONFIG_KERN
72         if (proc_preemptAllowed())
73                 proc_yield();
74 #endif
75
76 #if CONFIG_WATCHDOG
77         wdt_reset();
78 #endif
79 }
80
81 /**
82  * Stop the processor until the next interrupt occurs.
83  *
84  * Pausing the CPU effectively reduces power usage, and should be used
85  * whenever the program is idle waiting for the next event to occur.
86  *
87  * To avoid deadlocking, the caller should normally check for the
88  * desired condition with interrupts disabled, and enter this function
89  * while interrupts are still disabled:
90  *
91  * \code
92  *     IRQ_DISABLE();
93  *     while (!event_occurred)
94  *         cpu_pause();
95  *     IRQ_ENABLE();
96  * \endcode
97  *
98  * \note Some implementations of cpu_pause() may return before any interrupt
99  *       has occurred.  Calling code should take this possibility into account.
100  *
101  * \note This function is currently unimplemented
102  *
103  * \see cpu_relax() cpu_yield()
104  */
105 INLINE void cpu_pause(void)
106 {
107         //ASSERT_IRQ_DISABLED();
108         //IRQ_ENABLE();
109         cpu_relax();
110         //IRQ_DISABLE();
111 }
112
113 /**
114  * Safely call cpu_pause() until the COND predicate becomes true.
115  */
116 #define CPU_PAUSE_ON(COND) ATOMIC(while (!(COND)) { cpu_pause(); })
117
118 #endif /* CPU_POWER_H */