1 | /* |
---|
2 | * Copyright (c) 2016 embedded brains GmbH. All rights reserved. |
---|
3 | * |
---|
4 | * The license and distribution terms for this file may be |
---|
5 | * found in the file LICENSE in this distribution or at |
---|
6 | * http://www.rtems.org/license/LICENSE. |
---|
7 | */ |
---|
8 | |
---|
9 | #ifndef LIBBSP_ARM_ATSAM_POWER_H |
---|
10 | #define LIBBSP_ARM_ATSAM_POWER_H |
---|
11 | |
---|
12 | #include <sys/types.h> |
---|
13 | |
---|
14 | #include <stdint.h> |
---|
15 | |
---|
16 | #ifdef __cplusplus |
---|
17 | extern "C"{ |
---|
18 | #endif /* __cplusplus */ |
---|
19 | |
---|
20 | /** |
---|
21 | * @brief Status of the Low Power Support |
---|
22 | */ |
---|
23 | typedef enum { |
---|
24 | /** |
---|
25 | * @brief Used for Initialization of Handlers |
---|
26 | */ |
---|
27 | ATSAM_POWER_INIT, |
---|
28 | /** |
---|
29 | * @brief Used for Switching On of Handlers |
---|
30 | */ |
---|
31 | ATSAM_POWER_ON, |
---|
32 | /** |
---|
33 | * @brief Used for Switching Off of Handlers |
---|
34 | */ |
---|
35 | ATSAM_POWER_OFF |
---|
36 | } atsam_power_state; |
---|
37 | |
---|
38 | /** |
---|
39 | * @brief Control structure for power control handling |
---|
40 | */ |
---|
41 | typedef struct atsam_power_control { |
---|
42 | /** |
---|
43 | * @brief Data pointer to the handler with its desired state |
---|
44 | */ |
---|
45 | void (*handler)( |
---|
46 | const struct atsam_power_control *control, |
---|
47 | atsam_power_state state |
---|
48 | ); |
---|
49 | /** |
---|
50 | * @brief Data chunk that is used by the handler |
---|
51 | */ |
---|
52 | union { |
---|
53 | void *arg; |
---|
54 | struct { |
---|
55 | uint8_t first; |
---|
56 | uint8_t last; |
---|
57 | } peripherals; |
---|
58 | } data; |
---|
59 | } atsam_power_control; |
---|
60 | |
---|
61 | /** |
---|
62 | * @brief Performs a power state change according to the state parameter. |
---|
63 | * |
---|
64 | * The handlers of the control table are invoked in forward order (invocation |
---|
65 | * starts with table index zero) for the ATSAM_POWER_INIT and ATSAM_POWER_OFF |
---|
66 | * states, otherwise the handlers are invoked in reverse order (invocation |
---|
67 | * starts with the last table index). |
---|
68 | * |
---|
69 | * @param controls Table with power controls. |
---|
70 | * @param n Count of power control table entries. |
---|
71 | * @param state The desired power state. |
---|
72 | * |
---|
73 | * @code |
---|
74 | * #include <rtems.h> |
---|
75 | * #include <pthread.h> |
---|
76 | * |
---|
77 | * #include <bsp/power.h> |
---|
78 | * |
---|
79 | * static atsam_power_data_rtc_driver rtc_data = { .interval = 5 }; |
---|
80 | * |
---|
81 | * static const atsam_power_control power_controls[] = { |
---|
82 | * ATSAM_POWER_CLOCK_DRIVER, |
---|
83 | * ATSAM_POWER_RTC_DRIVER(&rtc_data), |
---|
84 | * ATSAM_POWER_SLEEP_MODE |
---|
85 | * }; |
---|
86 | * |
---|
87 | * static pthread_once_t once = PTHREAD_ONCE_INIT; |
---|
88 | * |
---|
89 | * static void init(void) |
---|
90 | * { |
---|
91 | * atsam_power_change_state( |
---|
92 | * &power_controls[0], |
---|
93 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
94 | * ATSAM_POWER_INIT |
---|
95 | * ); |
---|
96 | * } |
---|
97 | * |
---|
98 | * void power_init(void) |
---|
99 | * { |
---|
100 | * pthread_once(&once, init); |
---|
101 | * } |
---|
102 | * |
---|
103 | * void low_power(void) |
---|
104 | * { |
---|
105 | * atsam_power_change_state( |
---|
106 | * &power_controls[0], |
---|
107 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
108 | * ATSAM_POWER_OFF |
---|
109 | * ); |
---|
110 | * atsam_power_change_state( |
---|
111 | * &power_controls[0], |
---|
112 | * RTEMS_ARRAY_SIZE(power_controls), |
---|
113 | * ATSAM_POWER_ON |
---|
114 | * ); |
---|
115 | * } |
---|
116 | * @end |
---|
117 | */ |
---|
118 | void atsam_power_change_state( |
---|
119 | const atsam_power_control *controls, |
---|
120 | size_t n, |
---|
121 | atsam_power_state state |
---|
122 | ); |
---|
123 | |
---|
124 | /** |
---|
125 | * @brief Power handler for a set of peripherals according to the specified |
---|
126 | * peripheral indices. |
---|
127 | * |
---|
128 | * For the power off state, the peripherals are enabled in the PMC. |
---|
129 | * |
---|
130 | * For the power on state, the peripherals are disabled in the Power Management |
---|
131 | * Controller (PMC). |
---|
132 | * |
---|
133 | * @see ATSAM_POWER_PERIPHERAL(). |
---|
134 | */ |
---|
135 | void atsam_power_handler_peripheral( |
---|
136 | const atsam_power_control *controls, |
---|
137 | atsam_power_state state |
---|
138 | ); |
---|
139 | |
---|
140 | /** |
---|
141 | * @brief Power handler for the clock driver. |
---|
142 | * |
---|
143 | * For the power off state, the system tick is disabled. |
---|
144 | * |
---|
145 | * For the power on state, the system tick is enabled. In case no clock driver |
---|
146 | * is used by the application, then this may lead to a spurious interrupt |
---|
147 | * resulting in a fatal error. |
---|
148 | * |
---|
149 | * @see ATSAM_POWER_CLOCK_DRIVER(). |
---|
150 | */ |
---|
151 | void atsam_power_handler_clock_driver( |
---|
152 | const atsam_power_control *controls, |
---|
153 | atsam_power_state state |
---|
154 | ); |
---|
155 | |
---|
156 | /** |
---|
157 | * @brief Power handler for the RTC driver. |
---|
158 | * |
---|
159 | * This handler installs an interrupt handler during power support initialization. |
---|
160 | * |
---|
161 | * For the power off state, the RTC alarm interrupt is set up according to the |
---|
162 | * interval of the corresponding handler data. |
---|
163 | * |
---|
164 | * For the power on state, the RTC alarm interrupt is disabled. |
---|
165 | * |
---|
166 | * @see ATSAM_POWER_RTC_DRIVER(). |
---|
167 | */ |
---|
168 | void atsam_power_handler_rtc_driver( |
---|
169 | const atsam_power_control *controls, |
---|
170 | atsam_power_state state |
---|
171 | ); |
---|
172 | |
---|
173 | /** |
---|
174 | * @brief Power handler to enter the processor sleep mode. |
---|
175 | * |
---|
176 | * @see ATSAM_POWER_SLEEP_MODE(). |
---|
177 | */ |
---|
178 | void atsam_power_handler_sleep_mode( |
---|
179 | const atsam_power_control *controls, |
---|
180 | atsam_power_state state |
---|
181 | ); |
---|
182 | |
---|
183 | /** |
---|
184 | * @brief Power handler to enter the processor wait mode. |
---|
185 | * |
---|
186 | * The internal flash is put into deep sleep mode. |
---|
187 | * |
---|
188 | * @see ATSAM_POWER_WAIT_MODE(). |
---|
189 | */ |
---|
190 | void atsam_power_handler_wait_mode( |
---|
191 | const atsam_power_control *controls, |
---|
192 | atsam_power_state state |
---|
193 | ); |
---|
194 | |
---|
195 | /** |
---|
196 | * @brief Initializer for a peripheral power support. |
---|
197 | * |
---|
198 | * @param f The first peripheral index. |
---|
199 | * @param l The last peripheral index. |
---|
200 | */ |
---|
201 | #define ATSAM_POWER_PERIPHERAL(f, l) \ |
---|
202 | { \ |
---|
203 | .handler = atsam_power_handler_peripheral, \ |
---|
204 | .data = { .peripherals = { .first = f, .last = l } } \ |
---|
205 | } |
---|
206 | |
---|
207 | #define ATSAM_POWER_HANDLER(h, a) \ |
---|
208 | { \ |
---|
209 | .handler = h, \ |
---|
210 | .data = { .arg = a } \ |
---|
211 | } |
---|
212 | |
---|
213 | #define ATSAM_POWER_CLOCK_DRIVER \ |
---|
214 | { .handler = atsam_power_handler_clock_driver } |
---|
215 | |
---|
216 | #define ATSAM_POWER_SLEEP_MODE \ |
---|
217 | { .handler = atsam_power_handler_sleep_mode } |
---|
218 | |
---|
219 | #define ATSAM_POWER_WAIT_MODE \ |
---|
220 | { .handler = atsam_power_handler_wait_mode } |
---|
221 | |
---|
222 | /** |
---|
223 | * @brief Data for RTC driver power support. |
---|
224 | * |
---|
225 | * @see ATSAM_POWER_RTC_DRIVER(). |
---|
226 | */ |
---|
227 | typedef struct { |
---|
228 | /** |
---|
229 | * @brief Interval in seconds for which the power off mode should be active. |
---|
230 | * |
---|
231 | * An interval up to 24h is supported. |
---|
232 | */ |
---|
233 | uint32_t interval; |
---|
234 | } atsam_power_data_rtc_driver; |
---|
235 | |
---|
236 | /** |
---|
237 | * @brief Initializer for RTC driver power support. |
---|
238 | * |
---|
239 | * @param a Pointer to RTC driver power data. |
---|
240 | * |
---|
241 | * @see atsam_power_data_rtc_driver. |
---|
242 | */ |
---|
243 | #define ATSAM_POWER_RTC_DRIVER(a) \ |
---|
244 | { \ |
---|
245 | .handler = atsam_power_handler_rtc_driver, \ |
---|
246 | .data = { .arg = a } \ |
---|
247 | } |
---|
248 | |
---|
249 | #ifdef __cplusplus |
---|
250 | } |
---|
251 | #endif /* __cplusplus */ |
---|
252 | |
---|
253 | #endif /* LIBBSP_ARM_ATSAM_POWER_H */ |
---|