Subversion Repositories FlightCtrl

Rev

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

Rev 1179 Rev 1180
Line 51... Line 51...
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
52
#include <avr/boot.h>
52
#include <avr/boot.h>
Line 53... Line 53...
53
 
53
 
54
#include <avr/io.h>
54
#include <avr/io.h>
-
 
55
#include <avr/interrupt.h>
Line 55... Line 56...
55
#include <avr/interrupt.h>
56
#include <util/delay.h>
56
 
57
 
57
#include "main.h"
58
#include "main.h"
58
#include "timer0.h"
59
#include "timer0.h"
59
#include "timer2.h"
-
 
60
#include "uart.h"
60
#include "timer2.h"
61
#if defined (__AVR_ATmega644P__)
-
 
62
#include "uart1.h"
61
#include "uart0.h"
63
#endif
62
#include "uart1.h"
64
#include "led.h"
63
#include "led.h"
65
#include "menu.h"
64
#include "menu.h"
66
#include "fc.h"
65
#include "fc.h"
Line 76... Line 75...
76
#ifdef USE_MK3MAG
75
#ifdef USE_MK3MAG
77
#include "mk3mag.h"
76
#include "mk3mag.h"
78
#endif
77
#endif
79
#include "twimaster.h"
78
#include "twimaster.h"
80
#include "eeprom.h"
79
#include "eeprom.h"
81
#include "_Settings.h"
-
 
Line 82... Line 80...
82
 
80
 
-
 
81
 
-
 
82
uint8_t BoardRelease = 10;
-
 
83
uint8_t CPUType = ATMEGA644;
-
 
84
 
-
 
85
 
-
 
86
uint8_t GetCPUType(void)
-
 
87
{   // works only after reset or power on when the registers have default values
-
 
88
        uint8_t CPUType = ATMEGA644;
-
 
89
        if( (UCSR1A == 0x20) && (UCSR1C == 0x06) ) CPUType = ATMEGA644P;  // initial Values for 644P after reset
Line 83... Line -...
83
 
-
 
84
uint8_t BoardRelease = 10;
-
 
85
 
90
        return CPUType;
86
 
-
 
87
//############################################################################
91
}
88
//Hauptprogramm
92
 
-
 
93
 
Line 89... Line 94...
89
int main (void)
94
uint8_t GetBoardRelease(void)
90
//############################################################################
-
 
-
 
95
{
Line 91... Line -...
91
{
-
 
92
        unsigned int timer;
-
 
93
 
-
 
94
        // disable interrupts global
96
        uint8_t BoardRelease = 10;
95
        cli();
-
 
96
 
97
        // the board release is coded via the pull up or down the 2 status LED
97
        // get board release
98
 
98
    DDRB  = 0x00;
-
 
99
    PORTB = 0x00;
-
 
100
    for(timer = 0; timer < 1000; timer++); // make some delay
-
 
101
    if(PINB & (1<<PINB0))
99
    PORTB &= ~((1 << PORTB1)|(1 << PORTB0)); // set tristate
-
 
100
    DDRB  &= ~((1 << DDB0)|(1 << DDB0)); // set port direction as input
-
 
101
 
-
 
102
        _delay_loop_2(1000); // make some delay
-
 
103
 
-
 
104
    switch( PINB & ((1<<PINB1)|(1<<PINB0)) )
-
 
105
    {
-
 
106
                case 0x00:
102
    {
107
                        BoardRelease = 10; // 1.0
-
 
108
                        break;
-
 
109
                case 0x01:
103
                if(PINB & (1<<PINB1)) BoardRelease = 13;
110
                        BoardRelease = 11; // 1.1 or 1.2
-
 
111
                        break;
-
 
112
                case 0x02:
-
 
113
                        BoardRelease = 20; // 2.0
104
                else BoardRelease = 11; // 12 is the same hardware
114
                        break;
105
        }
-
 
106
    else
115
                case 0x03:
107
    {
116
                        BoardRelease = 13; // 1.3
108
                if(PINB & (1<<PINB1)) BoardRelease = 20; //
117
                        break;
109
                else BoardRelease = 10;
118
                default:
-
 
119
                        break;
-
 
120
        }
-
 
121
        // set LED ports as output
-
 
122
        DDRB |= (1<<DDB1)|(1<<DDB0);
-
 
123
        RED_ON;
-
 
124
        GRN_OFF;
-
 
125
        return BoardRelease;
-
 
126
}
-
 
127
 
-
 
128
 
-
 
129
int16_t main (void)
-
 
130
{
-
 
131
        unsigned int timer;
-
 
132
 
Line 110... Line 133...
110
        }
133
        // disable interrupts global
111
 
134
        cli();
112
        // set LED ports as output
135
 
113
        DDRB |= (1<<DDB1)|(1<<DDB0);
136
        // analyze hardware environment
Line 127... Line 150...
127
        StickNick = 0;
150
        StickNick = 0;
Line 128... Line 151...
128
 
151
 
Line 129... Line 152...
129
    RED_OFF;
152
    RED_OFF;
130
 
153
 
131
        // initalize modules
154
        // initalize modules
132
        //LED_Init(); Is done within ParamSet_Init() below
155
        LED_Init();
133
    TIMER0_Init();
156
    TIMER0_Init();
134
    TIMER2_Init();
-
 
135
        USART0_Init();
-
 
136
 
157
    TIMER2_Init();
137
        #if defined (__AVR_ATmega644P__)
-
 
138
        if (BoardRelease > 10) USART1_Init();
-
 
139
        #endif
158
        USART0_Init();
140
 
159
        if(CPUType == ATMEGA644P) USART1_Init();
141
    RC_Init();
160
    RC_Init();
142
        ADC_Init();
-
 
143
        I2C_Init();
-
 
144
 
161
        ADC_Init();
145
 
162
        I2C_Init();
146
        #ifdef USE_NAVICTRL
163
        #ifdef USE_NAVICTRL
147
        SPI_MasterInit();
164
        SPI_MasterInit();
148
        #endif
165
        #endif
Line 154... Line 171...
154
        #endif
171
        #endif
Line 155... Line 172...
155
 
172
 
156
        // enable interrupts global
173
        // enable interrupts global
Line -... Line 174...
-
 
174
        sei();
-
 
175
 
-
 
176
        printf("\n\rFlightControl");
-
 
177
        printf("\n\rHardware: %d.%d", BoardRelease/10, BoardRelease%10);
-
 
178
        if(CPUType == ATMEGA644P)
-
 
179
        printf("\r\n     CPU: Atmega644p");
157
        sei();
180
        else
158
 
181
        printf("\r\n     CPU: Atmega644");
159
        printf("\n\rFlightControl\n\rHardware:%d.%d\n\rSoftware:V%d.%d%c ",BoardRelease/10,BoardRelease%10, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
182
        printf("\n\rSoftware: V%d.%d%c",VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH + 'a');
Line 160... Line 183...
160
        printf("\n\r==============================");
183
        printf("\n\r==============================");
161
        GRN_ON;
184
        GRN_ON;
Line 162... Line 185...
162
 
185
 
163
        // Parameter set handling
186
        // Parameter Set handling
164
        ParamSet_Init();
187
        ParamSet_Init();
165
 
188
 
Line 166... Line 189...
166
    if(GetParamWord(PID_ACC_NICK) > 1023)
189
        if(GetParamWord(PID_ACC_NICK) > 1023)
167
     {
190
        {
168
       printf("\n\rACC not calibrated!");
191
                printf("\n\rACC not calibrated!");
Line 169... Line -...
169
     }
-
 
170
 
192
        }
171
        //wait for a short time (otherwise the RC channel check won't work below)
193
 
172
        timer = SetDelay(500);
194
        //wait for a short time (otherwise the RC channel check won't work below)
173
        while(!CheckDelay(timer));
195
        timer = SetDelay(500);
174
 
196
        while(!CheckDelay(timer));
175
 
197
 
176
        if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)
198
        if(ParamSet.GlobalConfig & CFG_HEIGHT_CONTROL)
177
         {
199
        {
Line 178... Line 200...
178
           printf("\n\rCalibrating air pressure sensor..");
200
                printf("\n\rCalibrating air pressure sensor..");
179
           timer = SetDelay(1000);
201
                timer = SetDelay(1000);
180
       SearchAirPressureOffset();
202
                SearchAirPressureOffset();
Line 192... Line 214...
192
 
214
 
193
        #ifdef USE_MK3MAG
215
        #ifdef USE_MK3MAG
194
        printf("\n\rSupport for MK3MAG Compass");
216
        printf("\n\rSupport for MK3MAG Compass");
Line 195... Line -...
195
        #endif
-
 
196
 
-
 
197
 
217
        #endif
198
 
-
 
199
        #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG))
-
 
200
        #if defined (__AVR_ATmega644P__)
-
 
201
        if(BoardRelease == 10)
-
 
202
        {
-
 
203
                printf("\n\rSupport for GPS at 1st UART");
-
 
204
        }
-
 
205
        else
218
 
206
        {
-
 
207
                printf("\n\rSupport for GPS at 2nd UART");
-
 
208
        }
219
        #if (defined (USE_KILLAGREG) || defined (USE_MK3MAG))
209
        #else // (__AVR_ATmega644__)
-
 
210
        printf("\n\rSupport for GPS at 1st UART");
220
        if(CPUType == ATMEGA644P) printf("\n\rSupport for GPS at 2nd UART");
Line 211... Line -...
211
        #endif
-
 
212
        #endif
221
        else                      printf("\n\rSupport for GPS at 1st UART");
Line 213... Line 222...
213
 
222
        #endif
Line 214... Line 223...
214
 
223
 
215
 
224
 
Line 216... Line 225...
216
        SetNeutral();
225
        SetNeutral(NO_ACC_CALIB);
217
 
226
 
218
        RED_OFF;
227
        RED_OFF;
Line 219... Line 228...
219
 
228
 
Line 220... Line 229...
220
    BeepTime = 2000;
229
        BeepTime = 2000;
221
    ExternControl.Digital[0] = 0x55;
-
 
222
 
-
 
Line -... Line 230...
-
 
230
        ExternControl.Digital[0] = 0x55;
223
 
231
 
224
        printf("\n\rControl: ");
232
 
225
        if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
233
        printf("\n\rControl: ");
226
        else printf("Neutral");
234
        if (ParamSet.GlobalConfig & CFG_HEADING_HOLD) printf("HeadingHold");
227
 
235
        else printf("Neutral");
228
        printf("\n\n\r");
-
 
229
 
-
 
230
    LCD_Clear();
-
 
231
 
-
 
232
    I2CTimeout = 5000;
-
 
233
 
-
 
234
        while (1)
-
 
235
        {
-
 
236
        if(UpdateMotor)      // control interval
-
 
237
        {
-
 
238
                        UpdateMotor = 0; // reset Flag, is enabled every 2 ms by isr of timer0
-
 
239
                        //PORTD |= (1<<PORTD4);
-
 
240
            MotorControl();
-
 
241
                        //PORTD &= ~(1<<PORTD4);
-
 
242
 
-
 
243
                        SendMotorData();
-
 
Line -... Line 236...
-
 
236
 
-
 
237
        printf("\n\n\r");
-
 
238
 
-
 
239
        LCD_Clear();
-
 
240
 
-
 
241
        I2CTimeout = 5000;
-
 
242
        while (1)
-
 
243
        {
-
 
244
                if(UpdateMotor && ADReady)      // control interval
-
 
245
                {
-
 
246
                        UpdateMotor = 0; // reset Flag, is enabled every 2 ms by ISR of timer0
-
 
247
 
-
 
248
                        //J4HIGH;
-
 
249
                        MotorControl();
-
 
250
                        //J4LOW;
-
 
251
 
-
 
252
                        SendMotorData(); // the flight control code
-
 
253
                        RED_OFF;
244
 
254
 
245
            RED_OFF;
255
 
246
 
256
 
247
            if(PcAccess) PcAccess--;
257
                        if(PcAccess) PcAccess--;
248
            else
258
                        else
249
            {
259
                        {
250
                           ExternControl.Config = 0;
260
                                ExternControl.Config = 0;
Line 267... Line 277...
267
                        {
277
                        {
268
                                I2CTimeout--;
278
                                I2CTimeout--;
269
                                RED_OFF;
279
                                RED_OFF;
270
                        }
280
                        }
Line -... Line 281...
-
 
281
 
271
 
282
                        // allow Serial Data Transmit if motors must not updated or motors are not running
272
                        if(SIO_DEBUG && (!UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) ))
283
                        if( !UpdateMotor || !(MKFlags & MKFLAG_MOTOR_RUN) )
273
                        {
284
                        {
274
                                USART0_TransmitTxData();
-
 
275
                                USART0_ProcessRxData();
285
                                USART0_TransmitTxData();
276
                        }
286
                        }
Line 277... Line 287...
277
                        else USART0_ProcessRxData();
287
                        USART0_ProcessRxData();
278
 
288
 
279
                        if(CheckDelay(timer))
289
                        if(CheckDelay(timer))
280
                        {
290
                        {
281
                if(UBat < ParamSet.LowVoltageWarning)
291
                                if(UBat < ParamSet.LowVoltageWarning)
282
                {
292
                                {
283
                                        BeepModulation = 0x0300;
293
                                        BeepModulation = 0x0300;
284
                                        if(!BeepTime )
294
                                        if(!BeepTime )
285
                                        {
295
                                        {
286
                                                BeepTime = 6000; // 0.6 seconds
296
                                                BeepTime = 6000; // 0.6 seconds
287
                                        }
297
                                        }
288
                }
298
                                }
289
                                #ifdef USE_NAVICTRL
299
                                #ifdef USE_NAVICTRL
290
                                SPI_StartTransmitPacket();
300
                                SPI_StartTransmitPacket();
291
                                SendSPI = 4;
301
                                SendSPI = 4;
292
                                #endif
302
                                #endif
Line 293... Line 303...
293
                                timer = SetDelay(20); // every 20 ms
303
                                timer = SetDelay(20); // every 20 ms
-
 
304
                        }
294
            }
305
 
Line 295... Line 306...
295
 
306
                        LED_Update();
296
            LED_Update();
307
                        //J4LOW;
297
                }
308
                }
Line 303... Line 314...
303
                        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
314
                        // I.e. the SPI_TransmitByte() is called at a rate of 9.765 kHz/4= 2441.25 Hz,
304
                        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
315
                        // and therefore the time of transmission of a complete spi-packet (32 bytes) is 32*4/9.765 kHz = 13.1 ms.
305
                        SPI_TransmitByte();
316
                        SPI_TransmitByte();
306
                }
317
                }
307
                #endif
318
                #endif
308
    }
319
        }
309
        return (1);
320
        return (1);
310
}
321
}