Subversion Repositories FlightCtrl

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2506 - 1
#ifndef _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
10
 
11
extern volatile uint8_t twi_state, ReadBlSize;
12
extern volatile uint8_t motor_write;
13
extern volatile uint8_t motor_read;
14
extern volatile uint8_t I2C_TransferActive;
15
extern uint8_t Max_I2C_Packets;
16
extern uint8_t MissingMotor;
17
 
18
#define MAX_MOTORS      12
19
#define MOTOR_STATE_PRESENT_MASK                0x80
20
#define MOTOR_STATE_ERROR_MASK                  0x7F
21
 
22
//Motor[x].Version
23
#define MOTOR_STATE_NEW_PROTOCOL_MASK   0x01
24
#define MOTOR_STATE_FAST_MODE           0x02
25
#define MOTOR_STATE_BL30                0x04   // extended Current measurement -> 200 = 20A    201 = 21A    255 = 75A (20+55)
26
 
27
#define BLFLAG_TX_COMPLETE              0x01
28
#define BLFLAG_READ_VERSION     0x02
29
 
30
extern volatile uint8_t BLFlags;
31
 
32
#define BL_READMODE_STATUS  0
33
#define BL_READMODE_CONFIG      16
34
 
35
typedef struct
36
{
37
        uint8_t Version;                        // the version of the BL (0 = old)
38
        uint8_t SetPoint;                       // written by attitude controller
39
        uint8_t SetPointLowerBits;      // for higher Resolution of new BLs
40
        uint8_t State;                          // 7 bit for I2C error counter, highest bit indicates if motor is present
41
        uint8_t ReadMode;                       // select data to read
42
        // the following bytes must be exactly in that order!
43
        uint8_t Current;                        // in 0.1 A steps, read back from BL
44
        uint8_t MaxPWM;                         // read back from BL -> is less than 255 if BL is in current limit, not running (250) or starting (40)
45
        uint8_t Temperature;            // old BL-Ctrl will return a 255 here, the new version the temp. in °C
46
        uint8_t RPM;                            // Raw value for RPM
47
        uint8_t reserved1;                      // Voltage (BL3) or mAh (BL2)
48
        uint8_t Voltage;                        // in 0.1V (BL3 is limited to 255, BL2 is only low-byte)
49
        uint8_t SlaveI2cError;          // BL2 & BL3
50
        uint8_t VersionMajor;           // BL2 & BL3
51
        uint8_t VersionMinor;           // BL2 & BL3
52
        uint8_t NotReadyCnt;            // Counts up is the Motor is not ready during flight -> MotorRestart etc.
53
} __attribute__((packed)) MotorData_t;
54
 
55
extern MotorData_t Motor[MAX_MOTORS];
56
 
57
// BitSate
58
#define BL_BIT_STATE_I2C_OK     0x01
59
#define BL_BIT_STATE_I2C_VALUE  0x02
60
#define BL_BIT_STATE_I2C_BAD    0x04
61
#define BL_BIT_STATE_PPM_OK             0x08
62
#define BL_BIT_STATE_MOTOR_RUN  0x10
63
 
64
typedef struct
65
{
66
        unsigned char BitSate;
67
        unsigned char Current;
68
        unsigned char State;  
69
        unsigned char TemperatureInDeg;  
70
        unsigned char Voltage80;         
71
} __attribute__((packed)) RedundantBl_t;
72
extern RedundantBl_t RedundantMotor[MAX_MOTORS];
73
 
74
#define BLCONFIG_REVISION 2
75
 
76
#define MASK_SET_PWM_SCALING            0x01
77
#define MASK_SET_CURRENT_LIMIT          0x02
78
#define MASK_SET_TEMP_LIMIT                     0x04
79
#define MASK_SET_CURRENT_SCALING        0x08
80
#define MASK_SET_BITCONFIG                      0x10
81
#define MASK_SET_STARTPWM                       0x20
82
#define MASK_SET_DEFAULT_PARAMS         0x40
83
#define MASK_SET_SAVE_EEPROM            0x80
84
 
85
#define BITCONF_REVERSE_ROTATION 0x01
86
#define BITCONF_STARTGAS1 0x02
87
#define BITCONF_STARTGAS2 0x04
88
#define BITCONF_STARTGAS3 0x08
89
#define BITCONF_RES4 0x10
90
#define BITCONF_RES5 0x20
91
#define BITCONF_RES6 0x40
92
#define BITCONF_RES7 0x80
93
 
94
typedef struct
95
{
96
        uint8_t Revision;                       // must be BL_REVISION
97
        uint8_t SetMask;                        // settings mask
98
        uint8_t PwmScaling;                     // maximum value of control pwm, acts like a thrust limit
99
        uint8_t CurrentLimit;           // current limit in A
100
        uint8_t TempLimit;                      // in °C
101
        uint8_t CurrentScaling;         // scaling factor for current measurement
102
        uint8_t BitConfig;                      // see defines above
103
        uint8_t crc;                            // checksum
104
}  __attribute__((packed)) BLConfig_t;
105
 
106
extern BLConfig_t BLConfig;
107
 
108
extern volatile uint16_t I2CTimeout;
109
 
110
void I2C_Init(char); // Initialize I2C
111
#define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);}
112
#define I2C_Stop(start_state)  {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);}
113
void I2C_Reset(void); // Reset I2C
114
 
115
#define BLCONFIG_SUCCESS                             0
116
#define BLCONFIG_ERR_MOTOR_RUNNING       1
117
#define BLCONFIG_ERR_MOTOR_NOT_EXIST     2
118
#define BLCONFIG_ERR_HW_NOT_COMPATIBLE   3
119
#define BLCONFIG_ERR_SW_NOT_COMPATIBLE   4
120
#define BLCONFIG_ERR_CHECKSUM            5
121
#define BLCONFIG_ERR_READ_NOT_POSSIBLE   6
122
 
123
uint8_t I2C_WriteBLConfig(uint8_t motor);
124
uint8_t I2C_ReadBLConfig(uint8_t motor);
125
 
126
#endif