Subversion Repositories FlightCtrl

Rev

Rev 1209 | Rev 1211 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1209 Rev 1210
1
/*############################################################################
1
/*############################################################################
2
############################################################################*/
2
############################################################################*/
3
 
3
 
4
#include "main.h"
4
#include "main.h"
5
 
5
 
6
volatile unsigned char twi_state = 0;
6
volatile unsigned char twi_state = 0;
7
unsigned char motor = 0;
7
unsigned char motor = 0;
8
unsigned char motorread = 0;
8
unsigned char motorread = 0,MissingMotor = 0;
9
unsigned char motor_rx[16],motor_rx2[16];
9
unsigned char motor_rx[16],motor_rx2[16];
-
 
10
unsigned char MotorPresent[MAX_MOTORS];
10
 
11
 
11
//############################################################################
12
//############################################################################
12
//Initzialisieren der I2C (TWI) Schnittstelle
13
//Initzialisieren der I2C (TWI) Schnittstelle
13
void i2c_init(void)
14
void i2c_init(void)
14
//############################################################################
15
//############################################################################
15
{
16
{
16
  TWSR = 0;
17
  TWSR = 0;
17
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
18
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
18
}
19
}
19
 
20
 
20
//############################################################################
21
//############################################################################
21
//Start I2C
22
//Start I2C
22
void i2c_start(void)
23
void i2c_start(void)
23
//############################################################################
24
//############################################################################
24
{
25
{
25
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
26
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
26
}
27
}
27
 
28
 
28
//############################################################################
29
//############################################################################
29
void i2c_stop(void)
30
void i2c_stop(void)
30
//############################################################################
31
//############################################################################
31
{
32
{
32
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
33
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
33
}
34
}
34
 
35
 
35
void i2c_reset(void)
36
void i2c_reset(void)
36
//############################################################################
37
//############################################################################
37
{
38
{
38
                 i2c_stop();                
39
                 i2c_stop();                
39
                 twi_state = 0;
40
                 twi_state = 0;
40
                 motor = TWDR;
41
                 motor = TWDR;
41
                 motor = 0;
42
                 motor = 0;
42
                 TWCR = 0x80;
43
                 TWCR = 0x80;
43
                 TWAMR = 0;
44
                 TWAMR = 0;
44
                 TWAR = 0;
45
                 TWAR = 0;
45
                 TWDR = 0;
46
                 TWDR = 0;
46
                 TWSR = 0;
47
                 TWSR = 0;
47
                 TWBR = 0;
48
                 TWBR = 0;
48
                 i2c_init();
49
                 i2c_init();
49
                 i2c_start();
50
                 i2c_start();
50
                 i2c_write_byte(0);
51
                 i2c_write_byte(0);
51
}
52
}
52
 
53
 
53
//############################################################################
54
//############################################################################
54
void i2c_write_byte(char byte)
55
void i2c_write_byte(char byte)
55
//############################################################################
56
//############################################################################
56
{
57
{
57
    TWSR = 0x00;
58
    TWSR = 0x00;
58
    TWDR = byte;
59
    TWDR = byte;
59
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
60
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
60
}
61
}
61
 
62
 
62
/****************************************/
63
/****************************************/
63
/*    Write to I2C                      */
64
/*    Write to I2C                      */
64
/****************************************/
65
/****************************************/
65
void I2C_WriteByte(int8_t byte)
66
void I2C_WriteByte(int8_t byte)
66
{
67
{
67
    // move byte to send into TWI Data Register
68
    // move byte to send into TWI Data Register
68
    TWDR = byte;
69
    TWDR = byte;
69
    // clear interrupt flag (TWINT = 1)
70
    // clear interrupt flag (TWINT = 1)
70
    // enable i2c bus (TWEN = 1)
71
    // enable i2c bus (TWEN = 1)
71
    // enable interrupt (TWIE = 1)
72
    // enable interrupt (TWIE = 1)
72
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
73
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
73
}
74
}
74
 
75
 
75
/****************************************/
76
/****************************************/
76
/*    Receive byte and send ACK         */
77
/*    Receive byte and send ACK         */
77
/****************************************/
78
/****************************************/
78
void I2C_ReceiveByte(void)
79
void I2C_ReceiveByte(void)
79
{
80
{
80
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
81
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
81
}
82
}
82
 
83
 
83
/****************************************/
84
/****************************************/
84
/* I2C receive last byte and send no ACK*/
85
/* I2C receive last byte and send no ACK*/
85
/****************************************/
86
/****************************************/
86
void I2C_ReceiveLastByte(void)
87
void I2C_ReceiveLastByte(void)
87
{
88
{
88
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
89
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
89
}
90
}
90
 
91
 
91
 
92
 
92
 
93
 
93
//############################################################################
94
//############################################################################
94
SIGNAL (TWI_vect)
95
SIGNAL (TWI_vect)
95
//############################################################################
96
//############################################################################
96
{
97
{
-
 
98
 static unsigned char missing_motor,cnt_motor;
97
    switch (twi_state++)
99
     switch(twi_state++)
98
        {
100
        {
-
 
101
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
102
// Writing the Data
-
 
103
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
99
        case 0:
104
        case 0:
100
J3High;
-
 
101
                        while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS-1) motor++;  // skip if not used
105
                        while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++;  // skip if not used
102
                                if(motor == MAX_MOTORS) { motor = 0; twi_state = 3; i2c_start(); } // writing finished -> now read 
106
                                if(motor == MAX_MOTORS)  // writing finished -> now read 
-
 
107
                                {
-
 
108
                                 motor = 0;
-
 
109
                                 twi_state = 3;
-
 
110
                                 i2c_write_byte(0x53+(motorread*2));
-
 
111
                                 }
103
                                else i2c_write_byte(0x52+(motor*2));
112
                                else i2c_write_byte(0x52+(motor*2));
104
                break;
113
                break;
105
        case 1:
114
        case 1:
106
                i2c_write_byte(Motor[motor++]);
115
                i2c_write_byte(Motor[motor++]);
107
                break;
116
                break;
108
        case 2:
117
        case 2:
-
 
118
                if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; }
109
                i2c_stop();
119
                i2c_stop();
-
 
120
                I2CTimeout = 10;
110
                twi_state = 0;
121
                twi_state = 0;
111
                i2c_start();  
122
                i2c_start();  
112
                break;
123
                break;
-
 
124
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
113
        //Liest Daten von Motor
125
// Reading Data
-
 
126
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
114
        case 3:
127
        case 3:
115
J5High;
-
 
116
                        while(Mixer.Motor[motorread][0] <= 0 && MAX_MOTORS-1) motorread++;
-
 
117
                                if(motorread >= MAX_MOTORS) {motorread = 0;J4High;}
-
 
118
                                else i2c_write_byte(0x53+(motorread*2));
-
 
119
                break;
-
 
120
        case 4:
-
 
121
                //Transmit 1st byte for reading
128
                //Transmit 1st byte for reading
-
 
129
 
-
 
130
                if(TWSR != 0x40)  // Error?
-
 
131
                                 {
-
 
132
                                  MotorPresent[motorread] = 0;
-
 
133
                                  motorread++;
-
 
134
                                  if(motorread >= MAX_MOTORS) motorread = 0;
-
 
135
                                  i2c_stop();
-
 
136
                  twi_state = 0;
-
 
137
                 }
-
 
138
                                 else
-
 
139
                                 {
-
 
140
                  MotorPresent[motorread] = ('1' - '-') + motorread;
-
 
141
                                 }
-
 
142
                MissingMotor = missing_motor;
122
                I2C_ReceiveByte();
143
                I2C_ReceiveByte();
123
                break;
144
                break;
124
        case 5: //Read 1st byte and transmit 2nd Byte
145
        case 4: //Read 1st byte and transmit 2nd Byte
125
                motor_rx[motorread] = TWDR;
146
                motor_rx[motorread] = TWDR;
126
                I2C_ReceiveLastByte(); //nack
147
                I2C_ReceiveLastByte(); //nack
127
                break;
148
                break;
128
        case 6:
149
        case 5:
129
                //Read 2nd byte
150
                //Read 2nd byte
130
                motor_rx2[motorread++] = TWDR;
151
                motor_rx2[motorread++] = TWDR;
131
                i2c_stop();
152
                i2c_stop();
132
                twi_state = 0;
153
                twi_state = 0;
133
                I2CTimeout = 10;
154
                                missing_motor = 0;
134
                break;
155
                break;
-
 
156
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
135
        case 8: // Gyro-Offset
157
// writing Gyro-Offset
-
 
158
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-
 
159
        case 8:
136
                i2c_write_byte(0x98); // Address of the DAC
160
                i2c_write_byte(0x98); // Address of the DAC
137
                break;
161
                break;
138
        case 9:
162
        case 9:
139
                i2c_write_byte(0x10); // Update Channel A
163
                i2c_write_byte(0x10); // Update Channel A
140
                break;
164
                break;
141
        case 10:
165
        case 10:
142
                i2c_write_byte(AnalogOffsetNick); // Value
166
                i2c_write_byte(AnalogOffsetNick); // Value
143
                break;
167
                break;
144
        case 11:
168
        case 11:
145
                i2c_write_byte(0x80); // Value
169
                i2c_write_byte(0x80); // Value
146
                break;
170
                break;
147
        case 12:
171
        case 12:
148
                i2c_stop();              
172
                i2c_stop();              
149
                I2CTimeout = 10;
173
                I2CTimeout = 10;
150
                i2c_start();  
174
                i2c_start();  
151
                break;
175
                break;
152
        case 13:
176
        case 13:
153
                i2c_write_byte(0x98); // Address of the DAC
177
                i2c_write_byte(0x98); // Address of the DAC
154
                break;
178
                break;
155
        case 14:
179
        case 14:
156
                i2c_write_byte(0x12); // Update Channel B
180
                i2c_write_byte(0x12); // Update Channel B
157
                break;
181
                break;
158
        case 15:
182
        case 15:
159
                i2c_write_byte(AnalogOffsetRoll); // Value
183
                i2c_write_byte(AnalogOffsetRoll); // Value
160
                break;
184
                break;
161
        case 16:
185
        case 16:
162
                i2c_write_byte(0x80); // Value
186
                i2c_write_byte(0x80); // Value
163
                break;
187
                break;
164
        case 17:
188
        case 17:
165
                i2c_stop();              
189
                i2c_stop();              
166
                I2CTimeout = 10;
190
                I2CTimeout = 10;
167
                i2c_start();  
191
                i2c_start();  
168
                break;
192
                break;
169
        case 18:
193
        case 18:
170
                i2c_write_byte(0x98); // Address of the DAC
194
                i2c_write_byte(0x98); // Address of the DAC
171
                break;
195
                break;
172
        case 19:
196
        case 19:
173
                i2c_write_byte(0x14); // Update Channel C
197
                i2c_write_byte(0x14); // Update Channel C
174
                break;
198
                break;
175
        case 20:
199
        case 20:
176
                i2c_write_byte(AnalogOffsetGier); // Value
200
                i2c_write_byte(AnalogOffsetGier); // Value
177
                break;
201
                break;
178
        case 21:
202
        case 21:
179
                i2c_write_byte(0x80); // Value
203
                i2c_write_byte(0x80); // Value
180
                break;
204
                break;
181
        case 22:
205
        case 22:
182
                i2c_stop();              
206
                i2c_stop();              
183
                I2CTimeout = 10;
207
                I2CTimeout = 10;
184
                twi_state = 0;
208
                twi_state = 0;
185
                break;
209
                break;
186
        default: twi_state = 0;
210
        default: twi_state = 0;
187
                break;        
211
                break;        
188
                }
212
                }
189
 TWCR |= 0x80;
213
 TWCR |= 0x80;
190
J3Low;
-
 
191
J4Low;
-
 
192
J5Low;
-
 
193
}
214
}
194
/*
215
/*
195
//############################################################################
216
//############################################################################
196
SIGNAL (TWI_vect)
217
SIGNAL (TWI_vect)
197
//############################################################################
218
//############################################################################
198
{
219
{
199
    switch (twi_state++)
220
    switch (twi_state++)
200
        {
221
        {
201
        case 0:
222
        case 0:
202
                i2c_write_byte(0x52+(motor*2));
223
                i2c_write_byte(0x52+(motor*2));
203
                break;
224
                break;
204
        case 1:
225
        case 1:
205
                switch(motor++)
226
                switch(motor++)
206
                    {
227
                    {
207
                    case 0:
228
                    case 0:
208
                            i2c_write_byte(Motor_Vorne);
229
                            i2c_write_byte(Motor_Vorne);
209
                            break;
230
                            break;
210
                    case 1:      
231
                    case 1:      
211
                            i2c_write_byte(Motor_Hinten);
232
                            i2c_write_byte(Motor_Hinten);
212
                            break;
233
                            break;
213
                    case 2:
234
                    case 2:
214
                            i2c_write_byte(Motor_Rechts);
235
                            i2c_write_byte(Motor_Rechts);
215
                            break;
236
                            break;
216
                    case 3:
237
                    case 3:
217
                            i2c_write_byte(Motor_Links);
238
                            i2c_write_byte(Motor_Links);
218
                            break;
239
                            break;
219
                    }
240
                    }
220
                break;
241
                break;
221
        case 2:
242
        case 2:
222
                i2c_stop();
243
                i2c_stop();
223
                if (motor<4) twi_state = 0;
244
                if (motor<4) twi_state = 0;
224
                else motor = 0;
245
                else motor = 0;
225
                i2c_start();  
246
                i2c_start();  
226
                break;
247
                break;
227
                   
248
                   
228
        //Liest Daten von Motor
249
        //Liest Daten von Motor
229
        case 3:
250
        case 3:
230
                i2c_write_byte(0x53+(motorread*2));
251
                i2c_write_byte(0x53+(motorread*2));
231
                break;
252
                break;
232
        case 4:
253
        case 4:
233
                switch(motorread)
254
                switch(motorread)
234
                    {
255
                    {
235
                    case 0:
256
                    case 0:
236
                        i2c_write_byte(Motor_Vorne);
257
                        i2c_write_byte(Motor_Vorne);
237
                        break;
258
                        break;
238
                    case 1:
259
                    case 1:
239
                        i2c_write_byte(Motor_Hinten);
260
                        i2c_write_byte(Motor_Hinten);
240
                        break;
261
                        break;
241
                    case 2:
262
                    case 2:
242
                        i2c_write_byte(Motor_Rechts);
263
                        i2c_write_byte(Motor_Rechts);
243
                        break;
264
                        break;
244
                    case 3:
265
                    case 3:
245
                        i2c_write_byte(Motor_Links);
266
                        i2c_write_byte(Motor_Links);
246
                        break;
267
                        break;
247
                    }
268
                    }
248
                break;
269
                break;
249
        case 5: //1 Byte vom Motor lesen      
270
        case 5: //1 Byte vom Motor lesen      
250
                motor_rx[motorread] = TWDR;
271
                motor_rx[motorread] = TWDR;
251
 
272
 
252
        case 6:
273
        case 6:
253
                switch(motorread)
274
                switch(motorread)
254
                    {
275
                    {
255
                    case 0:
276
                    case 0:
256
                        i2c_write_byte(Motor_Vorne);
277
                        i2c_write_byte(Motor_Vorne);
257
                        break;
278
                        break;
258
                    case 1:
279
                    case 1:
259
                        i2c_write_byte(Motor_Hinten);
280
                        i2c_write_byte(Motor_Hinten);
260
                        break;
281
                        break;
261
                    case 2:
282
                    case 2:
262
                        i2c_write_byte(Motor_Rechts);
283
                        i2c_write_byte(Motor_Rechts);
263
                        break;
284
                        break;
264
                    case 3:
285
                    case 3:
265
                        i2c_write_byte(Motor_Links);
286
                        i2c_write_byte(Motor_Links);
266
                        break;
287
                        break;
267
                    }
288
                    }
268
                break;  
289
                break;  
269
        case 7: //2 Byte vom Motor lesen      
290
        case 7: //2 Byte vom Motor lesen      
270
                motor_rx[motorread+4] = TWDR;
291
                motor_rx[motorread+4] = TWDR;
271
                motorread++;
292
                motorread++;
272
                if (motorread>3) motorread=0;
293
                if (motorread>3) motorread=0;
273
                i2c_stop();
294
                i2c_stop();
274
                I2CTimeout = 10;
295
                I2CTimeout = 10;
275
                twi_state = 0;
296
                twi_state = 0;
276
                break;
297
                break;
277
        case 8: // Gyro-Offset
298
        case 8: // Gyro-Offset
278
                i2c_write_byte(0x98); // Address of the DAC
299
                i2c_write_byte(0x98); // Address of the DAC
279
                break;
300
                break;
280
        case 9:
301
        case 9:
281
                i2c_write_byte(0x10); // Update Channel A
302
                i2c_write_byte(0x10); // Update Channel A
282
                break;
303
                break;
283
        case 10:
304
        case 10:
284
                i2c_write_byte(AnalogOffsetNick); // Value
305
                i2c_write_byte(AnalogOffsetNick); // Value
285
                break;
306
                break;
286
        case 11:
307
        case 11:
287
                i2c_write_byte(0x80); // Value
308
                i2c_write_byte(0x80); // Value
288
                break;
309
                break;
289
        case 12:
310
        case 12:
290
                i2c_stop();              
311
                i2c_stop();              
291
                I2CTimeout = 10;
312
                I2CTimeout = 10;
292
                i2c_start();  
313
                i2c_start();  
293
                break;
314
                break;
294
        case 13:
315
        case 13:
295
                i2c_write_byte(0x98); // Address of the DAC
316
                i2c_write_byte(0x98); // Address of the DAC
296
                break;
317
                break;
297
        case 14:
318
        case 14:
298
                i2c_write_byte(0x12); // Update Channel B
319
                i2c_write_byte(0x12); // Update Channel B
299
                break;
320
                break;
300
        case 15:
321
        case 15:
301
                i2c_write_byte(AnalogOffsetRoll); // Value
322
                i2c_write_byte(AnalogOffsetRoll); // Value
302
                break;
323
                break;
303
        case 16:
324
        case 16:
304
                i2c_write_byte(0x80); // Value
325
                i2c_write_byte(0x80); // Value
305
                break;
326
                break;
306
        case 17:
327
        case 17:
307
                i2c_stop();              
328
                i2c_stop();              
308
                I2CTimeout = 10;
329
                I2CTimeout = 10;
309
                i2c_start();  
330
                i2c_start();  
310
                break;
331
                break;
311
        case 18:
332
        case 18:
312
                i2c_write_byte(0x98); // Address of the DAC
333
                i2c_write_byte(0x98); // Address of the DAC
313
                break;
334
                break;
314
        case 19:
335
        case 19:
315
                i2c_write_byte(0x14); // Update Channel C
336
                i2c_write_byte(0x14); // Update Channel C
316
                break;
337
                break;
317
        case 20:
338
        case 20:
318
                i2c_write_byte(AnalogOffsetGier); // Value
339
                i2c_write_byte(AnalogOffsetGier); // Value
319
                break;
340
                break;
320
        case 21:
341
        case 21:
321
                i2c_write_byte(0x80); // Value
342
                i2c_write_byte(0x80); // Value
322
                break;
343
                break;
323
        case 22:
344
        case 22:
324
                i2c_stop();              
345
                i2c_stop();              
325
                I2CTimeout = 10;
346
                I2CTimeout = 10;
326
                twi_state = 0;
347
                twi_state = 0;
327
                break;
348
                break;
328
        }
349
        }
329
 TWCR |= 0x80;
350
 TWCR |= 0x80;
330
}
351
}
331
*/
352
*/