Rev 1209 | Rev 1211 | Go to most recent revision | Details | Compare with Previous | 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; |
1 | ingob | 7 | unsigned char motor = 0; |
1210 | hbuss | 8 | unsigned char motorread = 0,MissingMotor = 0; |
1209 | hbuss | 9 | unsigned char motor_rx[16],motor_rx2[16]; |
1210 | hbuss | 10 | unsigned char MotorPresent[MAX_MOTORS]; |
1 | ingob | 11 | |
12 | //############################################################################ |
||
13 | //Initzialisieren der I2C (TWI) Schnittstelle |
||
14 | void i2c_init(void) |
||
15 | //############################################################################ |
||
16 | { |
||
17 | TWSR = 0; |
||
18 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
||
19 | } |
||
20 | |||
21 | //############################################################################ |
||
22 | //Start I2C |
||
1209 | hbuss | 23 | void i2c_start(void) |
1 | ingob | 24 | //############################################################################ |
25 | { |
||
26 | TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
||
27 | } |
||
28 | |||
29 | //############################################################################ |
||
30 | void i2c_stop(void) |
||
31 | //############################################################################ |
||
32 | { |
||
33 | TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
||
34 | } |
||
35 | |||
173 | holgerb | 36 | void i2c_reset(void) |
1 | ingob | 37 | //############################################################################ |
173 | holgerb | 38 | { |
39 | i2c_stop(); |
||
40 | twi_state = 0; |
||
41 | motor = TWDR; |
||
42 | motor = 0; |
||
43 | TWCR = 0x80; |
||
44 | TWAMR = 0; |
||
45 | TWAR = 0; |
||
46 | TWDR = 0; |
||
47 | TWSR = 0; |
||
48 | TWBR = 0; |
||
49 | i2c_init(); |
||
50 | i2c_start(); |
||
51 | i2c_write_byte(0); |
||
52 | } |
||
53 | |||
54 | //############################################################################ |
||
1209 | hbuss | 55 | void i2c_write_byte(char byte) |
1 | ingob | 56 | //############################################################################ |
57 | { |
||
58 | TWSR = 0x00; |
||
59 | TWDR = byte; |
||
60 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
61 | } |
||
62 | |||
1209 | hbuss | 63 | /****************************************/ |
64 | /* Write to I2C */ |
||
65 | /****************************************/ |
||
66 | void I2C_WriteByte(int8_t byte) |
||
67 | { |
||
68 | // move byte to send into TWI Data Register |
||
69 | TWDR = byte; |
||
70 | // clear interrupt flag (TWINT = 1) |
||
71 | // enable i2c bus (TWEN = 1) |
||
72 | // enable interrupt (TWIE = 1) |
||
73 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
74 | } |
||
75 | |||
76 | /****************************************/ |
||
77 | /* Receive byte and send ACK */ |
||
78 | /****************************************/ |
||
79 | void I2C_ReceiveByte(void) |
||
80 | { |
||
81 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
||
82 | } |
||
83 | |||
84 | /****************************************/ |
||
85 | /* I2C receive last byte and send no ACK*/ |
||
86 | /****************************************/ |
||
87 | void I2C_ReceiveLastByte(void) |
||
88 | { |
||
89 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
||
90 | } |
||
91 | |||
92 | |||
93 | |||
1 | ingob | 94 | //############################################################################ |
95 | SIGNAL (TWI_vect) |
||
96 | //############################################################################ |
||
97 | { |
||
1210 | hbuss | 98 | static unsigned char missing_motor,cnt_motor; |
99 | switch(twi_state++) |
||
1 | ingob | 100 | { |
1210 | hbuss | 101 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
102 | // Writing the Data |
||
103 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1 | ingob | 104 | case 0: |
1210 | hbuss | 105 | while(Mixer.Motor[motor][0] <= 0 && motor < MAX_MOTORS) motor++; // skip if not used |
106 | if(motor == MAX_MOTORS) // writing finished -> now read |
||
107 | { |
||
108 | motor = 0; |
||
109 | twi_state = 3; |
||
110 | i2c_write_byte(0x53+(motorread*2)); |
||
111 | } |
||
1209 | hbuss | 112 | else i2c_write_byte(0x52+(motor*2)); |
1 | ingob | 113 | break; |
114 | case 1: |
||
1209 | hbuss | 115 | i2c_write_byte(Motor[motor++]); |
1111 | hbuss | 116 | break; |
117 | case 2: |
||
1210 | hbuss | 118 | if(TWSR == 0x30) { if(!missing_motor) missing_motor = motor; } |
1111 | hbuss | 119 | i2c_stop(); |
1210 | hbuss | 120 | I2CTimeout = 10; |
1209 | hbuss | 121 | twi_state = 0; |
1111 | hbuss | 122 | i2c_start(); |
123 | break; |
||
1210 | hbuss | 124 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
125 | // Reading Data |
||
126 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
1111 | hbuss | 127 | case 3: |
1209 | hbuss | 128 | //Transmit 1st byte for reading |
1210 | hbuss | 129 | |
130 | if(TWSR != 0x40) // Error? |
||
131 | { |
||
132 | MotorPresent[motorread] = 0; |
||
133 | motorread++; |
||
134 | if(motorread >= MAX_MOTORS) motorread = 0; |
||
135 | i2c_stop(); |
||
136 | twi_state = 0; |
||
137 | } |
||
138 | else |
||
139 | { |
||
140 | MotorPresent[motorread] = ('1' - '-') + motorread; |
||
141 | } |
||
142 | MissingMotor = missing_motor; |
||
1209 | hbuss | 143 | I2C_ReceiveByte(); |
1111 | hbuss | 144 | break; |
1210 | hbuss | 145 | case 4: //Read 1st byte and transmit 2nd Byte |
1111 | hbuss | 146 | motor_rx[motorread] = TWDR; |
1209 | hbuss | 147 | I2C_ReceiveLastByte(); //nack |
148 | break; |
||
1210 | hbuss | 149 | case 5: |
1209 | hbuss | 150 | //Read 2nd byte |
151 | motor_rx2[motorread++] = TWDR; |
||
1111 | hbuss | 152 | i2c_stop(); |
1209 | hbuss | 153 | twi_state = 0; |
1210 | hbuss | 154 | missing_motor = 0; |
1111 | hbuss | 155 | break; |
1210 | hbuss | 156 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
157 | // writing Gyro-Offset |
||
158 | //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
||
159 | case 8: |
||
1111 | hbuss | 160 | i2c_write_byte(0x98); // Address of the DAC |
161 | break; |
||
162 | case 9: |
||
163 | i2c_write_byte(0x10); // Update Channel A |
||
164 | break; |
||
165 | case 10: |
||
166 | i2c_write_byte(AnalogOffsetNick); // Value |
||
167 | break; |
||
168 | case 11: |
||
169 | i2c_write_byte(0x80); // Value |
||
170 | break; |
||
171 | case 12: |
||
172 | i2c_stop(); |
||
173 | I2CTimeout = 10; |
||
174 | i2c_start(); |
||
175 | break; |
||
176 | case 13: |
||
177 | i2c_write_byte(0x98); // Address of the DAC |
||
178 | break; |
||
179 | case 14: |
||
180 | i2c_write_byte(0x12); // Update Channel B |
||
181 | break; |
||
182 | case 15: |
||
183 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
184 | break; |
||
185 | case 16: |
||
186 | i2c_write_byte(0x80); // Value |
||
187 | break; |
||
188 | case 17: |
||
189 | i2c_stop(); |
||
190 | I2CTimeout = 10; |
||
191 | i2c_start(); |
||
192 | break; |
||
193 | case 18: |
||
194 | i2c_write_byte(0x98); // Address of the DAC |
||
195 | break; |
||
196 | case 19: |
||
197 | i2c_write_byte(0x14); // Update Channel C |
||
198 | break; |
||
199 | case 20: |
||
200 | i2c_write_byte(AnalogOffsetGier); // Value |
||
201 | break; |
||
202 | case 21: |
||
203 | i2c_write_byte(0x80); // Value |
||
204 | break; |
||
205 | case 22: |
||
206 | i2c_stop(); |
||
207 | I2CTimeout = 10; |
||
208 | twi_state = 0; |
||
209 | break; |
||
1209 | hbuss | 210 | default: twi_state = 0; |
211 | break; |
||
212 | } |
||
1111 | hbuss | 213 | TWCR |= 0x80; |
214 | } |
||
1209 | hbuss | 215 | /* |
1111 | hbuss | 216 | //############################################################################ |
217 | SIGNAL (TWI_vect) |
||
218 | //############################################################################ |
||
219 | { |
||
220 | switch (twi_state++) |
||
221 | { |
||
222 | case 0: |
||
223 | i2c_write_byte(0x52+(motor*2)); |
||
224 | break; |
||
225 | case 1: |
||
226 | switch(motor++) |
||
227 | { |
||
228 | case 0: |
||
1 | ingob | 229 | i2c_write_byte(Motor_Vorne); |
230 | break; |
||
231 | case 1: |
||
232 | i2c_write_byte(Motor_Hinten); |
||
233 | break; |
||
234 | case 2: |
||
235 | i2c_write_byte(Motor_Rechts); |
||
236 | break; |
||
237 | case 3: |
||
238 | i2c_write_byte(Motor_Links); |
||
239 | break; |
||
240 | } |
||
241 | break; |
||
242 | case 2: |
||
243 | i2c_stop(); |
||
244 | if (motor<4) twi_state = 0; |
||
245 | else motor = 0; |
||
246 | i2c_start(); |
||
247 | break; |
||
248 | |||
249 | //Liest Daten von Motor |
||
250 | case 3: |
||
251 | i2c_write_byte(0x53+(motorread*2)); |
||
252 | break; |
||
253 | case 4: |
||
254 | switch(motorread) |
||
255 | { |
||
256 | case 0: |
||
257 | i2c_write_byte(Motor_Vorne); |
||
258 | break; |
||
259 | case 1: |
||
260 | i2c_write_byte(Motor_Hinten); |
||
261 | break; |
||
262 | case 2: |
||
263 | i2c_write_byte(Motor_Rechts); |
||
264 | break; |
||
265 | case 3: |
||
266 | i2c_write_byte(Motor_Links); |
||
267 | break; |
||
268 | } |
||
269 | break; |
||
270 | case 5: //1 Byte vom Motor lesen |
||
271 | motor_rx[motorread] = TWDR; |
||
173 | holgerb | 272 | |
1 | ingob | 273 | case 6: |
274 | switch(motorread) |
||
275 | { |
||
276 | case 0: |
||
277 | i2c_write_byte(Motor_Vorne); |
||
278 | break; |
||
279 | case 1: |
||
280 | i2c_write_byte(Motor_Hinten); |
||
281 | break; |
||
282 | case 2: |
||
283 | i2c_write_byte(Motor_Rechts); |
||
284 | break; |
||
285 | case 3: |
||
286 | i2c_write_byte(Motor_Links); |
||
287 | break; |
||
288 | } |
||
289 | break; |
||
290 | case 7: //2 Byte vom Motor lesen |
||
291 | motor_rx[motorread+4] = TWDR; |
||
292 | motorread++; |
||
293 | if (motorread>3) motorread=0; |
||
294 | i2c_stop(); |
||
173 | holgerb | 295 | I2CTimeout = 10; |
1 | ingob | 296 | twi_state = 0; |
918 | hbuss | 297 | break; |
298 | case 8: // Gyro-Offset |
||
299 | i2c_write_byte(0x98); // Address of the DAC |
||
300 | break; |
||
301 | case 9: |
||
302 | i2c_write_byte(0x10); // Update Channel A |
||
303 | break; |
||
304 | case 10: |
||
305 | i2c_write_byte(AnalogOffsetNick); // Value |
||
306 | break; |
||
307 | case 11: |
||
308 | i2c_write_byte(0x80); // Value |
||
309 | break; |
||
310 | case 12: |
||
311 | i2c_stop(); |
||
312 | I2CTimeout = 10; |
||
313 | i2c_start(); |
||
314 | break; |
||
315 | case 13: |
||
316 | i2c_write_byte(0x98); // Address of the DAC |
||
317 | break; |
||
318 | case 14: |
||
319 | i2c_write_byte(0x12); // Update Channel B |
||
320 | break; |
||
321 | case 15: |
||
322 | i2c_write_byte(AnalogOffsetRoll); // Value |
||
323 | break; |
||
324 | case 16: |
||
325 | i2c_write_byte(0x80); // Value |
||
326 | break; |
||
327 | case 17: |
||
328 | i2c_stop(); |
||
329 | I2CTimeout = 10; |
||
330 | i2c_start(); |
||
331 | break; |
||
332 | case 18: |
||
333 | i2c_write_byte(0x98); // Address of the DAC |
||
334 | break; |
||
335 | case 19: |
||
336 | i2c_write_byte(0x14); // Update Channel C |
||
337 | break; |
||
338 | case 20: |
||
339 | i2c_write_byte(AnalogOffsetGier); // Value |
||
340 | break; |
||
341 | case 21: |
||
342 | i2c_write_byte(0x80); // Value |
||
343 | break; |
||
344 | case 22: |
||
345 | i2c_stop(); |
||
346 | I2CTimeout = 10; |
||
347 | twi_state = 0; |
||
348 | break; |
||
1 | ingob | 349 | } |
173 | holgerb | 350 | TWCR |= 0x80; |
1 | ingob | 351 | } |
1209 | hbuss | 352 | */ |