Subversion Repositories FlightCtrl

Rev

Rev 2124 | Rev 2132 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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