Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
1 | ingob | 1 | /*############################################################################ |
2 | ############################################################################*/ |
||
3 | |||
4 | #include "main.h" |
||
5 | |||
918 | hbuss | 6 | volatile unsigned char twi_state = 0; |
1643 | holgerb | 7 | unsigned char motor = 0,TransmitBlConfig = 1; |
1210 | hbuss | 8 | unsigned char motorread = 0,MissingMotor = 0; |
1643 | holgerb | 9 | unsigned char ReadTemperature = 0; |
1479 | killagreg | 10 | |
11 | MotorData_t Motor[MAX_MOTORS]; |
||
12 | |||
1322 | hbuss | 13 | unsigned int I2CError = 0; |
1 | ingob | 14 | |
15 | //############################################################################ |
||
16 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
17 | void i2c_init(void) |
||
18 | //############################################################################ |
||
19 | { |
||
20 | TWSR = 0; |
||
1479 | killagreg | 21 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
1 | ingob | 22 | } |
23 | |||
173 | holgerb | 24 | void i2c_reset(void) |
1 | ingob | 25 | //############################################################################ |
173 | holgerb | 26 | { |
1639 | holgerb | 27 | I2C_Stop(); |
173 | holgerb | 28 | twi_state = 0; |
29 | motor = TWDR; |
||
30 | motor = 0; |
||
31 | TWCR = 0x80; |
||
32 | TWAMR = 0; |
||
33 | TWAR = 0; |
||
34 | TWDR = 0; |
||
35 | TWSR = 0; |
||
36 | TWBR = 0; |
||
37 | i2c_init(); |
||
1639 | holgerb | 38 | I2C_Start(); |
173 | holgerb | 39 | i2c_write_byte(0); |
40 | } |
||
1 | ingob | 41 | |
1639 | holgerb | 42 | /* |
1 | ingob | 43 | //############################################################################ |
44 | SIGNAL (TWI_vect) |
||
45 | //############################################################################ |
||
46 | { |
||
1211 | hbuss | 47 | static unsigned char missing_motor; |
1210 | hbuss | 48 | switch(twi_state++) |
1 | ingob | 49 | { |
1210 | hbuss | 50 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
51 | // Writing the Data |
||
52 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 53 | case 0: |
1210 | hbuss | 54 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
1479 | killagreg | 55 | if(motor == MAX_MOTORS) // writing finished -> now read |
56 | { |
||
57 | motor = 0; |
||
58 | twi_state = 3; |
||
1210 | hbuss | 59 | i2c_write_byte(0x53+(motorread*2)); |
1479 | killagreg | 60 | } |
1209 | hbuss | 61 | else i2c_write_byte(0x52+(motor*2)); |
1 | ingob | 62 | break; |
63 | case 1: |
||
1479 | killagreg | 64 | i2c_write_byte(Motor[motor++].SetPoint); |
1111 | hbuss | 65 | break; |
66 | case 2: |
||
1479 | killagreg | 67 | if(TWSR == 0x30) |
68 | { |
||
69 | if(!missing_motor) missing_motor = motor; |
||
70 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
||
1322 | hbuss | 71 | } |
1639 | holgerb | 72 | I2C_Stop(); |
1210 | hbuss | 73 | I2CTimeout = 10; |
1209 | hbuss | 74 | twi_state = 0; |
1639 | holgerb | 75 | I2C_Start(); |
1479 | killagreg | 76 | break; |
1210 | hbuss | 77 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
78 | // Reading Data |
||
79 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1111 | hbuss | 80 | case 3: |
1211 | hbuss | 81 | //Transmit 1st byte for reading |
1210 | hbuss | 82 | if(TWSR != 0x40) // Error? |
1479 | killagreg | 83 | { |
84 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
85 | motorread++; |
||
1211 | hbuss | 86 | if(motorread >= MAX_MOTORS) motorread = 0; |
1639 | holgerb | 87 | I2C_Stop(); |
1210 | hbuss | 88 | twi_state = 0; |
89 | } |
||
1479 | killagreg | 90 | else |
1211 | hbuss | 91 | { |
1479 | killagreg | 92 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
1211 | hbuss | 93 | I2C_ReceiveByte(); |
94 | } |
||
1210 | hbuss | 95 | MissingMotor = missing_motor; |
1211 | hbuss | 96 | missing_motor = 0; |
1111 | hbuss | 97 | break; |
1210 | hbuss | 98 | case 4: //Read 1st byte and transmit 2nd Byte |
1479 | killagreg | 99 | Motor[motorread].Current = TWDR; |
1209 | hbuss | 100 | I2C_ReceiveLastByte(); //nack |
101 | break; |
||
1210 | hbuss | 102 | case 5: |
1209 | hbuss | 103 | //Read 2nd byte |
1479 | killagreg | 104 | Motor[motorread].MaxPWM = TWDR; |
105 | motorread++; // next motor |
||
1211 | hbuss | 106 | if(motorread >= MAX_MOTORS) motorread = 0; |
1639 | holgerb | 107 | I2C_Stop(); |
1209 | hbuss | 108 | twi_state = 0; |
1111 | hbuss | 109 | break; |
1479 | killagreg | 110 | |
1210 | hbuss | 111 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
112 | // writing Gyro-Offset |
||
113 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1479 | killagreg | 114 | case 8: |
1111 | hbuss | 115 | i2c_write_byte(0x98); // Address of the DAC |
116 | break; |
||
1479 | killagreg | 117 | case 9: |
1111 | hbuss | 118 | i2c_write_byte(0x10); // Update Channel A |
119 | break; |
||
1479 | killagreg | 120 | case 10: |
1111 | hbuss | 121 | i2c_write_byte(AnalogOffsetNick); // Value |
122 | break; |
||
1479 | killagreg | 123 | case 11: |
1111 | hbuss | 124 | i2c_write_byte(0x80); // Value |
125 | break; |
||
1479 | killagreg | 126 | case 12: |
1639 | holgerb | 127 | I2C_Stop(); |
1111 | hbuss | 128 | I2CTimeout = 10; |
1639 | holgerb | 129 | I2C_Start(); |
1111 | hbuss | 130 | break; |
1479 | killagreg | 131 | case 13: |
1111 | hbuss | 132 | i2c_write_byte(0x98); // Address of the DAC |
133 | break; |
||
1479 | killagreg | 134 | case 14: |
1111 | hbuss | 135 | i2c_write_byte(0x12); // Update Channel B |
136 | break; |
||
1479 | killagreg | 137 | case 15: |
1111 | hbuss | 138 | i2c_write_byte(AnalogOffsetRoll); // Value |
139 | break; |
||
1479 | killagreg | 140 | case 16: |
1111 | hbuss | 141 | i2c_write_byte(0x80); // Value |
142 | break; |
||
1479 | killagreg | 143 | case 17: |
1639 | holgerb | 144 | I2C_Stop(); |
1111 | hbuss | 145 | I2CTimeout = 10; |
1639 | holgerb | 146 | I2C_Start(); |
1111 | hbuss | 147 | break; |
1479 | killagreg | 148 | case 18: |
1111 | hbuss | 149 | i2c_write_byte(0x98); // Address of the DAC |
150 | break; |
||
1479 | killagreg | 151 | case 19: |
1111 | hbuss | 152 | i2c_write_byte(0x14); // Update Channel C |
153 | break; |
||
1479 | killagreg | 154 | case 20: |
1111 | hbuss | 155 | i2c_write_byte(AnalogOffsetGier); // Value |
156 | break; |
||
1479 | killagreg | 157 | case 21: |
1111 | hbuss | 158 | i2c_write_byte(0x80); // Value |
159 | break; |
||
1479 | killagreg | 160 | case 22: |
1639 | holgerb | 161 | I2C_Stop(); |
1111 | hbuss | 162 | I2CTimeout = 10; |
163 | twi_state = 0; |
||
164 | break; |
||
1209 | hbuss | 165 | default: twi_state = 0; |
1479 | killagreg | 166 | break; |
1209 | hbuss | 167 | } |
1111 | hbuss | 168 | TWCR |= 0x80; |
169 | } |
||
1638 | holgerb | 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 |
||
1643 | holgerb | 184 | unsigned char test[] = {0,0,'#',0x1F,255,30,99,49,0x00,7,8,9,10}; |
185 | static unsigned char missing_motor,send = 0,crc = 0,read_more = 0; |
||
1639 | holgerb | 186 | |
1638 | holgerb | 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 |
||
194 | if(motor == MAX_MOTORS) // writing finished -> now read |
||
195 | { |
||
196 | motor = 0; |
||
197 | twi_state = 4; |
||
198 | i2c_write_byte(0x53+(motorread*2)); |
||
199 | } |
||
200 | else i2c_write_byte(0x52+(motor*2)); |
||
201 | send = 0; |
||
202 | break; |
||
203 | case 1: |
||
204 | i2c_write_byte(Motor[motor].SetPoint); |
||
1643 | holgerb | 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 |
||
1638 | holgerb | 208 | break; |
209 | case 2: |
||
1642 | killagreg | 210 | if(!send++) |
1638 | holgerb | 211 | { |
1643 | holgerb | 212 | i2c_write_byte((Motor[motor].SetPointLowerBits << 1) & 0x07); |
1638 | holgerb | 213 | crc = 0xAA; |
214 | } |
||
215 | else |
||
1639 | holgerb | 216 | if(send == 9) i2c_write_byte(crc) |
1642 | killagreg | 217 | else |
1638 | holgerb | 218 | { |
219 | crc += test[send]; |
||
220 | i2c_write_byte(test[send]); |
||
1642 | killagreg | 221 | } |
222 | if(TransmitBlConfig && !MotorenEin && motor == motorread && (Motor[motorread].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) |
||
1639 | holgerb | 223 | { |
224 | if(send <= 10) twi_state--; |
||
1642 | killagreg | 225 | } |
1639 | holgerb | 226 | |
1638 | holgerb | 227 | break; |
228 | case 3: |
||
1642 | killagreg | 229 | motor++; |
1638 | holgerb | 230 | if(TWSR == 0x30) |
231 | { |
||
232 | if(!missing_motor) missing_motor = motor; |
||
233 | if((Motor[motor-1].State & MOTOR_STATE_ERROR_MASK) < MOTOR_STATE_ERROR_MASK) Motor[motor-1].State++; // increment error counter and handle overflow |
||
234 | } |
||
1639 | holgerb | 235 | I2C_Stop(); |
1638 | holgerb | 236 | I2CTimeout = 10; |
237 | twi_state = 0; |
||
1639 | holgerb | 238 | I2C_Start(); |
1638 | holgerb | 239 | break; |
240 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
241 | // Reading Data |
||
242 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
243 | case 4: |
||
244 | //Transmit 1st byte for reading |
||
245 | if(TWSR != 0x40) // Error? |
||
246 | { |
||
247 | Motor[motorread].State &= ~MOTOR_STATE_PRESENT_MASK; // clear present bit |
||
248 | motorread++; |
||
1643 | holgerb | 249 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
1639 | holgerb | 250 | I2C_Stop(); |
1638 | holgerb | 251 | twi_state = 0; |
252 | } |
||
253 | else |
||
254 | { |
||
255 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
1643 | holgerb | 256 | if(motorread == ReadTemperature || TransmitBlConfig) |
257 | { |
||
258 | read_more = 1; |
||
259 | I2C_ReceiveByte(); |
||
260 | } |
||
261 | else |
||
262 | { |
||
263 | I2C_ReceiveLastByte(); |
||
264 | read_more = 0; |
||
265 | } |
||
1638 | holgerb | 266 | } |
267 | MissingMotor = missing_motor; |
||
268 | missing_motor = 0; |
||
269 | break; |
||
270 | case 5: //Read 1st byte and transmit 2nd Byte |
||
271 | Motor[motorread].Current = TWDR; |
||
1643 | holgerb | 272 | if(read_more) I2C_ReceiveByte() //nack |
273 | else |
||
274 | { |
||
275 | motorread++; // next motor |
||
276 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
||
277 | I2C_Stop(); |
||
278 | twi_state = 0; |
||
279 | } |
||
1638 | holgerb | 280 | break; |
1642 | killagreg | 281 | case 6: |
1638 | holgerb | 282 | //Read 2nd byte and transmit 3rd Byte |
283 | Motor[motorread].MaxPWM = TWDR; |
||
1642 | killagreg | 284 | if(TWDR == 250) |
1638 | holgerb | 285 | { |
1643 | holgerb | 286 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
1642 | killagreg | 287 | } |
288 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
||
1638 | holgerb | 289 | I2C_ReceiveLastByte(); //nack |
290 | break; |
||
1642 | killagreg | 291 | case 7: // read next |
1638 | holgerb | 292 | Motor[motorread].Temperature = TWDR; |
293 | motorread++; // next motor |
||
1639 | holgerb | 294 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
295 | I2C_Stop(); |
||
1638 | holgerb | 296 | twi_state = 0; |
1643 | holgerb | 297 | break; |
1638 | holgerb | 298 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
299 | // writing Gyro-Offset |
||
300 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
301 | case 18: |
||
302 | i2c_write_byte(0x98); // Address of the DAC |
||
303 | break; |
||
304 | case 19: |
||
305 | i2c_write_byte(0x10); // Update Channel A |
||
306 | break; |
||
307 | case 20: |
||
308 | i2c_write_byte(AnalogOffsetNick); // Value |
||
309 | break; |
||
310 | case 21: |
||
311 | i2c_write_byte(0x80); // Value |
||
312 | break; |
||
313 | case 22: |
||
1639 | holgerb | 314 | I2C_Stop(); |
1638 | holgerb | 315 | I2CTimeout = 10; |
1639 | holgerb | 316 | I2C_Start(); |
1638 | holgerb | 317 | break; |
318 | case 23: |
||
319 | i2c_write_byte(0x98); // Address of the DAC |
||
320 | break; |
||
321 | case 24: |
||
322 | i2c_write_byte(0x12); // Update Channel B |
||
323 | break; |
||
324 | case 25: |
||
325 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
326 | break; |
||
327 | case 26: |
||
328 | i2c_write_byte(0x80); // Value |
||
329 | break; |
||
330 | case 27: |
||
1639 | holgerb | 331 | I2C_Stop(); |
1638 | holgerb | 332 | I2CTimeout = 10; |
1639 | holgerb | 333 | I2C_Start(); |
1638 | holgerb | 334 | break; |
335 | case 28: |
||
336 | i2c_write_byte(0x98); // Address of the DAC |
||
337 | break; |
||
338 | case 29: |
||
339 | i2c_write_byte(0x14); // Update Channel C |
||
340 | break; |
||
341 | case 30: |
||
342 | i2c_write_byte(AnalogOffsetGier); // Value |
||
343 | break; |
||
344 | case 31: |
||
345 | i2c_write_byte(0x80); // Value |
||
346 | break; |
||
347 | case 32: |
||
1639 | holgerb | 348 | I2C_Stop(); |
1638 | holgerb | 349 | I2CTimeout = 10; |
350 | twi_state = 0; |
||
351 | break; |
||
352 | default: twi_state = 0; |
||
353 | break; |
||
354 | } |
||
355 | TWCR |= 0x80; |
||
356 | } |
||
1639 | holgerb | 357 |