Subversion Repositories FlightCtrl

Rev

Rev 1639 | Rev 1650 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*############################################################################
############################################################################*/


#ifndef _I2C_MASTER_H
#define _I2C_MASTER_H

//############################################################################

// I2C Konstanten
#define SCL_CLOCK  200000L
#define I2C_TIMEOUT 30000
#define I2C_START          0x08
#define I2C_REPEATED_START 0x10
#define I2C_TX_SLA_ACK     0x18
#define I2C_TX_DATA_ACK    0x28
#define I2C_RX_SLA_ACK     0x40
#define I2C_RX_DATA_ACK    0x50

//############################################################################

extern volatile unsigned char twi_state;
extern unsigned char motor,MissingMotor;
extern unsigned char motorread;

#define MAX_MOTORS      12
#define MOTOR_STATE_PRESENT_MASK                0x80
#define MOTOR_STATE_ERROR_MASK                  0x7F


#define MOTOR_STATE_NEW_PROTOCOL_MASK   0x01

#define BLFLAG_READ_VERSION     0x01
#define BLFLAG_SEND_CONFIG              0x02
#define BLFLAG_TX_COMPLETE              0x04

extern unsigned char BLFlags;

#define MASK_SET_PWM_SCALING            0x01
#define MASK_SET_CURRENT_LIMIT          0x02
#define MASK_SET_TEMP_LIMIT                     0x04
#define MASK_SET_CURRENT_SCALING        0x08
#define MASK_SET_BITCONFIG                      0x10
#define MASK_RESET_CAPCOUNTER           0x20
#define MASK_SET_DEFAULT_PARAMS         0x40
#define MASK_SET_SAVE_EEPROM            0x80

#define BITCONF_REVERSE_ROTATION 0x01
#define BITCONF_RES1 0x02
#define BITCONF_RES2 0x04
#define BITCONF_RES3 0x08
#define BITCONF_RES4 0x10
#define BITCONF_RES5 0x20
#define BITCONF_RES6 0x40
#define BITCONF_RES7 0x80

typedef struct
{
        unsigned char SetPoint; // written by attitude controller
        unsigned char State;    // 7 bit for I2C error counter, highest bit indicates if motor is present
        unsigned char Current;  // in 0.1 A steps, read back from BL
        unsigned char MaxPWM;   // read back from BL is less than 255 if BL is in current limit
        unsigned char Temperature; // old BL-Ctrl will return a 255 here
        unsigned char SetPointLowerBits; // for higher Resolution
    unsigned char Version;
} __attribute__((packed)) MotorData_t;

extern MotorData_t Motor[MAX_MOTORS];

typedef struct
{
        unsigned char SetMask;                  // settings mask
        unsigned char PwmScaling;                       // maximum value of control pwm, acts like a thrust limit
        unsigned char CurrentLimit;             // current limit in A
        unsigned char TempLimit;                        // in °C
        unsigned char CurrentScaling;           // scaling factor for current measurement
        unsigned char BitConfig;                        // see defines below
}  __attribute__((packed)) BLConfig_t;

extern BLConfig_t BLConfig[MAX_MOTORS];

void i2c_reset(void);
extern void i2c_init (void); // I2C initialisieren
extern void i2c_write_byte (char byte); // 1 Byte schreiben
extern void i2c_reset(void);
extern void I2C_SendBLConfig(void);

#define I2C_Start()  {TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);}
#define I2C_Stop() {TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);}
#define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);}
#define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);}
#define i2c_write_byte(byte) {TWSR = 0x00; TWDR = byte;  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);}

#endif