Subversion Repositories FlightCtrl

Rev

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

Rev 1657 Rev 1662
1
/*############################################################################
-
 
2
############################################################################*/
-
 
3
 
-
 
4
#ifndef _I2C_MASTER_H
1
#ifndef _I2C_MASTER_H
5
#define _I2C_MASTER_H
2
#define _I2C_MASTER_H
-
 
3
 
-
 
4
 
-
 
5
#include <inttypes.h>
-
 
6
 
-
 
7
#define TWI_STATE_MOTOR_TX                      0
-
 
8
#define TWI_STATE_MOTOR_RX                      5
-
 
9
#define TWI_STATE_GYRO_OFFSET_TX        18
6
 
10
 
-
 
11
extern volatile uint8_t twi_state;
7
//############################################################################
-
 
8
 
-
 
9
// I2C Konstanten
-
 
10
#define SCL_CLOCK  200000L
-
 
11
#define I2C_TIMEOUT 30000
-
 
12
#define I2C_START          0x08
-
 
13
#define I2C_REPEATED_START 0x10
-
 
14
#define I2C_TX_SLA_ACK     0x18
-
 
15
#define I2C_TX_DATA_ACK    0x28
-
 
16
#define I2C_RX_SLA_ACK     0x40
-
 
17
#define I2C_RX_DATA_ACK    0x50
-
 
18
 
12
extern volatile uint8_t motor_write;
19
//############################################################################
-
 
20
 
13
extern volatile uint8_t motor_read;
21
extern volatile unsigned char twi_state;
-
 
22
extern volatile unsigned char motor,MissingMotor;
14
 
23
extern volatile unsigned char motorread;
15
 
24
 
16
extern uint8_t MissingMotor;
25
#define MAX_MOTORS      12
17
 
26
#define MOTOR_STATE_PRESENT_MASK                0x80
18
#define MAX_MOTORS      12
27
#define MOTOR_STATE_ERROR_MASK                  0x7F
19
#define MOTOR_STATE_PRESENT_MASK                0x80
28
 
20
#define MOTOR_STATE_ERROR_MASK                  0x7F
29
#define MOTOR_STATE_NEW_PROTOCOL_MASK   0x01
21
 
30
 
22
#define MOTOR_STATE_NEW_PROTOCOL_MASK   0x01
31
#define BLFLAG_READ_VERSION     0x01
23
 
32
#define BLFLAG_SEND_CONFIG              0x02
-
 
33
#define BLFLAG_TX_COMPLETE              0x04
24
#define BLFLAG_TX_COMPLETE              0x01
34
 
25
#define BLFLAG_READ_VERSION     0x02
35
extern volatile unsigned char BLFlags;
26
 
36
 
27
extern volatile uint8_t BLFlags;
37
typedef struct
28
 
38
{
29
typedef struct
-
 
30
{
39
        unsigned char SetPoint; // written by attitude controller
31
        uint8_t Version;                        // the version of the BL (0 = old)
-
 
32
        uint8_t SetPoint;                       // written by attitude controller
40
        unsigned char State;    // 7 bit for I2C error counter, highest bit indicates if motor is present
33
        uint8_t SetPointLowerBits;      // for higher Resolution of new BLs
-
 
34
        uint8_t State;                          // 7 bit for I2C error counter, highest bit indicates if motor is present
41
        unsigned char Current;  // in 0.1 A steps, read back from BL
35
        // the following bytes must be exactly in that order!
42
        unsigned char MaxPWM;   // read back from BL is less than 255 if BL is in current limit
36
        uint8_t Current;                        // in 0.1 A steps, read back from BL
43
        unsigned char Temperature; // old BL-Ctrl will return a 255 here
37
        uint8_t MaxPWM;                         // read back from BL is less than 255 if BL is in current limit
44
        unsigned char SetPointLowerBits; // for higher Resolution
-
 
45
    unsigned char Version;
-
 
46
} __attribute__((packed)) MotorData_t;
38
        int8_t  Temperature;            // old BL-Ctrl will return a 255 here, the new version the temp. in °C
47
 
39
} __attribute__((packed)) MotorData_t;
48
extern MotorData_t Motor[MAX_MOTORS];
40
 
-
 
41
extern MotorData_t Motor[MAX_MOTORS];
-
 
42
 
-
 
43
#define BLCONFIG_REVISION 1
-
 
44
 
-
 
45
#define MASK_SET_PWM_SCALING            0x01
-
 
46
#define MASK_SET_CURRENT_LIMIT          0x02
-
 
47
#define MASK_SET_TEMP_LIMIT                     0x04
-
 
48
#define MASK_SET_CURRENT_SCALING        0x08
-
 
49
#define MASK_SET_BITCONFIG                      0x10
-
 
50
#define MASK_RESET_CAPCOUNTER           0x20
-
 
51
#define MASK_SET_DEFAULT_PARAMS         0x40
-
 
52
#define MASK_SET_SAVE_EEPROM            0x80
49
 
53
 
-
 
54
#define BITCONF_REVERSE_ROTATION 0x01
-
 
55
#define BITCONF_RES1 0x02
-
 
56
#define BITCONF_RES2 0x04
-
 
57
#define BITCONF_RES3 0x08
-
 
58
#define BITCONF_RES4 0x10
-
 
59
#define BITCONF_RES5 0x20
-
 
60
#define BITCONF_RES6 0x40
-
 
61
#define BITCONF_RES7 0x80
-
 
62
 
-
 
63
typedef struct
-
 
64
{
-
 
65
        uint8_t Revision;                       // must be BL_REVISION
50
void i2c_reset(void);
66
        uint8_t SetMask;                        // settings mask
-
 
67
        uint8_t PwmScaling;                     // maximum value of control pwm, acts like a thrust limit
51
extern void i2c_init (void); // I2C initialisieren
68
        uint8_t CurrentLimit;           // current limit in A
-
 
69
        uint8_t TempLimit;                      // in °C
-
 
70
        uint8_t CurrentScaling;         // scaling factor for current measurement
-
 
71
        uint8_t BitConfig;                      // see defines above
-
 
72
        uint8_t crc;                            // checksum
52
extern void i2c_write_byte (char byte); // 1 Byte schreiben
73
}  __attribute__((packed)) BLConfig_t;
-
 
74
 
53
extern void i2c_reset(void);
75
extern BLConfig_t BLConfig;
54
extern void I2C_SendBLConfig(void);
76
 
-
 
77
extern volatile uint16_t I2CTimeout;
55
 
78
 
56
#define I2C_Start() {BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);}
79
void I2C_Init (void); // Initialize I2C
57
#define I2C_Stop() {TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);}
80
#define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);}
58
#define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);}
81
#define I2C_Stop(start_state)  {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);}
59
#define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);}
82
void I2C_Reset(void); // Reset I2C
60
#define i2c_write_byte(byte) {TWSR = 0x00; TWDR = byte;  TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);}
83
uint8_t I2C_WriteBLConfig(uint8_t motor);
61
 
84
uint8_t I2C_ReadBLConfig(uint8_t motor);
62
#endif
85
 
63
 
86
#endif
-
 
87