Subversion Repositories FlightCtrl

Rev

Rev 2099 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1910 - 1
#include <avr/boot.h>
2
#include <avr/io.h>
3
#include <avr/interrupt.h>
4
#include <util/delay.h>
5
 
6
#include "timer0.h"
7
#include "timer2.h"
8
#include "uart0.h"
9
#include "output.h"
10
#include "attitude.h"
11
#include "flight.h"
12
#include "controlMixer.h"
13
#include "rc.h"
14
#include "analog.h"
15
#include "configuration.h"
16
#include "twimaster.h"
17
#ifdef USE_NAVICTRL
18
#include "spi.h"
19
#endif
20
#ifdef USE_MK3MAG
21
#include "mk3mag.h"
22
#endif
23
#include "eeprom.h"
24
 
25
int16_t main(void) {
26
  uint16_t timer;
27
 
28
  // disable interrupts global
29
  cli();
30
 
31
  // analyze hardware environment
32
  CPUType = getCPUType();
33
  BoardRelease = getBoardRelease();
34
 
35
  // disable watchdog
36
  MCUSR &= ~(1 << WDRF);
37
  WDTCSR |= (1 << WDCE) | (1 << WDE);
38
  WDTCSR = 0;
39
 
40
  // PPM_in[CH_THROTTLE] = 0;
41
  // Why??? They are already initialized to 0.
42
  // stickPitch = stickRoll = stickYaw = 0;
43
 
44
  RED_OFF;
45
 
46
  // initalize modules
47
  output_init();
48
  timer0_init();
49
  usart0_Init();
50
  RC_Init();
51
  analog_init();
52
  I2C_init();
53
#ifdef USE_NAVICTRL
54
  SPI_MasterInit();
55
#endif
56
#ifdef USE_MK3MAG
57
  MK3MAG_Init();
58
#endif
59
 
60
  // enable interrupts global
61
  sei();
62
 
63
  // Parameter Set handling
64
  ParamSet_Init();
65
 
66
  // Wait for a short time (otherwise the RC channel check won't work below)
67
  // timer = SetDelay(500);
68
  // while(!CheckDelay(timer));
69
 
70
  // Instead, while away the time by flashing the 2 outputs:
71
  // First J16, then J17. Makes it easier to see which is which.
72
  timer = setDelay(500);
73
  OUTPUT_SET(0,1);
74
  GRN_OFF;
75
  RED_ON;
76
  while (!checkDelay(timer))
77
    ;
78
 
79
  OUTPUT_SET(0,0);
80
 
81
  timer = setDelay(500);
82
  while (!checkDelay(timer))
83
    ;
84
 
85
  OUTPUT_SET(1,1);
86
  RED_OFF;
87
  GRN_ON;
88
  timer = setDelay(500);
89
  while (!checkDelay(timer))
90
    ;
91
 
92
  timer = setDelay(500);
93
  while (!checkDelay(timer))
94
    ;
95
 
96
  OUTPUT_SET(1,0);
97
 
98
  beep(2000);
99
 
100
  timer = setDelay(4000);
101
  while (!checkDelay(timer))
102
    ;
103
 
104
  controlMixer_setNeutral();
105
  // Cal. attitude sensors and reset integrals.
106
  attitude_setNeutral();
107
  // Init flight parameters
108
  flight_setNeutral();
109
 
110
  // RED_OFF;
111
 
112
  beep(1000);
113
  timer2_init();
114
 
115
  I2CTimeout = 5000;
116
 
117
  while (1) {
118
    if (runFlightControl) { // control interval
119
      runFlightControl = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
120
      if (!analogDataReady) {
121
        DebugOut.Digital[0] |= DEBUG_MAINLOOP_TIMER;
122
      } else {
123
        DebugOut.Digital[0] &= ~DEBUG_MAINLOOP_TIMER;
124
 
125
        J4HIGH;
126
        flight_control();
127
        J4LOW;
128
 
129
        // Allow Serial Data Transmit if motors must not updated or motors are not running
130
        if (!runFlightControl || !(MKFlags & MKFLAG_MOTOR_RUN)) {
131
          usart0_TransmitTxData();
132
        }
133
 
134
        usart0_ProcessRxData();
135
 
136
        if (checkDelay(timer)) {
137
          if (UBat <= UBAT_AT_5V) {
138
            // Do nothing. The voltage on the input side of the regulator is <5V;
139
            // we must be running off USB power. Keep it quiet.
140
          } else if (UBat < staticParams.LowVoltageWarning) {
141
            beepBatteryAlarm();
142
          }
143
 
144
#ifdef USE_NAVICTRL
145
          SPI_StartTransmitPacket();
146
          SendSPI = 4;
147
#endif
148
          timer = setDelay(20); // every 20 ms
149
        }
150
        output_update();
151
      }
152
 
153
#ifdef USE_NAVICTRL
154
      if(!SendSPI) {
155
        // SendSPI is decremented in timer0.c with a rate of 9.765 kHz.
156
        // within the SPI_TransmitByte() routine the value is set to 4.
157
        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
158
        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
159
        SPI_TransmitByte();
160
      }
161
#endif
162
    }
163
  }
164
  return (1);
165
}