Subversion Repositories FlightCtrl

Rev

Rev 1984 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 ingob 1
#ifndef _I2C_MASTER_H
2
#define _I2C_MASTER_H
1662 killagreg 3
 
1 ingob 4
 
1662 killagreg 5
#include <inttypes.h>
1 ingob 6
 
1662 killagreg 7
#define TWI_STATE_MOTOR_TX                      0
8
#define TWI_STATE_MOTOR_RX                      5
9
#define TWI_STATE_GYRO_OFFSET_TX        18
1 ingob 10
 
1662 killagreg 11
extern volatile uint8_t twi_state;
12
extern volatile uint8_t motor_write;
13
extern volatile uint8_t motor_read;
1744 holgerb 14
extern volatile uint8_t I2C_TransferActive;
1 ingob 15
 
1662 killagreg 16
extern uint8_t MissingMotor;
17
 
1479 killagreg 18
#define MAX_MOTORS      12
1638 holgerb 19
#define MOTOR_STATE_PRESENT_MASK                0x80
20
#define MOTOR_STATE_ERROR_MASK                  0x7F
1479 killagreg 21
 
1638 holgerb 22
#define MOTOR_STATE_NEW_PROTOCOL_MASK   0x01
23
 
1662 killagreg 24
#define BLFLAG_TX_COMPLETE              0x01
25
#define BLFLAG_READ_VERSION     0x02
1648 killagreg 26
 
1662 killagreg 27
extern volatile uint8_t BLFlags;
1648 killagreg 28
 
1666 killagreg 29
 
30
#define BL_READMODE_STATUS  0
31
#define BL_READMODE_CONFIG      16
32
 
1479 killagreg 33
typedef struct
34
{
1662 killagreg 35
        uint8_t Version;                        // the version of the BL (0 = old)
36
        uint8_t SetPoint;                       // written by attitude controller
37
        uint8_t SetPointLowerBits;      // for higher Resolution of new BLs
38
        uint8_t State;                          // 7 bit for I2C error counter, highest bit indicates if motor is present
1666 killagreg 39
        uint8_t ReadMode;                       // select data to read
1662 killagreg 40
        // the following bytes must be exactly in that order!
41
        uint8_t Current;                        // in 0.1 A steps, read back from BL
1948 holgerb 42
        uint8_t MaxPWM;                         // read back from BL -> is less than 255 if BL is in current limit, not running (250) or starting (40)
1662 killagreg 43
        int8_t  Temperature;            // old BL-Ctrl will return a 255 here, the new version the temp. in °C
1479 killagreg 44
} __attribute__((packed)) MotorData_t;
45
 
46
extern MotorData_t Motor[MAX_MOTORS];
47
 
1680 holgerb 48
#define BLCONFIG_REVISION 2
1 ingob 49
 
1662 killagreg 50
#define MASK_SET_PWM_SCALING            0x01
51
#define MASK_SET_CURRENT_LIMIT          0x02
52
#define MASK_SET_TEMP_LIMIT                     0x04
53
#define MASK_SET_CURRENT_SCALING        0x08
54
#define MASK_SET_BITCONFIG                      0x10
55
#define MASK_RESET_CAPCOUNTER           0x20
56
#define MASK_SET_DEFAULT_PARAMS         0x40
57
#define MASK_SET_SAVE_EEPROM            0x80
1639 holgerb 58
 
1662 killagreg 59
#define BITCONF_REVERSE_ROTATION 0x01
60
#define BITCONF_RES1 0x02
61
#define BITCONF_RES2 0x04
62
#define BITCONF_RES3 0x08
63
#define BITCONF_RES4 0x10
64
#define BITCONF_RES5 0x20
65
#define BITCONF_RES6 0x40
66
#define BITCONF_RES7 0x80
67
 
68
typedef struct
69
{
70
        uint8_t Revision;                       // must be BL_REVISION
71
        uint8_t SetMask;                        // settings mask
72
        uint8_t PwmScaling;                     // maximum value of control pwm, acts like a thrust limit
73
        uint8_t CurrentLimit;           // current limit in A
74
        uint8_t TempLimit;                      // in °C
75
        uint8_t CurrentScaling;         // scaling factor for current measurement
76
        uint8_t BitConfig;                      // see defines above
77
        uint8_t crc;                            // checksum
78
}  __attribute__((packed)) BLConfig_t;
79
 
80
extern BLConfig_t BLConfig;
81
 
82
extern volatile uint16_t I2CTimeout;
83
 
1743 holgerb 84
void I2C_Init(char); // Initialize I2C
1662 killagreg 85
#define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);}
86
#define I2C_Stop(start_state)  {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);}
87
void I2C_Reset(void); // Reset I2C
1665 killagreg 88
 
89
#define BLCONFIG_SUCCESS                             0
90
#define BLCONFIG_ERR_MOTOR_RUNNING       1
91
#define BLCONFIG_ERR_MOTOR_NOT_EXIST     2
92
#define BLCONFIG_ERR_HW_NOT_COMPATIBLE   3
93
#define BLCONFIG_ERR_SW_NOT_COMPATIBLE   4
94
#define BLCONFIG_ERR_CHECKSUM            5
1673 killagreg 95
#define BLCONFIG_ERR_READ_NOT_POSSIBLE   6
1665 killagreg 96
 
1662 killagreg 97
uint8_t I2C_WriteBLConfig(uint8_t motor);
98
uint8_t I2C_ReadBLConfig(uint8_t motor);
99
 
1 ingob 100
#endif