1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (c) 2016 embedded brains GmbH & Co. KG |
---|
5 | * |
---|
6 | * Redistribution and use in source and binary forms, with or without |
---|
7 | * modification, are permitted provided that the following conditions |
---|
8 | * are met: |
---|
9 | * 1. Redistributions of source code must retain the above copyright |
---|
10 | * notice, this list of conditions and the following disclaimer. |
---|
11 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer in the |
---|
13 | * documentation and/or other materials provided with the distribution. |
---|
14 | * |
---|
15 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
16 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
17 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
18 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
19 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
20 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
21 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
22 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
23 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
24 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
25 | * POSSIBILITY OF SUCH DAMAGE. |
---|
26 | */ |
---|
27 | |
---|
28 | #ifndef LIBBSP_ARM_ATSAM_POWER_H |
---|
29 | #define LIBBSP_ARM_ATSAM_POWER_H |
---|
30 | |
---|
31 | #include <sys/types.h> |
---|
32 | |
---|
33 | #include <stdint.h> |
---|
34 | |
---|
35 | #ifdef __cplusplus |
---|
36 | extern "C"{ |
---|
37 | #endif /* __cplusplus */ |
---|
38 | |
---|
39 | /** |
---|
40 | * @brief Status of the Low Power Support |
---|
41 | */ |
---|
42 | typedef enum { |
---|
43 | /** |
---|
44 | * @brief Used for Initialization of Handlers |
---|
45 | */ |
---|
46 | ATSAM_POWER_INIT, |
---|
47 | /** |
---|
48 | * @brief Used for Switching On of Handlers |
---|
49 | */ |
---|
50 | ATSAM_POWER_ON, |
---|
51 | /** |
---|
52 | * @brief Used for Switching Off of Handlers |
---|
53 | */ |
---|
54 | ATSAM_POWER_OFF |
---|
55 | } atsam_power_state; |
---|
56 | |
---|
57 | /** |
---|
58 | * @brief Control structure for power control handling |
---|
59 | */ |
---|
60 | typedef struct atsam_power_control { |
---|
61 | /** |
---|
62 | * @brief Data pointer to the handler with its desired state |
---|
63 | */ |
---|
64 | void (*handler)( |
---|
65 | const struct atsam_power_control *control, |
---|
66 | atsam_power_state state |
---|
67 | ); |
---|
68 | /** |
---|
69 | * @brief Data chunk that is used by the handler |
---|
70 | */ |
---|
71 | union { |
---|
72 | void *arg; |
---|
73 | struct { |
---|
74 | uint8_t first; |
---|
75 | uint8_t last; |
---|
76 | } peripherals; |
---|
77 | } data; |
---|
78 | } atsam_power_control; |
---|
79 | |
---|
80 | /** |
---|
81 | * @brief Performs a power state change according to the state parameter. |
---|
82 | * |
---|
83 | * The handlers of the control table are invoked in forward order (invocation |
---|
84 | * starts with table index zero) for the ATSAM_POWER_INIT and ATSAM_POWER_OFF |
---|
85 | * states, otherwise the handlers are invoked in reverse order (invocation |
---|
86 | * starts with the last table index). |
---|
87 | * |
---|
88 | * @param controls Table with power controls. |
---|
89 | * @param n Count of power control table entries. |
---|
90 | * @param state The desired power state. |
---|
91 | * |
---|
92 | * @code |
---|
93 | * #include <rtems.h> |
---|
94 | * #include <pthread.h> |
---|
95 | * |
---|
96 | * #include <bsp/power.h> |
---|
97 | * |
---|
98 | * static atsam_power_data_rtc_driver rtc_data = { .interval = 5 }; |
---|
99 | * |
---|
100 | * static const atsam_power_control power_controls[] = { |
---|
101 | * ATSAM_POWER_CLOCK_DRIVER, |
---|
102 | * ATSAM_POWER_RTC_DRIVER(&rtc_data), |
---|
103 | * ATSAM_POWER_SLEEP_MODE |
---|
104 | * }; |
---|
105 | * |
---|
106 | * static pthread_once_t once = PTHREAD_ONCE_INIT; |
---|
107 | * |
---|
108 | * static void init(void) |
---|
109 | * { |
---|
110 | * atsam_power_change_state( |
---|
111 | * &power_controls[0], |
---|
112 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
113 | * ATSAM_POWER_INIT |
---|
114 | * ); |
---|
115 | * } |
---|
116 | * |
---|
117 | * void power_init(void) |
---|
118 | * { |
---|
119 | * pthread_once(&once, init); |
---|
120 | * } |
---|
121 | * |
---|
122 | * void low_power(void) |
---|
123 | * { |
---|
124 | * atsam_power_change_state( |
---|
125 | * &power_controls[0], |
---|
126 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
127 | * ATSAM_POWER_OFF |
---|
128 | * ); |
---|
129 | * atsam_power_change_state( |
---|
130 | * &power_controls[0], |
---|
131 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
132 | * ATSAM_POWER_ON |
---|
133 | * ); |
---|
134 | * } |
---|
135 | * @end |
---|
136 | */ |
---|
137 | void atsam_power_change_state( |
---|
138 | const atsam_power_control *controls, |
---|
139 | size_t n, |
---|
140 | atsam_power_state state |
---|
141 | ); |
---|
142 | |
---|
143 | /** |
---|
144 | * @brief Power handler for a set of peripherals according to the specified |
---|
145 | * peripheral indices. |
---|
146 | * |
---|
147 | * For the power off state, the peripherals are enabled in the PMC. |
---|
148 | * |
---|
149 | * For the power on state, the peripherals are disabled in the Power Management |
---|
150 | * Controller (PMC). |
---|
151 | * |
---|
152 | * @see ATSAM_POWER_PERIPHERAL(). |
---|
153 | */ |
---|
154 | void atsam_power_handler_peripheral( |
---|
155 | const atsam_power_control *controls, |
---|
156 | atsam_power_state state |
---|
157 | ); |
---|
158 | |
---|
159 | /** |
---|
160 | * @brief Power handler for the clock driver. |
---|
161 | * |
---|
162 | * For the power off state, the system tick is disabled. |
---|
163 | * |
---|
164 | * For the power on state, the system tick is enabled. In case no clock driver |
---|
165 | * is used by the application, then this may lead to a spurious interrupt |
---|
166 | * resulting in a fatal error. |
---|
167 | * |
---|
168 | * @see ATSAM_POWER_CLOCK_DRIVER(). |
---|
169 | */ |
---|
170 | void atsam_power_handler_clock_driver( |
---|
171 | const atsam_power_control *controls, |
---|
172 | atsam_power_state state |
---|
173 | ); |
---|
174 | |
---|
175 | /** |
---|
176 | * @brief Power handler for the RTC driver. |
---|
177 | * |
---|
178 | * This handler installs an interrupt handler during power support initialization. |
---|
179 | * |
---|
180 | * For the power off state, the RTC alarm interrupt is set up according to the |
---|
181 | * interval of the corresponding handler data. |
---|
182 | * |
---|
183 | * For the power on state, the RTC alarm interrupt is disabled. |
---|
184 | * |
---|
185 | * @see ATSAM_POWER_RTC_DRIVER(). |
---|
186 | */ |
---|
187 | void atsam_power_handler_rtc_driver( |
---|
188 | const atsam_power_control *controls, |
---|
189 | atsam_power_state state |
---|
190 | ); |
---|
191 | |
---|
192 | /** |
---|
193 | * @brief Power handler to enter the processor sleep mode. |
---|
194 | * |
---|
195 | * @see ATSAM_POWER_SLEEP_MODE(). |
---|
196 | */ |
---|
197 | void atsam_power_handler_sleep_mode( |
---|
198 | const atsam_power_control *controls, |
---|
199 | atsam_power_state state |
---|
200 | ); |
---|
201 | |
---|
202 | /** |
---|
203 | * @brief Power handler to enter the processor wait mode. |
---|
204 | * |
---|
205 | * The internal flash is put into deep sleep mode. |
---|
206 | * |
---|
207 | * @see ATSAM_POWER_WAIT_MODE(). |
---|
208 | */ |
---|
209 | void atsam_power_handler_wait_mode( |
---|
210 | const atsam_power_control *controls, |
---|
211 | atsam_power_state state |
---|
212 | ); |
---|
213 | |
---|
214 | /** |
---|
215 | * @brief Initializer for a peripheral power support. |
---|
216 | * |
---|
217 | * @param f The first peripheral index. |
---|
218 | * @param l The last peripheral index. |
---|
219 | */ |
---|
220 | #define ATSAM_POWER_PERIPHERAL(f, l) \ |
---|
221 | { \ |
---|
222 | .handler = atsam_power_handler_peripheral, \ |
---|
223 | .data = { .peripherals = { .first = f, .last = l } } \ |
---|
224 | } |
---|
225 | |
---|
226 | #define ATSAM_POWER_HANDLER(h, a) \ |
---|
227 | { \ |
---|
228 | .handler = h, \ |
---|
229 | .data = { .arg = a } \ |
---|
230 | } |
---|
231 | |
---|
232 | #define ATSAM_POWER_CLOCK_DRIVER \ |
---|
233 | { .handler = atsam_power_handler_clock_driver } |
---|
234 | |
---|
235 | #define ATSAM_POWER_SLEEP_MODE \ |
---|
236 | { .handler = atsam_power_handler_sleep_mode } |
---|
237 | |
---|
238 | #define ATSAM_POWER_WAIT_MODE \ |
---|
239 | { .handler = atsam_power_handler_wait_mode } |
---|
240 | |
---|
241 | /** |
---|
242 | * @brief Data for RTC driver power support. |
---|
243 | * |
---|
244 | * @see ATSAM_POWER_RTC_DRIVER(). |
---|
245 | */ |
---|
246 | typedef struct { |
---|
247 | /** |
---|
248 | * @brief Interval in seconds for which the power off mode should be active. |
---|
249 | * |
---|
250 | * An interval up to 24h is supported. |
---|
251 | */ |
---|
252 | uint32_t interval; |
---|
253 | } atsam_power_data_rtc_driver; |
---|
254 | |
---|
255 | /** |
---|
256 | * @brief Initializer for RTC driver power support. |
---|
257 | * |
---|
258 | * @param a Pointer to RTC driver power data. |
---|
259 | * |
---|
260 | * @see atsam_power_data_rtc_driver. |
---|
261 | */ |
---|
262 | #define ATSAM_POWER_RTC_DRIVER(a) \ |
---|
263 | { \ |
---|
264 | .handler = atsam_power_handler_rtc_driver, \ |
---|
265 | .data = { .arg = a } \ |
---|
266 | } |
---|
267 | |
---|
268 | #ifdef __cplusplus |
---|
269 | } |
---|
270 | #endif /* __cplusplus */ |
---|
271 | |
---|
272 | #endif /* LIBBSP_ARM_ATSAM_POWER_H */ |
---|