4
/* Replacement for tk replacement delay functions, now with powersaving sleep.
6
* 2017 by Flintrock (C) highly inspired by original from TK.
8
* This program is free software: you can redistribute it and/or modify
9
* it under the terms of the GNU General Public License as published by
10
* the Free Software Foundation, either version 3 of the License, or
11
* (at your option) any later version.
13
* This program is distributed in the hope that it will be useful,
14
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
* GNU General Public License for more details.
18
* You should have received a copy of the GNU General Public License
19
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23
#include "fr-tk-attiny.h"
25
// USE timed sleeps for delay, by FR, only costs a few bytes.
26
// adding a global time counter (costs a bit more) to allow timeout of fast-presses
28
#ifndef USE_PWM4 // uses PWM4 instead if active
29
ISR(_TIMER0_OVF_vect_, ISR_NAKED){
30
__asm__ volatile("reti");
31
} // timer overflow wake interrupt.
34
// global clock is an alternative method to alternative method to determine
35
// timeout of fast_presses (to get to config)
36
//register uint16_t global_clock asm ("r2");
37
void _delay_sleep_ms(uint16_t n)
39
set_sleep_mode(SLEEP_MODE_IDLE);
41
#if ! defined(USE_PWM4) // prefer timer 0 because it's setup twice slower
42
_TIMSK_ |= (1<<TOIE0); // enable timer overflow interrupt.
43
TCNT0 = 1; // restart the clock. Will glitch the PWM, but that's fine.
44
#define cycle_counts 500 // approximate for timing math, to get int result
45
#else // else an interrupt is already setup in PWM.
46
TCNT1 = 1; // restart the clock. Will glitch the PWM, but that's fine.
47
#define cycle_counts 250 // will use twice slower prescale when pwm4 is enabled
48
// but will only ramp up not down and will wake twice per cycle.
49
// net effect, still 250 counts per cycle (average).
52
// prescale set to 1 in bistro, interrupt on "bottom", counts up, then down, so every 512 cycles.
53
// so F_CPU/512/1000 ticks per ms, rounding to 500, for attiny 25 8mhz it's 8 ticks per ms.
54
// we could maybe make this more exact (ex 0.1ms) using OCR0A/B but they're in use for PWM
55
// Is there another way?
56
for(counter=0; counter<(F_CPU/cycle_counts/1000); counter++){
60
// global_clock+=n>>2; // Only keep track of delays of 4ms or more, it's good enough and saves space with a 1 byte clock.
61
// This will be used to reset fast_presses when it rolls over so 255*2 ms or half a second.
62
// Change to n>>1 to reset at 1/2 second instead.
68
_delay_sleep_ms(1000);
b'\\ No newline at end of file'