#ifndef power_h #define power_h /* power.h - Power reduction mesures. */ /* binwatch - Tiny binary wristwatch. {{{ * * Copyright (C) 2011 Nicolas Schodet * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Contact : * Web: http://ni.fr.eu.org/ * Email: * }}} */ #include #include "io.h" /** Initialise power reduction. */ void power_init (void); /** Enter power down deep sleep mode, use an enabled interrupt to wake-up. * Interrupts will be enabled only for the duration of the sleep. */ void power_sleep (void); /** Own version of clock_prescale_set which does not touch I bit. */ static inline void __attribute__((__always_inline__)) power_clock_prescale_set (uint8_t x) { uint8_t tmp = _BV (CLKPCE); __asm__ __volatile__ ( "out %1, %0" "\n\t" "out %1, %2" "\n\t" : /* no outputs */ : "d" (tmp), "I" (_SFR_IO_ADDR (CLKPR)), "d" (x) : "r0"); } #define CLOCK_COEF (1 << (AC_POWER_CLKPS_LOW - AC_POWER_CLKPS_INITIAL)) /** Wait for a given duration, trying to reduce clock speed. */ static inline void __attribute__((always_inline)) power_delay_ms (double ms) { double slow_ticks = (AC_FREQ / 1e3 / CLOCK_COEF) * ms; if (slow_ticks > 3.0) { /* Can use slow clock, round down as some extra instructions will be * executed using slow clock. */ if (slow_ticks / 3.0 <= 256) { uint8_t loops = (uint8_t) (slow_ticks / 3.0); power_clock_prescale_set (AC_POWER_CLKPS_LOW); _delay_loop_1 (loops); power_clock_prescale_set (AC_POWER_CLKPS_INITIAL); } else { uint16_t loops = (uint16_t) (slow_ticks / 4.0); power_clock_prescale_set (AC_POWER_CLKPS_LOW); _delay_loop_2 (loops); power_clock_prescale_set (AC_POWER_CLKPS_INITIAL); } /* Do not support huge delay. */ } else { /* Here, the delay is small, use a short loop. */ uint8_t loops = (uint8_t) (ms * AC_FREQ / 3e3); _delay_loop_1 (loops); } } #endif /* power_h */