Subversion Repositories FlightCtrl

Compare Revisions

Ignore whitespace Rev 1221 → Rev 1222

/branches/V0.73d Code Redesign killagreg/twimaster.c
51,46 → 51,34
 
#include <avr/io.h>
#include <avr/interrupt.h>
 
#include <util/twi.h>
#include "main.h"
#include "eeprom.h"
#include "twimaster.h"
#include "fc.h"
#include "analog.h"
 
volatile uint8_t twi_state = 0;
uint8_t motor_write = 0;
uint8_t motor_read = 0;
volatile uint8_t twi_state = TWI_STATE_MOTOR_TX;
volatile uint8_t dac_channel = 0;
volatile uint8_t motor_write = 0;
volatile uint8_t motor_read = 0;
 
#ifdef USE_QUADRO
uint8_t motor_rx[8];
#else
uint8_t motor_rx[16];
#endif
 
volatile uint16_t I2CTimeout = 100;
 
uint8_t MissingMotor = 0;
 
 
MotorData_t Motor[MAX_MOTORS];
 
#define SCL_CLOCK 200000L
#define I2C_TIMEOUT 30000
 
#define TWSR_STATUS_MASK 0xF8
// for Master Transmitter Mode
 
#define I2C_STATUS_START 0x08
#define I2C_STATUS_REPEATSTART 0x10
#define I2C_STATUS_TX_SLA_ACK 0x18
#define I2C_STATUS_SLAW_NOACK 0x20
#define I2C_STATUS_TX_DATA_ACK 0x28
#define I2C_STATUS_TX_DATA_NOTACK 0x30
#define I2C_STATUS_RX_DATA_ACK 0x50
#define I2C_STATUS_RX_DATA_NOTACK 0x58
 
/**************************************************/
/* Initialize I2C (TWI) */
/**************************************************/
void I2C_Init(void)
{
uint8_t i;
uint8_t sreg = SREG;
cli();
 
108,10 → 96,18
// set TWI Bit Rate Register
TWBR = ((SYSCLK/SCL_CLOCK)-16)/2;
 
twi_state = 0;
twi_state = TWI_STATE_MOTOR_TX;
motor_write = 0;
motor_read = 0;
 
for(i=0; i < MAX_MOTORS; i++)
{
Motor[i].SetPoint = 0;
Motor[i].Present = 0;
Motor[i].Error = 0;
Motor[i].MaxPWM = 0;
}
 
SREG = sreg;
}
 
118,8 → 114,9
/****************************************/
/* Start I2C */
/****************************************/
void I2C_Start(void)
void I2C_Start(uint8_t start_state)
{
twi_state = start_state;
// TWI Control Register
// clear TWI interrupt flag (TWINT=1)
// disable TWI Acknowledge Bit (TWEA = 0)
134,8 → 131,9
/****************************************/
/* Stop I2C */
/****************************************/
void I2C_Stop(void)
void I2C_Stop(uint8_t start_state)
{
twi_state = start_state;
// TWI Control Register
// clear TWI interrupt flag (TWINT=1)
// disable TWI Acknowledge Bit (TWEA = 0)
153,12 → 151,12
/****************************************/
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 interrupt (TWIE = 1)
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
// move byte to send into TWI Data Register
TWDR = byte;
// clear interrupt flag (TWINT = 1)
// enable i2c bus (TWEN = 1)
// enable interrupt (TWIE = 1)
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
}
 
 
167,7 → 165,7
/****************************************/
void I2C_ReceiveByte(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
}
 
/****************************************/
175,7 → 173,7
/****************************************/
void I2C_ReceiveLastByte(void)
{
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE);
}
 
 
185,7 → 183,7
void I2C_Reset(void)
{
// stop i2c bus
I2C_Stop();
I2C_Stop(TWI_STATE_MOTOR_TX);
twi_state = 0;
motor_write = TWDR;
motor_write = 0;
197,8 → 195,7
TWSR = 0;
TWBR = 0;
I2C_Init();
I2C_Start();
I2C_WriteByte(0);
I2C_Start(TWI_STATE_MOTOR_TX);
}
 
 
205,199 → 202,68
/****************************************/
/* I2C ISR */
/****************************************/
 
#ifdef USE_QUADRO
ISR (TWI_vect)
{
static uint8_t missing_motor = 0;
 
switch (twi_state++) // First i2c_start from SendMotorData()
{
// Master Transmit
case 0: // Send SLA-W
I2C_WriteByte(0x52 + (motor_write * 2) );
case 0: // TWI_STATE_MOTOR_TX
// skip motor if not used in mixer
while((Mixer.Motor[motor_write][MIX_GAS] <= 0) && (motor_write < MAX_MOTORS)) motor_write++;
if(motor_write >= MAX_MOTORS) // writing finished, read now
{
motor_write = 0;
twi_state = TWI_STATE_MOTOR_RX;
I2C_WriteByte(0x53 + (motor_read * 2) ); // select slave adress in rx mode
}
else I2C_WriteByte(0x52 + (motor_write * 2) ); // select slave adress in tx mode
break;
case 1: // Send Data to Slave
switch(motor_write)
{
case 0:
I2C_WriteByte(Motor1);
break;
case 1:
I2C_WriteByte(Motor2);
break;
case 2:
I2C_WriteByte(Motor3);
break;
case 3:
I2C_WriteByte(Motor4);
break;
}
I2C_WriteByte(Motor[motor_write].SetPoint); // transmit rotation rate setpoint
break;
case 2: // repeat case 0+1 for all motors
I2C_Stop();
if (motor_write < 3)
{
motor_write++; // jump to next motor
twi_state = 0; // and repeat from state 0
}
else
{ // data to last motor send
motor_write = 0; // reset motor write counter
}
I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
break;
 
// Master Receive
case 3: // Send SLA-R
I2C_WriteByte(0x53 + (motor_read * 2) );
break;
case 4:
//Transmit 1st byte
I2C_ReceiveByte();
break;
case 5: //Read 1st byte and transmit 2nd Byte
motor_rx[motor_read] = TWDR;
I2C_ReceiveLastByte();
break;
case 6:
//Read 2nd byte
motor_rx[motor_read + 4] = TWDR;
motor_read++;
if (motor_read > 3) motor_read = 0;
I2C_Stop();
twi_state = 0;
I2CTimeout = 10;
break;
 
// Gyro-Offsets
case 7:
I2C_WriteByte(0x98); // Address the DAC
break;
 
case 8:
I2C_WriteByte(0x10 + (dac_channel * 2)); // Select DAC Channel (0x10 = A, 0x12 = B, 0x14 = C)
break;
 
case 9:
switch(dac_channel)
if(TWSR == TW_MT_DATA_NACK) // Data transmitted, NACK received
{
case 0:
I2C_WriteByte(DacOffsetGyroNick); // 1st byte for Channel A
break;
case 1:
I2C_WriteByte(DacOffsetGyroRoll); // 1st byte for Channel B
break;
case 2:
I2C_WriteByte(DacOffsetGyroYaw ); // 1st byte for Channel C
break;
if(!missing_motor) missing_motor = motor_write + 1;
if(++Motor[motor_write].Error == 0) Motor[motor_write].Error = 255; // increment error counter and handle overflow
}
break;
 
case 10:
I2C_WriteByte(0x80); // 2nd byte for all channels is 0x80
break;
 
case 11:
I2C_Stop();
I2CTimeout = 10;
// repeat case 7...10 until all DAC Channels are updated
if(dac_channel < 2)
{
dac_channel ++; // jump to next channel
twi_state = 7; // and repeat from state 7
I2C_Start(); // start transmission for next channel
}
else
{ // data to last motor send
dac_channel = 0; // reset dac channel counter
twi_state = 0; // reset twi_state
}
I2C_Stop(TWI_STATE_MOTOR_TX);
I2CTimeout = 10;
motor_write++; // next motor
I2C_Start(TWI_STATE_MOTOR_TX); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
break;
 
default:
I2C_Stop();
twi_state = 0;
I2CTimeout = 10;
motor_write = 0;
motor_read = 0;
}
}
#else // USE_OCTO, USE_OCTO2, USE_OCTO3
ISR (TWI_vect)
{
 
switch (twi_state++) // First i2c_start from SendMotorData()
{
// Master Transmit
case 0: // Send SLA-W
I2C_WriteByte(0x52 + (motor_write * 2) );
break;
case 1: // Send Data to Slave
switch(motor_write)
{
case 0:
I2C_WriteByte(Motor1);
break;
case 1:
I2C_WriteByte(Motor2);
break;
case 2:
I2C_WriteByte(Motor3);
break;
case 3:
I2C_WriteByte(Motor4);
break;
case 5:
I2C_WriteByte(Motor5);
break;
case 6:
I2C_WriteByte(Motor6);
break;
case 7:
I2C_WriteByte(Motor7);
break;
case 8:
I2C_WriteByte(Motor8);
break;
}
break;
case 2: // repeat case 0+1 for all motors
I2C_Stop();
if (motor_write < 7)
{
motor_write++; // jump to next motor
twi_state = 0; // and repeat from state 0
// Master Receive Data
case 3:
if(TWSR != TW_MR_SLA_ACK) // SLA+R transmitted, if not ACK received
{ // no response from the addressed slave received
Motor[motor_read].Present = 0;
motor_read++; // next motor
if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one
I2C_Stop(TWI_STATE_MOTOR_TX);
}
else
{ // data to last motor send
motor_write = 0; // reset motor write counter
{
Motor[motor_read].Present = ('1' - '-') + motor_read;
I2C_ReceiveByte(); //Transmit 1st byte
}
I2C_Start(); // Repeated start -> switch slave or switch Master Transmit -> Master Receive
MissingMotor = missing_motor;
missing_motor = 0;
break;
 
// Master Receive
case 3: // Send SLA-R
I2C_WriteByte(0x53 + (motor_read * 2) );
break;
case 4:
//Transmit 1st byte
I2C_ReceiveByte();
break;
case 5: //Read 1st byte and transmit 2nd Byte
motor_rx[motor_read] = TWDR;
I2C_ReceiveLastByte();
case 4: //Read 1st byte and transmit 2nd Byte
Motor[motor_read].Current = TWDR;
I2C_ReceiveLastByte(); // nack
break;
case 6:
case 5:
//Read 2nd byte
motor_rx[motor_read + 8] = TWDR;
motor_read++;
if (motor_read > 7) motor_read = 0;
I2C_Stop();
twi_state = 0;
I2CTimeout = 10;
Motor[motor_read].MaxPWM = TWDR;;
motor_read++; // next motor
if(motor_read >= MAX_MOTORS) motor_read = 0; // restart reading of first motor if we have reached the last one
I2C_Stop(TWI_STATE_MOTOR_TX);
break;
 
// Gyro-Offsets
// writing Gyro-Offsets
case 7:
I2C_WriteByte(0x98); // Address the DAC
break;
426,29 → 292,24
break;
 
case 11:
I2C_Stop();
I2C_Stop(TWI_STATE_MOTOR_TX);
I2CTimeout = 10;
// repeat case 7...10 until all DAC Channels are updated
if(dac_channel < 2)
{
dac_channel ++; // jump to next channel
twi_state = 7; // and repeat from state 7
I2C_Start(); // start transmission for next channel
I2C_Start(TWI_STATE_GYRO_OFFSET_TX); // start transmission for next channel
}
else
{ // data to last motor send
dac_channel = 0; // reset dac channel counter
twi_state = 0; // reset twi_state
}
break;
 
default:
I2C_Stop();
twi_state = 0;
I2C_Stop(TWI_STATE_MOTOR_TX);
I2CTimeout = 10;
motor_write = 0;
motor_read = 0;
}
}
#endif // USE_OCTO, USE_OCTO2