Update TODO list
[trut64.git] / avr / trutprofile.c
bloba43110968161b34b03879a4d31a2717671d365a3
1 /*
2 * Copyright (C) 2007 Anton Blad
3 * Copyright (C) 2007 Fredrik Kuivinen
4 * Copyright (C) 2007 Jakob Rosén
6 * This file is licensed under GPL v2.
7 */
9 #ifdef TRUT_PROFILE
11 #include "trutprofile.h"
13 #include <avr/io.h>
14 #include <avr/interrupt.h>
16 static uint16_t profile_stamps[NPROFILE];
17 static uint16_t profile_mwc[NPROFILE];
19 volatile static uint8_t timer0_high;
21 // Allocated measurements:
22 // 0 - TIMER1_COMPA interrupt
23 // 1 - TIMER1_OVF interrupt
24 // 2 - TIMER1_CAPT interrupt
26 // Atomic read of timer stamp.
27 static inline uint16_t profile_stamp()
29 // Save interrupt enable flag.
30 uint8_t sreg = SREG;
31 uint8_t low;
32 uint8_t high;
34 cli();
36 low = TCNT0;
37 if(low < 0xf0 && (TIFR0 & _BV(TOV0)))
38 high = timer0_high + 1;
39 else
40 high = timer0_high;
42 // Reenable interrupts.
43 SREG = sreg;
45 return (high << 8) | low;
48 void profile_init()
50 uint8_t i;
52 for(i = 0; i < NPROFILE; i++)
54 profile_stamps[i] = profile_mwc[i] = 0;
57 timer0_high = 0;
59 TCCR0A = 0;
60 #if PROFILE_PRESCALE == 1
61 TCCR0B = _BV(CS00);
62 #elif PROFILE_PRESCALE == 8
63 TCCR0B = _BV(CS01);
64 #elif PROFILE_PRESCALE == 64
65 TCCR0B = _BV(CS00) | _BV(CS01);
66 #elif PROFILE_PRESCALE == 256
67 TCCR0B = _BV(CS02);
68 #else
69 #error Invalid PROFILE_PRESCALE value
70 #endif
71 TIMSK0 |= _BV(TOIE0);
74 void profile_start(uint8_t m)
76 profile_stamps[m] = profile_stamp();
79 void profile_stop(uint8_t m)
81 uint16_t len = profile_stamp() - profile_stamps[m];
83 if(len > profile_mwc[m])
84 profile_mwc[m] = len;
87 uint16_t profile_wc(uint8_t m)
89 return profile_mwc[m];
92 ISR(TIMER0_OVF_vect)
94 timer0_high++;
97 #endif // TRUT_PROFILE