1,152 → 1,184 |
/*############################################################################ |
############################################################################*/ |
|
#include <avr/io.h> |
#include <avr/interrupt.h> |
|
#include "main.h" |
#include "twimaster.h" |
#include "fc.h" |
|
unsigned char twi_state = 0; |
unsigned char motor = 0; |
unsigned char motorread = 0; |
unsigned char motor_rx[8]; |
volatile uint8_t twi_state = 0; |
volatile uint8_t motor = 0; |
volatile uint8_t motor_rx[8]; |
|
//############################################################################ |
//Initzialisieren der I2C (TWI) Schnittstelle |
void i2c_init(void) |
//############################################################################ |
/**************************************************/ |
/* Initialize I2C (TWI) */ |
/**************************************************/ |
void I2C_Init(void) |
{ |
TWSR = 0; |
TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
uint8_t sreg = SREG; |
cli(); |
|
// SDA is INPUT |
DDRC &= ~(1<<DDC1); |
// SCL is output |
DDRC |= (1<<DDC0); |
// pull up SDA |
PORTC |= (1<<PORTC0)|(1<<PORTC1); |
|
// TWI Status Register |
// prescaler 1 (TWPS1 = 0, TWPS0 = 0) |
TWSR &= ~((1<<TWPS1)|(1<<TWPS0)); |
|
// set TWI Bit Rate Register |
TWBR = ((SYSCLK/SCL_CLOCK)-16)/2; |
|
SREG = sreg; |
} |
|
//############################################################################ |
//Start I2C |
char i2c_start(void) |
//############################################################################ |
/****************************************/ |
/* Start I2C */ |
/****************************************/ |
void I2C_Start(void) |
{ |
TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE); |
return(0); |
// TWI Control Register |
// clear TWI interrupt flag (TWINT=1) |
// disable TWI Acknowledge Bit (TWEA = 0) |
// enable TWI START Condition Bit (TWSTA = 1), MASTER |
// disable TWI STOP Condition Bit (TWSTO = 0) |
// disable TWI Write Collision Flag (TWWC = 0) |
// enable i2c (TWIE = 1) |
// enable TWI Interrupt (TWIE = 1) |
TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN) | (1<<TWIE); |
} |
|
//############################################################################ |
//Start I2C |
void i2c_stop(void) |
//############################################################################ |
/****************************************/ |
/* Stop I2C */ |
/****************************************/ |
void I2C_Stop(void) |
{ |
TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT); |
// TWI Control Register |
// clear TWI interrupt flag (TWINT=1) |
// disable TWI Acknowledge Bit (TWEA = 0) |
// diable TWI START Condition Bit (TWSTA = 1), no MASTER |
// enable TWI STOP Condition Bit (TWSTO = 1) |
// disable TWI Write Collision Flag (TWWC = 0) |
// enable i2c (TWIE = 1) |
// disable TWI Interrupt (TWIE = 0) |
TWCR = (1<<TWINT) | (1<<TWSTO) | (1<<TWEN); |
} |
|
void i2c_reset(void) |
//############################################################################ |
/****************************************/ |
/* Reset I2C */ |
/****************************************/ |
void I2C_Reset(void) |
{ |
i2c_stop(); |
twi_state = 0; |
motor = TWDR; |
motor = 0; |
TWCR = 0x80; |
TWAMR = 0; |
TWAR = 0; |
TWDR = 0; |
TWSR = 0; |
TWBR = 0; |
i2c_init(); |
i2c_start(); |
i2c_write_byte(0); |
// stop i2c bus |
I2C_Stop(); |
twi_state = 0; |
motor = TWDR; // ?? |
motor = 0; |
TWCR = (1<<TWINT); // reset to original state incl. interrupt flag reset |
TWAMR = 0; |
TWAR = 0; |
TWDR = 0; |
TWSR = 0; |
TWBR = 0; |
I2C_Init(); |
I2C_Start(); |
I2C_WriteByte(0); |
} |
|
//############################################################################ |
//Start I2C |
char i2c_write_byte(char byte) |
//############################################################################ |
{ |
TWSR = 0x00; |
/****************************************/ |
/* Write to I2C */ |
/****************************************/ |
void I2C_WriteByte(int8_t byte) |
{ |
// move byte to send into TWI Data Register |
TWDR = byte; |
// clear interrupt flag (TWINT = 1) |
// enable i2c bus (TWEN = 1) |
// enable intterupt (TWIW = 1) |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
|
return(0); |
|
} |
|
//############################################################################ |
//Start I2C |
SIGNAL (TWI_vect) |
//############################################################################ |
|
/****************************************/ |
/* Receive byte and send ACK */ |
/****************************************/ |
void I2C_ReceiveByte(void) |
{ |
switch (twi_state++) |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA); |
} |
|
/****************************************/ |
/* I2C receive last byte and send no ACK*/ |
/****************************************/ |
void I2C_ReceiveLastByte(void) |
{ |
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE); |
} |
|
|
/****************************************/ |
/* I2C ISR */ |
/****************************************/ |
ISR (TWI_vect) |
|
{ |
static uint8_t motorread = 0; |
|
switch (twi_state++) // First i2s_start from SendMotorData() |
{ |
case 0: |
i2c_write_byte(0x52+(motor*2)); |
// Master Transmit |
case 0: // Send SLA-W |
I2C_WriteByte(0x52+(motor*2)); |
break; |
case 1: |
case 1: // Send Data to Salve |
switch(motor++) |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
I2C_WriteByte(Motor_Front); |
break; |
case 1: |
i2c_write_byte(Motor_Hinten); |
case 1: |
I2C_WriteByte(Motor_Rear); |
break; |
case 2: |
i2c_write_byte(Motor_Rechts); |
I2C_WriteByte(Motor_Right); |
break; |
case 3: |
i2c_write_byte(Motor_Links); |
I2C_WriteByte(Motor_Left); |
break; |
} |
break; |
case 2: |
i2c_stop(); |
case 2: // repeat case 0+1 for all Slaves |
if (motor<4) twi_state = 0; |
else motor = 0; |
i2c_start(); |
break; |
|
//Liest Daten von Motor |
case 3: |
i2c_write_byte(0x53+(motorread*2)); |
I2C_Start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive |
break; |
|
// Master Receive |
case 3: // Send SLA-R |
I2C_WriteByte(0x53+(motorread*2)); |
break; |
case 4: |
switch(motorread) |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
break; |
case 1: |
i2c_write_byte(Motor_Hinten); |
break; |
case 2: |
i2c_write_byte(Motor_Rechts); |
break; |
case 3: |
i2c_write_byte(Motor_Links); |
break; |
} |
//Transmit 1st byte |
I2C_ReceiveByte(); |
break; |
case 5: //1 Byte vom Motor lesen |
case 5: //Read 1st byte and transmit 2nd Byte |
motor_rx[motorread] = TWDR; |
I2C_ReceiveLastByte(); |
break; |
case 6: |
//Read 2nd byte |
motor_rx[motorread+4] = TWDR; |
motorread++; |
if (motorread > 3) motorread=0; |
|
case 6: |
switch(motorread) |
{ |
case 0: |
i2c_write_byte(Motor_Vorne); |
break; |
case 1: |
i2c_write_byte(Motor_Hinten); |
break; |
case 2: |
i2c_write_byte(Motor_Rechts); |
break; |
case 3: |
i2c_write_byte(Motor_Links); |
break; |
} |
break; |
case 7: //2 Byte vom Motor lesen |
motor_rx[motorread+4] = TWDR; |
motorread++; |
if (motorread>3) motorread=0; |
i2c_stop(); |
default: |
I2C_Stop(); |
twi_state = 0; |
I2CTimeout = 10; |
twi_state = 0; |
motor = 0; |
} |
TWCR |= 0x80; |
} |