Rev 1662 | Rev 1666 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1662 | Rev 1665 | ||
---|---|---|---|
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 | 14 | ||
15 | 15 | ||
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 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
22 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
23 | 23 | ||
24 | #define BLFLAG_TX_COMPLETE 0x01 |
24 | #define BLFLAG_TX_COMPLETE 0x01 |
25 | #define BLFLAG_READ_VERSION 0x02 |
25 | #define BLFLAG_READ_VERSION 0x02 |
26 | 26 | ||
27 | extern volatile uint8_t BLFlags; |
27 | extern volatile uint8_t BLFlags; |
28 | 28 | ||
29 | typedef struct |
29 | typedef struct |
30 | { |
30 | { |
31 | uint8_t Version; // the version of the BL (0 = old) |
31 | uint8_t Version; // the version of the BL (0 = old) |
32 | uint8_t SetPoint; // written by attitude controller |
32 | uint8_t SetPoint; // written by attitude controller |
33 | uint8_t SetPointLowerBits; // for higher Resolution of new BLs |
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 |
34 | uint8_t State; // 7 bit for I2C error counter, highest bit indicates if motor is present |
35 | // the following bytes must be exactly in that order! |
35 | // the following bytes must be exactly in that order! |
36 | uint8_t Current; // in 0.1 A steps, read back from BL |
36 | uint8_t Current; // in 0.1 A steps, read back from BL |
37 | uint8_t MaxPWM; // read back from BL is less than 255 if BL is in current limit |
37 | uint8_t MaxPWM; // read back from BL is less than 255 if BL is in current limit |
38 | int8_t Temperature; // old BL-Ctrl will return a 255 here, the new version the temp. in °C |
38 | int8_t Temperature; // old BL-Ctrl will return a 255 here, the new version the temp. in °C |
39 | } __attribute__((packed)) MotorData_t; |
39 | } __attribute__((packed)) MotorData_t; |
40 | 40 | ||
41 | extern MotorData_t Motor[MAX_MOTORS]; |
41 | extern MotorData_t Motor[MAX_MOTORS]; |
42 | 42 | ||
43 | #define BLCONFIG_REVISION 1 |
43 | #define BLCONFIG_REVISION 1 |
44 | 44 | ||
45 | #define MASK_SET_PWM_SCALING 0x01 |
45 | #define MASK_SET_PWM_SCALING 0x01 |
46 | #define MASK_SET_CURRENT_LIMIT 0x02 |
46 | #define MASK_SET_CURRENT_LIMIT 0x02 |
47 | #define MASK_SET_TEMP_LIMIT 0x04 |
47 | #define MASK_SET_TEMP_LIMIT 0x04 |
48 | #define MASK_SET_CURRENT_SCALING 0x08 |
48 | #define MASK_SET_CURRENT_SCALING 0x08 |
49 | #define MASK_SET_BITCONFIG 0x10 |
49 | #define MASK_SET_BITCONFIG 0x10 |
50 | #define MASK_RESET_CAPCOUNTER 0x20 |
50 | #define MASK_RESET_CAPCOUNTER 0x20 |
51 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
51 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
52 | #define MASK_SET_SAVE_EEPROM 0x80 |
52 | #define MASK_SET_SAVE_EEPROM 0x80 |
53 | 53 | ||
54 | #define BITCONF_REVERSE_ROTATION 0x01 |
54 | #define BITCONF_REVERSE_ROTATION 0x01 |
55 | #define BITCONF_RES1 0x02 |
55 | #define BITCONF_RES1 0x02 |
56 | #define BITCONF_RES2 0x04 |
56 | #define BITCONF_RES2 0x04 |
57 | #define BITCONF_RES3 0x08 |
57 | #define BITCONF_RES3 0x08 |
58 | #define BITCONF_RES4 0x10 |
58 | #define BITCONF_RES4 0x10 |
59 | #define BITCONF_RES5 0x20 |
59 | #define BITCONF_RES5 0x20 |
60 | #define BITCONF_RES6 0x40 |
60 | #define BITCONF_RES6 0x40 |
61 | #define BITCONF_RES7 0x80 |
61 | #define BITCONF_RES7 0x80 |
62 | 62 | ||
63 | typedef struct |
63 | typedef struct |
64 | { |
64 | { |
65 | uint8_t Revision; // must be BL_REVISION |
65 | uint8_t Revision; // must be BL_REVISION |
66 | uint8_t SetMask; // settings mask |
66 | uint8_t SetMask; // settings mask |
67 | uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit |
67 | uint8_t PwmScaling; // maximum value of control pwm, acts like a thrust limit |
68 | uint8_t CurrentLimit; // current limit in A |
68 | uint8_t CurrentLimit; // current limit in A |
69 | uint8_t TempLimit; // in °C |
69 | uint8_t TempLimit; // in °C |
70 | uint8_t CurrentScaling; // scaling factor for current measurement |
70 | uint8_t CurrentScaling; // scaling factor for current measurement |
71 | uint8_t BitConfig; // see defines above |
71 | uint8_t BitConfig; // see defines above |
72 | uint8_t crc; // checksum |
72 | uint8_t crc; // checksum |
73 | } __attribute__((packed)) BLConfig_t; |
73 | } __attribute__((packed)) BLConfig_t; |
74 | 74 | ||
75 | extern BLConfig_t BLConfig; |
75 | extern BLConfig_t BLConfig; |
76 | 76 | ||
77 | extern volatile uint16_t I2CTimeout; |
77 | extern volatile uint16_t I2CTimeout; |
78 | 78 | ||
79 | void I2C_Init (void); // Initialize I2C |
79 | void I2C_Init (void); // Initialize I2C |
80 | #define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
80 | #define I2C_Start(start_state) {twi_state = start_state; BLFlags &= ~BLFLAG_TX_COMPLETE; TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
81 | #define I2C_Stop(start_state) {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
81 | #define I2C_Stop(start_state) {twi_state = start_state; TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
- | 82 | void I2C_Reset(void); // Reset I2C |
|
- | 83 | ||
- | 84 | #define BLCONFIG_SUCCESS 0 |
|
- | 85 | #define BLCONFIG_ERR_MOTOR_RUNNING 1 |
|
- | 86 | #define BLCONFIG_ERR_MOTOR_NOT_EXIST 2 |
|
- | 87 | #define BLCONFIG_ERR_HW_NOT_COMPATIBLE 3 |
|
- | 88 | #define BLCONFIG_ERR_SW_NOT_COMPATIBLE 4 |
|
- | 89 | #define BLCONFIG_ERR_CHECKSUM 5 |
|
82 | void I2C_Reset(void); // Reset I2C |
90 | |
83 | uint8_t I2C_WriteBLConfig(uint8_t motor); |
91 | uint8_t I2C_WriteBLConfig(uint8_t motor); |
84 | uint8_t I2C_ReadBLConfig(uint8_t motor); |
92 | uint8_t I2C_ReadBLConfig(uint8_t motor); |
85 | 93 | ||
86 | #endif |
94 | #endif |
87 | 95 |