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 -... Line 1...
-
 
1
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
2
// + Copyright (c) 04.2007 Holger Buss
-
 
3
// + Nur für den privaten Gebrauch
-
 
4
// + www.MikroKopter.com
-
 
5
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
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.
-
 
8
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
-
 
9
// + bzgl. der Nutzungsbedingungen aufzunehmen.
-
 
10
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
-
 
11
// + Verkauf von Luftbildaufnahmen, usw.
-
 
12
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
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
-
 
15
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
16
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
-
 
17
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
-
 
18
// + eindeutig als Ursprung verlinkt werden
-
 
19
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
20
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
-
 
21
// + Benutzung auf eigene Gefahr
-
 
22
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
-
 
23
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
24
// + Die Portierung der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
-
 
25
// + mit unserer Zustimmung zulässig
-
 
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)
-
 
35
// +     Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
-
 
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
1
/*############################################################################
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
2
############################################################################*/
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 3... Line 51...
3
 
51
 
4
#include <avr/io.h>
52
#include <avr/io.h>
Line 5... Line 53...
5
#include <avr/interrupt.h>
53
#include <avr/interrupt.h>
6
 
54
 
7
#include "main.h"
55
#include "main.h"
8
#include "twimaster.h"
56
#include "twimaster.h"
Line 9... Line 57...
9
#include "fc.h"
57
#include "fc.h"
10
#include "analog.h"
58
#include "analog.h"
11
 
59
 
12
volatile uint8_t twi_state              = 0;
60
volatile uint8_t twi_state              = 0;
-
 
61
uint8_t motor_write     = 0;
-
 
62
uint8_t motor_read      = 0;
13
volatile uint8_t motor_write    = 0;
63
volatile uint8_t dac_channel    = 0;
-
 
64
 
-
 
65
#ifdef USE_QUADRO
-
 
66
uint8_t motor_rx[8];
-
 
67
#else
14
volatile uint8_t motor_read     = 0;
68
uint8_t motor_rx[16];
Line 15... Line 69...
15
volatile uint8_t dac_channel    = 0;
69
#endif
16
volatile uint8_t motor_rx[8];
70
 
Line 145... Line 199...
145
        I2C_Init();
199
        I2C_Init();
146
        I2C_Start();
200
        I2C_Start();
147
        I2C_WriteByte(0);
201
        I2C_WriteByte(0);
148
}
202
}
Line -... Line 203...
-
 
203
 
149
 
204
 
150
/****************************************/
205
/****************************************/
151
/*        I2C ISR                       */
206
/*        I2C ISR                       */
152
/****************************************/
-
 
Line -... Line 207...
-
 
207
/****************************************/
-
 
208
 
153
ISR (TWI_vect)
209
#ifdef USE_QUADRO
Line 154... Line 210...
154
 
210
ISR (TWI_vect)
155
{
211
{
156
 
212
 
Line 162... Line 218...
162
                break;
218
                break;
163
        case 1: // Send Data to Slave
219
        case 1: // Send Data to Slave
164
                switch(motor_write)
220
                switch(motor_write)
165
                {
221
                {
166
                    case 0:
222
                    case 0:
167
                            I2C_WriteByte(Motor_Front);
223
                            I2C_WriteByte(Motor1);
168
                            break;
224
                            break;
169
                    case 1:
225
                    case 1:
170
                            I2C_WriteByte(Motor_Rear);
226
                            I2C_WriteByte(Motor2);
171
                            break;
227
                            break;
172
                    case 2:
228
                    case 2:
173
                            I2C_WriteByte(Motor_Right);
229
                            I2C_WriteByte(Motor3);
174
                            break;
230
                            break;
175
                    case 3:
231
                    case 3:
176
                            I2C_WriteByte(Motor_Left);
232
                            I2C_WriteByte(Motor4);
177
                            break;
233
                            break;
178
                }
234
                }
179
                break;
235
                break;
180
        case 2: // repeat case 0+1 for all motors
236
        case 2: // repeat case 0+1 for all motors
181
                        I2C_Stop();
237
                        I2C_Stop();
Line 224... Line 280...
224
 
280
 
225
                case 9:
281
                case 9:
226
                                switch(dac_channel)
282
                                switch(dac_channel)
227
                                {
283
                                {
228
                                        case 0:
284
                                        case 0:
229
                                                        I2C_WriteByte(AnalogOffsetNick); // 1st byte for Channel A
285
                                                        I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
230
                                                        break;
286
                                                        break;
231
                                        case 1:
287
                                        case 1:
232
                                                        I2C_WriteByte(AnalogOffsetRoll); // 1st byte for Channel B
288
                                                        I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
233
                                                        break;
289
                                                        break;
234
                                        case 2:
290
                                        case 2:
235
                                                        I2C_WriteByte(AnalogOffsetYaw ); // 1st byte for Channel C
291
                                                        I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
236
                                                        break;
292
                                                        break;
237
                                }
293
                                }
Line 238... Line 294...
238
                                break;
294
                                break;
Line 264... Line 320...
264
                I2CTimeout = 10;
320
                I2CTimeout = 10;
265
                motor_write = 0;
321
                motor_write = 0;
266
                motor_read = 0;
322
                motor_read = 0;
267
        }
323
        }
268
}
324
}
-
 
325
#else // USE_OCTO, USE_OCTO2, USE_OCTO3
-
 
326
ISR (TWI_vect)
-
 
327
{
-
 
328
 
-
 
329
    switch (twi_state++) // First i2c_start from SendMotorData()
-
 
330
        {
-
 
331
                // Master Transmit
-
 
332
        case 0: // Send SLA-W
-
 
333
                I2C_WriteByte(0x52 + (motor_write * 2) );
-
 
334
                break;
-
 
335
        case 1: // Send Data to Slave
-
 
336
                switch(motor_write)
-
 
337
                {
-
 
338
                    case 0:
-
 
339
                            I2C_WriteByte(Motor1);
-
 
340
                            break;
-
 
341
                    case 1:
-
 
342
                            I2C_WriteByte(Motor2);
-
 
343
                            break;
-
 
344
                    case 2:
-
 
345
                            I2C_WriteByte(Motor3);
-
 
346
                            break;
-
 
347
                    case 3:
-
 
348
                            I2C_WriteByte(Motor4);
-
 
349
                            break;
-
 
350
                                        case 5:
-
 
351
                                                        I2C_WriteByte(Motor5);
-
 
352
                                                        break;
-
 
353
                                        case 6:
-
 
354
                                                        I2C_WriteByte(Motor6);
-
 
355
                                                        break;
-
 
356
                                        case 7:
-
 
357
                                                        I2C_WriteByte(Motor7);
-
 
358
                                                        break;
-
 
359
                                        case 8:
-
 
360
                                                        I2C_WriteByte(Motor8);
-
 
361
                            break;
-
 
362
                }
-
 
363
                break;
-
 
364
        case 2: // repeat case 0+1 for all motors
-
 
365
                        I2C_Stop();
-
 
366
                if (motor_write < 7)
-
 
367
                {
-
 
368
                                        motor_write++; // jump to next motor
-
 
369
                                        twi_state = 0; // and repeat from state 0
-
 
370
                                }
-
 
371
                else
-
 
372
                {       // data to last motor send
-
 
373
                                        motor_write = 0; // reset motor write counter
-
 
374
                                }
-
 
375
                I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
-
 
376
                break;
-
 
377
 
-
 
378
        // Master Receive
-
 
379
        case 3: // Send SLA-R
-
 
380
                I2C_WriteByte(0x53 + (motor_read * 2) );
-
 
381
                break;
-
 
382
        case 4:
-
 
383
                //Transmit 1st byte
-
 
384
                                I2C_ReceiveByte();
-
 
385
                break;
-
 
386
        case 5: //Read 1st byte and transmit 2nd Byte
-
 
387
                motor_rx[motor_read] = TWDR;
-
 
388
                                I2C_ReceiveLastByte();
-
 
389
                                break;
-
 
390
        case 6:
-
 
391
                //Read 2nd byte
-
 
392
                                motor_rx[motor_read + 8] = TWDR;
-
 
393
                                motor_read++;
-
 
394
                if (motor_read > 7) motor_read = 0;
-
 
395
                I2C_Stop();
-
 
396
                                twi_state = 0;
-
 
397
                I2CTimeout = 10;
-
 
398
                break;
-
 
399
 
-
 
400
                // Gyro-Offsets
-
 
401
                case 7:
-
 
402
                                I2C_WriteByte(0x98); // Address the DAC
-
 
403
                                break;
-
 
404
 
-
 
405
                case 8:
-
 
406
                                I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
-
 
407
                                break;
-
 
408
 
-
 
409
                case 9:
-
 
410
                                switch(dac_channel)
-
 
411
                                {
-
 
412
                                        case 0:
-
 
413
                                                        I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
-
 
414
                                                        break;
-
 
415
                                        case 1:
-
 
416
                                                        I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
-
 
417
                                                        break;
-
 
418
                                        case 2:
-
 
419
                                                        I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
-
 
420
                                                        break;
-
 
421
                                }
-
 
422
                                break;
-
 
423
 
-
 
424
                case 10:
-
 
425
                                I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
-
 
426
                                break;
-
 
427
 
-
 
428
                case 11:
-
 
429
                                I2C_Stop();
-
 
430
                                I2CTimeout = 10;
-
 
431
                                // repeat case 7...10 until all DAC Channels are updated
-
 
432
                                if(dac_channel < 2)
-
 
433
                                {
-
 
434
                                        dac_channel ++;         // jump to next channel
-
 
435
                                        twi_state = 7;          // and repeat from state 7
-
 
436
                                        I2C_Start();            // start transmission for next channel
-
 
437
                                }
-
 
438
                                else
-
 
439
                                {       // data to last motor send
-
 
440
                                        dac_channel = 0; // reset dac channel counter
-
 
441
                                        twi_state = 0;   // reset twi_state
-
 
442
                                }
-
 
443
                break;
-
 
444
 
-
 
445
        default:
-
 
446
                I2C_Stop();
-
 
447
                twi_state = 0;
-
 
448
                I2CTimeout = 10;
-
 
449
                motor_write = 0;
-
 
450
                motor_read = 0;
-
 
451
        }
-
 
452
}
-
 
453
#endif // USE_OCTO, USE_OCTO2
-
 
454