Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1612 dongfang 1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2
// + Copyright (c) 04.2007 Holger Buss
1963 - 3
// + Nur f�r den privaten Gebrauch
1612 dongfang 4
// + www.MikroKopter.com
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1963 - 6
// + Es gilt f�r das gesamte Projekt (Hardware, Software, Bin�rfiles, Sourcecode und Dokumentation),
7
// + dass eine Nutzung (auch auszugsweise) nur f�r den privaten (nicht-kommerziellen) Gebrauch zul�ssig ist.
1612 dongfang 8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
1963 - 10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Best�ckung und Verkauf von Platinen oder Baus�tzen,
1612 dongfang 11
// + Verkauf von Luftbildaufnahmen, usw.
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1963 - 13
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder ver�ffentlicht,
14
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright m�ssen dann beiliegen
1612 dongfang 15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
1963 - 17
// + auf anderen Webseiten oder sonstigen Medien ver�ffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
1612 dongfang 18
// + eindeutig als Ursprung verlinkt werden
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1963 - 20
// + Keine Gew�hr auf Fehlerfreiheit, Vollst�ndigkeit oder Funktion
1612 dongfang 21
// + Benutzung auf eigene Gefahr
1963 - 22
// + Wir �bernehmen keinerlei Haftung f�r direkte oder indirekte Personen- oder Sachsch�den
1612 dongfang 23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
1963 - 25
// + mit unserer Zustimmung zul�ssig
1612 dongfang 26
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
27
// + Die Funktion printf_P() unterliegt ihrer eigenen Lizenz und ist hiervon nicht betroffen
28
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
29
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
30
// + this list of conditions and the following disclaimer.
31
// +   * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
32
// +     from this software without specific prior written permission.
33
// +   * The use of this project (hardware, software, binary files, sources and documentation) is only permittet
34
// +     for non-commercial use (directly or indirectly)
1868 - 35
// +     Commercial use (for example: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
1612 dongfang 36
// +     with our written permission
37
// +   * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
38
// +     clearly linked as origin
39
// +   * porting to systems other than hardware from www.mikrokopter.de is not allowed
40
// +  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
41
// +  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42
// +  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
43
// +  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
44
// +  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
45
// +  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
46
// +  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
47
// +  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN// +  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48
// +  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
49
// +  POSSIBILITY OF SUCH DAMAGE.
50
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
#include <inttypes.h>
52
#include <avr/io.h>
53
#include <avr/interrupt.h>
54
#include "eeprom.h"
55
#include "analog.h"
1963 - 56
#include "controlMixer.h"
1612 dongfang 57
 
1908 - 58
#include "timer0.h"
1869 - 59
#include "output.h"
1775 - 60
 
2052 - 61
#ifdef USE_MK3MAG
1612 dongfang 62
#include "mk3mag.h"
63
#endif
64
 
1887 - 65
volatile uint16_t millisecondsCount = 0;
66
volatile uint8_t  runFlightControl = 0;
1612 dongfang 67
volatile uint16_t cntKompass = 0;
1887 - 68
volatile uint16_t beepTime = 0;
1908 - 69
volatile uint16_t beepModulation = BEEP_MODULATION_NONE;
1612 dongfang 70
 
71
#ifdef USE_NAVICTRL
72
volatile uint8_t SendSPI = 0;
73
#endif
74
 
75
/*****************************************************
76
 * Initialize Timer 0                  
77
 *****************************************************/
78
// timer 0 is used for the PWM generation to control the offset voltage at the air pressure sensor
79
// Its overflow interrupt routine is used to generate the beep signal and the flight control motor update rate
80
void timer0_init(void) {
1872 - 81
  uint8_t sreg = SREG;
1612 dongfang 82
 
1872 - 83
  // disable all interrupts before reconfiguration
84
  cli();
1612 dongfang 85
 
1872 - 86
  // Configure speaker port as output.
1612 dongfang 87
 
1960 - 88
  if (boardRelease == 10) { // Speaker at PD2
1872 - 89
    DDRD |= (1 << DDD2);
90
    PORTD &= ~(1 << PORTD2);
91
  } else { // Speaker at PC7
92
    DDRC |= (1 << DDC7);
93
    PORTC &= ~(1 << PORTC7);
94
  }
1612 dongfang 95
 
1872 - 96
  // set PB3 and PB4 as output for the PWM used as offset for the pressure sensor
97
  DDRB |= (1 << DDB4) | (1 << DDB3);
98
  PORTB &= ~((1 << PORTB4) | (1 << PORTB3));
1612 dongfang 99
 
1872 - 100
  // Timer/Counter 0 Control Register A
1612 dongfang 101
 
1872 - 102
  // Waveform Generation Mode is Fast PWM (Bits WGM02 = 0, WGM01 = 1, WGM00 = 1)
103
  // Clear OC0A on Compare Match, set OC0A at BOTTOM, noninverting PWM (Bits COM0A1 = 1, COM0A0 = 0)
104
  // Clear OC0B on Compare Match, set OC0B at BOTTOM, (Bits COM0B1 = 1, COM0B0 = 0)
105
  TCCR0A &= ~((1 << COM0A0) | (1 << COM0B0));
106
  TCCR0A |= (1 << COM0A1) | (1 << COM0B1) | (1 << WGM01) | (1 << WGM00);
1612 dongfang 107
 
1872 - 108
  // Timer/Counter 0 Control Register B
1612 dongfang 109
 
1872 - 110
  // set clock divider for timer 0 to SYSKLOCK/8 = 20MHz / 8 = 2.5MHz
111
  // i.e. the timer increments from 0x00 to 0xFF with an update rate of 2.5 MHz
112
  // hence the timer overflow interrupt frequency is 2.5 MHz / 256 = 9.765 kHz
1612 dongfang 113
 
1872 - 114
  // divider 8 (Bits CS02 = 0, CS01 = 1, CS00 = 0)
115
  TCCR0B &= ~((1 << FOC0A) | (1 << FOC0B) | (1 << WGM02));
116
  TCCR0B = (TCCR0B & 0xF8) | (0 << CS02) | (1 << CS01) | (0 << CS00);
1612 dongfang 117
 
1872 - 118
  // initialize the Output Compare Register A & B used for PWM generation on port PB3 & PB4
119
  OCR0A = 0; // for PB3
120
  OCR0B = 120; // for PB4
1612 dongfang 121
 
1872 - 122
  // init Timer/Counter 0 Register
123
  TCNT0 = 0;
1612 dongfang 124
 
1872 - 125
  // Timer/Counter 0 Interrupt Mask Register
126
  // enable timer overflow interrupt only
127
  TIMSK0 &= ~((1 << OCIE0B) | (1 << OCIE0A));
128
  TIMSK0 |= (1 << TOIE0);
1821 - 129
 
1872 - 130
  SREG = sreg;
1612 dongfang 131
}
132
 
133
/*****************************************************/
134
/*          Interrupt Routine of Timer 0             */
135
/*****************************************************/
1821 - 136
ISR(TIMER0_OVF_vect)
137
{ // 9765.625 Hz
1872 - 138
  static uint8_t cnt_1ms = 1, cnt = 0;
2044 - 139
  uint8_t beeperOn = 0;
1821 - 140
 
1612 dongfang 141
#ifdef USE_NAVICTRL
1872 - 142
  if(SendSPI) SendSPI--; // if SendSPI is 0, the transmit of a byte via SPI bus to and from The Navicontrol is done
1612 dongfang 143
#endif
1821 - 144
 
1872 - 145
  if (!cnt--) { // every 10th run (9.765625kHz/10 = 976.5625Hz)
146
    cnt = 9;
147
    cnt_1ms ^= 1;
148
    if (!cnt_1ms) {
149
      if (runFlightControl == 1)
1955 - 150
        debugOut.digital[1] |= DEBUG_MAINLOOP_TIMER;
1872 - 151
      else
1955 - 152
        debugOut.digital[1] &= ~DEBUG_MAINLOOP_TIMER;
1872 - 153
      runFlightControl = 1; // every 2nd run (976.5625 Hz/2 = 488.28125 Hz)
154
    }
1887 - 155
    millisecondsCount++; // increment millisecond counter
1872 - 156
  }
1821 - 157
 
1872 - 158
  // beeper on if duration is not over
1887 - 159
  if (beepTime) {
160
    beepTime--; // decrement BeepTime
161
    if (beepTime & beepModulation)
2044 - 162
      beeperOn = 1;
1872 - 163
    else
2044 - 164
      beeperOn = 0;
1872 - 165
  } else { // beeper off if duration is over
2044 - 166
    beeperOn = 0;
1908 - 167
    beepModulation = BEEP_MODULATION_NONE;
1872 - 168
  }
1821 - 169
 
2044 - 170
  if (beeperOn) {
1872 - 171
    // set speaker port to high.
1960 - 172
    if (boardRelease == 10)
1872 - 173
      PORTD |= (1 << PORTD2); // Speaker at PD2
174
    else
175
      PORTC |= (1 << PORTC7); // Speaker at PC7
176
  } else { // beeper is off
177
    // set speaker port to low
1960 - 178
    if (boardRelease == 10)
1872 - 179
      PORTD &= ~(1 << PORTD2);// Speaker at PD2
180
    else
181
      PORTC &= ~(1 << PORTC7);// Speaker at PC7
182
  }
1821 - 183
 
2052 - 184
#ifdef USE_MK3MAG
1872 - 185
  // update compass value if this option is enabled in the settings
2052 - 186
  if (staticParams.bitConfig & (CFG_COMPASS_ENABLED | CFG_GPS_ENABLED)) {
2039 - 187
    MK3MAG_periodicTask(); // read out mk3mag pwm
2052 - 188
  }
1612 dongfang 189
#endif
190
}
191
 
192
// -----------------------------------------------------------------------
1887 - 193
uint16_t setDelay(uint16_t t) {
194
  return (millisecondsCount + t - 1);
1612 dongfang 195
}
196
 
197
// -----------------------------------------------------------------------
1887 - 198
int8_t checkDelay(uint16_t t) {
199
  return (((t - millisecondsCount) & 0x8000) >> 8); // check sign bit
1612 dongfang 200
}
201
 
202
// -----------------------------------------------------------------------
1887 - 203
void delay_ms(uint16_t w) {
204
  uint16_t t_stop = setDelay(w);
205
  while (!checkDelay(t_stop))
1872 - 206
    ;
1612 dongfang 207
}
208
 
209
// -----------------------------------------------------------------------
2015 - 210
void delay_ms_with_adc_measurement(uint16_t w, uint8_t stop) {
1872 - 211
  uint16_t t_stop;
1887 - 212
  t_stop = setDelay(w);
213
  while (!checkDelay(t_stop)) {
2015 - 214
        if (analogDataReady) {
215
          analog_update();
216
          startAnalogConversionCycle();
217
        }
1872 - 218
  }
2015 - 219
  if (stop) {
220
  // Wait for new samples to get prepared but do not restart AD conversion after that!
221
  // Caller MUST to that.
222
        while (!analogDataReady);
223
  }
1612 dongfang 224
}