Subversion Repositories FlightCtrl

Rev

Rev 2088 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1612 dongfang 1
#include <inttypes.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include "eeprom.h"
5
#include "analog.h"
1963 - 6
#include "controlMixer.h"
1612 dongfang 7
 
1908 - 8
#include "timer0.h"
1869 - 9
#include "output.h"
1775 - 10
 
2052 - 11
#ifdef USE_MK3MAG
1612 dongfang 12
#include "mk3mag.h"
13
#endif
14
 
2060 - 15
volatile uint32_t globalMillisClock = 0;
1887 - 16
volatile uint8_t  runFlightControl = 0;
17
volatile uint16_t beepTime = 0;
1908 - 18
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
1612 dongfang 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) {
1872 - 30
  uint8_t sreg = SREG;
1612 dongfang 31
 
1872 - 32
  // disable all interrupts before reconfiguration
33
  cli();
1612 dongfang 34
 
1872 - 35
  // Configure speaker port as output.
1960 - 36
  if (boardRelease == 10) { // Speaker at PD2
1872 - 37
    DDRD |= (1 << DDD2);
38
    PORTD &= ~(1 << PORTD2);
39
  } else { // Speaker at PC7
40
    DDRC |= (1 << DDC7);
41
    PORTC &= ~(1 << PORTC7);
42
  }
1612 dongfang 43
 
1872 - 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));
1612 dongfang 47
 
1872 - 48
  // Timer/Counter 0 Control Register A
1612 dongfang 49
 
1872 - 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);
1612 dongfang 55
 
1872 - 56
  // Timer/Counter 0 Control Register B
2060 - 57
  // set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz
1872 - 58
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
2060 - 59
  // hence the timer overflow interrupt frequency is 2.5 MHz/256 = 9.765 kHz
1612 dongfang 60
 
1872 - 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);
1612 dongfang 64
 
1872 - 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
1612 dongfang 68
 
1872 - 69
  // init Timer/Counter 0 Register
70
  TCNT0 = 0;
1612 dongfang 71
 
1872 - 72
  // Timer/Counter 0 Interrupt Mask Register
73
  // enable timer overflow interrupt only
74
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
75
  TIMSK0 |= (1 << TOIE0);
1821 - 76
 
1872 - 77
  SREG = sreg;
1612 dongfang 78
}
79
 
80
/*****************************************************/
81
/*          Interrupt Routine of Timer 0             */
82
/*****************************************************/
2060 - 83
ISR(TIMER0_OVF_vect) { // 9765.625 Hz
1872 - 84
  static uint8_t cnt_1ms = 1, cnt = 0;
2044 - 85
  uint8_t beeperOn = 0;
1821 - 86
 
1612 dongfang 87
#ifdef USE_NAVICTRL
1872 - 88
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
1612 dongfang 89
#endif
1821 - 90
 
1872 - 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)
1955 - 96
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
1872 - 97
      else
1955 - 98
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
1872 - 99
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
100
    }
2060 - 101
    globalMillisClock++; // increment millisecond counter
1872 - 102
  }
1821 - 103
 
1872 - 104
  // beeper on if duration is not over
1887 - 105
  if (beepTime) {
106
    beepTime--; // decrement BeepTime
107
    if (beepTime & beepModulation)
2044 - 108
      beeperOn = 1;
1872 - 109
    else
2044 - 110
      beeperOn = 0;
1872 - 111
  } else { // beeper off if duration is over
2044 - 112
    beeperOn = 0;
1908 - 113
    beepModulation = BEEP_MODULATION_NONE;
1872 - 114
  }
1821 - 115
 
2044 - 116
  if (beeperOn) {
1872 - 117
    // set speaker port to high.
1960 - 118
    if (boardRelease == 10)
1872 - 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
1960 - 124
    if (boardRelease == 10)
1872 - 125
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
126
    else
127
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
128
  }
1821 - 129
 
2052 - 130
#ifdef USE_MK3MAG
1872 - 131
  // update compass value if this option is enabled in the settings
2088 - 132
  if (staticParams.bitConfig & CFG_COMPASS_ENABLED) {
2039 - 133
    MK3MAG_periodicTask(); // read out mk3mag pwm
2052 - 134
  }
1612 dongfang 135
#endif
136
}
137
 
138
// -----------------------------------------------------------------------
1887 - 139
uint16_t setDelay(uint16_t t) {
2060 - 140
  return (globalMillisClock + t - 1);
1612 dongfang 141
}
142
 
143
// -----------------------------------------------------------------------
1887 - 144
int8_t checkDelay(uint16_t t) {
2060 - 145
  return (((t - globalMillisClock) & 0x8000) >> 8); // check sign bit
1612 dongfang 146
}
147
 
148
// -----------------------------------------------------------------------
1887 - 149
void delay_ms(uint16_t w) {
150
  uint16_t t_stop = setDelay(w);
151
  while (!checkDelay(t_stop))
1872 - 152
    ;
1612 dongfang 153
}
154
 
155
// -----------------------------------------------------------------------
2015 - 156
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
1872 - 157
  uint16_t t_stop;
1887 - 158
  t_stop = setDelay(w);
159
  while (!checkDelay(t_stop)) {
2015 - 160
        if (analogDataReady) {
161
          analog_update();
162
          startAnalogConversionCycle();
163
        }
1872 - 164
  }
2015 - 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
  }
1612 dongfang 170
}