Subversion Repositories FlightCtrl

Rev

Rev 1910 | Rev 2124 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1910 Rev 2099
Line 1... Line 1...
1
#include <inttypes.h>
1
#include <inttypes.h>
2
#include <avr/io.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
3
#include <avr/interrupt.h>
4
#include "eeprom.h"
4
#include "eeprom.h"
5
#include "analog.h"
5
#include "analog.h"
-
 
6
#include "controlMixer.h"
Line 6... Line -...
6
 
-
 
7
// for debugging!
7
 
8
#include "uart0.h"
8
#include "timer0.h"
Line 9... Line 9...
9
#include "output.h"
9
#include "output.h"
10
 
10
 
11
#ifdef USE_MK3MAG
11
#ifdef USE_MK3MAG
Line 12... Line 12...
12
#include "mk3mag.h"
12
#include "mk3mag.h"
13
#endif
13
#endif
14
 
-
 
15
volatile uint16_t millisecondsCount = 0;
14
 
16
volatile uint8_t  runFlightControl = 0;
15
volatile uint32_t globalMillisClock = 0;
Line 17... Line 16...
17
volatile uint16_t cntKompass = 0;
16
volatile uint8_t  runFlightControl = 0;
18
volatile uint16_t beepTime = 0;
17
volatile uint16_t beepTime = 0;
19
volatile uint16_t beepModulation = 0xFFFF;
18
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
Line 32... Line 31...
32
 
31
 
33
  // disable all interrupts before reconfiguration
32
  // disable all interrupts before reconfiguration
Line 34... Line 33...
34
  cli();
33
  cli();
35
 
-
 
36
  // Configure speaker port as output.
34
 
37
 
35
  // Configure speaker port as output.
38
  if (BoardRelease == 10) { // Speaker at PD2
36
  if (boardRelease == 10) { // Speaker at PD2
39
    DDRD |= (1 << DDD2);
37
    DDRD |= (1 << DDD2);
40
    PORTD &= ~(1 << PORTD2);
38
    PORTD &= ~(1 << PORTD2);
41
  } else { // Speaker at PC7
39
  } else { // Speaker at PC7
Line 54... Line 52...
54
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
52
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
55
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
53
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
56
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
54
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
Line 57... Line 55...
57
 
55
 
58
  // Timer/Counter 0 Control Register B
-
 
59
 
56
  // Timer/Counter 0 Control Register B
60
  // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
57
  // set clock divider for timer 0 to SYSCLOCK/8 = 20MHz/8 = 2.5MHz
61
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
58
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
Line 62... Line 59...
62
  // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
59
  // hence the timer overflow interrupt frequency is 2.5 MHz/256 = 9.765 kHz
63
 
60
 
64
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
61
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
Line 81... Line 78...
81
}
78
}
Line 82... Line 79...
82
 
79
 
83
/*****************************************************/
80
/*****************************************************/
84
/*          Interrupt Routine of Timer 0             */
81
/*          Interrupt Routine of Timer 0             */
85
/*****************************************************/
82
/*****************************************************/
86
ISR(TIMER0_OVF_vect)
-
 
87
{ // 9765.625 Hz
83
ISR(TIMER0_OVF_vect) { // 9765.625 Hz
88
  static uint8_t cnt_1ms = 1, cnt = 0;
84
  static uint8_t cnt_1ms = 1, cnt = 0;
Line 89... Line 85...
89
  uint8_t beeper_On = 0;
85
  uint8_t beeperOn = 0;
90
 
86
 
91
#ifdef USE_NAVICTRL
87
#ifdef USE_NAVICTRL
Line 92... Line 88...
92
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
88
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
93
#endif
89
#endif
94
 
90
 
95
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
91
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
96
    cnt = 9;
92
    cnt = 9;
97
    cnt_1ms ^= 1;
93
    cnt_1ms ^= 1;
98
    if (!cnt_1ms) {
94
    if (!cnt_1ms) {
99
      if (runFlightControl == 1)
95
      if (runFlightControl == 1)
100
        DebugOut.Digital[1] |= DEBUG_MAINLOOP_TIMER;
96
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
101
      else
97
      else
102
        DebugOut.Digital[1] &= ~DEBUG_MAINLOOP_TIMER;
98
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
103
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
99
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
Line 104... Line 100...
104
    }
100
    }
105
    millisecondsCount++; // increment millisecond counter
101
    globalMillisClock++; // increment millisecond counter
106
  }
102
  }
107
 
103
 
108
  // beeper on if duration is not over
104
  // beeper on if duration is not over
109
  if (beepTime) {
105
  if (beepTime) {
110
    beepTime--; // decrement BeepTime
106
    beepTime--; // decrement BeepTime
111
    if (beepTime & beepModulation)
107
    if (beepTime & beepModulation)
112
      beeper_On = 1;
108
      beeperOn = 1;
113
    else
109
    else
114
      beeper_On = 0;
110
      beeperOn = 0;
Line 115... Line -...
115
  } else { // beeper off if duration is over
-
 
116
    beeper_On = 0;
111
  } else { // beeper off if duration is over
117
    beepModulation = 0xFFFF;
112
    beeperOn = 0;
118
  }
113
    beepModulation = BEEP_MODULATION_NONE;
119
 
114
  }
120
  // if beeper is on
115
 
121
  if (beeper_On) {
116
  if (beeperOn) {
122
    // set speaker port to high.
117
    // set speaker port to high.
123
    if (BoardRelease == 10)
118
    if (boardRelease == 10)
124
      PORTD |= (1 << PORTD2); // Speaker at PD2
119
      PORTD |= (1 << PORTD2); // Speaker at PD2
125
    else
120
    else
126
      PORTC |= (1 << PORTC7); // Speaker at PC7
121
      PORTC |= (1 << PORTC7); // Speaker at PC7
127
  } else { // beeper is off
122
  } else { // beeper is off
128
    // set speaker port to low
123
    // set speaker port to low
Line 129... Line -...
129
    if (BoardRelease == 10)
-
 
130
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
-
 
131
    else
-
 
132
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
124
    if (boardRelease == 10)
-
 
125
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
-
 
126
    else
133
  }
127
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
134
 
-
 
135
#ifndef USE_NAVICTRL
128
  }
136
  // update compass value if this option is enabled in the settings
129
 
137
  if (staticParams.GlobalConfig & (CFG_COMPASS_ACTIVE | CFG_GPS_ACTIVE)) {
130
#ifdef USE_MK3MAG
Line 138... Line 131...
138
#ifdef USE_MK3MAG
131
  // update compass value if this option is enabled in the settings
139
    MK3MAG_Update(); // read out mk3mag pwm
132
  if (staticParams.bitConfig & CFG_COMPASS_ENABLED) {
140
#endif
133
    MK3MAG_periodicTask(); // read out mk3mag pwm
141
  }
134
  }
Line 142... Line 135...
142
#endif
135
#endif
143
}
136
}
144
 
137
 
145
// -----------------------------------------------------------------------
138
// -----------------------------------------------------------------------
Line 146... Line 139...
146
uint16_t setDelay(uint16_t t) {
139
uint16_t setDelay(uint16_t t) {
147
  return (millisecondsCount + t - 1);
140
  return (globalMillisClock + t - 1);
148
}
141
}
149
 
142
 
150
// -----------------------------------------------------------------------
143
// -----------------------------------------------------------------------
151
int8_t checkDelay(uint16_t t) {
144
int8_t checkDelay(uint16_t t) {
Line 152... Line 145...
152
  return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit
145
  return (((t - globalMillisClock) & 0x8000) >> 8); // check sign bit
153
}
146
}
154
 
147
 
155
// -----------------------------------------------------------------------
148
// -----------------------------------------------------------------------
156
void delay_ms(uint16_t w) {
149
void delay_ms(uint16_t w) {
157
  uint16_t t_stop = setDelay(w);
150
  uint16_t t_stop = setDelay(w);
158
  while (!checkDelay(t_stop))
151
  while (!checkDelay(t_stop))
159
    ;
152
    ;
160
}
153
}
-
 
154
 
-
 
155
// -----------------------------------------------------------------------
-
 
156
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
-
 
157
  uint16_t t_stop;
-
 
158
  t_stop = setDelay(w);
161
 
159
  while (!checkDelay(t_stop)) {
162
// -----------------------------------------------------------------------
160
        if (analogDataReady) {