Rev 694 |
Blame |
Last modification |
View Log
| RSS feed
/*############################################################################
############################################################################*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include "main.h"
#include "twimaster.h"
#include "fc.h"
volatile uint8_t twi_state = 0;
volatile uint8_t motor = 0;
volatile uint8_t motor_rx[8];
/**************************************************/
/* Initialize I2C (TWI) */
/**************************************************/
void i2c_init(void)
{
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 */
/****************************************/
void i2c_start(void)
{
// 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);
}
/****************************************/
/* Stop I2C */
/****************************************/
void i2c_stop(void)
{
// 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)
{
// 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);
}
/****************************************/
/* Write to I2C */
/****************************************/
void i2c_write_byte(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);
}
/****************************************/
/* 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 no ACK*/
/****************************************/
void i2c_receive_last_byte(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()
{
// Master Transmit
case 0: // Send SLA-W
i2c_write_byte(0x52+(motor*2));
break;
case 1: // Send Data to Salve
switch(motor++)
{
case 0:
i2c_write_byte(Motor_Front);
break;
case 1:
i2c_write_byte(Motor_Rear);
break;
case 2:
i2c_write_byte(Motor_Right);
break;
case 3:
i2c_write_byte(Motor_Left);
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: // Send SLA-R
i2c_write_byte(0x53+(motorread*2));
break;
case 4:
//Transmit 1st byte
i2c_receive_byte();
break;
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;
default:
i2c_stop();
twi_state = 0;
I2CTimeout = 10;
motor = 0;
}
}