Rev 2386 | Rev 2418 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2386 | Rev 2407 | ||
---|---|---|---|
1 | #ifndef _I2C_MASTER_H |
1 | #ifndef _I2C_MASTER_H |
2 | #define _I2C_MASTER_H |
2 | #define _I2C_MASTER_H |
3 | 3 | ||
4 | 4 | ||
5 | #include <inttypes.h> |
5 | #include <inttypes.h> |
6 | 6 | ||
7 | #define TWI_STATE_MOTOR_TX 0 |
7 | #define TWI_STATE_MOTOR_TX 0 |
8 | #define TWI_STATE_MOTOR_RX 5 |
8 | #define TWI_STATE_MOTOR_RX 5 |
9 | #define TWI_STATE_GYRO_OFFSET_TX 18 |
9 | #define TWI_STATE_GYRO_OFFSET_TX 18 |
10 | 10 | ||
11 | extern volatile uint8_t twi_state; |
11 | extern volatile uint8_t twi_state; |
12 | extern volatile uint8_t motor_write; |
12 | extern volatile uint8_t motor_write; |
13 | extern volatile uint8_t motor_read; |
13 | extern volatile uint8_t motor_read; |
14 | extern volatile uint8_t I2C_TransferActive; |
- | |
- | 14 | extern volatile uint8_t I2C_TransferActive; |
|
15 | 15 | extern uint8_t Max_I2C_Packets; |
|
16 | extern uint8_t MissingMotor; |
16 | extern uint8_t MissingMotor; |
17 | 17 | ||
18 | #define MAX_MOTORS 12 |
18 | #define MAX_MOTORS 12 |
19 | #define MOTOR_STATE_PRESENT_MASK 0x80 |
19 | #define MOTOR_STATE_PRESENT_MASK 0x80 |
20 | #define MOTOR_STATE_ERROR_MASK 0x7F |
20 | #define MOTOR_STATE_ERROR_MASK 0x7F |
21 | 21 | ||
22 | //Motor[x].Version |
22 | //Motor[x].Version |
23 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
23 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
24 | #define MOTOR_STATE_FAST_MODE 0x02 |
24 | #define MOTOR_STATE_FAST_MODE 0x02 |
25 | #define MOTOR_STATE_BL30 0x04 // extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
25 | #define MOTOR_STATE_BL30 0x04 // extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55) |
26 | 26 | ||
27 | #define BLFLAG_TX_COMPLETE 0x01 |
27 | #define BLFLAG_TX_COMPLETE 0x01 |
28 | #define BLFLAG_READ_VERSION 0x02 |
28 | #define BLFLAG_READ_VERSION 0x02 |
29 | 29 | ||
30 | extern volatile uint8_t BLFlags; |
30 | extern volatile uint8_t BLFlags; |
31 | 31 | ||
32 | #define BL_READMODE_STATUS 0 |
32 | #define BL_READMODE_STATUS 0 |
33 | #define BL_READMODE_CONFIG 16 |
33 | #define BL_READMODE_CONFIG 16 |
34 | 34 | ||
35 | typedef struct |
35 | typedef struct |
36 | { |
36 | { |
37 | uint8_t Version; // the version of the BL (0 = old) |
37 | uint8_t Version; // the version of the BL (0 = old) |
38 | uint8_t SetPoint; // written by attitude controller |
38 | uint8_t SetPoint; // written by attitude controller |
39 | uint8_t SetPointLowerBits; // for higher Resolution of new BLs |
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 |
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 |
41 | uint8_t ReadMode; // select data to read |
42 | // the following bytes must be exactly in that order! |
42 | // the following bytes must be exactly in that order! |
43 | uint8_t Current; // in 0.1 A steps, read back from BL |
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) |
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 | int8_t Temperature; // old BL-Ctrl will return a 255 here, the new version the temp. in °C |
45 | int8_t Temperature; // old BL-Ctrl will return a 255 here, the new version the temp. in °C |
46 | } __attribute__((packed)) MotorData_t; |
46 | } __attribute__((packed)) MotorData_t; |
47 | 47 | ||
48 | extern MotorData_t Motor[MAX_MOTORS]; |
48 | extern MotorData_t Motor[MAX_MOTORS]; |
49 | 49 | ||
50 | #define BLCONFIG_REVISION 2 |
50 | #define BLCONFIG_REVISION 2 |
51 | 51 | ||
52 | #define MASK_SET_PWM_SCALING 0x01 |
52 | #define MASK_SET_PWM_SCALING 0x01 |
53 | #define MASK_SET_CURRENT_LIMIT 0x02 |
53 | #define MASK_SET_CURRENT_LIMIT 0x02 |
54 | #define MASK_SET_TEMP_LIMIT 0x04 |
54 | #define MASK_SET_TEMP_LIMIT 0x04 |
55 | #define MASK_SET_CURRENT_SCALING 0x08 |
55 | #define MASK_SET_CURRENT_SCALING 0x08 |
56 | #define MASK_SET_BITCONFIG 0x10 |
56 | #define MASK_SET_BITCONFIG 0x10 |
57 | #define MASK_SET_STARTPWM 0x20 |
57 | #define MASK_SET_STARTPWM 0x20 |
58 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
58 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
59 | #define MASK_SET_SAVE_EEPROM 0x80 |
59 | #define MASK_SET_SAVE_EEPROM 0x80 |
60 | 60 | ||
61 | #define BITCONF_REVERSE_ROTATION 0x01 |
61 | #define BITCONF_REVERSE_ROTATION 0x01 |
62 | #define BITCONF_STARTGAS1 0x02 |
62 | #define BITCONF_STARTGAS1 0x02 |
63 | #define BITCONF_STARTGAS2 0x04 |
63 | #define BITCONF_STARTGAS2 0x04 |
64 | #define BITCONF_STARTGAS3 0x08 |
64 | #define BITCONF_STARTGAS3 0x08 |
65 | #define BITCONF_RES4 0x10 |
65 | #define BITCONF_RES4 0x10 |
66 | #define BITCONF_RES5 0x20 |
66 | #define BITCONF_RES5 0x20 |
67 | #define BITCONF_RES6 0x40 |
67 | #define BITCONF_RES6 0x40 |
68 | #define BITCONF_RES7 0x80 |
68 | #define BITCONF_RES7 0x80 |
69 | 69 | ||
70 | typedef struct |
70 | typedef struct |
71 | { |
71 | { |
72 | uint8_t Revision; // must be BL_REVISION |
72 | uint8_t Revision; // must be BL_REVISION |
73 | uint8_t SetMask; // settings mask |
73 | uint8_t SetMask; // settings mask |
74 | uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit |
74 | uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit |
75 | uint8_t CurrentLimit; // current limit in A |
75 | uint8_t CurrentLimit; // current limit in A |
76 | uint8_t TempLimit; // in °C |
76 | uint8_t TempLimit; // in °C |
77 | uint8_t CurrentScaling; // scaling factor for current measurement |
77 | uint8_t CurrentScaling; // scaling factor for current measurement |
78 | uint8_t BitConfig; // see defines above |
78 | uint8_t BitConfig; // see defines above |
79 | uint8_t crc; // checksum |
79 | uint8_t crc; // checksum |
80 | } __attribute__((packed)) BLConfig_t; |
80 | } __attribute__((packed)) BLConfig_t; |
81 | 81 | ||
82 | extern BLConfig_t BLConfig; |
82 | extern BLConfig_t BLConfig; |
83 | 83 | ||
84 | extern volatile uint16_t I2CTimeout; |
84 | extern volatile uint16_t I2CTimeout; |
85 | 85 | ||
86 | void I2C_Init(char); // Initialize I2C |
86 | void I2C_Init(char); // Initialize I2C |
87 | #define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
87 | #define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
88 | #define I2C_Stop(start_state) {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
88 | #define I2C_Stop(start_state) {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
89 | void I2C_Reset(void); // Reset I2C |
89 | void I2C_Reset(void); // Reset I2C |
90 | 90 | ||
91 | #define BLCONFIG_SUCCESS 0 |
91 | #define BLCONFIG_SUCCESS 0 |
92 | #define BLCONFIG_ERR_MOTOR_RUNNING 1 |
92 | #define BLCONFIG_ERR_MOTOR_RUNNING 1 |
93 | #define BLCONFIG_ERR_MOTOR_NOT_EXIST 2 |
93 | #define BLCONFIG_ERR_MOTOR_NOT_EXIST 2 |
94 | #define BLCONFIG_ERR_HW_NOT_COMPATIBLE 3 |
94 | #define BLCONFIG_ERR_HW_NOT_COMPATIBLE 3 |
95 | #define BLCONFIG_ERR_SW_NOT_COMPATIBLE 4 |
95 | #define BLCONFIG_ERR_SW_NOT_COMPATIBLE 4 |
96 | #define BLCONFIG_ERR_CHECKSUM 5 |
96 | #define BLCONFIG_ERR_CHECKSUM 5 |
97 | #define BLCONFIG_ERR_READ_NOT_POSSIBLE 6 |
97 | #define BLCONFIG_ERR_READ_NOT_POSSIBLE 6 |
98 | 98 | ||
99 | uint8_t I2C_WriteBLConfig(uint8_t motor); |
99 | uint8_t I2C_WriteBLConfig(uint8_t motor); |
100 | uint8_t I2C_ReadBLConfig(uint8_t motor); |
100 | uint8_t I2C_ReadBLConfig(uint8_t motor); |
101 | 101 | ||
102 | #endif |
102 | #endif |
103 | 103 |