Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
2108 - 1
#include <inttypes.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include "eeprom.h"
5
#include "analog.h"
6
#include "controlMixer.h"
7
 
8
#include "timer0.h"
9
#include "output.h"
10
 
11
#ifdef USE_MK3MAG
12
#include "mk3mag.h"
13
#endif
14
 
15
volatile uint32_t globalMillisClock = 0;
16
volatile uint8_t  runFlightControl = 0;
17
volatile uint16_t beepTime = 0;
18
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
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
 
2109 - 35
  // Configure speaker port as output and also the diags pin D2
36
  DDRD |= ((1 << DDD5) | (1 << DDD2));
37
  PORTD &= ~((1 << PORTD5) | (1 << DDD2));
2108 - 38
 
39
  // Timer/Counter 0 Control Register A
40
 
41
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
42
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
43
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
44
  // TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
45
  // TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
46
 
47
  // Timer/Counter 0 Control Register B
48
  // set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz
49
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
50
  // hence the timer overflow interrupt frequency is 2.5 MHz/256 = 9.765 kHz
51
 
52
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
53
  TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
54
  TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
55
 
56
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
57
  OCR0A = 0;    // for PB3
58
  OCR0B = 120;  // for PB4
59
 
60
  // init Timer/Counter 0 Register
61
  TCNT0 = 0;
62
 
63
  // Timer/Counter 0 Interrupt Mask Register
64
  // enable timer overflow interrupt only
65
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
66
  TIMSK0 |= (1 << TOIE0);
67
 
68
  SREG = sreg;
69
}
70
 
71
/*****************************************************/
72
/*          Interrupt Routine of Timer 0             */
73
/*****************************************************/
74
ISR(TIMER0_OVF_vect) { // 9765.625 Hz
75
  static uint8_t cnt_1ms = 1, cnt = 0;
76
  uint8_t beeperOn = 0;
77
 
78
#ifdef USE_NAVICTRL
79
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
80
#endif
81
 
82
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
83
    cnt = 9;
84
    cnt_1ms ^= 1;
85
    if (!cnt_1ms) {
86
      if (runFlightControl == 1)
87
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
88
      else
89
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
90
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
91
    }
92
    globalMillisClock++; // increment millisecond counter
93
  }
94
 
95
  // beeper on if duration is not over
96
  if (beepTime) {
97
    beepTime--; // decrement BeepTime
98
    if (beepTime & beepModulation)
99
      beeperOn = 1;
100
    else
101
      beeperOn = 0;
102
  } else { // beeper off if duration is over
103
    beeperOn = 0;
104
    beepModulation = BEEP_MODULATION_NONE;
105
  }
106
 
107
  if (beeperOn) {
108
    // set speaker port to high.
109
      PORTD |= (1 << PORTD5); // Speaker at PD5
110
  } else { // beeper is off
111
    // set speaker port to low
112
      PORTD &= ~(1 << PORTD5);// Speaker at PD5
113
  }
114
 
115
#ifdef USE_MK3MAG
116
  // update compass value if this option is enabled in the settings
117
  if (staticParams.bitConfig & CFG_COMPASS_ENABLED) {
118
    MK3MAG_periodicTask(); // read out mk3mag pwm
119
  }
120
#endif
121
}
122
 
123
// -----------------------------------------------------------------------
124
uint16_t setDelay(uint16_t t) {
125
  return (globalMillisClock + t - 1);
126
}
127
 
128
// -----------------------------------------------------------------------
129
int8_t checkDelay(uint16_t t) {
130
  return (((t - globalMillisClock) & 0x8000) >> 8); // check sign bit
131
}
132
 
133
// -----------------------------------------------------------------------
134
void delay_ms(uint16_t w) {
135
  uint16_t t_stop = setDelay(w);
136
  while (!checkDelay(t_stop))
137
    ;
138
}
139
 
140
// -----------------------------------------------------------------------
141
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
142
  uint16_t t_stop;
143
  t_stop = setDelay(w);
144
  while (!checkDelay(t_stop)) {
2109 - 145
    debugOut.analog[30]++;
2108 - 146
        if (sensorDataReady == ALL_DATA_READY) {
147
          analog_update();
148
          startAnalogConversionCycle();
149
        }
150
  }
151
  if (stop) {
152
  // Wait for new samples to get prepared but do not restart AD conversion after that!
153
  // Caller MUST to that.
2109 - 154
        // while (!sensorDataReady != ALL_DATA_READY);
2108 - 155
  }
156
}