Rev 2443 | Rev 2468 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2443 | Rev 2466 | ||
---|---|---|---|
Line 60... | Line 60... | ||
60 | #include "analog.h" |
60 | #include "analog.h" |
61 | #include "uart.h" |
61 | #include "uart.h" |
62 | #include "timer0.h" |
62 | #include "timer0.h" |
63 | #include "main.h" |
63 | #include "main.h" |
Line 64... | Line 64... | ||
64 | 64 | ||
65 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX; |
65 | volatile uint8_t twi_state = TWI_STATE_MOTOR_TX, ReadBlSize = 9; |
66 | volatile uint8_t dac_channel = 0; |
66 | volatile uint8_t dac_channel = 0; |
67 | volatile uint8_t motor_write = 0; |
67 | volatile uint8_t motor_write = 0; |
68 | volatile uint8_t motor_read = 0; |
68 | volatile uint8_t motor_read = 0; |
69 | volatile uint8_t I2C_TransferActive = 0; |
69 | volatile uint8_t I2C_TransferActive = 0; |
Line 121... | Line 121... | ||
121 | motor_write = 0; |
121 | motor_write = 0; |
122 | motor_read = 0; |
122 | motor_read = 0; |
Line 123... | Line 123... | ||
123 | 123 | ||
124 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
124 | if(clear) for(i=0; i < MAX_MOTORS; i++) |
125 | { |
125 | { |
126 | Motor[i].Version = 0; |
126 | Motor[i].Version = 0; |
127 | Motor[i].SetPoint = 0; |
127 | Motor[i].SetPoint = 0; |
128 | Motor[i].SetPointLowerBits = 0; |
128 | Motor[i].SetPointLowerBits = 0; |
129 | Motor[i].State = 0; |
129 | Motor[i].State = 0; |
130 | Motor[i].ReadMode = BL_READMODE_STATUS; |
130 | Motor[i].ReadMode = BL_READMODE_STATUS; |
131 | Motor[i].Current = 0; |
131 | Motor[i].Current = 0; |
132 | Motor[i].MaxPWM = 0; |
132 | Motor[i].MaxPWM = 0; |
133 | Motor[i].Temperature = 0; |
133 | Motor[i].Temperature = 0; |
- | 134 | Motor[i].NotReadyCnt = 0; |
|
- | 135 | Motor[i].reserved1 = 0; |
|
- | 136 | Motor[i].reserved2 = 0; |
|
- | 137 | Motor[i].Voltage = 0; |
|
- | 138 | Motor[i].SlaveI2cError = 0; |
|
- | 139 | Motor[i].VersionMajor = 0; |
|
134 | Motor[i].NotReadyCnt = 0; |
140 | Motor[i].VersionMinor = 0; |
135 | } |
141 | } |
136 | sei(); |
142 | sei(); |
137 | SREG = sreg; |
143 | SREG = sreg; |
Line 263... | Line 269... | ||
263 | BuffLen = sizeof(BLConfig_t); |
269 | BuffLen = sizeof(BLConfig_t); |
264 | Motor[motor_read].ReadMode = BL_READMODE_STATUS; // only once |
270 | Motor[motor_read].ReadMode = BL_READMODE_STATUS; // only once |
265 | break; |
271 | break; |
266 | case BL_READMODE_STATUS: |
272 | case BL_READMODE_STATUS: |
267 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
273 | pBuff = (uint8_t*)&(Motor[motor_read].Current); |
268 | if(motor_read == motor_read_temperature) BuffLen = 3; // read Current, MaxPwm & Temp |
274 | if(motor_read == motor_read_temperature) BuffLen = ReadBlSize; // read Current, MaxPwm & Temp (is 3 or 9) |
269 | else BuffLen = 1;// read Current only |
275 | else BuffLen = 1;// read Current only |
270 | break; |
276 | break; |
271 | } |
277 | } |
272 | } |
278 | } |
273 | else // old BL version |
279 | else // old BL version |
Line 404... | Line 410... | ||
404 | } |
410 | } |
Line 405... | Line 411... | ||
405 | 411 | ||
406 | 412 | ||
407 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
413 | uint8_t I2C_WriteBLConfig(uint8_t motor) |
408 | { |
414 | { |
409 | uint8_t i; |
- | |
410 | uint16_t timer; |
415 | uint8_t i, packets; |
411 | 416 | uint16_t timer; |
|
412 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
417 | if(MotorenEin || PC_MotortestActive) return(BLCONFIG_ERR_MOTOR_RUNNING); // not when motors are running! |
413 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
418 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
414 | if(motor) |
419 | if(motor) |
Line 419... | Line 424... | ||
419 | // check BL configuration to send |
424 | // check BL configuration to send |
420 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
425 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
421 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
426 | i = RAM_Checksum((uint8_t*)&BLConfig, sizeof(BLConfig_t) - 1); |
422 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
427 | if(i != BLConfig.crc) return(BLCONFIG_ERR_CHECKSUM); // bad checksum |
Line -... | Line 428... | ||
- | 428 | ||
- | 429 | packets = Max_I2C_Packets; |
|
- | 430 | Max_I2C_Packets = 12; |
|
- | 431 | I2CTimeout = 100; |
|
423 | 432 | ||
424 | timer = SetDelay(2000); |
433 | timer = SetDelay(100); |
Line 425... | Line 434... | ||
425 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
434 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
426 | - | ||
427 | // prepare the bitmask |
- | |
428 | if(!motor) // 0 means all |
435 | |
429 | { |
- | |
430 | BLConfig_WriteMask = 0xFF; // all motors at once with the same configuration |
- | |
431 | } |
- | |
432 | else //only one specific motor |
436 | // prepare the bitmask |
433 | { |
- | |
434 | BLConfig_WriteMask = 0x0001<<(motor-1); |
- | |
435 | } |
- | |
436 | for(i = 0; i < MAX_MOTORS; i++) |
- | |
437 | { |
- | |
438 | if((0x0001<<i) & BLConfig_WriteMask) |
- | |
439 | { |
- | |
440 | Motor[i].SetPoint = 0; |
- | |
441 | Motor[i].SetPointLowerBits = 0; |
- | |
Line 442... | Line 437... | ||
442 | } |
437 | if(!motor) BLConfig_WriteMask = 0x0FFF; // 0 means all -> all motors at once with the same configuration |
- | 438 | else BLConfig_WriteMask = 0x0001<<(motor-1); //only one specific motor |
|
443 | } |
439 | |
- | 440 | motor_write = 0; |
|
444 | 441 | motor_read = 0; |
|
445 | motor_write = 0; |
442 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
446 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
443 | timer = SetDelay(1000); |
447 | do |
444 | do |
448 | { |
445 | { |
- | 446 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
|
449 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
447 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
450 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
448 | } while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
451 | }while(BLConfig_WriteMask && !CheckDelay(timer)); // repeat until the BL config has been sent |
449 | Max_I2C_Packets = packets; |
Line 452... | Line 450... | ||
452 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
450 | if(BLConfig_WriteMask) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); |
Line 462... | Line 460... | ||
462 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
460 | if(motor > MAX_MOTORS) return (BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
463 | if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE); |
461 | if(motor == 0) return (BLCONFIG_ERR_READ_NOT_POSSIBLE); |
464 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
462 | if(!(Motor[motor-1].State & MOTOR_STATE_PRESENT_MASK)) return(BLCONFIG_ERR_MOTOR_NOT_EXIST); // motor does not exist! |
465 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
463 | if(!(Motor[motor-1].Version & MOTOR_STATE_NEW_PROTOCOL_MASK)) return(BLCONFIG_ERR_HW_NOT_COMPATIBLE); // not a new BL! |
Line 466... | Line 464... | ||
466 | 464 | ||
467 | timer = SetDelay(2000); |
465 | timer = SetDelay(1000); |
Line 468... | Line 466... | ||
468 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
466 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
469 | 467 | ||
Line 470... | Line -... | ||
470 | // prepare the bitmask |
- | |
471 | BLConfig_ReadMask = 0x0001<<(motor-1); |
- | |
472 | - | ||
473 | for(i = 0; i < MAX_MOTORS; i++) |
- | |
474 | { |
468 | // prepare the bitmask |
475 | if((0x0001<<i) & BLConfig_ReadMask) |
- | |
476 | { |
- | |
477 | Motor[i].SetPoint = 0; |
- | |
478 | Motor[i].SetPointLowerBits = 0; |
- | |
479 | } |
469 | BLConfig_ReadMask = 0x0001<<(motor-1); |
480 | } |
470 | |
481 | 471 | motor_write = 0; |
|
482 | motor_read = 0; |
472 | motor_read = 0; |
483 | BLConfig.Revision = 0; // bad revision |
473 | BLConfig.Revision = 0; // bad revision |
484 | BLConfig.crc = 0; // bad checksum |
474 | BLConfig.crc = 0; // bad checksum |
485 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
475 | // needs at least MAX_MOTORS loops of 2 ms (12*2ms = 24ms) |
486 | do |
476 | do |
487 | { |
477 | { |
488 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
478 | I2C_Start(TWI_STATE_MOTOR_TX); // start an i2c transmission |
489 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
479 | while(!(BLFlags & BLFLAG_TX_COMPLETE) && !CheckDelay(timer)); //wait for complete transfer |
490 | }while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
480 | } while(BLConfig_ReadMask && !CheckDelay(timer)); // repeat until the BL config has been received from all motors |
491 | // validate result |
481 | // validate result |
492 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |
482 | if((BLConfig.Revision & 0x0B) != BLCONFIG_REVISION) return (BLCONFIG_ERR_SW_NOT_COMPATIBLE); // bad revison |