Rev 1648 | Rev 1651 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1648 | Rev 1650 | ||
---|---|---|---|
1 | /*############################################################################ |
1 | /*############################################################################ |
2 | ############################################################################*/ |
2 | ############################################################################*/ |
3 | 3 | ||
4 | #ifndef _I2C_MASTER_H |
4 | #ifndef _I2C_MASTER_H |
5 | #define _I2C_MASTER_H |
5 | #define _I2C_MASTER_H |
6 | 6 | ||
7 | //############################################################################ |
7 | //############################################################################ |
8 | 8 | ||
9 | // I2C Konstanten |
9 | // I2C Konstanten |
10 | #define SCL_CLOCK 200000L |
10 | #define SCL_CLOCK 200000L |
11 | #define I2C_TIMEOUT 30000 |
11 | #define I2C_TIMEOUT 30000 |
12 | #define I2C_START 0x08 |
12 | #define I2C_START 0x08 |
13 | #define I2C_REPEATED_START 0x10 |
13 | #define I2C_REPEATED_START 0x10 |
14 | #define I2C_TX_SLA_ACK 0x18 |
14 | #define I2C_TX_SLA_ACK 0x18 |
15 | #define I2C_TX_DATA_ACK 0x28 |
15 | #define I2C_TX_DATA_ACK 0x28 |
16 | #define I2C_RX_SLA_ACK 0x40 |
16 | #define I2C_RX_SLA_ACK 0x40 |
17 | #define I2C_RX_DATA_ACK 0x50 |
17 | #define I2C_RX_DATA_ACK 0x50 |
18 | 18 | ||
19 | //############################################################################ |
19 | //############################################################################ |
20 | 20 | ||
21 | extern volatile unsigned char twi_state; |
21 | extern volatile unsigned char twi_state; |
22 | extern unsigned char motor,MissingMotor; |
22 | extern volatile unsigned char motor,MissingMotor; |
23 | extern unsigned char motorread; |
23 | extern volatile unsigned char motorread; |
24 | 24 | ||
25 | #define MAX_MOTORS 12 |
25 | #define MAX_MOTORS 12 |
26 | #define MOTOR_STATE_PRESENT_MASK 0x80 |
26 | #define MOTOR_STATE_PRESENT_MASK 0x80 |
27 | #define MOTOR_STATE_ERROR_MASK 0x7F |
27 | #define MOTOR_STATE_ERROR_MASK 0x7F |
28 | 28 | ||
29 | 29 | ||
30 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
30 | #define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01 |
31 | 31 | ||
32 | #define BLFLAG_READ_VERSION 0x01 |
32 | #define BLFLAG_READ_VERSION 0x01 |
33 | #define BLFLAG_SEND_CONFIG 0x02 |
33 | #define BLFLAG_SEND_CONFIG 0x02 |
34 | #define BLFLAG_TX_COMPLETE 0x04 |
34 | #define BLFLAG_TX_COMPLETE 0x04 |
35 | 35 | ||
36 | extern unsigned char BLFlags; |
36 | extern volatile unsigned char BLFlags; |
37 | 37 | ||
38 | #define MASK_SET_PWM_SCALING 0x01 |
38 | #define MASK_SET_PWM_SCALING 0x01 |
39 | #define MASK_SET_CURRENT_LIMIT 0x02 |
39 | #define MASK_SET_CURRENT_LIMIT 0x02 |
40 | #define MASK_SET_TEMP_LIMIT 0x04 |
40 | #define MASK_SET_TEMP_LIMIT 0x04 |
41 | #define MASK_SET_CURRENT_SCALING 0x08 |
41 | #define MASK_SET_CURRENT_SCALING 0x08 |
42 | #define MASK_SET_BITCONFIG 0x10 |
42 | #define MASK_SET_BITCONFIG 0x10 |
43 | #define MASK_RESET_CAPCOUNTER 0x20 |
43 | #define MASK_RESET_CAPCOUNTER 0x20 |
44 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
44 | #define MASK_SET_DEFAULT_PARAMS 0x40 |
45 | #define MASK_SET_SAVE_EEPROM 0x80 |
45 | #define MASK_SET_SAVE_EEPROM 0x80 |
46 | 46 | ||
47 | #define BITCONF_REVERSE_ROTATION 0x01 |
47 | #define BITCONF_REVERSE_ROTATION 0x01 |
48 | #define BITCONF_RES1 0x02 |
48 | #define BITCONF_RES1 0x02 |
49 | #define BITCONF_RES2 0x04 |
49 | #define BITCONF_RES2 0x04 |
50 | #define BITCONF_RES3 0x08 |
50 | #define BITCONF_RES3 0x08 |
51 | #define BITCONF_RES4 0x10 |
51 | #define BITCONF_RES4 0x10 |
52 | #define BITCONF_RES5 0x20 |
52 | #define BITCONF_RES5 0x20 |
53 | #define BITCONF_RES6 0x40 |
53 | #define BITCONF_RES6 0x40 |
54 | #define BITCONF_RES7 0x80 |
54 | #define BITCONF_RES7 0x80 |
55 | 55 | ||
56 | typedef struct |
56 | typedef struct |
57 | { |
57 | { |
58 | unsigned char SetPoint; // written by attitude controller |
58 | unsigned char SetPoint; // written by attitude controller |
59 | unsigned char State; // 7 bit for I2C error counter, highest bit indicates if motor is present |
59 | unsigned char State; // 7 bit for I2C error counter, highest bit indicates if motor is present |
60 | unsigned char Current; // in 0.1 A steps, read back from BL |
60 | unsigned char Current; // in 0.1 A steps, read back from BL |
61 | unsigned char MaxPWM; // read back from BL is less than 255 if BL is in current limit |
61 | unsigned char MaxPWM; // read back from BL is less than 255 if BL is in current limit |
62 | unsigned char Temperature; // old BL-Ctrl will return a 255 here |
62 | unsigned char Temperature; // old BL-Ctrl will return a 255 here |
63 | unsigned char SetPointLowerBits; // for higher Resolution |
63 | unsigned char SetPointLowerBits; // for higher Resolution |
64 | unsigned char Version; |
64 | unsigned char Version; |
65 | } __attribute__((packed)) MotorData_t; |
65 | } __attribute__((packed)) MotorData_t; |
66 | 66 | ||
67 | extern MotorData_t Motor[MAX_MOTORS]; |
67 | extern MotorData_t Motor[MAX_MOTORS]; |
68 | 68 | ||
69 | typedef struct |
69 | typedef struct |
70 | { |
70 | { |
71 | unsigned char SetMask; // settings mask |
71 | unsigned char SetMask; // settings mask |
72 | unsigned char PwmScaling; // maximum value of control pwm, acts like a thrust limit |
72 | unsigned char PwmScaling; // maximum value of control pwm, acts like a thrust limit |
73 | unsigned char CurrentLimit; // current limit in A |
73 | unsigned char CurrentLimit; // current limit in A |
74 | unsigned char TempLimit; // in °C |
74 | unsigned char TempLimit; // in °C |
75 | unsigned char CurrentScaling; // scaling factor for current measurement |
75 | unsigned char CurrentScaling; // scaling factor for current measurement |
76 | unsigned char BitConfig; // see defines below |
76 | unsigned char BitConfig; // see defines below |
77 | } __attribute__((packed)) BLConfig_t; |
77 | } __attribute__((packed)) BLConfig_t; |
78 | 78 | ||
79 | extern BLConfig_t BLConfig[MAX_MOTORS]; |
79 | extern BLConfig_t BLConfig[MAX_MOTORS]; |
80 | 80 | ||
81 | void i2c_reset(void); |
81 | void i2c_reset(void); |
82 | extern void i2c_init (void); // I2C initialisieren |
82 | extern void i2c_init (void); // I2C initialisieren |
83 | extern void i2c_write_byte (char byte); // 1 Byte schreiben |
83 | extern void i2c_write_byte (char byte); // 1 Byte schreiben |
84 | extern void i2c_reset(void); |
84 | extern void i2c_reset(void); |
85 | extern void I2C_SendBLConfig(void); |
85 | extern void I2C_SendBLConfig(void); |
86 | 86 | ||
87 | #define I2C_Start() {TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
87 | #define I2C_Start() {TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);} |
88 | #define I2C_Stop() {TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
88 | #define I2C_Stop() {TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);} |
89 | #define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);} |
89 | #define I2C_ReceiveByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);} |
90 | #define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
90 | #define I2C_ReceiveLastByte() {TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
91 | #define i2c_write_byte(byte) {TWSR = 0x00; TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
91 | #define i2c_write_byte(byte) {TWSR = 0x00; TWDR = byte; TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);} |
92 | 92 | ||
93 | #endif |
93 | #endif |
94 | 94 |