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