Subversion Repositories FlightCtrl

Rev

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

Rev 1638 Rev 1639
Line 2... Line 2...
2
############################################################################*/
2
############################################################################*/
Line 3... Line 3...
3
 
3
 
Line 4... Line 4...
4
#include "main.h"
4
#include "main.h"
5
 
5
 
6
volatile unsigned char twi_state = 0;
6
volatile unsigned char twi_state = 0;
Line 7... Line 7...
7
unsigned char motor = 0;
7
unsigned char motor = 0,TransmitBlConfig = 0;
Line 8... Line 8...
8
unsigned char motorread = 0,MissingMotor = 0;
8
unsigned char motorread = 0,MissingMotor = 0;
Line 18... Line 18...
18
{
18
{
19
  TWSR = 0;
19
  TWSR = 0;
20
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
20
  TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
21
}
21
}
Line 22... Line -...
22
 
-
 
23
//############################################################################
-
 
24
//Start I2C
-
 
25
void i2c_start(void)
-
 
26
//############################################################################
-
 
27
{
-
 
28
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
-
 
29
}
-
 
30
 
-
 
31
//############################################################################
-
 
32
void i2c_stop(void)
-
 
33
//############################################################################
-
 
34
{
-
 
35
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
-
 
36
}
-
 
37
 
22
 
38
void i2c_reset(void)
23
void i2c_reset(void)
39
//############################################################################
24
//############################################################################
40
{
25
{
41
                 i2c_stop();
26
                 I2C_Stop();
42
                 twi_state = 0;
27
                 twi_state = 0;
43
                 motor = TWDR;
28
                 motor = TWDR;
44
                 motor = 0;
29
                 motor = 0;
45
                 TWCR = 0x80;
30
                 TWCR = 0x80;
46
                 TWAMR = 0;
31
                 TWAMR = 0;
47
                 TWAR = 0;
32
                 TWAR = 0;
48
                 TWDR = 0;
33
                 TWDR = 0;
49
                 TWSR = 0;
34
                 TWSR = 0;
50
                 TWBR = 0;
35
                 TWBR = 0;
51
                 i2c_init();
36
                 i2c_init();
52
                 i2c_start();
37
                 I2C_Start();
53
                 i2c_write_byte(0);
38
                 i2c_write_byte(0);
54
}
39
}
55
 
40
/*
56
//############################################################################
41
//############################################################################
57
void i2c_write_byte(char byte)
42
void i2c_write_byte(char byte)
58
//############################################################################
43
//############################################################################
59
{
44
{
60
    TWSR = 0x00;
45
    TWSR = 0x00;
61
    TWDR = byte;
46
    TWDR = byte;
62
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
47
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
-
 
48
}
Line 63... Line -...
63
}
-
 
64
 
-
 
65
/****************************************/
-
 
-
 
49
*/
66
/*    Write to I2C                      */
50
 
67
/****************************************/
51
/*
68
void I2C_WriteByte(int8_t byte)
52
void I2C_WriteByte(int8_t byte)
69
{
53
{
70
    // move byte to send into TWI Data Register
54
    // move byte to send into TWI Data Register
71
    TWDR = byte;
55
    TWDR = byte;
72
    // clear interrupt flag (TWINT = 1)
56
    // clear interrupt flag (TWINT = 1)
73
    // enable i2c bus (TWEN = 1)
57
    // enable i2c bus (TWEN = 1)
74
    // enable interrupt (TWIE = 1)
58
    // enable interrupt (TWIE = 1)
75
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
-
 
76
}
-
 
77
 
-
 
78
/****************************************/
-
 
79
/*    Receive byte and send ACK         */
-
 
80
/****************************************/
59
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
81
void I2C_ReceiveByte(void)
-
 
82
{
-
 
83
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
-
 
84
}
-
 
85
 
-
 
86
/****************************************/
-
 
87
/* I2C receive last byte and send no ACK*/
-
 
88
/****************************************/
-
 
89
void I2C_ReceiveLastByte(void)
-
 
90
{
-
 
91
   TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
-
 
Line 92... Line 60...
92
}
60
}
93
 
61
*/
94
 
62
 
95
/*
63
/*
Line 120... Line 88...
120
                if(TWSR == 0x30)
88
                if(TWSR == 0x30)
121
                                 {
89
                                 {
122
                                  if(!missing_motor) missing_motor = motor;
90
                                  if(!missing_motor) missing_motor = motor;
123
                                  if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow
91
                                  if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow
124
                                 }
92
                                 }
125
                i2c_stop();
93
                I2C_Stop();
126
                I2CTimeout = 10;
94
                I2CTimeout = 10;
127
                twi_state = 0;
95
                twi_state = 0;
128
                i2c_start();
96
                I2C_Start();
129
                break;
97
                break;
130
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
98
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131
// Reading Data
99
// Reading Data
132
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
100
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
133
        case 3:
101
        case 3:
Line 135... Line 103...
135
                if(TWSR != 0x40)  // Error?
103
                if(TWSR != 0x40)  // Error?
136
                 {
104
                 {
137
                  Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit
105
                  Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit
138
                  motorread++;
106
                  motorread++;
139
                  if(motorread >= MAX_MOTORS) motorread = 0;
107
                  if(motorread >= MAX_MOTORS) motorread = 0;
140
                  i2c_stop();
108
                  I2C_Stop();
141
                  twi_state = 0;
109
                  twi_state = 0;
142
                 }
110
                 }
143
                 else
111
                 else
144
                 {
112
                 {
145
                  Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
113
                  Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
Line 155... Line 123...
155
        case 5:
123
        case 5:
156
                //Read 2nd byte
124
                //Read 2nd byte
157
                Motor[motorread].MaxPWM = TWDR;
125
                Motor[motorread].MaxPWM = TWDR;
158
                                motorread++; // next motor
126
                                motorread++; // next motor
159
                if(motorread >= MAX_MOTORS) motorread = 0;
127
                if(motorread >= MAX_MOTORS) motorread = 0;
160
                i2c_stop();
128
                I2C_Stop();
161
                twi_state = 0;
129
                twi_state = 0;
162
                break;
130
                break;
Line 163... Line 131...
163
 
131
 
164
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
132
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 175... Line 143...
175
                break;
143
                break;
176
        case 11:
144
        case 11:
177
                i2c_write_byte(0x80); // Value
145
                i2c_write_byte(0x80); // Value
178
                break;
146
                break;
179
        case 12:
147
        case 12:
180
                i2c_stop();
148
                I2C_Stop();
181
                I2CTimeout = 10;
149
                I2CTimeout = 10;
182
                i2c_start();
150
                I2C_Start();
183
                break;
151
                break;
184
        case 13:
152
        case 13:
185
                i2c_write_byte(0x98); // Address of the DAC
153
                i2c_write_byte(0x98); // Address of the DAC
186
                break;
154
                break;
187
        case 14:
155
        case 14:
Line 192... Line 160...
192
                break;
160
                break;
193
        case 16:
161
        case 16:
194
                i2c_write_byte(0x80); // Value
162
                i2c_write_byte(0x80); // Value
195
                break;
163
                break;
196
        case 17:
164
        case 17:
197
                i2c_stop();
165
                I2C_Stop();
198
                I2CTimeout = 10;
166
                I2CTimeout = 10;
199
                i2c_start();
167
                I2C_Start();
200
                break;
168
                break;
201
        case 18:
169
        case 18:
202
                i2c_write_byte(0x98); // Address of the DAC
170
                i2c_write_byte(0x98); // Address of the DAC
203
                break;
171
                break;
204
        case 19:
172
        case 19:
Line 209... Line 177...
209
                break;
177
                break;
210
        case 21:
178
        case 21:
211
                i2c_write_byte(0x80); // Value
179
                i2c_write_byte(0x80); // Value
212
                break;
180
                break;
213
        case 22:
181
        case 22:
214
                i2c_stop();
182
                I2C_Stop();
215
                I2CTimeout = 10;
183
                I2CTimeout = 10;
216
                twi_state = 0;
184
                twi_state = 0;
217
                break;
185
                break;
218
        default: twi_state = 0;
186
        default: twi_state = 0;
219
                break;
187
                break;
Line 233... Line 201...
233
//############################################################################
201
//############################################################################
234
SIGNAL (TWI_vect)
202
SIGNAL (TWI_vect)
235
//############################################################################
203
//############################################################################
236
{                           // 2    3   4   5   6   7   8  9
204
{                           // 2    3   4   5   6   7   8  9
237
 unsigned char test[] = {0,0,'#',0x1F,255,30,100,64,0x00,7,8,9,10};
205
 unsigned char test[] = {0,0,'#',0x1F,255,30,100,64,0x00,7,8,9,10};
238
 static unsigned char missing_motor,send = 0,crc = 0;
206
 static unsigned char missing_motor,send = 0,crc = 0, read_temperature = 0;
-
 
207
 
239
     switch(twi_state++)
208
     switch(twi_state++)
240
        {
209
        {
241
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
210
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
242
// Writing the Data
211
// Writing the Data
243
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
212
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 252... Line 221...
252
                                else i2c_write_byte(0x52+(motor*2));
221
                                else i2c_write_byte(0x52+(motor*2));
253
                                send = 0;
222
                                send = 0;
254
                break;
223
                break;
255
        case 1:
224
        case 1:
256
                i2c_write_byte(Motor[motor].SetPoint);
225
                i2c_write_byte(Motor[motor].SetPoint);
257
                if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) twi_state++;
226
                if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)/* || !Motor[motor].SetPointLowerBits*/)
-
 
227
                                  twi_state++;
258
                break;
228
                break;
259
        case 2:
229
        case 2:
260
                if(!send++)
230
                if(!send++)
261
                                 {
231
                                 {
262
                                  i2c_write_byte((Motor[motor].SetPointLowerBits << 1));// + (7 << 0));
232
                                  i2c_write_byte((Motor[motor].SetPointLowerBits << 1));// + (7 << 0));
263
                                  crc = 0xAA;
233
                                  crc = 0xAA;
264
                                 }
234
                                 }
265
                                else
235
                                else
266
                if(send == 9) i2c_write_byte(crc);
236
                if(send == 9) i2c_write_byte(crc)
267
                                else          
237
                                else          
268
                                 {
238
                                 {
269
                                  crc += test[send];
239
                                  crc += test[send];
270
                                  i2c_write_byte(test[send]);
240
                                  i2c_write_byte(test[send]);
271
                                }
241
                                }
272
                if(!MotorenEin && motor == motorread  && !PC_MotortestActive && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) if(send <= 10) twi_state--;
242
                if(TransmitBlConfig && !MotorenEin && motor == motorread && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK))
-
 
243
                                 {
-
 
244
                                  if(send <= 10) twi_state--;
-
 
245
                                 }
-
 
246
 
273
                break;
247
                break;
274
        case 3:
248
        case 3:
275
                        motor++;
249
                        motor++;
276
                if(TWSR == 0x30)
250
                if(TWSR == 0x30)
277
                                 {
251
                                 {
278
                                  if(!missing_motor) missing_motor = motor;
252
                                  if(!missing_motor) missing_motor = motor;
279
                                  if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow
253
                                  if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow
280
                                 }
254
                                 }
281
                i2c_stop();
255
                I2C_Stop();
282
                I2CTimeout = 10;
256
                I2CTimeout = 10;
283
                twi_state = 0;
257
                twi_state = 0;
284
                i2c_start();
258
                I2C_Start();
285
                break;
259
                break;
286
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
260
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
287
// Reading Data
261
// Reading Data
288
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
262
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
289
        case 4:
263
        case 4:
290
               //Transmit 1st byte for reading
264
               //Transmit 1st byte for reading
291
                if(TWSR != 0x40)  // Error?
265
                if(TWSR != 0x40)  // Error?
292
                 {
266
                 {
293
                  Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit
267
                  Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit
294
                  motorread++;
268
                  motorread++;
295
                  if(motorread >= MAX_MOTORS) motorread = 0;
269
                  if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; }
296
                  i2c_stop();
270
                  I2C_Stop();
297
                  twi_state = 0;
271
                  twi_state = 0;
298
                 }
272
                 }
299
                 else
273
                 else
300
                 {
274
                 {
301
                  Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
275
                  Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
Line 311... Line 285...
311
        case 6:
285
        case 6:
312
                       //Read 2nd byte and transmit 3rd Byte
286
                       //Read 2nd byte and transmit 3rd Byte
313
                Motor[motorread].MaxPWM = TWDR;
287
                Motor[motorread].MaxPWM = TWDR;
314
                                if(TWDR == 250)  
288
                                if(TWDR == 250)  
315
                                 {
289
                                 {
316
                                  //if(!Motor[motor].SetPoint) 
-
 
317
                                  Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK;
290
                                  Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK;
318
                                 }
291
                                 }
-
 
292
                                 else if(TransmitBlConfig) Motor[motorread].Version = 0;
319
                                I2C_ReceiveLastByte(); //nack
293
                                I2C_ReceiveLastByte(); //nack
320
                break;
294
                break;
321
        case 7: // read next 
295
        case 7: // read next 
322
                                if(TWDR == 255) { if(!MotorenEin) Motor[motorread].Version = 0; DebugOut.Analog[24]++;}
-
 
323
                                Motor[motorread].Temperature = TWDR;
296
                                Motor[motorread].Temperature = TWDR;
324
                                motorread++; // next motor
297
                                motorread++; // next motor
325
                if(motorread >= MAX_MOTORS) { motorread = 0; }
298
                if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; }
326
                i2c_stop();
299
                I2C_Stop();
327
                twi_state = 0;
300
                twi_state = 0;
328
                break;
301
                break;
329
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
302
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
330
// writing Gyro-Offset
303
// writing Gyro-Offset
331
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
304
//++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Line 340... Line 313...
340
                break;
313
                break;
341
        case 21:
314
        case 21:
342
                i2c_write_byte(0x80); // Value
315
                i2c_write_byte(0x80); // Value
343
                break;
316
                break;
344
        case 22:
317
        case 22:
345
                i2c_stop();
318
                I2C_Stop();
346
                I2CTimeout = 10;
319
                I2CTimeout = 10;
347
                i2c_start();
320
                I2C_Start();
348
                break;
321
                break;
349
        case 23:
322
        case 23:
350
                i2c_write_byte(0x98); // Address of the DAC
323
                i2c_write_byte(0x98); // Address of the DAC
351
                break;
324
                break;
352
        case 24:
325
        case 24:
Line 357... Line 330...
357
                break;
330
                break;
358
        case 26:
331
        case 26:
359
                i2c_write_byte(0x80); // Value
332
                i2c_write_byte(0x80); // Value
360
                break;
333
                break;
361
        case 27:
334
        case 27:
362
                i2c_stop();
335
                I2C_Stop();
363
                I2CTimeout = 10;
336
                I2CTimeout = 10;
364
                i2c_start();
337
                I2C_Start();
365
                break;
338
                break;
366
        case 28:
339
        case 28:
367
                i2c_write_byte(0x98); // Address of the DAC
340
                i2c_write_byte(0x98); // Address of the DAC
368
                break;
341
                break;
369
        case 29:
342
        case 29:
Line 374... Line 347...
374
                break;
347
                break;
375
        case 31:
348
        case 31:
376
                i2c_write_byte(0x80); // Value
349
                i2c_write_byte(0x80); // Value
377
                break;
350
                break;
378
        case 32:
351
        case 32:
379
                i2c_stop();
352
                I2C_Stop();
380
                I2CTimeout = 10;
353
                I2CTimeout = 10;
381
                twi_state = 0;
354
                twi_state = 0;
382
                break;
355
                break;
383
        default: twi_state = 0;
356
        default: twi_state = 0;
384
                break;
357
                break;
385
                }
358
                }
386
 TWCR |= 0x80;
359
 TWCR |= 0x80;
387
}
360
}
-
 
361