Rev 1644 | Rev 1650 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1644 | Rev 1648 | ||
---|---|---|---|
1 | /*############################################################################ |
1 | /*############################################################################ |
2 | ############################################################################*/ |
2 | ############################################################################*/ |
3 | 3 | ||
4 | #include "main.h" |
4 | #include "main.h" |
5 | 5 | ||
6 | volatile unsigned char twi_state = 0; |
6 | volatile unsigned char twi_state = 0; |
7 | unsigned char motor = 0,TransmitBlConfig = 1; |
7 | unsigned char motor = 0; |
8 | unsigned char motorread = 0,MissingMotor = 0; |
8 | unsigned char motorread = 0,MissingMotor = 0; |
9 | unsigned char ReadTemperature = 0; |
9 | unsigned char BLFlags = 0; |
10 | 10 | ||
- | 11 | MotorData_t Motor[MAX_MOTORS]; |
|
- | 12 | BLConfig_t BLConfig[MAX_MOTORS]; |
|
- | 13 | ||
11 | MotorData_t Motor[MAX_MOTORS]; |
14 | unsigned char *pTxBuff = (uint8_t*)&BLConfig[0]; |
12 | 15 | ||
13 | unsigned int I2CError = 0; |
16 | unsigned int I2CError = 0; |
- | 17 | ||
- | 18 | #define BL_CONF_SYNC_BYTE '#' |
|
14 | 19 | ||
15 | //############################################################################ |
20 | //############################################################################ |
16 | //Initzialisieren der I2C (TWI) Schnittstelle |
21 | //Initzialisieren der I2C (TWI) Schnittstelle |
17 | void i2c_init(void) |
22 | void i2c_init(void) |
18 | //############################################################################ |
23 | //############################################################################ |
19 | { |
24 | { |
- | 25 | unsigned char i; |
|
- | 26 | ||
20 | TWSR = 0; |
27 | TWSR = 0; |
21 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
28 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
- | 29 | ||
- | 30 | for(i=0; i < MAX_MOTORS; i++) |
|
- | 31 | { |
|
- | 32 | Motor[i].Version = 0; |
|
- | 33 | Motor[i].SetPoint = 0; |
|
- | 34 | Motor[i].SetPointLowerBits = 0; |
|
- | 35 | Motor[i].State = 0; |
|
- | 36 | Motor[i].Current = 0; |
|
- | 37 | Motor[i].MaxPWM = 0; |
|
- | 38 | BLConfig[i].SetMask = MASK_SET_PWM_SCALING|MASK_SET_CURRENT_LIMIT|MASK_SET_TEMP_LIMIT|MASK_SET_CURRENT_SCALING|MASK_SET_BITCONFIG; |
|
- | 39 | BLConfig[i].PwmScaling = 255; // MaxPWM |
|
- | 40 | BLConfig[i].CurrentLimit = 30; // Current Limit in A |
|
- | 41 | BLConfig[i].TempLimit = 99; // Temperature Limit in °C |
|
- | 42 | BLConfig[i].CurrentScaling = 64; // Current Scaling |
|
- | 43 | BLConfig[i].BitConfig = 0; // BitConfig |
|
- | 44 | } |
|
22 | } |
45 | } |
23 | 46 | ||
24 | void i2c_reset(void) |
47 | void i2c_reset(void) |
25 | //############################################################################ |
48 | //############################################################################ |
26 | { |
49 | { |
27 | I2C_Stop(); |
50 | I2C_Stop(); |
28 | twi_state = 0; |
51 | twi_state = 0; |
29 | motor = TWDR; |
52 | motor = TWDR; |
30 | motor = 0; |
53 | motor = 0; |
31 | TWCR = 0x80; |
54 | TWCR = 0x80; |
32 | TWAMR = 0; |
55 | TWAMR = 0; |
33 | TWAR = 0; |
56 | TWAR = 0; |
34 | TWDR = 0; |
57 | TWDR = 0; |
35 | TWSR = 0; |
58 | TWSR = 0; |
36 | TWBR = 0; |
59 | TWBR = 0; |
37 | i2c_init(); |
60 | i2c_init(); |
38 | I2C_Start(); |
61 | I2C_Start(); |
39 | i2c_write_byte(0); |
62 | i2c_write_byte(0); |
40 | } |
63 | } |
41 | 64 | ||
42 | /* |
65 | |
43 | //############################################################################ |
66 | //############################################################################ |
44 | SIGNAL (TWI_vect) |
67 | SIGNAL (TWI_vect) |
45 | //############################################################################ |
68 | //############################################################################ |
46 | { |
69 | { |
47 | static unsigned char missing_motor; |
70 | static unsigned char missing_motor = 0, byte_counter = 0, crc = 0, read_more = 0, motorread_temperature = 0; |
- | 71 | J4High; |
|
48 | switch(twi_state++) |
72 | switch(twi_state++) |
49 | { |
73 | { |
50 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
74 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | // Writing the Data |
75 | // Writing the Data |
52 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
76 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
53 | case 0: |
77 | case 0: |
54 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
78 | while(Mixer.Motor[motor][MIX_GAS] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
55 | if(motor == MAX_MOTORS) // writing finished -> now read |
79 | if(motor == MAX_MOTORS) // writing finished -> now read |
56 | { |
80 | { |
57 | motor = 0; |
81 | motor = 0; |
58 | twi_state = 3; |
82 | twi_state = 5; |
59 | i2c_write_byte(0x53+(motorread*2)); |
83 | i2c_write_byte(0x53+(motorread*2)); |
60 | } |
84 | } |
61 | else i2c_write_byte(0x52+(motor*2)); |
85 | else i2c_write_byte(0x52+(motor*2)); |
62 | break; |
86 | break; |
63 | case 1: |
87 | case 1: |
64 | i2c_write_byte(Motor[motor++].SetPoint); |
88 | i2c_write_byte(Motor[motor].SetPoint); |
- | 89 | if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) ) |
|
- | 90 | { |
|
- | 91 | twi_state = 4; //jump over sending more data |
|
- | 92 | } |
|
- | 93 | else if(!(Motor[motor].SetPointLowerBits || (BLFlags & BLFLAG_SEND_CONFIG))) |
|
- | 94 | { |
|
- | 95 | twi_state = 4; // skip state |
|
- | 96 | } |
|
- | 97 | break; |
|
- | 98 | case 2: // lower bits of setpoint (higher resolution) |
|
- | 99 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1) & 0x07); // send the lower bits of setpoint |
|
- | 100 | // transmit config only on demand and the motors are not running and only for one motor per round trip |
|
- | 101 | if( (BLFlags & BLFLAG_SEND_CONFIG) && !MotorenEin && (motor == motorread)) |
|
- | 102 | { // prepare sending of configuration |
|
- | 103 | byte_counter = 0; // reset send byte counter |
|
65 | break; |
104 | crc = 0xAA; // init checksum |
- | 105 | } |
|
- | 106 | else |
|
- | 107 | { // jump to state for end of transmission for that motor |
|
- | 108 | twi_state = 4; |
|
- | 109 | } |
|
- | 110 | break; |
|
- | 111 | case 3: |
|
- | 112 | if(!byte_counter) // first byte? |
|
- | 113 | { |
|
- | 114 | i2c_write_byte(BL_CONF_SYNC_BYTE); |
|
- | 115 | crc += BL_CONF_SYNC_BYTE; // update crc |
|
- | 116 | pTxBuff = (uint8_t*)&BLConfig[motor]; // select configuration for motor |
|
- | 117 | } |
|
- | 118 | else if(byte_counter >= sizeof(BLConfig_t)+1) // last byte? |
|
- | 119 | { // send crc byte at the end |
|
- | 120 | i2c_write_byte(crc); |
|
- | 121 | } |
|
- | 122 | else // transmit configuration to BLs |
|
- | 123 | { |
|
- | 124 | i2c_write_byte(pTxBuff[byte_counter-1]); // submit next byte |
|
- | 125 | crc += pTxBuff[byte_counter-1]; // update crc |
|
- | 126 | twi_state = 3; // stay in this state |
|
- | 127 | } |
|
- | 128 | byte_counter++; // next byte |
|
- | 129 | break; |
|
66 | case 2: |
130 | case 4: |
- | 131 | ||
67 | if(TWSR == 0x30) |
132 | if(TWSR == 0x30) |
68 | { |
133 | { |
69 | if(!missing_motor) missing_motor = motor; |
134 | if(!missing_motor) missing_motor = motor + 1; |
70 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
135 | if((Motor[motor].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor].State++; // increment error counter and handle overflow |
71 | } |
136 | } |
72 | I2C_Stop(); |
137 | I2C_Stop(); |
73 | I2CTimeout = 10; |
138 | I2CTimeout = 10; |
- | 139 | motor++; |
|
74 | twi_state = 0; |
140 | twi_state = 0; |
75 | I2C_Start(); |
141 | I2C_Start(); |
76 | break; |
142 | break; |
77 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
143 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
78 | // Reading Data |
144 | // Reading Data |
79 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
145 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
80 | case 3: |
- | |
81 | //Transmit 1st byte for reading |
- | |
82 | if(TWSR != 0x40) // Error? |
- | |
83 | { |
- | |
84 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
- | |
85 | motorread++; |
- | |
86 | if(motorread >= MAX_MOTORS) motorread = 0; |
- | |
87 | I2C_Stop(); |
- | |
88 | twi_state = 0; |
- | |
89 | } |
- | |
90 | else |
- | |
91 | { |
- | |
92 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
- | |
93 | I2C_ReceiveByte(); |
- | |
94 | } |
- | |
95 | MissingMotor = missing_motor; |
- | |
96 | missing_motor = 0; |
- | |
97 | break; |
- | |
98 | case 4: //Read 1st byte and transmit 2nd Byte |
- | |
99 | Motor[motorread].Current = TWDR; |
- | |
100 | I2C_ReceiveLastByte(); //nack |
- | |
101 | break; |
- | |
102 | case 5: |
146 | case 5: |
103 | //Read 2nd byte |
147 | //Transmit 1st byte for reading |
104 | Motor[motorread].MaxPWM = TWDR; |
- | |
105 | motorread++; // next motor |
148 | if(TWSR != 0x40) // Error? |
106 | if(motorread >= MAX_MOTORS) motorread = 0; |
- | |
107 | I2C_Stop(); |
- | |
108 | twi_state = 0; |
- | |
109 | break; |
- | |
110 | - | ||
111 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
112 | // writing Gyro-Offset |
- | |
113 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
114 | case 8: |
- | |
115 | i2c_write_byte(0x98); // Address of the DAC |
- | |
116 | break; |
- | |
117 | case 9: |
- | |
118 | i2c_write_byte(0x10); // Update Channel A |
- | |
119 | break; |
- | |
120 | case 10: |
- | |
121 | i2c_write_byte(AnalogOffsetNick); // Value |
- | |
122 | break; |
- | |
123 | case 11: |
- | |
124 | i2c_write_byte(0x80); // Value |
- | |
125 | break; |
- | |
126 | case 12: |
- | |
127 | I2C_Stop(); |
- | |
128 | I2CTimeout = 10; |
- | |
129 | I2C_Start(); |
- | |
130 | break; |
- | |
131 | case 13: |
- | |
132 | i2c_write_byte(0x98); // Address of the DAC |
- | |
133 | break; |
- | |
134 | case 14: |
- | |
135 | i2c_write_byte(0x12); // Update Channel B |
- | |
136 | break; |
- | |
137 | case 15: |
- | |
138 | i2c_write_byte(AnalogOffsetRoll); // Value |
- | |
139 | break; |
- | |
140 | case 16: |
- | |
141 | i2c_write_byte(0x80); // Value |
- | |
142 | break; |
- | |
143 | case 17: |
- | |
144 | I2C_Stop(); |
- | |
145 | I2CTimeout = 10; |
- | |
146 | I2C_Start(); |
- | |
147 | break; |
- | |
148 | case 18: |
- | |
149 | i2c_write_byte(0x98); // Address of the DAC |
- | |
150 | break; |
- | |
151 | case 19: |
- | |
152 | i2c_write_byte(0x14); // Update Channel C |
- | |
153 | break; |
- | |
154 | case 20: |
- | |
155 | i2c_write_byte(AnalogOffsetGier); // Value |
- | |
156 | break; |
- | |
157 | case 21: |
- | |
158 | i2c_write_byte(0x80); // Value |
- | |
159 | break; |
- | |
160 | case 22: |
- | |
161 | I2C_Stop(); |
- | |
162 | I2CTimeout = 10; |
- | |
163 | twi_state = 0; |
- | |
164 | break; |
- | |
165 | default: twi_state = 0; |
- | |
166 | break; |
- | |
167 | } |
149 | { |
168 | TWCR |= 0x80; |
- | |
169 | } |
- | |
170 | */ |
- | |
171 | - | ||
172 | /* |
- | |
173 | if(DataArray[3] & 0x01) Parameter.PwmScaling = DataArray[4]; |
- | |
174 | if(DataArray[3] & 0x02) Parameter.MaxStrom = DataArray[5]; |
- | |
175 | if(DataArray[3] & 0x04) Parameter.TemperaturLimiter = DataArray[6]; |
- | |
176 | if(DataArray[3] & 0x08) Parameter.SkaliereStrom = DataArray[7]; |
- | |
177 | if(DataArray[3] & 0x10) Parameter.Bitconfig = DataArray[8]; |
- | |
178 | */ |
- | |
179 | - | ||
180 | //############################################################################ |
- | |
181 | SIGNAL (TWI_vect) |
- | |
182 | //############################################################################ |
- | |
183 | { // 2 3 4 5 6 7 8 9 |
- | |
184 | unsigned char test[] = {0,0,'#',0x1F,255,30,99,64,0x00,7,8,9,10}; |
- | |
185 | static unsigned char missing_motor,send = 0,crc = 0,read_more = 0; |
- | |
186 | J4High; |
- | |
187 | switch(twi_state++) |
- | |
188 | { |
- | |
189 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
190 | // Writing the Data |
- | |
191 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
192 | case 0: |
- | |
193 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
150 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
194 | if(motor == MAX_MOTORS) // writing finished -> now read |
151 | if(++motorread >= MAX_MOTORS) |
195 | { |
152 | { |
196 | motor = 0; |
- | |
197 | twi_state = 4; |
- | |
198 | i2c_write_byte(0x53+(motorread*2)); |
- | |
199 | } |
- | |
200 | else i2c_write_byte(0x52+(motor*2)); |
153 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
201 | send = 0; |
154 | motorread = 0; |
202 | break; |
- | |
203 | case 1: |
- | |
204 | i2c_write_byte(Motor[motor].SetPoint); |
155 | if(++motorread_temperature >= MAX_MOTORS) |
205 | // if(!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) /*|| !Motor[motor].SetPointLowerBits*/) |
- | |
206 | if((!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) || !Motor[motor].SetPointLowerBits) && !TransmitBlConfig) |
- | |
207 | twi_state++; // skip |
- | |
208 | break; |
- | |
209 | case 2: |
- | |
210 | if(!send++) |
- | |
211 | { |
156 | { |
212 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1) & 0x07); |
157 | motorread_temperature = 0; |
213 | crc = 0xAA; |
158 | BLFlags &= ~BLFLAG_READ_VERSION; |
214 | } |
159 | } |
215 | else |
- | |
216 | if(send == 9) i2c_write_byte(crc) |
- | |
217 | else |
- | |
218 | { |
- | |
219 | crc += test[send]; |
- | |
220 | i2c_write_byte(test[send]); |
- | |
221 | } |
160 | } |
222 | if(TransmitBlConfig && !MotorenEin && motor == motorread && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
- | |
223 | { |
161 | I2C_Stop(); |
- | 162 | twi_state = 0; |
|
224 | if(send <= 10) twi_state--; |
163 | BLFlags |= BLFLAG_TX_COMPLETE; |
225 | } |
164 | } |
226 | break; |
- | |
227 | case 3: |
- | |
228 | motor++; |
- | |
229 | if(TWSR == 0x30) |
- | |
230 | { |
165 | else |
231 | if(!missing_motor) missing_motor = motor; |
- | |
232 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
- | |
233 | } |
166 | { |
234 | I2C_Stop(); |
- | |
235 | I2CTimeout = 10; |
- | |
236 | twi_state = 0; |
- | |
237 | I2C_Start(); |
- | |
238 | break; |
- | |
239 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
240 | // Reading Data |
- | |
241 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
- | |
242 | case 4: |
- | |
243 | //Transmit 1st byte for reading |
- | |
244 | if(TWSR != 0x40) // Error? |
- | |
245 | { |
- | |
246 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
- | |
247 | motorread++; |
- | |
248 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
- | |
249 | I2C_Stop(); |
- | |
250 | twi_state = 0; |
- | |
251 | } |
- | |
252 | else |
- | |
253 | { |
- | |
254 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
167 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
255 | if(motorread == ReadTemperature || TransmitBlConfig) |
168 | if(motorread == motorread_temperature) |
256 | { |
169 | { |
257 | read_more = 1; |
170 | read_more = 1; |
258 | I2C_ReceiveByte(); |
171 | I2C_ReceiveByte(); |
259 | } |
172 | } |
260 | else |
173 | else |
261 | { |
174 | { |
262 | I2C_ReceiveLastByte(); |
- | |
263 | read_more = 0; |
175 | read_more = 0; |
- | 176 | I2C_ReceiveLastByte(); |
|
264 | } |
177 | } |
265 | } |
178 | } |
266 | MissingMotor = missing_motor; |
179 | MissingMotor = missing_motor; |
267 | missing_motor = 0; |
180 | missing_motor = 0; |
268 | break; |
181 | break; |
269 | case 5: //Read 1st byte and transmit 2nd Byte |
182 | case 6: //Read 1st byte and transmit 2nd Byte |
270 | Motor[motorread].Current = TWDR; |
183 | Motor[motorread].Current = TWDR; |
271 | if(read_more) I2C_ReceiveByte() //nack |
184 | if(read_more) |
- | 185 | { |
|
- | 186 | I2C_ReceiveByte() //ack |
|
- | 187 | ||
- | 188 | } |
|
272 | else |
189 | else |
- | 190 | { |
|
- | 191 | if(++motorread >= MAX_MOTORS) |
|
273 | { |
192 | { |
274 | motorread++; // next motor |
193 | motorread = 0; // restart from beginning |
- | 194 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
|
275 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
195 | if(++motorread_temperature >= MAX_MOTORS) |
- | 196 | { |
|
- | 197 | motorread_temperature = 0; |
|
- | 198 | BLFlags &= ~BLFLAG_READ_VERSION; |
|
- | 199 | } |
|
- | 200 | } |
|
276 | I2C_Stop(); |
201 | I2C_Stop(); |
277 | twi_state = 0; |
202 | twi_state = 0; |
- | 203 | BLFlags |= BLFLAG_TX_COMPLETE; |
|
278 | } |
204 | } |
279 | break; |
205 | break; |
280 | case 6: |
206 | case 7: |
281 | //Read 2nd byte and transmit 3rd Byte |
207 | //Read 2nd byte and transmit 3rd Byte |
282 | Motor[motorread].MaxPWM = TWDR; |
208 | Motor[motorread].MaxPWM = TWDR; |
- | 209 | if(BLFlags & BLFLAG_READ_VERSION) |
|
- | 210 | { |
|
283 | if(TWDR == 250) |
211 | if(TWDR == 250) |
284 | { |
212 | { |
285 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
213 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
286 | } |
214 | } |
- | 215 | else |
|
- | 216 | { |
|
287 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
217 | Motor[motorread].Version = 0; |
- | 218 | } |
|
- | 219 | } |
|
288 | I2C_ReceiveLastByte(); //nack |
220 | I2C_ReceiveLastByte(); //nack |
289 | break; |
221 | break; |
290 | case 7: // read next |
222 | case 8: // read next |
291 | Motor[motorread].Temperature = TWDR; |
223 | Motor[motorread].Temperature = TWDR; |
292 | motorread++; // next motor |
224 | if(++motorread >= MAX_MOTORS) |
- | 225 | { |
|
293 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
226 | motorread = 0; // restart reading of first motor |
- | 227 | BLFlags &= ~BLFLAG_SEND_CONFIG; |
|
- | 228 | if(++motorread_temperature >= MAX_MOTORS) |
|
- | 229 | { |
|
- | 230 | motorread_temperature = 0; |
|
- | 231 | BLFlags &= ~BLFLAG_READ_VERSION; |
|
- | 232 | } |
|
- | 233 | } |
|
294 | I2C_Stop(); |
234 | I2C_Stop(); |
- | 235 | BLFlags |= BLFLAG_TX_COMPLETE; |
|
295 | twi_state = 0; |
236 | twi_state = 0; |
296 | break; |
237 | break; |
297 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
238 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
298 | // writing Gyro-Offset |
239 | // writing Gyro-Offset |
299 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
240 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
300 | case 18: |
241 | case 18: |
301 | i2c_write_byte(0x98); // Address of the DAC |
242 | i2c_write_byte(0x98); // Address of the DAC |
302 | break; |
243 | break; |
303 | case 19: |
244 | case 19: |
304 | i2c_write_byte(0x10); // Update Channel A |
245 | i2c_write_byte(0x10); // Update Channel A |
305 | break; |
246 | break; |
306 | case 20: |
247 | case 20: |
307 | i2c_write_byte(AnalogOffsetNick); // Value |
248 | i2c_write_byte(AnalogOffsetNick); // Value |
308 | break; |
249 | break; |
309 | case 21: |
250 | case 21: |
310 | i2c_write_byte(0x80); // Value |
251 | i2c_write_byte(0x80); // Value |
311 | break; |
252 | break; |
312 | case 22: |
253 | case 22: |
313 | I2C_Stop(); |
254 | I2C_Stop(); |
314 | I2CTimeout = 10; |
255 | I2CTimeout = 10; |
315 | I2C_Start(); |
256 | I2C_Start(); |
316 | break; |
257 | break; |
317 | case 23: |
258 | case 23: |
318 | i2c_write_byte(0x98); // Address of the DAC |
259 | i2c_write_byte(0x98); // Address of the DAC |
319 | break; |
260 | break; |
320 | case 24: |
261 | case 24: |
321 | i2c_write_byte(0x12); // Update Channel B |
262 | i2c_write_byte(0x12); // Update Channel B |
322 | break; |
263 | break; |
323 | case 25: |
264 | case 25: |
324 | i2c_write_byte(AnalogOffsetRoll); // Value |
265 | i2c_write_byte(AnalogOffsetRoll); // Value |
325 | break; |
266 | break; |
326 | case 26: |
267 | case 26: |
327 | i2c_write_byte(0x80); // Value |
268 | i2c_write_byte(0x80); // Value |
328 | break; |
269 | break; |
329 | case 27: |
270 | case 27: |
330 | I2C_Stop(); |
271 | I2C_Stop(); |
331 | I2CTimeout = 10; |
272 | I2CTimeout = 10; |
332 | I2C_Start(); |
273 | I2C_Start(); |
333 | break; |
274 | break; |
334 | case 28: |
275 | case 28: |
335 | i2c_write_byte(0x98); // Address of the DAC |
276 | i2c_write_byte(0x98); // Address of the DAC |
336 | break; |
277 | break; |
337 | case 29: |
278 | case 29: |
338 | i2c_write_byte(0x14); // Update Channel C |
279 | i2c_write_byte(0x14); // Update Channel C |
339 | break; |
280 | break; |
340 | case 30: |
281 | case 30: |
341 | i2c_write_byte(AnalogOffsetGier); // Value |
282 | i2c_write_byte(AnalogOffsetGier); // Value |
342 | break; |
283 | break; |
343 | case 31: |
284 | case 31: |
344 | i2c_write_byte(0x80); // Value |
285 | i2c_write_byte(0x80); // Value |
345 | break; |
286 | break; |
346 | case 32: |
287 | case 32: |
347 | I2C_Stop(); |
288 | I2C_Stop(); |
348 | I2CTimeout = 10; |
289 | I2CTimeout = 10; |
349 | twi_state = 0; |
290 | twi_state = 0; |
350 | break; |
291 | break; |
351 | default: twi_state = 0; |
292 | default: twi_state = 0; |
352 | break; |
293 | break; |
353 | } |
294 | } |
354 | TWCR |= 0x80; |
295 | TWCR |= 0x80; |
355 | J4Low; |
296 | J4Low; |
- | 297 | } |
|
- | 298 | ||
- | 299 | void I2C_SendBLConfig(void) |
|
- | 300 | { |
|
- | 301 | unsigned char i; |
|
- | 302 | while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer |
|
- | 303 | BLFlags |= BLFLAG_SEND_CONFIG; // enable sending of BL config |
|
- | 304 | for(i = 0; i < MAX_MOTORS; i++) |
|
- | 305 | { |
|
- | 306 | Motor[i].SetPoint = 0; |
|
- | 307 | Motor[i].SetPointLowerBits = 0; |
|
- | 308 | } |
|
- | 309 | motorread = 0; |
|
- | 310 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
|
- | 311 | do |
|
- | 312 | { |
|
- | 313 | twi_state = 0; |
|
- | 314 | I2C_Start(); // start an i2c transmission |
|
- | 315 | while(!(BLFlags & BLFLAG_TX_COMPLETE)); //wait for complete transfer |
|
- | 316 | }while(BLFlags & BLFLAG_SEND_CONFIG); // repeat until the BL config has been send to all motors |
|
356 | } |
317 | } |
- | 318 | ||
- | 319 | ||
357 | 320 | ||
358 | 321 |