Rev 1 |
Rev 4 |
Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
#include <avr/io.h>
#include <util/twi.h>
#include "main.h"
unsigned char I2C_RxBufferSize, I2C_TxBufferSize;
unsigned char *I2C_TxBuffer, *I2C_RxBuffer;
unsigned char Tx_Idx=0, Rx_Idx=0, I2C_Direction;
unsigned char I2C_Command;
unsigned char Tx_Idx, Rx_Idx;
struct str_I2C_Heading I2C_Heading;
struct str_I2C_WriteNickRoll I2C_WriteNickRoll;
struct str_I2C_Mag I2C_Mag;
struct str_I2C_EEPROM I2C_ReadEEPROM, I2C_WriteEEPROM;
struct str_I2C_Version I2C_Version;
//############################################################################
//I2C (TWI) Interface Init
void InitIC2_Slave(void)
//############################################################################
{
TWAR = I2C_SLAVE_ADDRESS; // Eigene Adresse setzen
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
I2C_Version.Hauptversion = VersionInfo.Hauptversion;
I2C_Version.Nebenversion = VersionInfo.Nebenversion;
I2C_Version.Comp = 1;
}
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
//############################################################################
//ISR, die bei einem Ereignis auf dem Bus ausgelöst wird. Im Register TWSR befindet
//sich dann ein Statuscode, anhand dessen die Situation festgestellt werden kann.
ISR (TWI_vect)
//############################################################################
{
switch (TWSR & 0xF8)
{
case SR_SLA_ACK:
//TWCR |= (1<<TWINT);
Rx_Idx = 0;
TWCR_ACK;
return;
// Daten Empfangen
case SR_PREV_ACK:
if (Rx_Idx == 0)
{ I2C_Command = TWDR;
switch(I2C_Command)
{
case I2C_CMD_VERSION:
I2C_TxBuffer = (unsigned char *)&I2C_Version;
I2C_TxBufferSize = sizeof(I2C_Version);
I2C_RxBufferSize = 0;
break;
case I2C_CMD_WRITE_EEPROM:
I2C_TxBufferSize = 0;
I2C_RxBuffer = (unsigned char *)&I2C_WriteEEPROM;
I2C_RxBufferSize = sizeof(I2C_WriteEEPROM);
break;
case I2C_CMD_READ_EEPROM:
I2C_TxBuffer = (unsigned char *)&I2C_ReadEEPROM.Inhalt;
I2C_TxBufferSize = 2;
I2C_RxBuffer = (unsigned char *)&I2C_ReadEEPROM;
I2C_RxBufferSize = 1;
break;
case I2C_CMD_READ_MAG:
I2C_TxBuffer = (unsigned char *)&I2C_Mag;
I2C_TxBufferSize = sizeof(I2C_Mag);
I2C_RxBufferSize = 0;
I2C_Mag.MagX = MagnetN;
I2C_Mag.MagY = MagnetR;
I2C_Mag.MagZ = MagnetZ;
break;
case I2C_CMD_READ_HEADING:
I2C_TxBuffer = (unsigned char *)&I2C_Heading;
I2C_TxBufferSize = sizeof(I2C_Heading);
I2C_RxBuffer = (unsigned char *)&I2C_WriteNickRoll;
I2C_RxBufferSize = sizeof(I2C_WriteNickRoll);
I2C_Heading.Heading = Heading;
WinkelOut.Winkel[NICK] = I2C_WriteNickRoll.Nick;
WinkelOut.Winkel[ROLL] = I2C_WriteNickRoll.Roll;
break;
}
}
else
{
if (Rx_Idx < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx] = TWDR;
}
Rx_Idx++;
I2C_Timeout = 500;
//TWCR |= (1<<TWINT);
TWCR_ACK;
return;
// Daten Senden
case SW_SLA_ACK:
Tx_Idx = 0;
if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
// TWCR |= (1<<TWINT) | (1<< TWEA);
TWCR_ACK;
return;
// Daten Senden
case SW_DATA_ACK:
if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
else TWDR = 0x00;
//TWCR |= (1<<TWINT) | (1<< TWEA);
TWCR_ACK;
return;
// Bus-Fehler zurücksetzen
case TWI_BUS_ERR_2:
TWCR |=(1<<TWSTO) | (1<<TWINT);
// Bus-Fehler zurücksetzen
case TWI_BUS_ERR_1:
TWCR |=(1<<TWSTO) | (1<<TWINT);
}
TWCR =(1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); // TWI Reset
}