~gabe/flashlight-firmware/anduril2

« back to all changes in this revision

Viewing changes to Flintrock/bistro-hd/fr-delay.h

  • Committer: Selene Scriven
  • Date: 2017-09-12 23:34:36 UTC
  • mto: (188.1.3 trunk)
  • mto: This revision was merged to the branch mainline in revision 331.
  • Revision ID: bzr@toykeeper.net-20170912233436-d3w6nln0ts1subue
Added Flintrock's Bistro-HD 1.0.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#ifndef FR_DELAY_H
 
2
#define FR_DELAY_H
 
3
 
 
4
 /* Replacement for tk replacement delay functions, now with powersaving sleep.
 
5
 *
 
6
 * 2017 by Flintrock (C) highly inspired by original from TK.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
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/>.
 
20
 *
 
21
 */ 
 
22
 
 
23
#include "fr-tk-attiny.h"
 
24
 
 
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
 
27
 
 
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.
 
32
#endif
 
33
 
 
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)
 
38
{// 2017 by Flintrock
 
39
        set_sleep_mode(SLEEP_MODE_IDLE);
 
40
        uint8_t counter;
 
41
        #if ! defined(PWM4_LVL)  // 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).
 
50
        #endif 
 
51
        while (n-- > 0) {
 
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++){
 
57
                  sleep_cpu();
 
58
                }
 
59
        }
 
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.
 
63
}
 
64
 
 
65
 
 
66
void _delay_sleep_s()  
 
67
{
 
68
        _delay_sleep_ms(1000);
 
69
}
 
70
#endif
 
 
b'\\ No newline at end of file'