Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 693 → Rev 694

/branches/V0.68d Code Redesign killagreg/twimaster.c
8,79 → 8,134
#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
/**************************************************/
/* 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
/****************************************/
/* 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);
}
 
/****************************************/
/* 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_write_byte(0);
}
 
//############################################################################
//Start I2C
int8_t i2c_write_byte(int8_t byte)
//############################################################################
/****************************************/
/* Write to I2C */
/****************************************/
void i2c_write_byte(int8_t byte)
{
TWSR = 0x00;
// 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);
 
/****************************************/
/* Receive byte and send ACK */
/****************************************/
void i2c_receive_byte(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
}
 
//############################################################################
//Start I2C
SIGNAL (TWI_vect)
//############################################################################
/****************************************/
/* I2C receive last byte and send no ACK*/
/****************************************/
void i2c_receive_last_byte(void)
{
switch (twi_state++)
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:
// Master Transmit
case 0: // Send SLA-W
i2c_write_byte(0x52+(motor*2));
break;
case 1:
case 1: // Send Data to Salve
switch(motor++)
{
case 0:
97,61 → 152,33
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();
i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive
break;
 
//Liest Daten von Motor
case 3:
// Master Receive
case 3: // Send SLA-R
i2c_write_byte(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_receive_byte();
break;
case 5: //1 Byte vom Motor lesen
case 5: //Read 1st byte and transmit 2nd Byte
motor_rx[motorread] = TWDR;
i2c_receive_last_byte();
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;
default:
i2c_stop();
twi_state = 0;
I2CTimeout = 10;
twi_state = 0;
motor = 0;
}
TWCR |= 0x80;
}