Subversion Repositories FlightCtrl

Rev

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