Subversion Repositories FlightCtrl

Rev

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

Rev Author Line No. Line
1180 killagreg 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
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
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 ingob 51
 
886 killagreg 52
#include <avr/io.h>
53
#include <avr/interrupt.h>
54
 
1 ingob 55
#include "main.h"
886 killagreg 56
#include "twimaster.h"
57
#include "fc.h"
936 killagreg 58
#include "analog.h"
1 ingob 59
 
936 killagreg 60
volatile uint8_t twi_state              = 0;
1180 killagreg 61
uint8_t motor_write     = 0;
62
uint8_t motor_read      = 0;
936 killagreg 63
volatile uint8_t dac_channel    = 0;
1180 killagreg 64
 
65
#ifdef USE_QUADRO
66
uint8_t motor_rx[8];
67
#else
68
uint8_t motor_rx[16];
69
#endif
70
 
936 killagreg 71
volatile uint16_t I2CTimeout = 100;
1 ingob 72
 
936 killagreg 73
 
74
#define SCL_CLOCK  200000L
75
#define I2C_TIMEOUT 30000
76
 
77
#define TWSR_STATUS_MASK                        0xF8
78
// for Master Transmitter Mode
79
 
80
#define I2C_STATUS_START                        0x08
81
#define I2C_STATUS_REPEATSTART          0x10
82
#define I2C_STATUS_TX_SLA_ACK           0x18
83
#define I2C_STATUS_SLAW_NOACK           0x20
84
#define I2C_STATUS_TX_DATA_ACK          0x28
85
#define I2C_STATUS_TX_DATA_NOTACK       0x30
86
#define I2C_STATUS_RX_DATA_ACK          0x50
87
#define I2C_STATUS_RX_DATA_NOTACK       0x58
88
 
886 killagreg 89
/**************************************************/
90
/*   Initialize I2C (TWI)                         */
91
/**************************************************/
92
void I2C_Init(void)
1 ingob 93
{
886 killagreg 94
        uint8_t sreg = SREG;
95
        cli();
96
 
97
        // SDA is INPUT
98
        DDRC  &= ~(1<<DDC1);
99
        // SCL is output
100
        DDRC |= (1<<DDC0);
101
        // pull up SDA
102
        PORTC |= (1<<PORTC0)|(1<<PORTC1);
103
 
104
        // TWI Status Register
105
        // prescaler 1 (TWPS1 = 0, TWPS0 = 0)
106
        TWSR &= ~((1<<TWPS1)|(1<<TWPS0));
107
 
108
        // set TWI Bit Rate Register
109
        TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
110
 
936 killagreg 111
        twi_state               = 0;
112
        motor_write     = 0;
113
        motor_read              = 0;
114
 
886 killagreg 115
        SREG = sreg;
1 ingob 116
}
117
 
886 killagreg 118
/****************************************/
119
/*   Start I2C                          */
120
/****************************************/
121
void I2C_Start(void)
1 ingob 122
{
886 killagreg 123
        // TWI Control Register
124
        // clear TWI interrupt flag (TWINT=1)
125
        // disable TWI Acknowledge Bit (TWEA = 0)
126
        // enable TWI START Condition Bit (TWSTA = 1), MASTER
127
        // disable TWI STOP Condition Bit (TWSTO = 0)
128
        // disable TWI Write Collision Flag (TWWC = 0)
936 killagreg 129
        // enable i2c (TWEN = 1)
886 killagreg 130
        // enable TWI Interrupt (TWIE = 1)
131
    TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE);
1 ingob 132
}
133
 
886 killagreg 134
/****************************************/
135
/*    Stop I2C                          */
136
/****************************************/
137
void I2C_Stop(void)
1 ingob 138
{
886 killagreg 139
        // TWI Control Register
140
        // clear TWI interrupt flag (TWINT=1)
141
        // disable TWI Acknowledge Bit (TWEA = 0)
142
        // diable TWI START Condition Bit (TWSTA = 1), no MASTER
143
        // enable TWI STOP Condition Bit (TWSTO = 1)
144
        // disable TWI Write Collision Flag (TWWC = 0)
936 killagreg 145
        // enable i2c (TWEN = 1)
886 killagreg 146
        // disable TWI Interrupt (TWIE = 0)
147
    TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN);
1 ingob 148
}
149
 
173 holgerb 150
 
886 killagreg 151
/****************************************/
152
/*    Write to I2C                      */
153
/****************************************/
154
void I2C_WriteByte(int8_t byte)
155
{
156
    // move byte to send into TWI Data Register
1 ingob 157
    TWDR = byte;
886 killagreg 158
    // clear interrupt flag (TWINT = 1)
159
    // enable i2c bus (TWEN = 1)
936 killagreg 160
    // enable interrupt (TWIE = 1)
1 ingob 161
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
162
}
163
 
886 killagreg 164
 
165
/****************************************/
166
/*    Receive byte and send ACK         */
167
/****************************************/
168
void I2C_ReceiveByte(void)
1 ingob 169
{
886 killagreg 170
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
171
}
172
 
173
/****************************************/
174
/* I2C receive last byte and send no ACK*/
175
/****************************************/
176
void I2C_ReceiveLastByte(void)
177
{
178
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
179
}
180
 
181
 
182
/****************************************/
936 killagreg 183
/*    Reset I2C                         */
184
/****************************************/
185
void I2C_Reset(void)
186
{
187
        // stop i2c bus
188
        I2C_Stop();
189
        twi_state               = 0;
190
        motor_write     = TWDR;
191
        motor_write     = 0;
192
        motor_read              = 0;
193
        TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset
194
        TWAMR = 0;
195
        TWAR = 0;
196
        TWDR = 0;
197
        TWSR = 0;
198
        TWBR = 0;
199
        I2C_Init();
200
        I2C_Start();
201
        I2C_WriteByte(0);
202
}
203
 
1180 killagreg 204
 
936 killagreg 205
/****************************************/
886 killagreg 206
/*        I2C ISR                       */
207
/****************************************/
1180 killagreg 208
 
209
#ifdef USE_QUADRO
886 killagreg 210
ISR (TWI_vect)
211
{
212
 
936 killagreg 213
    switch (twi_state++) // First i2c_start from SendMotorData()
214
        {
886 killagreg 215
                // Master Transmit
216
        case 0: // Send SLA-W
936 killagreg 217
                I2C_WriteByte(0x52 + (motor_write * 2) );
1 ingob 218
                break;
936 killagreg 219
        case 1: // Send Data to Slave
220
                switch(motor_write)
221
                {
1 ingob 222
                    case 0:
1180 killagreg 223
                            I2C_WriteByte(Motor1);
1 ingob 224
                            break;
886 killagreg 225
                    case 1:
1180 killagreg 226
                            I2C_WriteByte(Motor2);
1 ingob 227
                            break;
228
                    case 2:
1180 killagreg 229
                            I2C_WriteByte(Motor3);
1 ingob 230
                            break;
231
                    case 3:
1180 killagreg 232
                            I2C_WriteByte(Motor4);
1 ingob 233
                            break;
936 killagreg 234
                }
1 ingob 235
                break;
936 killagreg 236
        case 2: // repeat case 0+1 for all motors
237
                        I2C_Stop();
238
                if (motor_write < 3)
239
                {
240
                                        motor_write++; // jump to next motor
241
                                        twi_state = 0; // and repeat from state 0
242
                                }
243
                else
244
                {       // data to last motor send
245
                                        motor_write = 0; // reset motor write counter
246
                                }
952 killagreg 247
                I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
1 ingob 248
                break;
886 killagreg 249
 
250
        // Master Receive
251
        case 3: // Send SLA-R
936 killagreg 252
                I2C_WriteByte(0x53 + (motor_read * 2) );
886 killagreg 253
                break;
1 ingob 254
        case 4:
886 killagreg 255
                //Transmit 1st byte
256
                                I2C_ReceiveByte();
1 ingob 257
                break;
886 killagreg 258
        case 5: //Read 1st byte and transmit 2nd Byte
936 killagreg 259
                motor_rx[motor_read] = TWDR;
886 killagreg 260
                                I2C_ReceiveLastByte();
261
                                break;
262
        case 6:
263
                //Read 2nd byte
936 killagreg 264
                                motor_rx[motor_read + 4] = TWDR;
265
                                motor_read++;
266
                if (motor_read > 3) motor_read = 0;
267
                I2C_Stop();
268
                                twi_state = 0;
269
                I2CTimeout = 10;
270
                break;
173 holgerb 271
 
936 killagreg 272
                // Gyro-Offsets
273
                case 7:
274
                                I2C_WriteByte(0x98); // Address the DAC
275
                                break;
276
 
277
                case 8:
278
                                I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
279
                                break;
280
 
281
                case 9:
282
                                switch(dac_channel)
283
                                {
284
                                        case 0:
1180 killagreg 285
                                                        I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
936 killagreg 286
                                                        break;
287
                                        case 1:
1180 killagreg 288
                                                        I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
936 killagreg 289
                                                        break;
290
                                        case 2:
1180 killagreg 291
                                                        I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
936 killagreg 292
                                                        break;
293
                                }
294
                                break;
295
 
296
                case 10:
297
                                I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
298
                                break;
299
 
300
                case 11:
301
                                I2C_Stop();
302
                                I2CTimeout = 10;
303
                                // repeat case 7...10 until all DAC Channels are updated
304
                                if(dac_channel < 2)
305
                                {
306
                                        dac_channel ++;         // jump to next channel
307
                                        twi_state = 7;          // and repeat from state 7
308
                                        I2C_Start();            // start transmission for next channel
309
                                }
310
                                else
311
                                {       // data to last motor send
312
                                        dac_channel = 0; // reset dac channel counter
313
                                        twi_state = 0;   // reset twi_state
314
                                }
315
                break;
316
 
886 killagreg 317
        default:
318
                I2C_Stop();
319
                twi_state = 0;
173 holgerb 320
                I2CTimeout = 10;
936 killagreg 321
                motor_write = 0;
322
                motor_read = 0;
323
        }
1 ingob 324
}
1180 killagreg 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