Subversion Repositories FlightCtrl

Rev

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