Subversion Repositories FlightCtrl

Rev

Rev 1910 | Rev 2124 | 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
 
11
#ifdef USE_MK3MAG
12
#include "mk3mag.h"
13
#endif
14
 
2099 - 15
volatile uint32_t globalMillisClock = 0;
1910 - 16
volatile uint8_t  runFlightControl = 0;
17
volatile uint16_t beepTime = 0;
2099 - 18
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
1910 - 19
 
20
#ifdef USE_NAVICTRL
21
volatile uint8_t SendSPI = 0;
22
#endif
23
 
24
/*****************************************************
25
 * Initialize Timer 0                  
26
 *****************************************************/
27
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
28
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
29
void timer0_init(void) {
30
  uint8_t sreg = SREG;
31
 
32
  // disable all interrupts before reconfiguration
33
  cli();
34
 
35
  // Configure speaker port as output.
2099 - 36
  if (boardRelease == 10) { // Speaker at PD2
1910 - 37
    DDRD |= (1 << DDD2);
38
    PORTD &= ~(1 << PORTD2);
39
  } else { // Speaker at PC7
40
    DDRC |= (1 << DDC7);
41
    PORTC &= ~(1 << PORTC7);
42
  }
43
 
44
  // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
45
  DDRB |= (1 << DDB4) | (1 << DDB3);
46
  PORTB &= ~((1 << PORTB4) | (1 << PORTB3));
47
 
48
  // Timer/Counter 0 Control Register A
49
 
50
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
51
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
52
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
53
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
54
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
55
 
56
  // Timer/Counter 0 Control Register B
2099 - 57
  // set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz
1910 - 58
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
2099 - 59
  // hence the timer overflow interrupt frequency is 2.5 MHz/256 = 9.765 kHz
1910 - 60
 
61
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
62
  TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
63
  TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
64
 
65
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
66
  OCR0A = 0; // for PB3
67
  OCR0B = 120; // for PB4
68
 
69
  // init Timer/Counter 0 Register
70
  TCNT0 = 0;
71
 
72
  // Timer/Counter 0 Interrupt Mask Register
73
  // enable timer overflow interrupt only
74
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
75
  TIMSK0 |= (1 << TOIE0);
76
 
77
  SREG = sreg;
78
}
79
 
80
/*****************************************************/
81
/*          Interrupt Routine of Timer 0             */
82
/*****************************************************/
2099 - 83
ISR(TIMER0_OVF_vect) { // 9765.625 Hz
1910 - 84
  static uint8_t cnt_1ms = 1, cnt = 0;
2099 - 85
  uint8_t beeperOn = 0;
1910 - 86
 
87
#ifdef USE_NAVICTRL
88
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
89
#endif
90
 
91
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
92
    cnt = 9;
93
    cnt_1ms ^= 1;
94
    if (!cnt_1ms) {
95
      if (runFlightControl == 1)
2099 - 96
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
1910 - 97
      else
2099 - 98
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
1910 - 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
}