Subversion Repositories FlightCtrl

Rev

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

Rev 1680 Rev 1683
Line 56... Line 56...
56
#include "eeprom.h"
56
#include "eeprom.h"
57
#include "twimaster.h"
57
#include "twimaster.h"
58
#include "fc.h"
58
#include "fc.h"
59
#include "analog.h"
59
#include "analog.h"
60
#include "uart.h"
60
#include "uart.h"
-
 
61
#include "timer0.h"
Line 61... Line 62...
61
 
62
 
62
volatile uint8_t twi_state      = TWI_STATE_MOTOR_TX;
63
volatile uint8_t twi_state      = TWI_STATE_MOTOR_TX;
63
volatile uint8_t dac_channel    = 0;
64
volatile uint8_t dac_channel    = 0;
64
volatile uint8_t motor_write    = 0;
65
volatile uint8_t motor_write    = 0;
Line 232... Line 233...
232
                                if(++motor_read >= MAX_MOTORS)
233
                                if(++motor_read >= MAX_MOTORS)
233
                                {       // all motors read
234
                                {       // all motors read
234
                                        motor_read = 0;                 // restart from beginning
235
                                        motor_read = 0;                 // restart from beginning
235
                                        BLConfig_ReadMask = 0;  // reset read configuration bitmask
236
                                        BLConfig_ReadMask = 0;  // reset read configuration bitmask
236
                                        if(++motor_read_temperature >= MAX_MOTORS)
237
                                        if(++motor_read_temperature >= MAX_MOTORS)
237
                                        {
238
                                        {
238
                                                motor_read_temperature = 0;
239
                                                motor_read_temperature = 0;
239
                                                BLFlags &= ~BLFLAG_READ_VERSION;
240
                                                BLFlags &= ~BLFLAG_READ_VERSION;
240
                                        }
241
                                        }
241
                                }
242
                                }
242
                                BLFlags |= BLFLAG_TX_COMPLETE;
243
                                BLFlags |= BLFLAG_TX_COMPLETE;
Line 374... Line 375...
374
 
375
 
375
 
376
 
376
uint8_t I2C_WriteBLConfig(uint8_t motor)
377
uint8_t I2C_WriteBLConfig(uint8_t motor)
-
 
378
{
Line 377... Line 379...
377
{
379
        uint8_t i;
378
        uint8_t i;
380
        uint16_t timer;
379
 
381
 
380
        if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING);        // not when motors are running!
382
        if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING);        // not when motors are running!
Line 387... Line 389...
387
        // check BL configuration to send
389
        // check BL configuration to send
388
        if(BLConfig.Revision != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison
390
        if(BLConfig.Revision != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison
389
        i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1);
391
        i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1);
390
        if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum
392
        if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum
Line -... Line 393...
-
 
393
 
391
 
394
        timer = SetDelay(2000);
Line 392... Line 395...
392
        while(!(BLFlags & BLFLAG_TX_COMPLETE));         //wait for complete transfer
395
        while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer));   //wait for complete transfer
393
 
396
 
394
        // prepare the bitmask
397
        // prepare the bitmask
395
        if(!motor) // 0 means all
398
        if(!motor) // 0 means all
Line 412... Line 415...
412
        motor_write = 0;
415
        motor_write = 0;
413
        // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms)
416
        // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms)
414
        do
417
        do
415
        {
418
        {
416
                I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission
419
                I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission
417
                while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer
420
                while(!(BLFlags & BLFLAG_TX_COMPLETE)  && !CheckDelay(timer)); //wait for complete transfer
418
        }while(BLConfig_WriteMask); // repeat until the BL config has been sent
421
        }while(BLConfig_WriteMask  && !CheckDelay(timer)); // repeat until the BL config has been sent
-
 
422
        if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST);
419
        return(BLCONFIG_SUCCESS);
423
        return(BLCONFIG_SUCCESS);
420
}
424
}
Line 421... Line 425...
421
 
425
 
422
uint8_t I2C_ReadBLConfig(uint8_t motor)
426
uint8_t I2C_ReadBLConfig(uint8_t motor)
423
{
427
{
-
 
428
        uint8_t i;
Line 424... Line 429...
424
        uint8_t i;
429
        uint16_t timer;
425
 
430
 
426
        if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running!
431
        if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running!
427
        if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST);           // motor does not exist!
432
        if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST);           // motor does not exist!
428
        if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE);
433
        if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE);
Line -... Line 434...
-
 
434
        if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist!
429
        if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist!
435
        if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL!
Line 430... Line 436...
430
        if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL!
436
 
431
 
437
        timer = SetDelay(2000);
Line 432... Line 438...
432
        while(!(BLFlags & BLFLAG_TX_COMPLETE));                                 //wait for complete transfer
438
        while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer));                           //wait for complete transfer
Line 448... Line 454...
448
        BLConfig.crc = 0;          // bad checksum
454
        BLConfig.crc = 0;          // bad checksum
449
        // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms)
455
        // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms)
450
        do
456
        do
451
        {
457
        {
452
                I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission
458
                I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission
453
                while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer
459
                while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer
454
        }while(BLConfig_ReadMask); // repeat until the BL config has been received from all motors
460
        }while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors
455
        // validate result
461
        // validate result
456
        if(BLConfig.Revision != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison
462
        if(BLConfig.Revision != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison
457
        i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1);
463
        i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1);
458
        if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum
464
        if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum
459
        return(BLCONFIG_SUCCESS);
465
        return(BLCONFIG_SUCCESS);