Subversion Repositories FlightCtrl

Rev

Blame | Last modification | View Log | RSS feed

/*############################################################################
############################################################################*/


#include "main.h"

unsigned char twi_state = 0;
unsigned char motor = 0;
unsigned char motor_rx[8];

//############################################################################
//Initzialisieren der I2C (TWI) Schnittstelle
void i2c_init(void)
//############################################################################
{
        TWSR = 0;
        TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
}

//############################################################################
//Start I2C
void i2c_start(void)
//############################################################################
{
    TWCR = (1<<TWSTA) | (1<<TWEN) | (1<<TWINT) | (1<<TWIE);
}

//############################################################################
//Stop I2C
void i2c_stop(void)
//############################################################################
{
    TWCR = (1<<TWEN) | (1<<TWSTO) | (1<<TWINT);
}

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);
}

//############################################################################
//Write to I2C
void i2c_write_byte(char byte)
//############################################################################
{
    TWDR = byte;
    TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
}

//############################################################################
// I2C receive byte and send ACK
void i2c_receive_byte(void)
//############################################################################
{
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
}

//############################################################################
// I2C receive last byte and send NOT ACK
void i2c_receive_last_byte(void)
//############################################################################
{
        TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
}

//############################################################################
//Start I2C
SIGNAL (TWI_vect)
//############################################################################
{
    static unsigned char motorread = 0;

    switch (twi_state++) // First i2c_start from SendMotorData()
    {
        // Master Transmit
        case 0: // Address Slave SL+W
                i2c_write_byte(0x52+(motor*2));
                break;
        case 1: // Send Data
                switch(motor++)
                {
                    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 2: // Repeat case 0+1 for all Slaves
                if (motor < 4) twi_state = 0;
                i2c_start(); // Repeated start -> switch salve or switch Master Transmit -> Master Receive
                break;

        // Master Receive
        case 3: // Address Slave SL+R
                i2c_write_byte(0x53+(motorread*2));
                break;
        case 4: //1. Byte vom Motor übertragen
                i2c_receive_byte();
                break;
        case 5: // 1. Byte lesen und 2. Byte übertragen
                motor_rx[motorread] = TWDR;
                i2c_receive_last_byte();
                break;
        case 6: //2. Byte lesen
                motor_rx[motorread+4] = TWDR;
                motorread++;
                if (motorread > 3) motorread=0;
        default:
                i2c_stop();
                twi_state = 0;
                                I2CTimeout = 10;
                motor = 0;
    }
}