Rev 1221 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1221 | Rev 1222 | ||
---|---|---|---|
Line 49... | Line 49... | ||
49 | // + POSSIBILITY OF SUCH DAMAGE. |
49 | // + POSSIBILITY OF SUCH DAMAGE. |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
50 | // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
Line 51... | Line 51... | ||
51 | 51 | ||
52 | #include <avr/io.h> |
52 | #include <avr/io.h> |
53 | #include <avr/interrupt.h> |
- | |
- | 53 | #include <avr/interrupt.h> |
|
54 | 54 | #include <util/twi.h> |
|
- | 55 | #include "main.h" |
|
55 | #include "main.h" |
56 | #include "eeprom.h" |
56 | #include "twimaster.h" |
57 | #include "twimaster.h" |
57 | #include "fc.h" |
58 | #include "fc.h" |
Line 58... | Line 59... | ||
58 | #include "analog.h" |
59 | #include "analog.h" |
59 | - | ||
60 | volatile uint8_t twi_state = 0; |
- | |
61 | uint8_t motor_write = 0; |
60 | |
62 | uint8_t motor_read = 0; |
- | |
63 | volatile uint8_t dac_channel = 0; |
- | |
64 | 61 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX; |
|
65 | #ifdef USE_QUADRO |
- | |
66 | uint8_t motor_rx[8]; |
62 | volatile uint8_t dac_channel = 0; |
67 | #else |
- | |
Line 68... | Line 63... | ||
68 | uint8_t motor_rx[16]; |
63 | volatile uint8_t motor_write = 0; |
Line -... | Line 64... | ||
- | 64 | volatile uint8_t motor_read = 0; |
|
Line 69... | Line -... | ||
69 | #endif |
- | |
70 | - | ||
Line 71... | Line -... | ||
71 | volatile uint16_t I2CTimeout = 100; |
- | |
72 | 65 | ||
Line 73... | Line 66... | ||
73 | 66 | volatile uint16_t I2CTimeout = 100; |
|
74 | #define SCL_CLOCK 200000L |
67 | |
75 | #define I2C_TIMEOUT 30000 |
- | |
76 | - | ||
77 | #define TWSR_STATUS_MASK 0xF8 |
- | |
78 | // for Master Transmitter Mode |
- | |
79 | - | ||
80 | #define I2C_STATUS_START 0x08 |
- | |
Line 81... | Line 68... | ||
81 | #define I2C_STATUS_REPEATSTART 0x10 |
68 | uint8_t MissingMotor = 0; |
82 | #define I2C_STATUS_TX_SLA_ACK 0x18 |
69 | |
83 | #define I2C_STATUS_SLAW_NOACK 0x20 |
70 | |
84 | #define I2C_STATUS_TX_DATA_ACK 0x28 |
71 | MotorData_t Motor[MAX_MOTORS]; |
85 | #define I2C_STATUS_TX_DATA_NOTACK 0x30 |
72 | |
- | 73 | #define SCL_CLOCK 200000L |
|
86 | #define I2C_STATUS_RX_DATA_ACK 0x50 |
74 | #define I2C_TIMEOUT 30000 |
87 | #define I2C_STATUS_RX_DATA_NOTACK 0x58 |
75 | |
Line 88... | Line 76... | ||
88 | 76 | /**************************************************/ |
|
89 | /**************************************************/ |
77 | /* Initialize I2C (TWI) */ |
Line 106... | Line 94... | ||
106 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
94 | TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
Line 107... | Line 95... | ||
107 | 95 | ||
108 | // set TWI Bit Rate Register |
96 | // set TWI Bit Rate Register |
Line 109... | Line 97... | ||
109 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
97 | TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
110 | 98 | ||
111 | twi_state = 0; |
99 | twi_state = TWI_STATE_MOTOR_TX; |
Line -... | Line 100... | ||
- | 100 | motor_write = 0; |
|
- | 101 | motor_read = 0; |
|
- | 102 | ||
- | 103 | for(i=0; i < MAX_MOTORS; i++) |
|
- | 104 | { |
|
- | 105 | Motor[i].SetPoint = 0; |
|
- | 106 | Motor[i].Present = 0; |
|
- | 107 | Motor[i].Error = 0; |
|
112 | motor_write = 0; |
108 | Motor[i].MaxPWM = 0; |
113 | motor_read = 0; |
109 | } |
Line 114... | Line 110... | ||
114 | 110 | ||
115 | SREG = sreg; |
111 | SREG = sreg; |
116 | } |
112 | } |
117 | 113 | ||
118 | /****************************************/ |
114 | /****************************************/ |
- | 115 | /* Start I2C */ |
|
119 | /* Start I2C */ |
116 | /****************************************/ |
120 | /****************************************/ |
117 | void I2C_Start(uint8_t start_state) |
121 | void I2C_Start(void) |
118 | { |
122 | { |
119 | twi_state = start_state; |
123 | // TWI Control Register |
120 | // TWI Control Register |
Line 132... | Line 129... | ||
132 | } |
129 | } |
Line 133... | Line 130... | ||
133 | 130 | ||
134 | /****************************************/ |
131 | /****************************************/ |
135 | /* Stop I2C */ |
132 | /* Stop I2C */ |
136 | /****************************************/ |
133 | /****************************************/ |
137 | void I2C_Stop(void) |
134 | void I2C_Stop(uint8_t start_state) |
- | 135 | { |
|
138 | { |
136 | twi_state = start_state; |
139 | // TWI Control Register |
137 | // TWI Control Register |
140 | // clear TWI interrupt flag (TWINT=1) |
138 | // clear TWI interrupt flag (TWINT=1) |
141 | // disable TWI Acknowledge Bit (TWEA = 0) |
139 | // disable TWI Acknowledge Bit (TWEA = 0) |
142 | // diable TWI START Condition Bit (TWSTA = 1), no MASTER |
140 | // diable TWI START Condition Bit (TWSTA = 1), no MASTER |
Line 151... | Line 149... | ||
151 | /****************************************/ |
149 | /****************************************/ |
152 | /* Write to I2C */ |
150 | /* Write to I2C */ |
153 | /****************************************/ |
151 | /****************************************/ |
154 | void I2C_WriteByte(int8_t byte) |
152 | void I2C_WriteByte(int8_t byte) |
155 | { |
153 | { |
156 | // move byte to send into TWI Data Register |
154 | // move byte to send into TWI Data Register |
157 | TWDR = byte; |
155 | TWDR = byte; |
158 | // clear interrupt flag (TWINT = 1) |
156 | // clear interrupt flag (TWINT = 1) |
159 | // enable i2c bus (TWEN = 1) |
157 | // enable i2c bus (TWEN = 1) |
160 | // enable interrupt (TWIE = 1) |
158 | // enable interrupt (TWIE = 1) |
161 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
159 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
162 | } |
160 | } |
Line 163... | Line 161... | ||
163 | 161 | ||
164 | 162 | ||
165 | /****************************************/ |
163 | /****************************************/ |
166 | /* Receive byte and send ACK */ |
164 | /* Receive byte and send ACK */ |
167 | /****************************************/ |
165 | /****************************************/ |
168 | void I2C_ReceiveByte(void) |
166 | void I2C_ReceiveByte(void) |
169 | { |
167 | { |
Line 170... | Line 168... | ||
170 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
168 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
171 | } |
169 | } |
172 | 170 | ||
173 | /****************************************/ |
171 | /****************************************/ |
174 | /* I2C receive last byte and send no ACK*/ |
172 | /* I2C receive last byte and send no ACK*/ |
175 | /****************************************/ |
173 | /****************************************/ |
176 | void I2C_ReceiveLastByte(void) |
174 | void I2C_ReceiveLastByte(void) |
Line 177... | Line 175... | ||
177 | { |
175 | { |
178 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
176 | TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
179 | } |
177 | } |
180 | 178 | ||
181 | 179 | ||
182 | /****************************************/ |
180 | /****************************************/ |
183 | /* Reset I2C */ |
181 | /* Reset I2C */ |
184 | /****************************************/ |
182 | /****************************************/ |
185 | void I2C_Reset(void) |
183 | void I2C_Reset(void) |
186 | { |
184 | { |
187 | // stop i2c bus |
185 | // stop i2c bus |
188 | I2C_Stop(); |
186 | I2C_Stop(TWI_STATE_MOTOR_TX); |
Line 195... | Line 193... | ||
195 | TWAR = 0; |
193 | TWAR = 0; |
196 | TWDR = 0; |
194 | TWDR = 0; |
197 | TWSR = 0; |
195 | TWSR = 0; |
198 | TWBR = 0; |
196 | TWBR = 0; |
199 | I2C_Init(); |
197 | I2C_Init(); |
200 | I2C_Start(); |
198 | I2C_Start(TWI_STATE_MOTOR_TX); |
201 | I2C_WriteByte(0); |
- | |
202 | } |
199 | } |
Line 203... | Line 200... | ||
203 | 200 | ||
204 | 201 | ||
205 | /****************************************/ |
202 | /****************************************/ |
206 | /* I2C ISR */ |
- | |
207 | /****************************************/ |
- | |
208 | 203 | /* I2C ISR */ |
|
209 | #ifdef USE_QUADRO |
204 | /****************************************/ |
- | 205 | ISR (TWI_vect) |
|
Line 210... | Line 206... | ||
210 | ISR (TWI_vect) |
206 | { |
211 | { |
207 | static uint8_t missing_motor = 0; |
212 | 208 | ||
213 | switch (twi_state++) // First i2c_start from SendMotorData() |
209 | switch (twi_state++) // First i2c_start from SendMotorData() |
- | 210 | { |
|
- | 211 | // Master Transmit |
|
- | 212 | case 0: // TWI_STATE_MOTOR_TX |
|
- | 213 | // skip motor if not used in mixer |
|
- | 214 | while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++; |
|
- | 215 | if(motor_write >= MAX_MOTORS) // writing finished, read now |
|
- | 216 | { |
|
- | 217 | motor_write = 0; |
|
214 | { |
218 | twi_state = TWI_STATE_MOTOR_RX; |
215 | // Master Transmit |
219 | I2C_WriteByte(0x53 + (motor_read * 2) ); // select slave adress in rx mode |
216 | case 0: // Send SLA-W |
220 | } |
217 | I2C_WriteByte(0x52 + (motor_write * 2) ); |
- | |
218 | break; |
- | |
219 | case 1: // Send Data to Slave |
- | |
220 | switch(motor_write) |
- | |
221 | { |
- | |
222 | case 0: |
- | |
223 | I2C_WriteByte(Motor1); |
221 | else I2C_WriteByte(0x52 + (motor_write * 2) ); // select slave adress in tx mode |
224 | break; |
- | |
225 | case 1: |
- | |
226 | I2C_WriteByte(Motor2); |
- | |
227 | break; |
- | |
228 | case 2: |
- | |
229 | I2C_WriteByte(Motor3); |
- | |
230 | break; |
- | |
231 | case 3: |
- | |
232 | I2C_WriteByte(Motor4); |
222 | break; |
233 | break; |
223 | case 1: // Send Data to Slave |
234 | } |
- | |
235 | break; |
- | |
236 | case 2: // repeat case 0+1 for all motors |
- | |
237 | I2C_Stop(); |
- | |
238 | if (motor_write < 3) |
- | |
239 | { |
- | |
240 | motor_write++; // jump to next motor |
- | |
241 | twi_state = 0; // and repeat from state 0 |
- | |
242 | } |
- | |
243 | else |
- | |
244 | { // data to last motor send |
- | |
245 | motor_write = 0; // reset motor write counter |
- | |
246 | } |
- | |
247 | I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
- | |
248 | break; |
- | |
249 | - | ||
250 | // Master Receive |
- | |
251 | case 3: // Send SLA-R |
- | |
252 | I2C_WriteByte(0x53 + (motor_read * 2) ); |
- | |
253 | break; |
- | |
254 | case 4: |
- | |
255 | //Transmit 1st byte |
224 | I2C_WriteByte(Motor[motor_write].SetPoint); // transmit rotation rate setpoint |
256 | I2C_ReceiveByte(); |
- | |
257 | break; |
- | |
258 | case 5: //Read 1st byte and transmit 2nd Byte |
- | |
259 | motor_rx[motor_read] = TWDR; |
- | |
260 | I2C_ReceiveLastByte(); |
- | |
261 | break; |
- | |
262 | case 6: |
- | |
263 | //Read 2nd byte |
- | |
264 | motor_rx[motor_read + 4] = TWDR; |
- | |
265 | motor_read++; |
- | |
266 | if (motor_read > 3) motor_read = 0; |
- | |
267 | I2C_Stop(); |
- | |
268 | twi_state = 0; |
- | |
269 | I2CTimeout = 10; |
- | |
270 | break; |
- | |
271 | - | ||
272 | // Gyro-Offsets |
- | |
273 | case 7: |
- | |
274 | I2C_WriteByte(0x98); // Address the DAC |
- | |
275 | break; |
- | |
276 | - | ||
277 | case 8: |
- | |
278 | I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C) |
- | |
279 | break; |
- | |
280 | 225 | break; |
|
281 | case 9: |
- | |
282 | switch(dac_channel) |
- | |
283 | { |
- | |
284 | case 0: |
- | |
285 | I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A |
226 | case 2: // repeat case 0+1 for all motors |
286 | break; |
- | |
287 | case 1: |
- | |
288 | I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B |
227 | if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received |
289 | break; |
- | |
290 | case 2: |
228 | { |
291 | I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C |
- | |
292 | break; |
- | |
293 | } |
- | |
294 | break; |
- | |
295 | - | ||
296 | case 10: |
- | |
297 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
- | |
298 | break; |
- | |
299 | - | ||
300 | case 11: |
- | |
301 | I2C_Stop(); |
- | |
302 | I2CTimeout = 10; |
- | |
303 | // repeat case 7...10 until all DAC Channels are updated |
- | |
304 | if(dac_channel < 2) |
- | |
305 | { |
- | |
306 | dac_channel ++; // jump to next channel |
- | |
307 | twi_state = 7; // and repeat from state 7 |
- | |
308 | I2C_Start(); // start transmission for next channel |
- | |
309 | } |
- | |
310 | else |
- | |
311 | { // data to last motor send |
- | |
312 | dac_channel = 0; // reset dac channel counter |
- | |
313 | twi_state = 0; // reset twi_state |
- | |
314 | } |
- | |
315 | break; |
229 | if(!missing_motor) missing_motor = motor_write + 1; |
316 | - | ||
317 | default: |
230 | if(++Motor[motor_write].Error == 0) Motor[motor_write].Error = 255; // increment error counter and handle overflow |
318 | I2C_Stop(); |
231 | } |
319 | twi_state = 0; |
- | |
320 | I2CTimeout = 10; |
- | |
321 | motor_write = 0; |
- | |
322 | motor_read = 0; |
- | |
323 | } |
- | |
324 | } |
- | |
325 | #else // USE_OCTO, USE_OCTO2, USE_OCTO3 |
- | |
326 | ISR (TWI_vect) |
- | |
327 | { |
- | |
328 | - | ||
329 | switch (twi_state++) // First i2c_start from SendMotorData() |
- | |
330 | { |
232 | I2C_Stop(TWI_STATE_MOTOR_TX); |
331 | // Master Transmit |
233 | I2CTimeout = 10; |
332 | case 0: // Send SLA-W |
- | |
333 | I2C_WriteByte(0x52 + (motor_write * 2) ); |
- | |
334 | break; |
- | |
335 | case 1: // Send Data to Slave |
- | |
336 | switch(motor_write) |
- | |
337 | { |
- | |
338 | case 0: |
- | |
339 | I2C_WriteByte(Motor1); |
- | |
340 | break; |
- | |
341 | case 1: |
234 | motor_write++; // next motor |
342 | I2C_WriteByte(Motor2); |
- | |
343 | break; |
- | |
344 | case 2: |
235 | I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
345 | I2C_WriteByte(Motor3); |
236 | break; |
346 | break; |
- | |
347 | case 3: |
- | |
348 | I2C_WriteByte(Motor4); |
- | |
349 | break; |
- | |
350 | case 5: |
- | |
351 | I2C_WriteByte(Motor5); |
- | |
352 | break; |
- | |
353 | case 6: |
- | |
354 | I2C_WriteByte(Motor6); |
- | |
355 | break; |
- | |
356 | case 7: |
- | |
357 | I2C_WriteByte(Motor7); |
- | |
358 | break; |
- | |
359 | case 8: |
- | |
360 | I2C_WriteByte(Motor8); |
- | |
361 | break; |
237 | // Master Receive Data |
362 | } |
- | |
363 | break; |
238 | case 3: |
364 | case 2: // repeat case 0+1 for all motors |
- | |
365 | I2C_Stop(); |
239 | if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted, if not ACK received |
- | 240 | { // no response from the addressed slave received |
|
366 | if (motor_write < 7) |
241 | Motor[motor_read].Present = 0; |
367 | { |
242 | motor_read++; // next motor |
368 | motor_write++; // jump to next motor |
243 | if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one |
369 | twi_state = 0; // and repeat from state 0 |
244 | I2C_Stop(TWI_STATE_MOTOR_TX); |
- | 245 | } |
|
370 | } |
246 | else |
371 | else |
247 | { |
372 | { // data to last motor send |
248 | Motor[motor_read].Present = ('1' - '-') + motor_read; |
- | 249 | I2C_ReceiveByte(); //Transmit 1st byte |
|
373 | motor_write = 0; // reset motor write counter |
250 | } |
374 | } |
- | |
375 | I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive |
- | |
376 | break; |
- | |
377 | - | ||
378 | // Master Receive |
- | |
379 | case 3: // Send SLA-R |
- | |
380 | I2C_WriteByte(0x53 + (motor_read * 2) ); |
- | |
381 | break; |
- | |
382 | case 4: |
- | |
383 | //Transmit 1st byte |
251 | MissingMotor = missing_motor; |
384 | I2C_ReceiveByte(); |
252 | missing_motor = 0; |
385 | break; |
253 | break; |
386 | case 5: //Read 1st byte and transmit 2nd Byte |
254 | case 4: //Read 1st byte and transmit 2nd Byte |
387 | motor_rx[motor_read] = TWDR; |
255 | Motor[motor_read].Current = TWDR; |
388 | I2C_ReceiveLastByte(); |
256 | I2C_ReceiveLastByte(); // nack |
389 | break; |
257 | break; |
390 | case 6: |
258 | case 5: |
391 | //Read 2nd byte |
259 | //Read 2nd byte |
392 | motor_rx[motor_read + 8] = TWDR; |
260 | Motor[motor_read].MaxPWM = TWDR;; |
393 | motor_read++; |
- | |
394 | if (motor_read > 7) motor_read = 0; |
- | |
395 | I2C_Stop(); |
261 | motor_read++; // next motor |
Line 396... | Line 262... | ||
396 | twi_state = 0; |
262 | if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one |
397 | I2CTimeout = 10; |
263 | I2C_Stop(TWI_STATE_MOTOR_TX); |
398 | break; |
264 | break; |
399 | 265 | ||
Line 400... | Line 266... | ||
400 | // Gyro-Offsets |
266 | // writing Gyro-Offsets |
Line 424... | Line 290... | ||
424 | case 10: |
290 | case 10: |
425 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
291 | I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80 |
426 | break; |
292 | break; |
Line 427... | Line 293... | ||
427 | 293 | ||
428 | case 11: |
294 | case 11: |
429 | I2C_Stop(); |
295 | I2C_Stop(TWI_STATE_MOTOR_TX); |
430 | I2CTimeout = 10; |
296 | I2CTimeout = 10; |
431 | // repeat case 7...10 until all DAC Channels are updated |
297 | // repeat case 7...10 until all DAC Channels are updated |
432 | if(dac_channel < 2) |
298 | if(dac_channel < 2) |
433 | { |
299 | { |
434 | dac_channel ++; // jump to next channel |
- | |
435 | twi_state = 7; // and repeat from state 7 |
300 | dac_channel ++; // jump to next channel |
436 | I2C_Start(); // start transmission for next channel |
301 | I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel |
437 | } |
302 | } |
438 | else |
303 | else |
439 | { // data to last motor send |
304 | { // data to last motor send |
440 | dac_channel = 0; // reset dac channel counter |
- | |
441 | twi_state = 0; // reset twi_state |
305 | dac_channel = 0; // reset dac channel counter |
442 | } |
306 | } |
Line 443... | Line 307... | ||
443 | break; |
307 | break; |
444 | 308 | ||
445 | default: |
- | |
446 | I2C_Stop(); |
309 | default: |
447 | twi_state = 0; |
310 | I2C_Stop(TWI_STATE_MOTOR_TX); |
448 | I2CTimeout = 10; |
311 | I2CTimeout = 10; |
449 | motor_write = 0; |
312 | motor_write = 0; |
450 | motor_read = 0; |
313 | motor_read = 0; |
451 | } |
- | |
452 | } |
- |