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 |
||
1644 | holgerb | 184 | unsigned char test[] = {0,0,'#',0x1F,255,30,99,64,0x00,7,8,9,10}; |
1643 | holgerb | 185 | static unsigned char missing_motor,send = 0,crc = 0,read_more = 0; |
1644 | holgerb | 186 | J4High; |
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*/) |
1644 | holgerb | 206 | if((!(Motor[motor].Version & MOTOR_STATE_NEW_PROTOCOL_MASK) || !Motor[motor].SetPointLowerBits) && !TransmitBlConfig) |
1643 | holgerb | 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 | } |
1638 | holgerb | 226 | break; |
227 | case 3: |
||
1642 | killagreg | 228 | motor++; |
1638 | holgerb | 229 | if(TWSR == 0x30) |
230 | { |
||
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 | } |
||
1639 | holgerb | 234 | I2C_Stop(); |
1638 | holgerb | 235 | I2CTimeout = 10; |
236 | twi_state = 0; |
||
1639 | holgerb | 237 | I2C_Start(); |
1638 | holgerb | 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++; |
||
1643 | holgerb | 248 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
1639 | holgerb | 249 | I2C_Stop(); |
1638 | holgerb | 250 | twi_state = 0; |
251 | } |
||
252 | else |
||
253 | { |
||
254 | Motor[motorread].State |= MOTOR_STATE_PRESENT_MASK; // set present bit |
||
1643 | holgerb | 255 | if(motorread == ReadTemperature || TransmitBlConfig) |
256 | { |
||
257 | read_more = 1; |
||
258 | I2C_ReceiveByte(); |
||
259 | } |
||
260 | else |
||
261 | { |
||
262 | I2C_ReceiveLastByte(); |
||
263 | read_more = 0; |
||
264 | } |
||
1638 | holgerb | 265 | } |
266 | MissingMotor = missing_motor; |
||
267 | missing_motor = 0; |
||
268 | break; |
||
269 | case 5: //Read 1st byte and transmit 2nd Byte |
||
270 | Motor[motorread].Current = TWDR; |
||
1643 | holgerb | 271 | if(read_more) I2C_ReceiveByte() //nack |
272 | else |
||
273 | { |
||
274 | motorread++; // next motor |
||
275 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; ReadTemperature = ++ReadTemperature & 0x0f;} |
||
276 | I2C_Stop(); |
||
277 | twi_state = 0; |
||
278 | } |
||
1638 | holgerb | 279 | break; |
1642 | killagreg | 280 | case 6: |
1638 | holgerb | 281 | //Read 2nd byte and transmit 3rd Byte |
282 | Motor[motorread].MaxPWM = TWDR; |
||
1642 | killagreg | 283 | if(TWDR == 250) |
1638 | holgerb | 284 | { |
1643 | holgerb | 285 | if(!MotorenEin) Motor[motorread].Version |= MOTOR_STATE_NEW_PROTOCOL_MASK; |
1642 | killagreg | 286 | } |
287 | else if(TransmitBlConfig) Motor[motorread].Version = 0; |
||
1638 | holgerb | 288 | I2C_ReceiveLastByte(); //nack |
289 | break; |
||
1642 | killagreg | 290 | case 7: // read next |
1638 | holgerb | 291 | Motor[motorread].Temperature = TWDR; |
292 | motorread++; // next motor |
||
1639 | holgerb | 293 | if(motorread >= MAX_MOTORS) { TransmitBlConfig = 0; motorread = 0; } |
294 | I2C_Stop(); |
||
1638 | holgerb | 295 | twi_state = 0; |
1643 | holgerb | 296 | break; |
1638 | holgerb | 297 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
298 | // writing Gyro-Offset |
||
299 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
300 | case 18: |
||
301 | i2c_write_byte(0x98); // Address of the DAC |
||
302 | break; |
||
303 | case 19: |
||
304 | i2c_write_byte(0x10); // Update Channel A |
||
305 | break; |
||
306 | case 20: |
||
307 | i2c_write_byte(AnalogOffsetNick); // Value |
||
308 | break; |
||
309 | case 21: |
||
310 | i2c_write_byte(0x80); // Value |
||
311 | break; |
||
312 | case 22: |
||
1639 | holgerb | 313 | I2C_Stop(); |
1638 | holgerb | 314 | I2CTimeout = 10; |
1639 | holgerb | 315 | I2C_Start(); |
1638 | holgerb | 316 | break; |
317 | case 23: |
||
318 | i2c_write_byte(0x98); // Address of the DAC |
||
319 | break; |
||
320 | case 24: |
||
321 | i2c_write_byte(0x12); // Update Channel B |
||
322 | break; |
||
323 | case 25: |
||
324 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
325 | break; |
||
326 | case 26: |
||
327 | i2c_write_byte(0x80); // Value |
||
328 | break; |
||
329 | case 27: |
||
1639 | holgerb | 330 | I2C_Stop(); |
1638 | holgerb | 331 | I2CTimeout = 10; |
1639 | holgerb | 332 | I2C_Start(); |
1638 | holgerb | 333 | break; |
334 | case 28: |
||
335 | i2c_write_byte(0x98); // Address of the DAC |
||
336 | break; |
||
337 | case 29: |
||
338 | i2c_write_byte(0x14); // Update Channel C |
||
339 | break; |
||
340 | case 30: |
||
341 | i2c_write_byte(AnalogOffsetGier); // Value |
||
342 | break; |
||
343 | case 31: |
||
344 | i2c_write_byte(0x80); // Value |
||
345 | break; |
||
346 | case 32: |
||
1639 | holgerb | 347 | I2C_Stop(); |
1638 | holgerb | 348 | I2CTimeout = 10; |
349 | twi_state = 0; |
||
350 | break; |
||
351 | default: twi_state = 0; |
||
352 | break; |
||
353 | } |
||
354 | TWCR |= 0x80; |
||
1644 | holgerb | 355 | J4Low; |
1638 | holgerb | 356 | } |
1639 | holgerb | 357 |