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