Subversion Repositories FlightCtrl

Rev

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

Rev 1743 Rev 1744
Line 62... Line 62...
62
 
62
 
63
volatile uint8_t twi_state      = TWI_STATE_MOTOR_TX;
63
volatile uint8_t twi_state      = TWI_STATE_MOTOR_TX;
64
volatile uint8_t dac_channel    = 0;
64
volatile uint8_t dac_channel    = 0;
65
volatile uint8_t motor_write    = 0;
65
volatile uint8_t motor_write    = 0;
66
volatile uint8_t motor_read     = 0;
-
 
-
 
66
volatile uint8_t motor_read     = 0;
Line 67... Line 67...
67
 
67
volatile uint8_t I2C_TransferActive = 0;
Line 68... Line 68...
68
 
68
 
Line 134... Line 134...
134
 
134
 
135
void I2C_Reset(void)
135
void I2C_Reset(void)
136
{
136
{
137
        // stop i2c bus
137
        // stop i2c bus
138
        I2C_Stop(TWI_STATE_MOTOR_TX);
-
 
139
        motor_write     = 0;
-
 
140
        motor_read              = 0;
138
        I2C_Stop(TWI_STATE_MOTOR_TX);
141
        TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset
139
        TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset
142
        TWAMR = 0;
140
        TWAMR = 0;
143
        TWAR = 0;
141
        TWAR = 0;
144
        TWDR = 0;
142
        TWDR = 0;
145
        TWSR = 0;
143
        TWSR = 0;
-
 
144
        TWBR = 0;
146
        TWBR = 0;
145
    I2C_TransferActive = 0;    
147
        I2C_Init(0);
146
        I2C_Init(0);
148
        I2C_WriteByte(0);
147
        I2C_WriteByte(0);
149
        BLFlags |= BLFLAG_READ_VERSION;
148
        BLFlags |= BLFLAG_READ_VERSION;
Line 160... Line 159...
160
 
159
 
161
    switch (twi_state++)
160
    switch (twi_state++)
162
        {
161
        {
163
                // Master Transmit
162
                // Master Transmit
164
        case 0: // TWI_STATE_MOTOR_TX
-
 
-
 
163
        case 0: // TWI_STATE_MOTOR_TX
165
 
164
            I2C_TransferActive = 1;
166
                        // skip motor if not used in mixer
165
                        // skip motor if not used in mixer
167
                        while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++;
166
                        while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++;
168
                        if(motor_write >= MAX_MOTORS) // writing finished, read now
167
                        if(motor_write >= MAX_MOTORS) // writing finished, read now
169
                        {
168
                        {
Line 174... Line 173...
174
                        }
173
                        }
175
                        else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode
174
                        else I2C_WriteByte(TWI_BASE_ADDRESS + TW_WRITE + (motor_write<<1) ); // select slave address in tx mode
176
                        break;
175
                        break;
177
        case 1: // Send Data to Slave
176
        case 1: // Send Data to Slave
178
                        I2C_WriteByte(Motor[motor_write].SetPoint); // transmit setpoint
177
                        I2C_WriteByte(Motor[motor_write].SetPoint); // transmit setpoint
-
 
178
if(Motor[motor_write].SetPoint == 0) if(MotorenEin) DebugOut.Analog[17]++;
179
                        // if old version has been detected
179
                        // if old version has been detected
180
                        if(!(Motor[motor_write].Version & MOTOR_STATE_NEW_PROTOCOL_MASK))
180
                        if(!(Motor[motor_write].Version & MOTOR_STATE_NEW_PROTOCOL_MASK))
181
                        {
181
                        {
182
                                twi_state = 4; //jump over sending more data
182
                                twi_state = 4; //jump over sending more data
183
                        }
183
                        }
Line 240... Line 240...
240
                                                BLFlags &= ~BLFLAG_READ_VERSION;
240
                                                BLFlags &= ~BLFLAG_READ_VERSION;
241
                                        }
241
                                        }
242
                                }
242
                                }
243
                                BLFlags |= BLFLAG_TX_COMPLETE;
243
                                BLFlags |= BLFLAG_TX_COMPLETE;
244
                                I2C_Stop(TWI_STATE_MOTOR_TX);
244
                                I2C_Stop(TWI_STATE_MOTOR_TX);
-
 
245
                                I2C_TransferActive = 0;
245
                        }
246
                        }
246
                        else
247
                        else
247
                        {       // motor successfully addressed
248
                        {       // motor successfully addressed
248
                                Motor[motor_read].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
249
                                Motor[motor_read].State |= MOTOR_STATE_PRESENT_MASK; // set present bit
249
 
-
 
250
                                if(Motor[motor_read].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)
250
                                if(Motor[motor_read].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)
251
                                {
251
                                {
252
                                        // new BL found
252
                                        // new BL found
253
                                        switch(Motor[motor_read].ReadMode)
253
                                        switch(Motor[motor_read].ReadMode)
254
                                        {
254
                                        {
Line 311... Line 311...
311
                                                BLFlags &= ~BLFLAG_READ_VERSION;
311
                                                BLFlags &= ~BLFLAG_READ_VERSION;
312
                                        }
312
                                        }
313
                                }
313
                                }
314
                                I2C_Stop(TWI_STATE_MOTOR_TX);
314
                                I2C_Stop(TWI_STATE_MOTOR_TX);
315
                                BLFlags |= BLFLAG_TX_COMPLETE;
315
                                BLFlags |= BLFLAG_TX_COMPLETE;
-
 
316
                I2C_TransferActive = 0;
316
                                return;
317
                                return;
317
                        }
318
                        }
318
                        twi_state = 6; // if there are some bytes left
319
                        twi_state = 6; // if there are some bytes left
319
                        break;
320
                        break;
Line 346... Line 347...
346
                        I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
347
                        I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
347
                        break;
348
                        break;
Line 348... Line 349...
348
 
349
 
349
                case 22:
350
                case 22:
-
 
351
                        I2C_Stop(TWI_STATE_MOTOR_TX);
350
                        I2C_Stop(TWI_STATE_MOTOR_TX);
352
                        I2C_TransferActive = 0;
351
                        I2CTimeout = 10;
353
                        I2CTimeout = 10;
352
                        // repeat case 18...22 until all DAC Channels are updated
354
                        // repeat case 18...22 until all DAC Channels are updated
353
                        if(dac_channel < 2)
355
                        if(dac_channel < 2)
354
                        {
356
                        {
Line 359... Line 361...
359
                        {
361
                        {
360
                                dac_channel = 0; // reset dac channel counter
362
                                dac_channel = 0; // reset dac channel counter
361
                                BLFlags |= BLFLAG_TX_COMPLETE;
363
                                BLFlags |= BLFLAG_TX_COMPLETE;
362
                        }
364
                        }
363
                        break;
365
                        break;
364
 
-
 
365
        default:
366
        default:
366
                        I2C_Stop(TWI_STATE_MOTOR_TX);
367
                        I2C_Stop(TWI_STATE_MOTOR_TX);
367
                        BLFlags |= BLFLAG_TX_COMPLETE;
368
                        BLFlags |= BLFLAG_TX_COMPLETE;
368
                        I2CTimeout = 10;
369
                        I2CTimeout = 10;
369
                        motor_write = 0;
370
                        motor_write = 0;
370
                        motor_read = 0;
371
                        motor_read = 0;
-
 
372
                        I2C_TransferActive = 0;
371
                        break;
373
                        break;
372
        }
374
        }
Line 373... Line 375...
373
 
375