AVR-LibC  2.3.0
Standard C library for AVR-GCC
 

AVR-LibC Manual

AVR-LibC Sources

Main Page

User Manual

Lib­rary Refe­rence

FAQ

Exam­ple Pro­jects

Index

Loading...
Searching...
No Matches
power.h
Go to the documentation of this file.
1/* Copyright (c) 2006, 2007, 2008 Eric B. Weddington
2 Copyright (c) 2011 Frédéric Nadeau
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 * Redistributions of source code must retain the above copyright
9 notice, this list of conditions and the following disclaimer.
10 * Redistributions in binary form must reproduce the above copyright
11 notice, this list of conditions and the following disclaimer in
12 the documentation and/or other materials provided with the
13 distribution.
14 * Neither the name of the copyright holders nor the names of
15 contributors may be used to endorse or promote products derived
16 from this software without specific prior written permission.
17
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 POSSIBILITY OF SUCH DAMAGE. */
29
30#ifndef _AVR_POWER_H_
31#define _AVR_POWER_H_ 1
32
33#include <avr/io.h>
34#include <stdint.h>
35#include <bits/attribs.h>
36
37/** \file */
38/** \defgroup avr_power <avr/power.h>: Power Reduction Management
39
40\code #include <avr/power.h>\endcode
41
42Many AVRs contain a Power Reduction Register (PRR) or Registers (PRRx) that
43allow you to reduce power consumption by disabling or enabling various on-board
44peripherals as needed. Some devices have the XTAL Divide Control Register
45(XDIV) which offer similar functionality as System Clock Prescale
46Register (CLKPR).
47
48There are many macros in this header file that provide an easy interface
49to enable or disable on-board peripherals to reduce power. See the table below.
50
51\note Not all AVR devices have a Power Reduction Register (for example
52the ATmega8). On those devices without a Power Reduction Register, the
53power reduction macros are not available..
54
55\note Not all AVR devices contain the same peripherals (for example, the LCD
56interface), or they will be named differently (for example, USART and
57USART0). Please consult your device's datasheet, or the header file, to
58find out which macros are applicable to your device.
59
60\note For device using the XTAL Divide Control Register (XDIV), when prescaler
61is used, Timer/Counter0 can only be used in asynchronous mode. Keep in mind
62that Timer/Counter0 source shall be less than ¼th of peripheral clock.
63Therefore, when using a typical 32.768 kHz crystal, one shall not scale
64the clock below 131.072 kHz.
65
66\anchor avr_powermacros
67<small>
68<table>
69 <caption>Power Macros</caption>
70 <tr>
71 <th>Power Macro
72 <th>Description
73 </tr>
74 <tr>
75 <td>\c power_aca_disable()</td>
76 <td>Disable the Analog Comparator on PortA</td>
77 </tr>
78 <tr>
79 <td>\c power_aca_enable()</td>
80 <td>Enable the Analog Comparator on PortA</td>
81 </tr>
82 <tr>
83 <td>\c power_adc_enable()</td>
84 <td>Enable the Analog to Digital Converter module</td>
85 </tr>
86 <tr>
87 <td>\c power_adc_disable()</td>
88 <td>Disable the Analog to Digital Converter module</td>
89 </tr>
90 <tr>
91 <td>\c power_adca_disable()</td>
92 <td>Disable the Analog to Digital Converter module on PortA</td>
93 </tr>
94 <tr>
95 <td>\c power_adca_enable()</td>
96 <td>Enable the Analog to Digital Converter module on PortA</td>
97 </tr>
98 <tr>
99 <td>\c power_evsys_disable()</td>
100 <td>Disable the EVSYS module</td>
101 </tr>
102 <tr>
103 <td>\c power_evsys_enable()</td>
104 <td>Enable the EVSYS module</td>
105 </tr>
106 <tr>
107 <td>\c power_hiresc_disable()</td>
108 <td>Disable the HIRES module on PortC</td>
109 </tr>
110 <tr>
111 <td>\c power_hiresc_enable()</td>
112 <td>Enable the HIRES module on PortC</td>
113 </tr>
114 <tr>
115 <td>\c power_lcd_enable()</td>
116 <td>Enable the LCD module</td>
117 </tr>
118 <tr>
119 <td>\c power_lcd_disable()</td>
120 <td>Disable the LCD module</td>
121 </tr>
122 <tr>
123 <td>\c power_pga_enable()</td>
124 <td>Enable the Programmable Gain Amplifier module</td>
125 </tr>
126 <tr>
127 <td>\c power_pga_disable()</td>
128 <td>Disable the Programmable Gain Amplifier module</td>
129 </tr>
130 <tr>
131 <td>\c power_pscr_enable()</td>
132 <td>Enable the Reduced Power Stage Controller module</td>
133 </tr>
134 <tr>
135 <td>\c power_pscr_disable()</td>
136 <td>Disable the Reduced Power Stage Controller module</td>
137 </tr>
138 <tr>
139 <td>\c power_psc0_enable()</td>
140 <td>Enable the Power Stage Controller 0 module</td>
141 </tr>
142 <tr>
143 <td>\c power_psc0_disable()</td>
144 <td>Disable the Power Stage Controller 0 module</td>
145 </tr>
146 <tr>
147 <td>\c power_psc1_enable()</td>
148 <td>Enable the Power Stage Controller 1 module</td>
149 </tr>
150 <tr>
151 <td>\c power_psc1_disable()</td>
152 <td>Disable the Power Stage Controller 1 module</td>
153 </tr>
154 <tr>
155 <td>\c power_psc2_enable()</td>
156 <td>Enable the Power Stage Controller 2 module</td>
157 </tr>
158 <tr>
159 <td>\c power_psc2_disable()</td>
160 <td>Disable the Power Stage Controller 2 module</td>
161 </tr>
162 <tr>
163 <td>\c power_ram0_enable()</td>
164 <td>Enable the SRAM block 0</td>
165 </tr>
166 <tr>
167 <td>\c power_ram0_disable()</td>
168 <td>Disable the SRAM block 0</td>
169 </tr>
170 <tr>
171 <td>\c power_ram1_enable()</td>
172 <td>Enable the SRAM block 1</td>
173 </tr>
174 <tr>
175 <td>\c power_ram1_disable()</td>
176 <td>Disable the SRAM block 1</td>
177 </tr>
178 <tr>
179 <td>\c power_ram2_enable()</td>
180 <td>Enable the SRAM block 2</td>
181 </tr>
182 <tr>
183 <td>\c power_ram2_disable()</td>
184 <td>Disable the SRAM block 2</td>
185 </tr>
186 <tr>
187 <td>\c power_ram3_enable()</td>
188 <td>Enable the SRAM block 3</td>
189 </tr>
190 <tr>
191 <td>\c power_ram3_disable()</td>
192 <td>Disable the SRAM block 3</td>
193 </tr>
194 <tr>
195 <td>\c power_rtc_disable()</td>
196 <td>Disable the RTC module</td>
197 </tr>
198 <tr>
199 <td>\c power_rtc_enable()</td>
200 <td>Enable the RTC module</td>
201 </tr>
202 <tr>
203 <td>\c power_spi_enable()</td>
204 <td>Enable the Serial Peripheral Interface module</td>
205 </tr>
206 <tr>
207 <td>\c power_spi_disable()</td>
208 <td>Disable the Serial Peripheral Interface module</td>
209 </tr>
210 <tr>
211 <td>\c power_spic_disable()</td>
212 <td>Disable the SPI module on PortC</td>
213 </tr>
214 <tr>
215 <td>\c power_spic_enable()</td>
216 <td>Enable the SPI module on PortC</td>
217 </tr>
218 <tr>
219 <td>\c power_spid_disable()</td>
220 <td>Disable the SPI module on PortD</td>
221 </tr>
222 <tr>
223 <td>\c power_spid_enable()</td>
224 <td>Enable the SPI module on PortD</td>
225 </tr>
226 <tr>
227 <td>\c power_tc0c_disable()</td>
228 <td>Disable the TC0 module on PortC</td>
229 </tr>
230 <tr>
231 <td>\c power_tc0c_enable()</td>
232 <td>Enable the TC0 module on PortC</td>
233 </tr>
234 <tr>
235 <td>\c power_tc0d_disable()</td>
236 <td>Disable the TC0 module on PortD</td>
237 </tr>
238 <tr>
239 <td>\c power_tc0d_enable()</td>
240 <td>Enable the TC0 module on PortD</td>
241 </tr>
242 <tr>
243 <td>\c power_tc0e_disable()</td>
244 <td>Disable the TC0 module on PortE</td>
245 </tr>
246 <tr>
247 <td>\c power_tc0e_enable()</td>
248 <td>Enable the TC0 module on PortE</td>
249 </tr>
250 <tr>
251 <td>\c power_tc0f_disable()</td>
252 <td>Disable the TC0 module on PortF</td>
253 </tr>
254 <tr>
255 <td>\c power_tc0f_enable()</td>
256 <td>Enable the TC0 module on PortF</td>
257 </tr>
258 <tr>
259 <td>\c power_tc1c_disable()</td>
260 <td>Disable the TC1 module on PortC</td>
261 </tr>
262 <tr>
263 <td>\c power_tc1c_enable()</td>
264 <td>Enable the TC1 module on PortC</td>
265 </tr>
266 <tr>
267 <td>\c power_twic_disable()</td>
268 <td>Disable the Two Wire Interface module on PortC</td>
269 </tr>
270 <tr>
271 <td>\c power_twic_enable()</td>
272 <td>Enable the Two Wire Interface module on PortC</td>
273 </tr>
274 <tr>
275 <td>\c power_twie_disable()</td>
276 <td>Disable the Two Wire Interface module on PortE</td>
277 </tr>
278 <tr>
279 <td>\c power_twie_enable()</td>
280 <td>Enable the Two Wire Interface module on PortE</td>
281 </tr>
282 <tr>
283 <td>\c power_timer0_enable()</td>
284 <td>Enable the Timer 0 module</td>
285 </tr>
286 <tr>
287 <td>\c power_timer0_disable()</td>
288 <td>Disable the Timer 0 module</td>
289 </tr>
290 <tr>
291 <td>\c power_timer1_enable()</td>
292 <td>Enable the Timer 1 module</td>
293 </tr>
294 <tr>
295 <td>\c power_timer1_disable()</td>
296 <td>Disable the Timer 1 module</td>
297 </tr>
298 <tr>
299 <td>\c power_timer2_enable()</td>
300 <td>Enable the Timer 2 module</td>
301 </tr>
302 <tr>
303 <td>\c power_timer2_disable()</td>
304 <td>Disable the Timer 2 module</td>
305 </tr>
306 <tr>
307 <td>\c power_timer3_enable()</td>
308 <td>Enable the Timer 3 module</td>
309 </tr>
310 <tr>
311 <td>\c power_timer3_disable()</td>
312 <td>Disable the Timer 3 module</td>
313 </tr>
314 <tr>
315 <td>\c power_timer4_enable()</td>
316 <td>Enable the Timer 4 module</td>
317 </tr>
318 <tr>
319 <td>\c power_timer4_disable()</td>
320 <td>Disable the Timer 4 module</td>
321 </tr>
322 <tr>
323 <td>\c power_timer5_enable()</td>
324 <td>Enable the Timer 5 module</td>
325 </tr>
326 <tr>
327 <td>\c power_timer5_disable()</td>
328 <td>Disable the Timer 5 module</td>
329 </tr>
330 <tr>
331 <td>\c power_twi_enable()</td>
332 <td>Enable the Two Wire Interface module</td>
333 </tr>
334 <tr>
335 <td>\c power_twi_disable()</td>
336 <td>Disable the Two Wire Interface module</td>
337 </tr>
338 <tr>
339 <td>\c power_usart_enable()</td>
340 <td>Enable the USART module</td>
341 </tr>
342 <tr>
343 <td>\c power_usart_disable()</td>
344 <td>Disable the USART module</td>
345 </tr>
346 <tr>
347 <td>\c power_usart0_enable()</td>
348 <td>Enable the USART 0 module</td>
349 </tr>
350 <tr>
351 <td>\c power_usart0_disable()</td>
352 <td>Disable the USART 0 module</td>
353 </tr>
354 <tr>
355 <td>\c power_usart1_enable()</td>
356 <td>Enable the USART 1 module</td>
357 </tr>
358 <tr>
359 <td>\c power_usart1_disable()</td>
360 <td>Disable the USART 1 module</td>
361 </tr>
362 <tr>
363 <td>\c power_usart2_enable()</td>
364 <td>Enable the USART 2 module</td>
365 </tr>
366 <tr>
367 <td>\c power_usart2_disable()</td>
368 <td>Disable the USART 2 module</td>
369 </tr>
370 <tr>
371 <td>\c power_usart3_enable()</td>
372 <td>Enable the USART 3 module</td>
373 </tr>
374 <tr>
375 <td>\c power_usart3_disable()</td>
376 <td>Disable the USART 3 module</td>
377 </tr>
378 <tr>
379 <td>\c power_usartc0_disable()</td>
380 <td> Disable the USART0 module on PortC</td>
381 </tr>
382 <tr>
383 <td>\c power_usartc0_enable()</td>
384 <td> Enable the USART0 module on PortC</td>
385 </tr>
386 <tr>
387 <td>\c power_usartd0_disable()</td>
388 <td> Disable the USART0 module on PortD</td>
389 </tr>
390 <tr>
391 <td>\c power_usartd0_enable()</td>
392 <td> Enable the USART0 module on PortD</td>
393 </tr>
394 <tr>
395 <td>\c power_usarte0_disable()</td>
396 <td> Disable the USART0 module on PortE</td>
397 </tr>
398 <tr>
399 <td>\c power_usarte0_enable()</td>
400 <td> Enable the USART0 module on PortE</td>
401 </tr>
402 <tr>
403 <td>\c power_usartf0_disable()</td>
404 <td> Disable the USART0 module on PortF</td>
405 </tr>
406 <tr>
407 <td>\c power_usartf0_enable()</td>
408 <td> Enable the USART0 module on PortF</td>
409 </tr>
410 <tr>
411 <td>\c power_usb_enable()</td>
412 <td>Enable the USB module</td>
413 </tr>
414 <tr>
415 <td>\c power_usb_disable()</td>
416 <td>Disable the USB module</td>
417 </tr>
418 <tr>
419 <td>\c power_usi_enable()</td>
420 <td>Enable the Universal Serial Interface module</td>
421 </tr>
422 <tr>
423 <td>\c power_usi_disable()</td>
424 <td>Disable the Universal Serial Interface module</td>
425 </tr>
426 <tr>
427 <td>\c power_vadc_enable()</td>
428 <td>Enable the Voltage ADC module</td>
429 </tr>
430 <tr>
431 <td>\c power_vadc_disable()</td>
432 <td>Disable the Voltage ADC module</td>
433 </tr>
434 <tr>
435 <td>\c power_all_enable()</td>
436 <td>Enable all modules</td>
437 </tr>
438 <tr>
439 <td>\c power_all_disable()</td>
440 <td>Disable all modules</td>
441 </tr>
442</table>
443</small>
444*/
445
446#if defined(__AVR_HAVE_PRR_PRADC)
447#define power_adc_enable() (PRR &= (uint8_t)~(1 << PRADC))
448#define power_adc_disable() (PRR |= (uint8_t)(1 << PRADC))
449#endif
450
451#if defined(__AVR_HAVE_PRR_PRCAN)
452#define power_can_enable() (PRR &= (uint8_t)~(1 << PRCAN))
453#define power_can_disable() (PRR |= (uint8_t)(1 << PRCAN))
454#endif
455
456#if defined(__AVR_HAVE_PRR_PRLCD)
457#define power_lcd_enable() (PRR &= (uint8_t)~(1 << PRLCD))
458#define power_lcd_disable() (PRR |= (uint8_t)(1 << PRLCD))
459#endif
460
461#if defined(__AVR_HAVE_PRR_PRLIN)
462#define power_lin_enable() (PRR &= (uint8_t)~(1 << PRLIN))
463#define power_lin_disable() (PRR |= (uint8_t)(1 << PRLIN))
464#endif
465
466#if defined(__AVR_HAVE_PRR_PRPSC)
467#define power_psc_enable() (PRR &= (uint8_t)~(1 << PRPSC))
468#define power_psc_disable() (PRR |= (uint8_t)(1 << PRPSC))
469#endif
470
471#if defined(__AVR_HAVE_PRR_PRPSC0)
472#define power_psc0_enable() (PRR &= (uint8_t)~(1 << PRPSC0))
473#define power_psc0_disable() (PRR |= (uint8_t)(1 << PRPSC0))
474#endif
475
476#if defined(__AVR_HAVE_PRR_PRPSC1)
477#define power_psc1_enable() (PRR &= (uint8_t)~(1 << PRPSC1))
478#define power_psc1_disable() (PRR |= (uint8_t)(1 << PRPSC1))
479#endif
480
481#if defined(__AVR_HAVE_PRR_PRPSC2)
482#define power_psc2_enable() (PRR &= (uint8_t)~(1 << PRPSC2))
483#define power_psc2_disable() (PRR |= (uint8_t)(1 << PRPSC2))
484#endif
485
486#if defined(__AVR_HAVE_PRR_PRPSCR)
487#define power_pscr_enable() (PRR &= (uint8_t)~(1 << PRPSCR))
488#define power_pscr_disable() (PRR |= (uint8_t)(1 << PRPSCR))
489#endif
490
491#if defined(__AVR_HAVE_PRR_PRSPI)
492#define power_spi_enable() (PRR &= (uint8_t)~(1 << PRSPI))
493#define power_spi_disable() (PRR |= (uint8_t)(1 << PRSPI))
494#endif
495
496#if defined(__AVR_HAVE_PRR_PRTIM0)
497#define power_timer0_enable() (PRR &= (uint8_t)~(1 << PRTIM0))
498#define power_timer0_disable() (PRR |= (uint8_t)(1 << PRTIM0))
499#endif
500
501#if defined(__AVR_HAVE_PRR_PRTIM1)
502#define power_timer1_enable() (PRR &= (uint8_t)~(1 << PRTIM1))
503#define power_timer1_disable() (PRR |= (uint8_t)(1 << PRTIM1))
504#endif
505
506#if defined(__AVR_HAVE_PRR_PRTIM2)
507#define power_timer2_enable() (PRR &= (uint8_t)~(1 << PRTIM2))
508#define power_timer2_disable() (PRR |= (uint8_t)(1 << PRTIM2))
509#endif
510
511#if defined(__AVR_HAVE_PRR_PRTWI)
512#define power_twi_enable() (PRR &= (uint8_t)~(1 << PRTWI))
513#define power_twi_disable() (PRR |= (uint8_t)(1 << PRTWI))
514#endif
515
516#if defined(__AVR_HAVE_PRR_PRUSART)
517#define power_usart_enable() (PRR &= (uint8_t)~(1 << PRUSART))
518#define power_usart_disable() (PRR |= (uint8_t)(1 << PRUSART))
519#endif
520
521#if defined(__AVR_HAVE_PRR_PRUSART0)
522#define power_usart0_enable() (PRR &= (uint8_t)~(1 << PRUSART0))
523#define power_usart0_disable() (PRR |= (uint8_t)(1 << PRUSART0))
524#endif
525
526#if defined(__AVR_HAVE_PRR_PRUSART1)
527#define power_usart1_enable() (PRR &= (uint8_t)~(1 << PRUSART1))
528#define power_usart1_disable() (PRR |= (uint8_t)(1 << PRUSART1))
529#endif
530
531#if defined(__AVR_HAVE_PRR_PRUSI)
532#define power_usi_enable() (PRR &= (uint8_t)~(1 << PRUSI))
533#define power_usi_disable() (PRR |= (uint8_t)(1 << PRUSI))
534#endif
535
536#if defined(__AVR_HAVE_PRR0_PRADC)
537#define power_adc_enable() (PRR0 &= (uint8_t)~(1 << PRADC))
538#define power_adc_disable() (PRR0 |= (uint8_t)(1 << PRADC))
539#endif
540
541#if defined(__AVR_HAVE_PRR0_PRC0)
542#define power_clock_output_enable() (PRR0 &= (uint8_t)~(1 << PRCO))
543#define power_clock_output_disable() (PRR0 |= (uint8_t)(1 << PRCO))
544#endif
545
546#if defined(__AVR_HAVE_PRR0_PRCRC)
547#define power_crc_enable() (PRR0 &= (uint8_t)~(1 << PRCRC))
548#define power_crc_disable() (PRR0 |= (uint8_t)(1 << PRCRC))
549#endif
550
551#if defined(__AVR_HAVE_PRR0_PRCU)
552#define power_crypto_enable() (PRR0 &= (uint8_t)~(1 << PRCU))
553#define power_crypto_disable() (PRR0 |= (uint8_t)(1 << PRCU))
554#endif
555
556#if defined(__AVR_HAVE_PRR0_PRDS)
557#define power_irdriver_enable() (PRR0 &= (uint8_t)~(1 << PRDS))
558#define power_irdriver_disable() (PRR0 |= (uint8_t)(1 << PRDS))
559#endif
560
561#if defined(__AVR_HAVE_PRR0_PRLFR)
562#define power_lfreceiver_enable() (PRR0 &= (uint8_t)~(1 << PRLFR))
563#define power_lfreceiver_disable() (PRR0 |= (uint8_t)(1 << PRLFR))
564#endif
565
566#if defined(__AVR_HAVE_PRR0_PRLFRS)
567#define power_lfrs_enable() (PRR0 &= (uint8_t)~(1 << PRLFRS))
568#define power_lfrs_disable() (PRR0 |= (uint8_t)(1 << PRLFRS))
569#endif
570
571#if defined(__AVR_HAVE_PRR0_PRLIN)
572#define power_lin_enable() (PRR0 &= (uint8_t)~(1 << PRLIN))
573#define power_lin_disable() (PRR0 |= (uint8_t)(1 << PRLIN))
574#endif
575
576#if defined(__AVR_HAVE_PRR0_PRPGA)
577#define power_pga_enable() (PRR0 &= (uint8_t)~(1 << PRPGA))
578#define power_pga_disable() (PRR0 |= (uint8_t)(1 << PRPGA))
579#endif
580
581#if defined(__AVR_HAVE_PRR0_PRRXDC)
582#define power_receive_dsp_control_enable() (PRR0 &= (uint8_t)~(1 << PRRXDC))
583#define power_receive_dsp_control_disable() (PRR0 |= (uint8_t)(1 << PRRXDC))
584#endif
585
586#if defined(__AVR_HAVE_PRR0_PRSPI)
587#define power_spi_enable() (PRR0 &= (uint8_t)~(1 << PRSPI))
588#define power_spi_disable() (PRR0 |= (uint8_t)(1 << PRSPI))
589#endif
590
591#if defined(__AVR_HAVE_PRR0_PRT0)
592#define power_timer0_enable() (PRR0 &= (uint8_t)~(1 << PRT0))
593#define power_timer0_disable() (PRR0 |= (uint8_t)(1 << PRT0))
594#endif
595
596#if defined(__AVR_HAVE_PRR0_PRTIM0)
597#define power_timer0_enable() (PRR0 &= (uint8_t)~(1 << PRTIM0))
598#define power_timer0_disable() (PRR0 |= (uint8_t)(1 << PRTIM0))
599#endif
600
601#if defined(__AVR_HAVE_PRR0_PRT1)
602#define power_timer1_enable() (PRR0 &= (uint8_t)~(1 << PRT1))
603#define power_timer1_disable() (PRR0 |= (uint8_t)(1 << PRT1))
604#endif
605
606#if defined(__AVR_HAVE_PRR0_PRTIM1)
607#define power_timer1_enable() (PRR0 &= (uint8_t)~(1 << PRTIM1))
608#define power_timer1_disable() (PRR0 |= (uint8_t)(1 << PRTIM1))
609#endif
610
611#if defined(__AVR_HAVE_PRR0_PRT2)
612#define power_timer2_enable() (PRR0 &= (uint8_t)~(1 << PRT2))
613#define power_timer2_disable() (PRR0 |= (uint8_t)(1 << PRT2))
614#endif
615
616#if defined(__AVR_HAVE_PRR0_PRTIM2)
617#define power_timer2_enable() (PRR0 &= (uint8_t)~(1 << PRTIM2))
618#define power_timer2_disable() (PRR0 |= (uint8_t)(1 << PRTIM2))
619#endif
620
621#if defined(__AVR_HAVE_PRR0_PRT3)
622#define power_timer3_enable() (PRR0 &= (uint8_t)~(1 << PRT3))
623#define power_timer3_disable() (PRR0 |= (uint8_t)(1 << PRT3))
624#endif
625
626#if defined(__AVR_HAVE_PRR0_PRTM)
627#define power_timermodulator_enable() (PRR0 &= (uint8_t)~(1 << PRTM))
628#define power_timermodulator_disable() (PRR0 |= (uint8_t)(1 << PRTM))
629#endif
630
631#if defined(__AVR_HAVE_PRR0_PRTWI)
632#define power_twi_enable() (PRR0 &= (uint8_t)~(1 << PRTWI))
633#define power_twi_disable() (PRR0 |= (uint8_t)(1 << PRTWI))
634#endif
635
636#if defined(__AVR_HAVE_PRR0_PRTWI0)
637#define power_twi0_enable() (PRR0 &= (uint8_t)~(1 << PRTWI0))
638#define power_twi0_disable() (PRR0 |= (uint8_t)(1 << PRTWI0))
639#if !defined(__AVR_HAVE_PRR0_PRTWI)
640#define power_twi_enable() power_twi0_enable()
641#define power_twi_disable() power_twi0_disable()
642#endif
643#endif
644
645#if defined(__AVR_HAVE_PRR0_PRTWI1)
646#define power_twi1_enable() (PRR0 &= (uint8_t)~(1 << PRTWI1))
647#define power_twi1_disable() (PRR0 |= (uint8_t)(1 << PRTWI1))
648#endif
649
650#if defined(__AVR_HAVE_PRR0_PRTXDC)
651#define power_transmit_dsp_control_enable() (PRR0 &= (uint8_t)~(1 << PRTXDC))
652#define power_transmit_dsp_control_disable() (PRR0 |= (uint8_t)(1 << PRTXDC))
653#endif
654
655#if defined(__AVR_HAVE_PRR0_PRUSART0)
656#define power_usart0_enable() (PRR0 &= (uint8_t)~(1 << PRUSART0))
657#define power_usart0_disable() (PRR0 |= (uint8_t)(1 << PRUSART0))
658#endif
659
660#if defined(__AVR_HAVE_PRR0_PRUSART1)
661#define power_usart1_enable() (PRR0 &= (uint8_t)~(1 << PRUSART1))
662#define power_usart1_disable() (PRR0 |= (uint8_t)(1 << PRUSART1))
663#endif
664
665#if defined(__AVR_HAVE_PRR0_PRVADC)
666#define power_vadc_enable() (PRR0 &= (uint8_t)~(1 << PRVADC))
667#define power_vadc_disable() (PRR0 |= (uint8_t)(1 << PRVADC))
668#endif
669
670#if defined(__AVR_HAVE_PRR0_PRVM)
671#define power_voltage_monitor_enable() (PRR0 &= (uint8_t)~(1 << PRVM))
672#define power_voltage_monitor_disable() (PRR0 |= (uint8_t)(1 << PRVM))
673#endif
674
675#if defined(__AVR_HAVE_PRR0_PRVRM)
676#define power_vrm_enable() (PRR0 &= (uint8_t)~(1 << PRVRM))
677#define power_vrm_disable() (PRR0 |= (uint8_t)(1 << PRVRM))
678#endif
679
680#if defined(__AVR_HAVE_PRR1_PRAES)
681#define power_aes_enable() (PRR1 &= (uint8_t)~(1 << PRAES))
682#define power_aes_disable() (PRR1 |= (uint8_t)(1 << PRAES))
683#endif
684
685#if defined(__AVR_HAVE_PRR1_PRCI)
686#define power_cinterface_enable() (PRR1 &= (uint8_t)~(1 << PRCI))
687#define power_cinterface_disable() (PRR1 |= (uint8_t)(1 << PRCI))
688#endif
689
690#if defined(__AVR_HAVE_PRR1_PRHSSPI)
691#define power_hsspi_enable() (PRR1 &= (uint8_t)~(1 << PRHSSPI))
692#define power_hsspi_disable() (PRR1 |= (uint8_t)(1 << PRHSSPI))
693#endif
694
695#if defined(__AVR_HAVE_PRR1_PRKB)
696#define power_kb_enable() (PRR1 &= (uint8_t)~(1 << PRKB))
697#define power_kb_disable() (PRR1 |= (uint8_t)(1 << PRKB))
698#endif
699
700#if defined(__AVR_HAVE_PRR1_PRLFPH)
701#define power_lfph_enable() (PRR1 &= (uint8_t)~(1 << PRLFPH))
702#define power_lfph_disable() (PRR1 |= (uint8_t)(1 << PRLFPH))
703#endif
704
705#if defined(__AVR_HAVE_PRR1_PRLFR)
706#define power_lfreceiver_enable() (PRR1 &= (uint8_t)~(1 << PRLFR))
707#define power_lfreceiver_disable() (PRR1 |= (uint8_t)(1 << PRLFR))
708#endif
709
710#if defined(__AVR_HAVE_PRR1_PRLFTP)
711#define power_lftp_enable() (PRR1 &= (uint8_t)~(1 << PRLFTP))
712#define power_lftp_disable() (PRR1 |= (uint8_t)(1 << PRLFTP))
713#endif
714
715#if defined(__AVR_HAVE_PRR1_PRSCI)
716#define power_sci_enable() (PRR1 &= (uint8_t)~(1 << PRSCI))
717#define power_sci_disable() (PRR1 |= (uint8_t)(1 << PRSCI))
718#endif
719
720#if defined(__AVR_HAVE_PRR1_PRSPI)
721#define power_spi_enable() (PRR1 &= (uint8_t)~(1 << PRSPI))
722#define power_spi_disable() (PRR1 |= (uint8_t)(1 << PRSPI))
723#endif
724
725#if defined(__AVR_HAVE_PRR1_PRT1)
726#define power_timer1_enable() (PRR1 &= (uint8_t)~(1 << PRT1))
727#define power_timer1_disable() (PRR1 |= (uint8_t)(1 << PRT1))
728#endif
729
730#if defined(__AVR_HAVE_PRR1_PRT2)
731#define power_timer2_enable() (PRR1 &= (uint8_t)~(1 << PRT2))
732#define power_timer2_disable() (PRR1 |= (uint8_t)(1 << PRT2))
733#endif
734
735#if defined(__AVR_HAVE_PRR1_PRT3)
736#define power_timer3_enable() (PRR1 &= (uint8_t)~(1 << PRT3))
737#define power_timer3_disable() (PRR1 |= (uint8_t)(1 << PRT3))
738#endif
739
740#if defined(__AVR_HAVE_PRR1_PRT4)
741#define power_timer4_enable() (PRR1 &= (uint8_t)~(1 << PRT4))
742#define power_timer4_disable() (PRR1 |= (uint8_t)(1 << PRT4))
743#endif
744
745#if defined(__AVR_HAVE_PRR1_PRT5)
746#define power_timer5_enable() (PRR1 &= (uint8_t)~(1 << PRT5))
747#define power_timer5_disable() (PRR1 |= (uint8_t)(1 << PRT5))
748#endif
749
750#if defined(__AVR_HAVE_PRR1_PRTIM3)
751#define power_timer3_enable() (PRR1 &= (uint8_t)~(1 << PRTIM3))
752#define power_timer3_disable() (PRR1 |= (uint8_t)(1 << PRTIM3))
753#endif
754
755#if defined(__AVR_HAVE_PRR1_PRTIM4)
756#define power_timer4_enable() (PRR1 &= (uint8_t)~(1 << PRTIM4))
757#define power_timer4_disable() (PRR1 |= (uint8_t)(1 << PRTIM4))
758#endif
759
760#if defined(__AVR_HAVE_PRR1_PRTIM5)
761#define power_timer5_enable() (PRR1 &= (uint8_t)~(1 << PRTIM5))
762#define power_timer5_disable() (PRR1 |= (uint8_t)(1 << PRTIM5))
763#endif
764
765#if defined(__AVR_HAVE_PRR1_PRTRX24)
766#define power_transceiver_enable() (PRR1 &= (uint8_t)~(1 << PRTRX24))
767#define power_transceiver_disable() (PRR1 |= (uint8_t)(1 << PRTRX24))
768#endif
769
770#if defined(__AVR_HAVE_PRR1_PRUSART1)
771#define power_usart1_enable() (PRR1 &= (uint8_t)~(1 << PRUSART1))
772#define power_usart1_disable() (PRR1 |= (uint8_t)(1 << PRUSART1))
773#endif
774
775#if defined(__AVR_HAVE_PRR1_PRUSART2)
776#define power_usart2_enable() (PRR1 &= (uint8_t)~(1 << PRUSART2))
777#define power_usart2_disable() (PRR1 |= (uint8_t)(1 << PRUSART2))
778#endif
779
780#if defined(__AVR_HAVE_PRR1_PRUSART3)
781#define power_usart3_enable() (PRR1 &= (uint8_t)~(1 << PRUSART3))
782#define power_usart3_disable() (PRR1 |= (uint8_t)(1 << PRUSART3))
783#endif
784
785#if defined(__AVR_HAVE_PRR1_PRUSB)
786#define power_usb_enable() (PRR1 &= (uint8_t)~(1 << PRUSB))
787#define power_usb_disable() (PRR1 |= (uint8_t)(1 << PRUSB))
788#endif
789
790#if defined(__AVR_HAVE_PRR1_PRUSBH)
791#define power_usbh_enable() (PRR1 &= (uint8_t)~(1 << PRUSBH))
792#define power_usbh_disable() (PRR1 |= (uint8_t)(1 << PRUSBH))
793#endif
794
795#if defined(__AVR_HAVE_PRR1_PRSPI1)
796#define power_spi1_enable() (PRR1 &= (uint8_t)~(1 << PRSPI1))
797#define power_spi1_disable() (PRR1 |= (uint8_t)(1 << PRSPI1))
798#endif
799
800#if defined(__AVR_HAVE_PRR1_PRPTC)
801#define power_ptc_enable() (PRR1 &= (uint8_t)~(1 << PRPTC))
802#define power_ptc_disable() (PRR1 |= (uint8_t)(1 << PRPTC))
803#endif
804
805#if defined(__AVR_HAVE_PRR1_PRTWI1)
806#define power_twi1_enable() (PRR1 &= (uint8_t)~(1 << PRTWI1))
807#define power_twi1_disable() (PRR1 |= (uint8_t)(1 << PRTWI1))
808#endif
809
810#if defined(__AVR_HAVE_PRR2_PRDF)
811#define power_data_fifo_enable() (PRR2 &= (uint8_t)~(1 << PRDF))
812#define power_data_fifo_disable() (PRR2 |= (uint8_t)(1 << PRDF))
813#endif
814
815#if defined(__AVR_HAVE_PRR2_PRIDS)
816#define power_id_scan_enable() (PRR2 &= (uint8_t)~(1 << PRIDS))
817#define power_id_scan_disable() (PRR2 |= (uint8_t)(1 << PRIDS))
818#endif
819
820#if defined(__AVR_HAVE_PRR2_PRRAM0)
821#define power_ram0_enable() (PRR2 &= (uint8_t)~(1 << PRRAM0))
822#define power_ram0_disable() (PRR2 |= (uint8_t)(1 << PRRAM0))
823#endif
824
825#if defined(__AVR_HAVE_PRR2_PRRAM1)
826#define power_ram1_enable() (PRR2 &= (uint8_t)~(1 << PRRAM1))
827#define power_ram1_disable() (PRR2 |= (uint8_t)(1 << PRRAM1))
828#endif
829
830#if defined(__AVR_HAVE_PRR2_PRRAM2)
831#define power_ram2_enable() (PRR2 &= (uint8_t)~(1 << PRRAM2))
832#define power_ram2_disable() (PRR2 |= (uint8_t)(1 << PRRAM2))
833#endif
834
835#if defined(__AVR_HAVE_PRR2_PRRAM3)
836#define power_ram3_enable() (PRR2 &= (uint8_t)~(1 << PRRAM3))
837#define power_ram3_disable() (PRR2 |= (uint8_t)(1 << PRRAM3))
838#endif
839
840#if defined(__AVR_HAVE_PRR2_PRRS)
841#define power_rssi_buffer_enable() (PRR2 &= (uint8_t)~(1 << PRRS))
842#define power_rssi_buffer_disable() (PRR2 |= (uint8_t)(1 << PRRS))
843#endif
844
845#if defined(__AVR_HAVE_PRR2_PRSF)
846#define power_preamble_rssi_fifo_enable() (PRR2 &= (uint8_t)~(1 << PRSF))
847#define power_preamble_rssi_fifo_disable() (PRR2 |= (uint8_t)(1 << PRSF))
848#endif
849
850#if defined(__AVR_HAVE_PRR2_PRSPI2)
851#define power_spi2_enable() (PRR2 &= (uint8_t)~(1 << PRSPI2))
852#define power_spi2_disable() (PRR2 |= (uint8_t)(1 << PRSPI2))
853#endif
854
855#if defined(__AVR_HAVE_PRR2_PRSSM)
856#define power_sequencer_state_machine_enable() (PRR2 &= (uint8_t)~(1 << PRSSM))
857#define power_sequencer_state_machine_disable() (PRR2 |= (uint8_t)(1 << PRSSM))
858#endif
859
860#if defined(__AVR_HAVE_PRR2_PRTM)
861#define power_tx_modulator_enable() (PRR2 &= (uint8_t)~(1 << PRTM))
862#define power_tx_modulator_disable() (PRR2 |= (uint8_t)(1 << PRTM))
863#endif
864
865#if defined(__AVR_HAVE_PRR2_PRTWI2)
866#define power_twi2_enable() (PRR2 &= (uint8_t)~(1 << PRTWI2))
867#define power_twi2_disable() (PRR2 |= (uint8_t)(1 << PRTWI2))
868#endif
869
870#if defined(__AVR_HAVE_PRR2_PRXA)
871#define power_rx_buffer_A_enable() (PRR2 &= (uint8_t)~(1 << PRXA))
872#define power_rx_buffer_A_disable() (PRR2 |= (uint8_t)(1 << PRXA))
873#endif
874
875#if defined(__AVR_HAVE_PRR2_PRXB)
876#define power_rx_buffer_B_enable() (PRR2 &= (uint8_t)~(1 << PRXB))
877#define power_rx_buffer_B_disable() (PRR2 |= (uint8_t)(1 << PRXB))
878#endif
879
880#if defined(__AVR_HAVE_PRGEN_AES)
881#define power_aes_enable() (PR_PRGEN &= (uint8_t)~(PR_AES_bm))
882#define power_aes_disable() (PR_PRGEN |= (uint8_t)PR_AES_bm)
883#endif
884
885#if defined(__AVR_HAVE_PRGEN_DMA)
886#define power_dma_enable() (PR_PRGEN &= (uint8_t)~(PR_DMA_bm))
887#define power_dma_disable() (PR_PRGEN |= (uint8_t)PR_DMA_bm)
888#endif
889
890#if defined(__AVR_HAVE_PRGEN_EBI)
891#define power_ebi_enable() (PR_PRGEN &= (uint8_t)~(PR_EBI_bm))
892#define power_ebi_disable() (PR_PRGEN |= (uint8_t)PR_EBI_bm)
893#endif
894
895#if defined(__AVR_HAVE_PRGEN_EDMA)
896#define power_edma_enable() (PR_PRGEN &= (uint8_t)~(PR_EDMA_bm))
897#define power_edma_disable() (PR_PRGEN |= (uint8_t)PR_EDMA_bm)
898#endif
899
900#if defined(__AVR_HAVE_PRGEN_EVSYS)
901#define power_evsys_enable() (PR_PRGEN &= (uint8_t)~(PR_EVSYS_bm))
902#define power_evsys_disable() (PR_PRGEN |= (uint8_t)PR_EVSYS_bm)
903#endif
904
905#if defined(__AVR_HAVE_PRGEN_LCD)
906#define power_lcd_enable() (PR_PRGEN &= (uint8_t)~(PR_LCD_bm))
907#define power_lcd_disable() (PR_PRGEN |= (uint8_t)PR_LCD_bm)
908#endif
909
910#if defined(__AVR_HAVE_PRGEN_RTC)
911#define power_rtc_enable() (PR_PRGEN &= (uint8_t)~(PR_RTC_bm))
912#define power_rtc_disable() (PR_PRGEN |= (uint8_t)PR_RTC_bm)
913#endif
914
915#if defined(__AVR_HAVE_PRGEN_USB)
916#define power_usb_enable() (PR_PRGEN &= (uint8_t)~(PR_USB_bm))
917#define power_usb_disable() (PR_PRGEN &= (uint8_t)(PR_USB_bm))
918#endif
919
920#if defined(__AVR_HAVE_PRGEN_XCL)
921#define power_xcl_enable() (PR_PRGEN &= (uint8_t)~(PR_XCL_bm))
922#define power_xcl_disable() (PR_PRGEN |= (uint8_t)PR_XCL_bm)
923#endif
924
925#if defined(__AVR_HAVE_PRPA_AC)
926#define power_aca_enable() (PR_PRPA &= (uint8_t)~(PR_AC_bm))
927#define power_aca_disable() (PR_PRPA |= (uint8_t)PR_AC_bm)
928#endif
929
930#if defined(__AVR_HAVE_PRPA_ADC)
931#define power_adca_enable() (PR_PRPA &= (uint8_t)~(PR_ADC_bm))
932#define power_adca_disable() (PR_PRPA |= (uint8_t)PR_ADC_bm)
933#endif
934
935#if defined(__AVR_HAVE_PRPA_DAC)
936#define power_daca_enable() (PR_PRPA &= (uint8_t)~(PR_DAC_bm))
937#define power_daca_disable() (PR_PRPA |= (uint8_t)PR_DAC_bm)
938#endif
939
940#if defined(__AVR_HAVE_PRPB_AC)
941#define power_acb_enable() (PR_PRPB &= (uint8_t)~(PR_AC_bm))
942#define power_acb_disable() (PR_PRPB |= (uint8_t)PR_AC_bm)
943#endif
944
945#if defined(__AVR_HAVE_PRPB_ADC)
946#define power_adcb_enable() (PR_PRPB &= (uint8_t)~(PR_ADC_bm))
947#define power_adcb_disable() (PR_PRPB |= (uint8_t)PR_ADC_bm)
948#endif
949
950#if defined(__AVR_HAVE_PRPB_DAC)
951#define power_dacb_enable() (PR_PRPB &= (uint8_t)~(PR_DAC_bm))
952#define power_dacb_disable() (PR_PRPB |= (uint8_t)PR_DAC_bm)
953#endif
954
955#if defined(__AVR_HAVE_PRPC_HIRES)
956#define power_hiresc_enable() (PR_PRPC &= (uint8_t)~(PR_HIRES_bm))
957#define power_hiresc_disable() (PR_PRPC |= (uint8_t)PR_HIRES_bm)
958#endif
959
960#if defined(__AVR_HAVE_PRPC_SPI)
961#define power_spic_enable() (PR_PRPC &= (uint8_t)~(PR_SPI_bm))
962#define power_spic_disable() (PR_PRPC |= (uint8_t)PR_SPI_bm)
963#endif
964
965#if defined(__AVR_HAVE_PRPC_TC0)
966#define power_tc0c_enable() (PR_PRPC &= (uint8_t)~(PR_TC0_bm))
967#define power_tc0c_disable() (PR_PRPC |= (uint8_t)PR_TC0_bm)
968#endif
969
970#if defined(__AVR_HAVE_PRPC_TC1)
971#define power_tc1c_enable() (PR_PRPC &= (uint8_t)~(PR_TC1_bm))
972#define power_tc1c_disable() (PR_PRPC |= (uint8_t)PR_TC1_bm)
973#endif
974
975#if defined(__AVR_HAVE_PRPC_TC4)
976#define power_tc4c_enable() (PR_PRPC &= (uint8_t)~(PR_TC4_bm))
977#define power_tc4c_disable() (PR_PRPC |= (uint8_t)PR_TC4_bm)
978#endif
979
980#if defined(__AVR_HAVE_PRPC_TC5)
981#define power_tc5c_enable() (PR_PRPC &= (uint8_t)~(PR_TC5_bm))
982#define power_tc5c_disable() (PR_PRPC |= (uint8_t)PR_TC5_bm)
983#endif
984
985#if defined(__AVR_HAVE_PRPC_TWI)
986#define power_twic_enable() (PR_PRPC &= (uint8_t)~(PR_TWI_bm))
987#define power_twic_disable() (PR_PRPC |= (uint8_t)PR_TWI_bm)
988#endif
989
990#if defined(__AVR_HAVE_PRPC_USART0)
991#define power_usartc0_enable() (PR_PRPC &= (uint8_t)~(PR_USART0_bm))
992#define power_usartc0_disable() (PR_PRPC |= (uint8_t)PR_USART0_bm)
993#endif
994
995#if defined(__AVR_HAVE_PRPC_USART1)
996#define power_usartc1_enable() (PR_PRPC &= (uint8_t)~(PR_USART1_bm))
997#define power_usartc1_disable() (PR_PRPC |= (uint8_t)PR_USART1_bm)
998#endif
999
1000#if defined(__AVR_HAVE_PRPD_HIRES)
1001#define power_hiresd_enable() (PR_PRPD &= (uint8_t)~(PR_HIRES_bm))
1002#define power_hiresd_disable() (PR_PRPD |= (uint8_t)PR_HIRES_bm)
1003#endif
1004
1005#if defined(__AVR_HAVE_PRPD_SPI)
1006#define power_spid_enable() (PR_PRPD &= (uint8_t)~(PR_SPI_bm))
1007#define power_spid_disable() (PR_PRPD |= (uint8_t)PR_SPI_bm)
1008#endif
1009
1010#if defined(__AVR_HAVE_PRPD_TC0)
1011#define power_tc0d_enable() (PR_PRPD &= (uint8_t)~(PR_TC0_bm))
1012#define power_tc0d_disable() (PR_PRPD |= (uint8_t)PR_TC0_bm)
1013#endif
1014
1015#if defined(__AVR_HAVE_PRPD_TC1)
1016#define power_tc1d_enable() (PR_PRPD &= (uint8_t)~(PR_TC1_bm))
1017#define power_tc1d_disable() (PR_PRPD |= (uint8_t)PR_TC1_bm)
1018#endif
1019
1020#if defined(__AVR_HAVE_PRPD_TC5)
1021#define power_tc5d_enable() (PR_PRPD &= (uint8_t)~(PR_TC5_bm))
1022#define power_tc5d_disable() (PR_PRPD |= (uint8_t)PR_TC5_bm)
1023#endif
1024
1025#if defined(__AVR_HAVE_PRPD_TWI)
1026#define power_twid_enable() (PR_PRPD &= (uint8_t)~(PR_TWI_bm))
1027#define power_twid_disable() (PR_PRPD |= (uint8_t)PR_TWI_bm)
1028#endif
1029
1030#if defined(__AVR_HAVE_PRPD_USART0)
1031#define power_usartd0_enable() (PR_PRPD &= (uint8_t)~(PR_USART0_bm))
1032#define power_usartd0_disable() (PR_PRPD |= (uint8_t)PR_USART0_bm)
1033#endif
1034
1035#if defined(__AVR_HAVE_PRPD_USART1)
1036#define power_usartd1_enable() (PR_PRPD &= (uint8_t)~(PR_USART1_bm))
1037#define power_usartd1_disable() (PR_PRPD |= (uint8_t)PR_USART1_bm)
1038#endif
1039
1040#if defined(__AVR_HAVE_PRPE_HIRES)
1041#define power_hirese_enable() (PR_PRPE &= (uint8_t)~(PR_HIRES_bm))
1042#define power_hirese_disable() (PR_PRPE |= (uint8_t)PR_HIRES_bm)
1043#endif
1044
1045#if defined(__AVR_HAVE_PRPE_SPI)
1046#define power_spie_enable() (PR_PRPE &= (uint8_t)~(PR_SPI_bm))
1047#define power_spie_disable() (PR_PRPE |= (uint8_t)PR_SPI_bm)
1048#endif
1049
1050#if defined(__AVR_HAVE_PRPE_TC0)
1051#define power_tc0e_enable() (PR_PRPE &= (uint8_t)~(PR_TC0_bm))
1052#define power_tc0e_disable() (PR_PRPE |= (uint8_t)PR_TC0_bm)
1053#endif
1054
1055#if defined(__AVR_HAVE_PRPE_TC1)
1056#define power_tc1e_enable() (PR_PRPE &= (uint8_t)~(PR_TC1_bm))
1057#define power_tc1e_disable() (PR_PRPE |= (uint8_t)PR_TC1_bm)
1058#endif
1059
1060#if defined(__AVR_HAVE_PRPE_TWI)
1061#define power_twie_enable() (PR_PRPE &= (uint8_t)~(PR_TWI_bm))
1062#define power_twie_disable() (PR_PRPE |= (uint8_t)PR_TWI_bm)
1063#endif
1064
1065#if defined(__AVR_HAVE_PRPE_USART0)
1066#define power_usarte0_enable() (PR_PRPE &= (uint8_t)~(PR_USART0_bm))
1067#define power_usarte0_disable() (PR_PRPE |= (uint8_t)PR_USART0_bm)
1068#endif
1069
1070#if defined(__AVR_HAVE_PRPE_USART1)
1071#define power_usarte1_enable() (PR_PRPE &= (uint8_t)~(PR_USART1_bm))
1072#define power_usarte1_disable() (PR_PRPE |= (uint8_t)PR_USART1_bm)
1073#endif
1074
1075#if defined(__AVR_HAVE_PRPF_HIRES)
1076#define power_hiresf_enable() (PR_PRPF &= (uint8_t)~(PR_HIRES_bm))
1077#define power_hiresf_disable() (PR_PRPF |= (uint8_t)PR_HIRES_bm)
1078#endif
1079
1080#if defined(__AVR_HAVE_PRPF_SPI)
1081#define power_spif_enable() (PR_PRPF &= (uint8_t)~(PR_SPI_bm))
1082#define power_spif_disable() (PR_PRPF |= (uint8_t)PR_SPI_bm)
1083#endif
1084
1085#if defined(__AVR_HAVE_PRPF_TC0)
1086#define power_tc0f_enable() (PR_PRPF &= (uint8_t)~(PR_TC0_bm))
1087#define power_tc0f_disable() (PR_PRPF |= (uint8_t)PR_TC0_bm)
1088#endif
1089
1090#if defined(__AVR_HAVE_PRPF_TC1)
1091#define power_tc1f_enable() (PR_PRPF &= (uint8_t)~(PR_TC1_bm))
1092#define power_tc1f_disable() (PR_PRPF |= (uint8_t)PR_TC1_bm)
1093#endif
1094
1095#if defined(__AVR_HAVE_PRPF_TWI)
1096#define power_twif_enable() (PR_PRPF &= (uint8_t)~(PR_TWI_bm))
1097#define power_twif_disable() (PR_PRPF |= (uint8_t)PR_TWI_bm)
1098#endif
1099
1100#if defined(__AVR_HAVE_PRPF_USART0)
1101#define power_usartf0_enable() (PR_PRPF &= (uint8_t)~(PR_USART0_bm))
1102#define power_usartf0_disable() (PR_PRPF |= (uint8_t)PR_USART0_bm)
1103#endif
1104
1105#if defined(__AVR_HAVE_PRPF_USART1)
1106#define power_usartf1_enable() (PR_PRPF &= (uint8_t)~(PR_USART1_bm))
1107#define power_usartf1_disable() (PR_PRPF |= (uint8_t)PR_USART1_bm)
1108#endif
1109
1110#ifdef __DOXYGEN__
1111/**
1112 \ingroup avr_power
1113 \fn void power_all_enable()
1114 Enable all modules.
1115 */
1116static __ATTR_ALWAYS_INLINE__ void power_all_enable();
1117#else
1118static __ATTR_ALWAYS_INLINE__ void __power_all_enable()
1119{
1120#ifdef __AVR_HAVE_PRR
1121 PRR &= (uint8_t)~(__AVR_HAVE_PRR);
1122#endif
1123
1124#ifdef __AVR_HAVE_PRR0
1125 PRR0 &= (uint8_t)~(__AVR_HAVE_PRR0);
1126#endif
1127
1128#ifdef __AVR_HAVE_PRR1
1129 PRR1 &= (uint8_t)~(__AVR_HAVE_PRR1);
1130#endif
1131
1132#ifdef __AVR_HAVE_PRR2
1133 PRR2 &= (uint8_t)~(__AVR_HAVE_PRR2);
1134#endif
1135
1136#ifdef __AVR_HAVE_PRGEN
1137 PR_PRGEN &= (uint8_t)~(__AVR_HAVE_PRGEN);
1138#endif
1139
1140#ifdef __AVR_HAVE_PRPA
1141 PR_PRPA &= (uint8_t)~(__AVR_HAVE_PRPA);
1142#endif
1143
1144#ifdef __AVR_HAVE_PRPB
1145 PR_PRPB &= (uint8_t)~(__AVR_HAVE_PRPB);
1146#endif
1147
1148#ifdef __AVR_HAVE_PRPC
1149 PR_PRPC &= (uint8_t)~(__AVR_HAVE_PRPC);
1150#endif
1151
1152#ifdef __AVR_HAVE_PRPD
1153 PR_PRPD &= (uint8_t)~(__AVR_HAVE_PRPD);
1154#endif
1155
1156#ifdef __AVR_HAVE_PRPE
1157 PR_PRPE &= (uint8_t)~(__AVR_HAVE_PRPE);
1158#endif
1159
1160#ifdef __AVR_HAVE_PRPF
1161 PR_PRPF &= (uint8_t)~(__AVR_HAVE_PRPF);
1162#endif
1163}
1164#endif /* __DOXYGEN__ */
1165
1166#ifdef __DOXYGEN__
1167/**
1168 \ingroup avr_power
1169 \fn void power_all_disable()
1170 Disable all modules.
1171 */
1172static __ATTR_ALWAYS_INLINE__ void power_all_disable();
1173#else
1174static __ATTR_ALWAYS_INLINE__ void __power_all_disable()
1175{
1176#ifdef __AVR_HAVE_PRR
1177 PRR |= (uint8_t)(__AVR_HAVE_PRR);
1178#endif
1179
1180#ifdef __AVR_HAVE_PRR0
1181 PRR0 |= (uint8_t)(__AVR_HAVE_PRR0);
1182#endif
1183
1184#ifdef __AVR_HAVE_PRR1
1185 PRR1 |= (uint8_t)(__AVR_HAVE_PRR1);
1186#endif
1187
1188#ifdef __AVR_HAVE_PRR2
1189 PRR2 |= (uint8_t)(__AVR_HAVE_PRR2);
1190#endif
1191
1192#ifdef __AVR_HAVE_PRGEN
1193 PR_PRGEN |= (uint8_t)(__AVR_HAVE_PRGEN);
1194#endif
1195
1196#ifdef __AVR_HAVE_PRPA
1197 PR_PRPA |= (uint8_t)(__AVR_HAVE_PRPA);
1198#endif
1199
1200#ifdef __AVR_HAVE_PRPB
1201 PR_PRPB |= (uint8_t)(__AVR_HAVE_PRPB);
1202#endif
1203
1204#ifdef __AVR_HAVE_PRPC
1205 PR_PRPC |= (uint8_t)(__AVR_HAVE_PRPC);
1206#endif
1207
1208#ifdef __AVR_HAVE_PRPD
1209 PR_PRPD |= (uint8_t)(__AVR_HAVE_PRPD);
1210#endif
1211
1212#ifdef __AVR_HAVE_PRPE
1213 PR_PRPE |= (uint8_t)(__AVR_HAVE_PRPE);
1214#endif
1215
1216#ifdef __AVR_HAVE_PRPF
1217 PR_PRPF |= (uint8_t)(__AVR_HAVE_PRPF);
1218#endif
1219}
1220#endif /* __DOXYGEN__ */
1221
1222#ifndef __DOXYGEN__
1223#ifndef power_all_enable
1224#define power_all_enable() __power_all_enable()
1225#endif
1226
1227#ifndef power_all_disable
1228#define power_all_disable() __power_all_disable()
1229#endif
1230#endif /* !__DOXYGEN__ */
1231
1232
1233#if defined(__DOXYGEN__) \
1234|| defined(__AVR_AT90CAN32__) \
1235|| defined(__AVR_AT90CAN64__) \
1236|| defined(__AVR_AT90CAN128__) \
1237|| defined(__AVR_AT90PWM1__) \
1238|| defined(__AVR_AT90PWM2__) \
1239|| defined(__AVR_AT90PWM2B__) \
1240|| defined(__AVR_AT90PWM3__) \
1241|| defined(__AVR_AT90PWM3B__) \
1242|| defined(__AVR_AT90PWM81__) \
1243|| defined(__AVR_AT90PWM161__) \
1244|| defined(__AVR_AT90PWM216__) \
1245|| defined(__AVR_AT90PWM316__) \
1246|| defined(__AVR_AT90SCR100__) \
1247|| defined(__AVR_AT90USB646__) \
1248|| defined(__AVR_AT90USB647__) \
1249|| defined(__AVR_AT90USB82__) \
1250|| defined(__AVR_AT90USB1286__) \
1251|| defined(__AVR_AT90USB1287__) \
1252|| defined(__AVR_AT90USB162__) \
1253|| defined(__AVR_ATA5505__) \
1254|| defined(__AVR_ATA5272__) \
1255|| defined(__AVR_ATmega1280__) \
1256|| defined(__AVR_ATmega1281__) \
1257|| defined(__AVR_ATmega1284__) \
1258|| defined(__AVR_ATmega128RFA1__) \
1259|| defined(__AVR_ATmega1284RFR2__) \
1260|| defined(__AVR_ATmega128RFR2__) \
1261|| defined(__AVR_ATmega1284P__) \
1262|| defined(__AVR_ATmega162__) \
1263|| defined(__AVR_ATmega164A__) \
1264|| defined(__AVR_ATmega164P__) \
1265|| defined(__AVR_ATmega164PA__) \
1266|| defined(__AVR_ATmega165__) \
1267|| defined(__AVR_ATmega165A__) \
1268|| defined(__AVR_ATmega165P__) \
1269|| defined(__AVR_ATmega165PA__) \
1270|| defined(__AVR_ATmega168__) \
1271|| defined(__AVR_ATmega168P__) \
1272|| defined(__AVR_ATmega168A__) \
1273|| defined(__AVR_ATmega168PA__) \
1274|| defined(__AVR_ATmega168PB__) \
1275|| defined(__AVR_ATmega169__) \
1276|| defined(__AVR_ATmega169A__) \
1277|| defined(__AVR_ATmega169P__) \
1278|| defined(__AVR_ATmega169PA__) \
1279|| defined(__AVR_ATmega16M1__) \
1280|| defined(__AVR_ATmega16U2__) \
1281|| defined(__AVR_ATmega16U4__) \
1282|| defined(__AVR_ATmega2560__) \
1283|| defined(__AVR_ATmega2561__) \
1284|| defined(__AVR_ATmega2564RFR2__) \
1285|| defined(__AVR_ATmega256RFR2__) \
1286|| defined(__AVR_ATmega324A__) \
1287|| defined(__AVR_ATmega324P__) \
1288|| defined(__AVR_ATmega324PA__) \
1289|| defined(__AVR_ATmega324PB__) \
1290|| defined(__AVR_ATmega325__) \
1291|| defined(__AVR_ATmega325A__) \
1292|| defined(__AVR_ATmega325PA__) \
1293|| defined(__AVR_ATmega3250__) \
1294|| defined(__AVR_ATmega3250A__) \
1295|| defined(__AVR_ATmega3250PA__) \
1296|| defined(__AVR_ATmega328__) \
1297|| defined(__AVR_ATmega328P__) \
1298|| defined(__AVR_ATmega328PB__) \
1299|| defined(__AVR_ATmega329__) \
1300|| defined(__AVR_ATmega329A__) \
1301|| defined(__AVR_ATmega329P__) \
1302|| defined(__AVR_ATmega329PA__) \
1303|| defined(__AVR_ATmega3290__) \
1304|| defined(__AVR_ATmega3290A__) \
1305|| defined(__AVR_ATmega3290P__) \
1306|| defined(__AVR_ATmega3290PA__) \
1307|| defined(__AVR_ATmega32C1__) \
1308|| defined(__AVR_ATmega32M1__) \
1309|| defined(__AVR_ATmega32U2__) \
1310|| defined(__AVR_ATmega32U4__) \
1311|| defined(__AVR_ATmega32U6__) \
1312|| defined(__AVR_ATmega48__) \
1313|| defined(__AVR_ATmega48A__) \
1314|| defined(__AVR_ATmega48PA__) \
1315|| defined(__AVR_ATmega48P__) \
1316|| defined(__AVR_ATmega640__) \
1317|| defined(__AVR_ATmega649P__) \
1318|| defined(__AVR_ATmega644__) \
1319|| defined(__AVR_ATmega644A__) \
1320|| defined(__AVR_ATmega644P__) \
1321|| defined(__AVR_ATmega644PA__) \
1322|| defined(__AVR_ATmega645__) \
1323|| defined(__AVR_ATmega645A__) \
1324|| defined(__AVR_ATmega645P__) \
1325|| defined(__AVR_ATmega6450__) \
1326|| defined(__AVR_ATmega6450A__) \
1327|| defined(__AVR_ATmega6450P__) \
1328|| defined(__AVR_ATmega649__) \
1329|| defined(__AVR_ATmega649A__) \
1330|| defined(__AVR_ATmega64M1__) \
1331|| defined(__AVR_ATmega64C1__) \
1332|| defined(__AVR_ATmega6490__) \
1333|| defined(__AVR_ATmega6490A__) \
1334|| defined(__AVR_ATmega6490P__) \
1335|| defined(__AVR_ATmega644RFR2__) \
1336|| defined(__AVR_ATmega64RFR2__) \
1337|| defined(__AVR_ATmega88__) \
1338|| defined(__AVR_ATmega88A__) \
1339|| defined(__AVR_ATmega88P__) \
1340|| defined(__AVR_ATmega88PA__) \
1341|| defined(__AVR_ATmega8U2__) \
1342|| defined(__AVR_ATmega16U2__) \
1343|| defined(__AVR_ATmega32U2__) \
1344|| defined(__AVR_ATtiny48__) \
1345|| defined(__AVR_ATtiny88__) \
1346|| defined(__AVR_ATtiny87__) \
1347|| defined(__AVR_ATtiny167__)
1348
1349
1350/** \addtogroup avr_power
1351
1352Some of the newer AVRs contain a System Clock Prescale Register (CLKPR) that
1353allows you to decrease the system clock frequency and the power consumption
1354when the need for processing power is low.
1355On some earlier AVRs (ATmega103, ATmega64, ATmega128), similar
1356functionality can be achieved through the XTAL Divide Control Register.
1357Below are two macros and an enumerated type that can be used to
1358interface to the Clock Prescale Register or
1359XTAL Divide Control Register.
1360
1361\note Not all AVR devices have a clock prescaler. On those devices
1362without a Clock Prescale Register or XTAL Divide Control Register, these
1363macros are not available.
1364
1365\code
1366typedef enum
1367{
1368 clock_div_1 = 0,
1369 clock_div_2 = 1,
1370 clock_div_4 = 2,
1371 clock_div_8 = 3,
1372 clock_div_16 = 4,
1373 clock_div_32 = 5,
1374 clock_div_64 = 6,
1375 clock_div_128 = 7,
1376 clock_div_256 = 8,
1377 clock_div_1_rc = 15, // ATmega128RFA1 only
1378} clock_div_t;
1379\endcode
1380Clock prescaler setting enumerations for device using
1381System Clock Prescale Register.
1382
1383\code
1384typedef enum
1385{
1386 clock_div_1 = 1,
1387 clock_div_2 = 2,
1388 clock_div_4 = 4,
1389 clock_div_8 = 8,
1390 clock_div_16 = 16,
1391 clock_div_32 = 32,
1392 clock_div_64 = 64,
1393 clock_div_128 = 128
1394} clock_div_t;
1395\endcode
1396Clock prescaler setting enumerations for device using
1397XTAL Divide Control Register.
1398
1399*/
1400#ifndef __DOXYGEN__
1401typedef enum
1402{
1403 clock_div_1 = 0,
1404 clock_div_2 = 1,
1405 clock_div_4 = 2,
1406 clock_div_8 = 3,
1407 clock_div_16 = 4,
1408 clock_div_32 = 5,
1409 clock_div_64 = 6,
1410 clock_div_128 = 7,
1411 clock_div_256 = 8
1412#if defined(__AVR_ATmega128RFA1__) \
1413|| defined(__AVR_ATmega2564RFR2__) \
1414|| defined(__AVR_ATmega1284RFR2__) \
1415|| defined(__AVR_ATmega644RFR2__) \
1416|| defined(__AVR_ATmega256RFR2__) \
1417|| defined(__AVR_ATmega128RFR2__) \
1418|| defined(__AVR_ATmega64RFR2__)
1419 , clock_div_1_rc = 15
1420#endif
1421} clock_div_t;
1422
1423static __ATTR_ALWAYS_INLINE__ void clock_prescale_set(clock_div_t);
1424#endif /* !__DOXYGEN__ */
1425
1426/**
1427 \ingroup avr_power
1428 \fn clock_prescale_set(clock_div_t x)
1429
1430Set the clock prescaler register select bits, selecting a system clock
1431division setting. This function is inlined, even if compiler
1432optimizations are disabled.
1433
1434The type of \c x is \c clock_div_t.
1435
1436\note For device with XTAL Divide Control Register (XDIV), \c x can actually range
1437from 1 to 129. Thus, one does not need to use \c clock_div_t type as argument.
1438*/
1439void clock_prescale_set(clock_div_t __x)
1440{
1441 uint8_t __tmp = _BV(CLKPCE);
1442 __asm__ __volatile__ (
1443 "in __tmp_reg__,__SREG__" "\n\t"
1444 "cli" "\n\t"
1445 "sts %1, %0" "\n\t"
1446 "sts %1, %2" "\n\t"
1447 "out __SREG__, __tmp_reg__"
1448 : /* no outputs */
1449 : "d" (__tmp),
1450 "M" (_SFR_MEM_ADDR(CLKPR)),
1451 "d" ((uint8_t) __x)
1452 : "r0", "memory");
1453}
1454
1455/** \ingroup avr_power
1456\def clock_prescale_get()
1457Gets and returns the clock prescaler register setting. The return type is \c clock_div_t.
1458
1459\note For device with XTAL Divide Control Register (XDIV), return can actually
1460range from 1 to 129. Care should be taken has the return value could differ from the
1461typedef enum clock_div_t. This should only happen if clock_prescale_set was previously
1462called with a value other than those defined by \c clock_div_t.
1463*/
1464#define clock_prescale_get() (clock_div_t)(CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)|(1<<CLKPS3)))
1465
1466#elif defined(__AVR_ATmega16HVB__) \
1467|| defined(__AVR_ATmega16HVBREVB__) \
1468|| defined(__AVR_ATmega32HVB__) \
1469|| defined(__AVR_ATmega32HVBREVB__)
1470
1471typedef enum
1472{
1473 clock_div_1 = 0,
1474 clock_div_2 = 1,
1475 clock_div_4 = 2,
1476 clock_div_8 = 3
1477} clock_div_t;
1478
1479static __ATTR_ALWAYS_INLINE__ void clock_prescale_set(clock_div_t);
1480
1481void clock_prescale_set(clock_div_t __x)
1482{
1483 uint8_t __tmp = _BV(CLKPCE);
1484 __asm__ __volatile__ (
1485 "in __tmp_reg__,__SREG__" "\n\t"
1486 "cli" "\n\t"
1487 "sts %1, %0" "\n\t"
1488 "sts %1, %2" "\n\t"
1489 "out __SREG__, __tmp_reg__"
1490 : /* no outputs */
1491 : "d" (__tmp),
1492 "M" (_SFR_MEM_ADDR(CLKPR)),
1493 "d" ((uint8_t) __x)
1494 : "r0", "memory");
1495}
1496
1497#define clock_prescale_get() (clock_div_t)(CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)))
1498
1499#elif defined(__AVR_ATA5790__) \
1500|| defined (__AVR_ATA5795__)
1501
1502typedef enum
1503{
1504 clock_div_1 = 0,
1505 clock_div_2 = 1,
1506 clock_div_4 = 2,
1507 clock_div_8 = 3,
1508 clock_div_16 = 4,
1509 clock_div_32 = 5,
1510 clock_div_64 = 6,
1511 clock_div_128 = 7,
1512} clock_div_t;
1513
1514static __ATTR_ALWAYS_INLINE__ void system_clock_prescale_set(clock_div_t);
1515
1516void system_clock_prescale_set(clock_div_t __x)
1517{
1518 uint8_t __tmp = _BV(CLKPCE);
1519 __asm__ __volatile__ (
1520 "in __tmp_reg__,__SREG__" "\n\t"
1521 "cli" "\n\t"
1522 "out %1, %0" "\n\t"
1523 "out %1, %2" "\n\t"
1524 "out __SREG__, __tmp_reg__"
1525 : /* no outputs */
1526 : "d" (__tmp),
1527 "I" (_SFR_IO_ADDR(CLKPR)),
1528 "d" ((uint8_t) __x)
1529 : "r0", "memory");
1530}
1531
1532#define system_clock_prescale_get() (clock_div_t)(CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)))
1533
1534typedef enum
1535{
1536 timer_clock_div_reset = 0,
1537 timer_clock_div_1 = 1,
1538 timer_clock_div_2 = 2,
1539 timer_clock_div_4 = 3,
1540 timer_clock_div_8 = 4,
1541 timer_clock_div_16 = 5,
1542 timer_clock_div_32 = 6,
1543 timer_clock_div_64 = 7
1544} timer_clock_div_t;
1545
1546static __ATTR_ALWAYS_INLINE__ void timer_clock_prescale_set(timer_clock_div_t);
1547
1548void timer_clock_prescale_set(timer_clock_div_t __x)
1549{
1550 uint8_t __t;
1551 __asm__ __volatile__ (
1552 "in __tmp_reg__,__SREG__" "\n\t"
1553 "cli" "\n\t"
1554 "in %[temp],%[clkpr]" "\n\t"
1555 "out %[clkpr],%[enable]" "\n\t"
1556 "cbr %[temp],%[not_CLTPS]" "\n\t"
1557 "or %[temp], %[set_value]" "\n\t"
1558 "out %[clkpr],%[temp]" "\n\t"
1559 "out __SREG__,__tmp_reg__"
1560 : [temp] "=d" (__t)
1561 : [clkpr] "I" (_SFR_IO_ADDR(CLKPR)),
1562 [enable] "r" ((uint8_t) _BV(CLKPCE)),
1563 [not_CLTPS] "M" ((1 << CLTPS2) | (1 << CLTPS1) | (1 << CLTPS0)),
1564 [set_value] "r" ((uint8_t) ((__x & 7) << 3))
1565 : "r0", "memory");
1566}
1567
1568#define timer_clock_prescale_get() (timer_clock_div_t)(CLKPR & (uint8_t)((1<<CLTPS0)|(1<<CLTPS1)|(1<<CLTPS2)))
1569
1570#elif defined(__AVR_ATA6285__) \
1571|| defined(__AVR_ATA6286__)
1572
1573typedef enum
1574{
1575 clock_div_1 = 0,
1576 clock_div_2 = 1,
1577 clock_div_4 = 2,
1578 clock_div_8 = 3,
1579 clock_div_16 = 4,
1580 clock_div_32 = 5,
1581 clock_div_64 = 6,
1582 clock_div_128 = 7
1583} clock_div_t;
1584
1585static __ATTR_ALWAYS_INLINE__ void system_clock_prescale_set(clock_div_t);
1586
1587void system_clock_prescale_set(clock_div_t __x)
1588{
1589 uint8_t __t;
1590 __asm__ __volatile__ (
1591 "in __tmp_reg__,__SREG__" "\n\t"
1592 "cli" "\n\t"
1593 "in %[temp],%[clpr]" "\n\t"
1594 "out %[clpr],%[enable]" "\n\t"
1595 "cbr %[temp],%[not_CLKPS]" "\n\t"
1596 "or %[temp], %[set_value]" "\n\t"
1597 "out %[clpr],%[temp]" "\n\t"
1598 "out __SREG__,__tmp_reg__"
1599 : [temp] "=d" (__t)
1600 : [clpr] "I" (_SFR_IO_ADDR(CLKPR)),
1601 [enable] "r" ((uint8_t) (1 << CLPCE)),
1602 [not_CLKPS] "M" ((1 << CLKPS2) | (1 << CLKPS1) | (1 << CLKPS0)),
1603 [set_value] "r" ((uint8_t) (__x & 7))
1604 : "r0", "memory");
1605}
1606
1607#define system_clock_prescale_get() (clock_div_t)(CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)))
1608
1609typedef enum
1610{
1611 timer_clock_div_reset = 0,
1612 timer_clock_div_1 = 1,
1613 timer_clock_div_2 = 2,
1614 timer_clock_div_4 = 3,
1615 timer_clock_div_8 = 4,
1616 timer_clock_div_16 = 5,
1617 timer_clock_div_32 = 6,
1618 timer_clock_div_64 = 7
1619} timer_clock_div_t;
1620
1621static __ATTR_ALWAYS_INLINE__ void timer_clock_prescale_set(timer_clock_div_t);
1622
1623void timer_clock_prescale_set(timer_clock_div_t __x)
1624{
1625 uint8_t __t;
1626 __asm__ __volatile__ (
1627 "in __tmp_reg__,__SREG__" "\n\t"
1628 "cli" "\n\t"
1629 "in %[temp],%[clpr]" "\n\t"
1630 "out %[clpr],%[enable]" "\n\t"
1631 "cbr %[temp],%[not_CLTPS]" "\n\t"
1632 "or %[temp], %[set_value]" "\n\t"
1633 "out %[clpr],%[temp]" "\n\t"
1634 "out __SREG__,__tmp_reg__"
1635 : [temp] "=d" (__t)
1636 : [clpr] "I" (_SFR_IO_ADDR(CLKPR)),
1637 [enable] "r" ((uint8_t) (1 << CLPCE)),
1638 [not_CLTPS] "M" ((1 << CLTPS2) | (1 << CLTPS1) | (1 << CLTPS0)),
1639 [set_value] "r" ((uint8_t) ((__x & 7) << 3))
1640 : "r0", "memory");
1641}
1642
1643#define timer_clock_prescale_get() (timer_clock_div_t)(CLKPR & (uint8_t)((1<<CLTPS0)|(1<<CLTPS1)|(1<<CLTPS2)))
1644
1645#elif defined(__AVR_ATtiny24__) \
1646|| defined(__AVR_ATtiny24A__) \
1647|| defined(__AVR_ATtiny44__) \
1648|| defined(__AVR_ATtiny44A__) \
1649|| defined(__AVR_ATtiny84__) \
1650|| defined(__AVR_ATtiny84A__) \
1651|| defined(__AVR_ATtiny25__) \
1652|| defined(__AVR_ATtiny45__) \
1653|| defined(__AVR_ATtiny85__) \
1654|| defined(__AVR_ATtiny261A__) \
1655|| defined(__AVR_ATtiny261__) \
1656|| defined(__AVR_ATtiny461__) \
1657|| defined(__AVR_ATtiny461A__) \
1658|| defined(__AVR_ATtiny861__) \
1659|| defined(__AVR_ATtiny861A__) \
1660|| defined(__AVR_ATtiny2313__) \
1661|| defined(__AVR_ATtiny2313A__) \
1662|| defined(__AVR_ATtiny4313__) \
1663|| defined(__AVR_ATtiny13__) \
1664|| defined(__AVR_ATtiny13A__) \
1665|| defined(__AVR_ATtiny43U__) \
1666
1667typedef enum
1668{
1669 clock_div_1 = 0,
1670 clock_div_2 = 1,
1671 clock_div_4 = 2,
1672 clock_div_8 = 3,
1673 clock_div_16 = 4,
1674 clock_div_32 = 5,
1675 clock_div_64 = 6,
1676 clock_div_128 = 7,
1677 clock_div_256 = 8
1678} clock_div_t;
1679
1680static __ATTR_ALWAYS_INLINE__ void clock_prescale_set(clock_div_t);
1681
1682void clock_prescale_set(clock_div_t __x)
1683{
1684 __asm__ __volatile__ (
1685 "in __tmp_reg__,__SREG__" "\n\t"
1686 "cli" "\n\t"
1687 "out %1, %0" "\n\t"
1688 "out %1, %2" "\n\t"
1689 "out __SREG__, __tmp_reg__"
1690 : /* no outputs */
1691 : "d" ((uint8_t) (1 << CLKPCE)),
1692 "I" (_SFR_IO_ADDR(CLKPR)),
1693 "d" ((uint8_t) __x)
1694 : "r0", "memory");
1695}
1696
1697
1698#define clock_prescale_get() (clock_div_t)(CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)|(1<<CLKPS3)))
1699
1700#elif defined(__AVR_ATtiny441__) \
1701|| defined(__AVR_ATtiny841__)
1702
1703typedef enum
1704{
1705 clock_div_1 = 0,
1706 clock_div_2 = 1,
1707 clock_div_4 = 2,
1708 clock_div_8 = 3,
1709 clock_div_16 = 4,
1710 clock_div_32 = 5,
1711 clock_div_64 = 6,
1712 clock_div_128 = 7,
1713 clock_div_256 = 8
1714} clock_div_t;
1715
1716static __ATTR_ALWAYS_INLINE__ void clock_prescale_set (clock_div_t);
1717
1718void clock_prescale_set (clock_div_t __x)
1719{
1720 __asm__ __volatile__ (
1721 "in __tmp_reg__,__SREG__" "\n\t"
1722 "cli" "\n\t"
1723 "sts %2, %3" "\n\t"
1724 "sts %1, %0" "\n\t"
1725 "out __SREG__, __tmp_reg__"
1726 : /* no outputs */
1727 : "r" ((uint8_t) __x),
1728 "n" (_SFR_MEM_ADDR(CLKPR)),
1729 "n" (_SFR_MEM_ADDR(CCP)),
1730 "r" ((uint8_t) 0xD8)
1731 : "r0", "memory");
1732}
1733
1734#define clock_prescale_get() (clock_div_t) (CLKPR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)|(1<<CLKPS3)))
1735
1736#elif defined(__AVR_ATmega64__) \
1737|| defined(__AVR_ATmega103__) \
1738|| defined(__AVR_ATmega128__)
1739
1740//Enum is declared for code compatibility
1741typedef enum
1742{
1743 clock_div_1 = 1,
1744 clock_div_2 = 2,
1745 clock_div_4 = 4,
1746 clock_div_8 = 8,
1747 clock_div_16 = 16,
1748 clock_div_32 = 32,
1749 clock_div_64 = 64,
1750 clock_div_128 = 128
1751} clock_div_t;
1752
1753static __ATTR_ALWAYS_INLINE__ void clock_prescale_set(clock_div_t);
1754
1755void clock_prescale_set(clock_div_t __x)
1756{
1757 if ((uint8_t) __x <= 0 || (uint8_t) __x > 129)
1758 {
1759 return;//Invalid value.
1760 }
1761 else
1762 {
1763 uint8_t __tmp = 0;
1764 //Algo explained:
1765 //1 - Clear XDIV in order for it to accept a new value (actually only
1766 // XDIVEN need to be cleared, but clearing XDIV is faster than
1767 // read-modify-write since we will rewrite XDIV later anyway)
1768 //2 - wait 8 clock cycle for stability, see datasheet errata
1769 //3 - Exit if requested prescaler is 1
1770 //4 - Calculate XDIV6..0 value = 129 - __x
1771 //5 - Set XDIVEN bit in calculated value
1772 //6 - write XDIV with calculated value
1773 //7 - wait 8 clock cycle for stability, see datasheet errata
1774 __asm__ __volatile__ (
1775 "in __tmp_reg__,__SREG__" "\n\t"
1776 "cli" "\n\t"
1777 "out %2, __zero_reg__" "\n\t"
1778 "nop" "\n\t"
1779 "nop" "\n\t"
1780 "nop" "\n\t"
1781 "nop" "\n\t"
1782 "nop" "\n\t"
1783 "nop" "\n\t"
1784 "nop" "\n\t"
1785 "nop" "\n\t"
1786 "cpi %1, 0x01" "\n\t"
1787 "breq L_%=" "\n\t"
1788 "ldi %0, 0x81" "\n\t" //129
1789 "sub %0, %1" "\n\t"
1790 "ori %0, 0x80" "\n\t" //128
1791 "out %2, %0" "\n\t"
1792 "nop" "\n\t"
1793 "nop" "\n\t"
1794 "nop" "\n\t"
1795 "nop" "\n\t"
1796 "nop" "\n\t"
1797 "nop" "\n\t"
1798 "nop" "\n\t"
1799 "nop" "\n\t"
1800 "L_%=: " "out __SREG__, __tmp_reg__"
1801 : "=d" (__tmp)
1802 : "d" ((uint8_t) __x),
1803 "I" (_SFR_IO_ADDR(XDIV))
1804 : "r0", "memory");
1805 }
1806}
1807
1808static __ATTR_ALWAYS_INLINE__ clock_div_t clock_prescale_get(void);
1809
1810clock_div_t clock_prescale_get(void)
1811{
1812 if (bit_is_clear(XDIV, XDIVEN))
1813 {
1814 return (clock_div_t) 1;
1815 }
1816 else
1817 {
1818 return (clock_div_t) (129 - (XDIV & 0x7F));
1819 }
1820}
1821
1822#elif defined(__AVR_ATtiny4__) \
1823|| defined(__AVR_ATtiny5__) \
1824|| defined(__AVR_ATtiny9__) \
1825|| defined(__AVR_ATtiny10__) \
1826|| defined(__AVR_ATtiny102__) \
1827|| defined(__AVR_ATtiny104__) \
1828|| defined(__AVR_ATtiny20__) \
1829|| defined(__AVR_ATtiny40__) \
1830
1831typedef enum
1832{
1833 clock_div_1 = 0,
1834 clock_div_2 = 1,
1835 clock_div_4 = 2,
1836 clock_div_8 = 3,
1837 clock_div_16 = 4,
1838 clock_div_32 = 5,
1839 clock_div_64 = 6,
1840 clock_div_128 = 7,
1841 clock_div_256 = 8
1842} clock_div_t;
1843
1844static __ATTR_ALWAYS_INLINE__ void clock_prescale_set(clock_div_t);
1845
1846void clock_prescale_set(clock_div_t __x)
1847{
1848 __asm__ __volatile__ (
1849 "in __tmp_reg__,__SREG__" "\n\t"
1850 "cli" "\n\t"
1851 "out %1, %0" "\n\t"
1852 "out %2, %3" "\n\t"
1853 "out __SREG__, __tmp_reg__"
1854 : /* no outputs */
1855 : "d" ((uint8_t) 0xD8),
1856 "I" (_SFR_IO_ADDR(CCP)),
1857 "I" (_SFR_IO_ADDR(CLKPSR)),
1858 "d" ((uint8_t) __x)
1859 : "r16", "memory");
1860}
1861
1862#define clock_prescale_get() (clock_div_t)(CLKPSR & (uint8_t)((1<<CLKPS0)|(1<<CLKPS1)|(1<<CLKPS2)|(1<<CLKPS3)))
1863
1864#endif
1865
1866#endif /* _AVR_POWER_H_ */
#define clock_prescale_get()
Definition: power.h:1464
static void power_all_enable()
static void power_all_disable()
void clock_prescale_set(clock_div_t __x)
Definition: power.h:1439
#define _BV(bit)
Definition: sfr_defs.h:206
#define bit_is_clear(sfr, bit)
Definition: sfr_defs.h:243
unsigned char uint8_t
Definition: stdint.h:88