Subversion Repositories FlightCtrl

Rev

Rev 2132 | Details | Compare with Previous | Last modification | View Log | RSS feed

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