Rev 2109 | Rev 2135 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
2108 | - | 1 | #include <inttypes.h> |
2 | #include <avr/io.h> |
||
3 | #include <avr/interrupt.h> |
||
2132 | - | 4 | #include <avr/wdt.h> |
2108 | - | 5 | #include "eeprom.h" |
6 | #include "analog.h" |
||
7 | #include "controlMixer.h" |
||
8 | |||
9 | #include "timer0.h" |
||
10 | #include "output.h" |
||
11 | |||
2132 | - | 12 | #ifdef DO_PROFILE |
13 | volatile uint32_t global10kHzClock = 0; |
||
14 | volatile int32_t profileTimers[NUM_PROFILE_TIMERS]; |
||
15 | volatile int32_t runningProfileTimers[NUM_PROFILE_TIMERS]; |
||
2108 | - | 16 | #endif |
17 | |||
18 | volatile uint32_t globalMillisClock = 0; |
||
19 | volatile uint8_t runFlightControl = 0; |
||
20 | volatile uint16_t beepTime = 0; |
||
21 | volatile uint16_t beepModulation = BEEP_MODULATION_NONE; |
||
22 | |||
23 | /***************************************************** |
||
24 | * Initialize Timer 0 |
||
25 | *****************************************************/ |
||
26 | // timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor |
||
27 | // Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate |
||
28 | void timer0_init(void) { |
||
29 | uint8_t sreg = SREG; |
||
30 | |||
31 | // disable all interrupts before reconfiguration |
||
32 | cli(); |
||
33 | |||
2109 | - | 34 | // Configure speaker port as output and also the diags pin D2 |
35 | DDRD |= ((1 << DDD5) | (1 << DDD2)); |
||
36 | PORTD &= ~((1 << PORTD5) | (1 << DDD2)); |
||
2108 | - | 37 | |
38 | // Timer/Counter 0 Control Register A |
||
39 | |||
40 | // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1) |
||
41 | // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0) |
||
42 | // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0) |
||
43 | // TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0)); |
||
44 | // TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00); |
||
45 | |||
46 | // Timer/Counter 0 Control Register B |
||
47 | // set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz |
||
48 | // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz |
||
49 | // hence the timer overflow interrupt frequency is 2.5 MHz/256 = 9.765 kHz |
||
50 | |||
51 | // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0) |
||
52 | TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02)); |
||
53 | TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00); |
||
54 | |||
55 | // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4 |
||
56 | OCR0A = 0; // for PB3 |
||
57 | OCR0B = 120; // for PB4 |
||
58 | |||
59 | // init Timer/Counter 0 Register |
||
60 | TCNT0 = 0; |
||
61 | |||
62 | // Timer/Counter 0 Interrupt Mask Register |
||
63 | // enable timer overflow interrupt only |
||
64 | TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A)); |
||
65 | TIMSK0 |= (1 << TOIE0); |
||
66 | |||
2132 | - | 67 | #ifdef DO_PROFILE |
68 | for (uint8_t i=0; i<NUM_PROFILE_TIMERS; i++) { |
||
69 | profileTimers[i] = 0; |
||
70 | } |
||
71 | #endif |
||
72 | |||
2108 | - | 73 | SREG = sreg; |
74 | } |
||
75 | |||
76 | /*****************************************************/ |
||
77 | /* Interrupt Routine of Timer 0 */ |
||
78 | /*****************************************************/ |
||
79 | ISR(TIMER0_OVF_vect) { // 9765.625 Hz |
||
80 | static uint8_t cnt_1ms = 1, cnt = 0; |
||
81 | uint8_t beeperOn = 0; |
||
82 | |||
2132 | - | 83 | #ifdef DO_PROFILE |
84 | global10kHzClock++; |
||
2108 | - | 85 | #endif |
86 | |||
87 | if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz) |
||
88 | cnt = 9; |
||
89 | cnt_1ms ^= 1; |
||
90 | if (!cnt_1ms) { |
||
91 | runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz) |
||
92 | } |
||
93 | globalMillisClock++; // increment millisecond counter |
||
94 | } |
||
95 | |||
96 | // beeper on if duration is not over |
||
97 | if (beepTime) { |
||
98 | beepTime--; // decrement BeepTime |
||
99 | if (beepTime & beepModulation) |
||
100 | beeperOn = 1; |
||
101 | else |
||
102 | beeperOn = 0; |
||
103 | } else { // beeper off if duration is over |
||
104 | beeperOn = 0; |
||
105 | beepModulation = BEEP_MODULATION_NONE; |
||
106 | } |
||
107 | |||
108 | if (beeperOn) { |
||
109 | // set speaker port to high. |
||
110 | PORTD |= (1 << PORTD5); // Speaker at PD5 |
||
111 | } else { // beeper is off |
||
112 | // set speaker port to low |
||
113 | PORTD &= ~(1 << PORTD5);// Speaker at PD5 |
||
114 | } |
||
115 | |||
116 | #ifdef USE_MK3MAG |
||
117 | // update compass value if this option is enabled in the settings |
||
118 | if (staticParams.bitConfig & CFG_COMPASS_ENABLED) { |
||
119 | MK3MAG_periodicTask(); // read out mk3mag pwm |
||
120 | } |
||
121 | #endif |
||
122 | } |
||
123 | |||
124 | // ----------------------------------------------------------------------- |
||
125 | uint16_t setDelay(uint16_t t) { |
||
126 | return (globalMillisClock + t - 1); |
||
127 | } |
||
128 | |||
129 | // ----------------------------------------------------------------------- |
||
130 | int8_t checkDelay(uint16_t t) { |
||
131 | return (((t - globalMillisClock) & 0x8000) >> 8); // check sign bit |
||
132 | } |
||
133 | |||
134 | // ----------------------------------------------------------------------- |
||
135 | void delay_ms(uint16_t w) { |
||
136 | uint16_t t_stop = setDelay(w); |
||
137 | while (!checkDelay(t_stop)) |
||
2132 | - | 138 | wdt_reset(); |
2108 | - | 139 | } |
140 | |||
141 | // ----------------------------------------------------------------------- |
||
142 | void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) { |
||
143 | uint16_t t_stop; |
||
144 | t_stop = setDelay(w); |
||
145 | while (!checkDelay(t_stop)) { |
||
2132 | - | 146 | wdt_reset(); |
2109 | - | 147 | debugOut.analog[30]++; |
2108 | - | 148 | if (sensorDataReady == ALL_DATA_READY) { |
149 | analog_update(); |
||
150 | startAnalogConversionCycle(); |
||
151 | } |
||
152 | } |
||
153 | if (stop) { |
||
154 | // Wait for new samples to get prepared but do not restart AD conversion after that! |
||
155 | // Caller MUST to that. |
||
2132 | - | 156 | while (!sensorDataReady != ALL_DATA_READY) wdt_reset(); |
2108 | - | 157 | } |
158 | } |
||
2132 | - | 159 | |
160 | #ifdef DO_PROFILE |
||
161 | void startProfileTimer(uint8_t timer) { |
||
162 | runningProfileTimers[timer] = global10kHzClock++; |
||
163 | } |
||
164 | |||
165 | void stopProfileTimer(uint8_t timer) { |
||
166 | int32_t t = global10kHzClock++ - runningProfileTimers[timer]; |
||
167 | profileTimers[timer] += t; |
||
168 | } |
||
169 | |||
170 | void debugProfileTimers(uint8_t index) { |
||
171 | for (uint8_t i=0; i<NUM_PROFILE_TIMERS; i++) { |
||
172 | uint16_t tenths = profileTimers[i] / 1000L; |
||
173 | debugOut.analog[i+index] = tenths; |
||
174 | } |
||
175 | uint16_t tenths = global10kHzClock / 1000L; |
||
176 | debugOut.analog[index + NUM_PROFILE_TIMERS] = tenths; |
||
177 | } |
||
178 | #endif; |