summaryrefslogtreecommitdiff
path: root/src/common/power.h
blob: ac1f03d7fefe0835e6a76e6b0eadd6ed49e45523 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#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: <nico at ni.fr.eu.org>
 * }}} */
#include <util/delay_basic.h>
#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 */