Subversion Repositories MK3Mag

Compare Revisions

Regard whitespace Rev 21 → Rev 22

/branches/MK3Mag V0.14 Code Redesign Killagreg/led.c
57,7 → 57,7
#include "led.h"
 
 
void Led_Init(void)
void LED_Init(void)
{
// Set PD7 as output
DDRD |= (1<<DDD7);
/branches/MK3Mag V0.14 Code Redesign Killagreg/led.h
7,6 → 7,6
#define LED_GRN_OFF PORTD &= ~(1<<PORTD7)
#define LED_GRN_TOGGLE PORTD ^= (1<<PORTD7)
 
void Led_Init(void);
void LED_Init(void);
 
#endif //_LED_H_
/branches/MK3Mag V0.14 Code Redesign Killagreg/main.c
104,9 → 104,12
UncalMagnetY = (1 * UncalMagnetY + (RawMagnet3a - RawMagnet3b)) / 2;
UncalMagnetZ = (1 * UncalMagnetZ + (RawMagnet2a - RawMagnet2b)) / 2;
 
MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
if(Calibration.X.Range != 0) MagnetX = (1024L * (int32_t)(UncalMagnetX - Calibration.X.Offset)) / (Calibration.X.Range);
else MagnetX = 0;
if(Calibration.Y.Range != 0) MagnetY = (1024L * (int32_t)(UncalMagnetY - Calibration.Y.Offset)) / (Calibration.Y.Range);
else MagnetY = 0;
if(Calibration.Y.Range != 0) MagnetZ = (1024L * (int32_t)(UncalMagnetZ - Calibration.Z.Offset)) / (Calibration.Z.Range);
else MagnetZ = 0;
}
 
 
159,13 → 162,12
static uint8_t calold = 0;
static int16_t Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0;
static uint8_t blinkcount = 0;
/*
 
// check both sources of communication for calibration request
if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
else cal = ExternData.CalState;*/
else cal = ExternData.CalState;
 
cal = ExternData.CalState;
 
DebugOut.Analog[30] = cal;
// blink code for current calibration state
if(cal)
{
280,6 → 282,7
DebugOut.Analog[15] = Heading;
DebugOut.Analog[16] = ExternData.UserParam[0];
DebugOut.Analog[17] = ExternData.UserParam[1];
DebugOut.Analog[30] = I2C_WriteCal.CalByte;
DebugOut.Analog[31] = PC_Connected;
}
 
286,21 → 289,23
 
int main (void)
{
Led_Init();
LED_GRN_ON;
// reset input pullup
DDRC &=~((1<<DDC6));
PORTC |= (1<<PORTC6);
 
LED_Init();
TIMER0_Init();
USART0_Init();
ADC_Init();
I2C_Init();
 
sei(); // enable globale interrupts
 
sei(); //Globale Interrupts Einschalten
LED_GRN_ON;
 
Debug_Timer = SetDelay(200);
Led_Timer = SetDelay(200);
 
 
Debug_Timer = SetDelay(100); // Sendeintervall
Led_Timer = SetDelay(100);
 
// read calibration info from eeprom
eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
 
308,9 → 313,13
ExternData.CalState = 0;
I2C_WriteCal.CalByte = 0;
 
Heading = 101;
 
// main loop
while (1)
{
 
 
FLIP_LOW;
Delay_ms(2);
RawMagnet1a = ADC_GetValue(ADC0);
328,8 → 337,7
 
CalcFields();
 
//if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
if(ExternData.CalState) Calibrate();
if(ExternData.CalState || I2C_WriteCal.CalByte) Calibrate();
else CalcHeading();
 
// check data from USART
/branches/MK3Mag V0.14 Code Redesign Killagreg/timer0.c
68,11 → 68,6
/*****************************************************/
void TIMER0_Init(void)
{
uint8_t sreg = SREG;
 
// disable all interrupts before reconfiguration
cli();
 
// set PB2 as output for the PWM used to signal compass heading
DDRB |= (1<<DDB2);
PORTB &= ~(1<<PORTB2);
103,7 → 98,6
TIMSK0 &= ~((1<<OCIE0B)|(1<<OCIE0A));
TIMSK0 |= (1<<TOIE0);
 
SREG = sreg;
}
 
 
/branches/MK3Mag V0.14 Code Redesign Killagreg/twislave.c
61,36 → 61,15
#include "uart.h"
#include "main.h"
#include "timer0.h"
#include "led.h"
 
#define TWI_BUS_ERR_1 0x00
#define TWI_BUS_ERR_2 0xF8
 
// Status Slave RX Mode
#define SR_SLA_ACK 0x60
#define SR_LOST_ACK 0x68
#define SR_GEN_CALL_ACK 0x70
#define GEN_LOST_ACK 0x78
#define SR_PREV_ACK 0x80
#define SR_PREV_NACK 0x88
#define GEN_PREV_ACK 0x90
#define GEN_PREV_NACK 0x98
#define STOP_CONDITION 0xA0
#define REPEATED_START 0xA0
uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0;
uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0;
uint8_t Tx_Idx = 0, Rx_Idx = 0;
 
// Status Slave TX mode
#define SW_SLA_ACK 0xA8
#define SW_LOST_ACK 0xB0
#define SW_DATA_ACK 0xB8
#define SW_DATA_NACK 0xC0
#define SW_LAST_ACK 0xC8
 
 
uint8_t I2C_RxBufferSize, I2C_TxBufferSize;
uint8_t *I2C_TxBuffer, *I2C_RxBuffer;
uint8_t Tx_Idx = 0, Rx_Idx = 0, I2C_Direction;
uint8_t I2C_Command;
 
 
I2C_Heading_t I2C_Heading;
I2C_WriteAttitude_t I2C_WriteAttitude;
I2C_Mag_t I2C_Mag;
101,60 → 80,83
 
void I2C_Init(void)
{
uint8_t sreg = SREG;
 
uint8_t sreg;
 
// backup status register
sreg = SREG;
// disable global interrupts
cli();
 
//SPI SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic
// SCK/SCL and MISO/SDA are at put together on the same connector pin in the schematic
 
// set PB4 (SCK) and PB5 (MISO) as input pull up
// set PB4 (SCK) and PB5 (MISO) as input tristate
DDRB &= ~((1<<DDB4)|(1<<DDB5));
PORTB |= ((1<<PORTB4)|(1<<PORTB5));
PORTB &= ~((1<<PORTB4)|(1<<PORTB5));
 
// set PC4 (SDA) and PC5 (SCL) as input pull up
// set PC4 (SDA) and PC5 (SCL) as input tristate
DDRC &= ~((1<<DDC4)|(1<<DDC5));
PORTC |= ((1<<PORTC4)|(1<<PORTC5));
PORTC &= ~((1<<PORTC4)|(1<<PORTC5));
 
I2C_TxBuffer = 0;
Tx_Idx = 0;
I2C_TxBufferSize = 0;
I2C_RxBuffer = 0;
Rx_Idx = 0;
I2C_RxBufferSize = 0;
 
 
 
TWCR &= ~(1<<TWSTA)|(1<<TWSTO);
TWCR|= (1<<TWEA) | (1<<TWEN)|(1<<TWIE);
 
// set own address
TWAR = I2C_SLAVE_ADDRESS; // set own address
// set own address in the upper 7 bits
TWAR = I2C_SLAVE_ADDRESS; // set own address only the upper 7 bits are relevant
// TWI Control Register
// clear TWI interrupt flag (TWINT=1)
// enable TWI Acknowledge Bit (TWEA = 1)
// disable TWI START Condition Bit (TWSTA = 0), SLAVE
// disable TWI STOP Condition Bit (TWSTO = 0), SLAVE
// disable TWI Write Collision Flag (TWWC = 0)
// enable i2c (TWEN = 1)
// enable TWI (TWEN = 1)
// enable TWI Interrupt (TWIE = 1)
TWCR = (1<<TWINT) | (1<<TWEN) | (1<<TWIE) | (1<<TWEA);
TWCR &= ~((1<<TWSTA)|(1<<TWSTO));
TWCR |= (1<<TWEA)|(1<<TWEN)|(1<<TWIE);
 
// update version info
I2C_Version.Major = VERSION_MAJOR;
I2C_Version.Minor = VERSION_MINOR;
I2C_Version.Compatible = I2C_PROTOCOL_COMP;
 
// resore status register
SREG = sreg;
}
 
 
 
// send ACK after recieving a byte / ACK is expected after transmitting a byte
#define TWCR_ACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
// send no ACK after recieving a byte / No ACK is expected after transmitting a byte
#define TWCR_NACK TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(0<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
// switched to the non adressed slave mode
#define TWCR_RESET TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(0<<TWSTO)|(0<<TWWC)
// The bit pattern for TWCR_ACK and TWCR_RESET are equal. This is no errro but used for better understanding.
#define TWCR_CLEARBUS TWCR =(1<<TWEA) | (1<<TWSTO) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE)
 
ISR (TWI_vect)
{
uint8_t data;
// check event
switch (TWSR & 0xF8)
switch (TW_STATUS)
{
case SR_SLA_ACK:
Rx_Idx = 0;
TWCR_ACK;
case TW_SR_SLA_ACK: // slave addressed in receiver mode and ack has been returned
Rx_Idx = 0xFF; // reset rx buffer pointer
TWCR_ACK; // trigger receiving of first data byte and send ack afterwards
return;
 
case SR_PREV_ACK: // data byte received
 
if (Rx_Idx == 0)
case TW_SR_DATA_ACK: // data has been received and ack has been returned
data = TWDR;
if (Rx_Idx == 0xFF)
{ // if the fisrt byte after slave addressing was received
switch(data)
{
I2C_Command = TWDR;
 
switch(I2C_Command)
{
case I2C_CMD_VERSION:
I2C_TxBuffer = (uint8_t *)&I2C_Version;
I2C_TxBufferSize = sizeof(I2C_Version);
201,48 → 203,81
ExternData.Attitude[NICK] = I2C_WriteAttitude.Nick;
ExternData.Attitude[ROLL] = I2C_WriteAttitude.Roll;
break;
default:
I2C_RxBuffer = 0;
I2C_RxBufferSize = 0;
I2C_TxBuffer = 0;
I2C_TxBufferSize = 0;
break;
}
Rx_Idx = 0; // set rx buffer index to start of the buffer
if(I2C_RxBufferSize > 1) TWCR_ACK; // prepare receiving of next byte and send ACK afterwards
else TWCR_NACK; // prepare receiving of next byte and send NACK afterwards
}
else // Rx_Idx != 0
else // Rx_Idx != 0xFF
{
// fill receiver buffer
if ((Rx_Idx - 1) < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx - 1] = TWDR;
// fill receiver buffer with byte that has been received
// if buffer exist
if(I2C_RxBuffer != 0)
{ // and there is still some free space
if (Rx_Idx < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx++] = data;
// if there is space for more than one byte
if(Rx_Idx < (I2C_RxBufferSize - 1)) TWCR_ACK;
// with the next incomming byte the rx buffer is full
else TWCR_NACK;
}
// next byte
Rx_Idx++;
// rx buffer does not exist
else TWCR_NACK; // prepare receiving of next byte and send NACK afterwards
}
return;
 
I2C_Timeout = 500;
TWCR_ACK; // send acknowledge
case TW_SR_DATA_NACK: // data has been received and NACK has been returned
// read the last byte that is expected
data = TWDR;
if((I2C_RxBuffer != 0) && (Rx_Idx != 0xFF))
{ // and there is still some free space
if (Rx_Idx < I2C_RxBufferSize) I2C_RxBuffer[Rx_Idx++] = data;
}
TWCR_RESET; // switched to the non adressed slave mode
return;
 
case SW_SLA_ACK: // slave transmitter selected
case TW_ST_SLA_ACK: // slave transmitter selected
// reset position in tx buffer
Tx_Idx = 0;
// write first bte o tx buffer to the twi data register
if (I2C_TxBufferSize > 0) TWDR = I2C_TxBuffer[Tx_Idx++];
// send acknowledge
TWCR_ACK;
case TW_ST_DATA_ACK: // data byte has been transmitted ack has been received
// put next byte from tx buffer to twi data register
if(I2C_TxBuffer != 0)
{
if (Tx_Idx < I2C_TxBufferSize)
{
TWDR = I2C_TxBuffer[Tx_Idx++];
if(Tx_Idx + 1 < I2C_TxBufferSize) TWCR_ACK; // more than one byte to send
else TWCR_NACK; // last byte was send NACK should be received
}
else
{ //
TWDR = 0x00;
TWCR_NACK;// NACK should be received
}
}
else // buffer not existent
{
TWDR = 0x00;
TWCR_NACK;// NACK should be received
}
return;
 
case SW_DATA_ACK: // send data byte
// put next byte from tx buffer to twi data register
if (Tx_Idx < I2C_TxBufferSize) TWDR = I2C_TxBuffer[Tx_Idx++];
else TWDR = 0x00;
TWCR_ACK; // send acknowledge
case TW_BUS_ERROR: // Bus-Error
TWCR_CLEARBUS; // free bus reset to nonselected slave
return;
 
// clear Bus-Error
case TWI_BUS_ERR_2:
TWCR |=(1<<TWSTO) | (1<<TWINT);
// clear Bus-Error
case TWI_BUS_ERR_1:
TWCR |=(1<<TWSTO) | (1<<TWINT);
case TW_ST_DATA_NACK: // data transmitted, NACK received
case TW_ST_LAST_DATA: // last data byte transmitted, ACK received
case TW_SR_STOP: // stop or repeated start condition received while selected
default:
TWCR_RESET; // switch to the not addressed slave mode, own SLA will be recognized
return;
}
// clear interrupt flag (TWINT = 1)
// enable TWI Acknowledge Bit (TWEA = 1)
// enable TWI (TWEN = 1)
// enable TWI interrpt (TWIE = 1)
TWCR = (1<<TWEA) | (1<<TWINT) | (1<<TWEN) | (1<<TWIE); // TWI Reset
}
 
 
/branches/MK3Mag V0.14 Code Redesign Killagreg/twislave.h
57,8 → 57,6
} I2C_Heading_t;
 
 
extern uint8_t Tx_Idx, Rx_Idx, I2C_Direction;
 
extern I2C_Heading_t I2C_Heading;
extern I2C_WriteAttitude_t I2C_WriteNickRoll;
extern I2C_Mag_t I2C_Mag;
/branches/MK3Mag V0.14 Code Redesign Killagreg/uart.c
136,8 → 136,6
/****************************************************************/
void USART0_Init (void)
{
uint8_t sreg = SREG;
 
uint16_t ubrr = (uint16_t) ((uint32_t) SYSCLK/(8 * BAUD_RATE) - 1);
 
// disable all interrupts before configuration
150,13 → 148,14
 
// set direction of RXD0 and TXD0 pins
 
// set RXD0 (PD0) as an input pin
PORTD |= (1 << PORTD0);
// set RXD0 (PD0) as an input pin tristate
DDRD &= ~(1 << DDD0);
PORTD &= ~(1 << PORTD0);
// set TXD0 (PD1) as an output pin
PORTD |= (1 << PORTD1);
DDRD |= (1 << DDD1);
PORTD &= ~(1 << PORTD1);
 
 
// USART0 Baud Rate Register
// set clock divider
UBRR0H = (uint8_t)(ubrr >> 8);
166,8 → 165,7
 
// enable double speed operation
UCSR0A |= (1 << U2X0);
// enable receiver and transmitter
UCSR0B |= (1 << TXEN0) | (1 << RXEN0);
 
// set asynchronous mode
UCSR0C &= ~(1 << UMSEL01);
UCSR0C &= ~(1 << UMSEL00);
181,10 → 179,13
UCSR0C |= (1 << UCSZ01);
UCSR0C |= (1 << UCSZ00);
 
// enable receiver and transmitter
UCSR0B |= (1 << RXEN0);
UCSR0B |= (1 << TXEN0);
 
// flush receive buffer
while ( UCSR0A & (1<<RXC0) ) UDR0;
 
// enable interrupts at the end
// enable RX-Interrupt
UCSR0B |= (1 << RXCIE0);
// enable TX-Interrupt
193,38 → 194,42
rxd_buffer_locked = FALSE;
txd_complete = TRUE;
 
Debug_Timer = SetDelay(200);
 
 
// restore global interrupt flags
SREG = sreg;
 
VersionInfo.Major = VERSION_MAJOR;
VersionInfo.Minor = VERSION_MINOR;
VersionInfo.PCCompatible = VERSION_COMPATIBLE;
 
 
// Version beim Start ausgeben (nicht schön, aber geht... )
uart_putchar ('\n');uart_putchar ('C');uart_putchar ('P');uart_putchar (':');
uart_putchar ('V');uart_putchar (0x30 + VERSION_MAJOR);uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10); uart_putchar (0x30 + VERSION_MINOR%10);
uart_putchar ('\n');
uart_putchar ('C');
uart_putchar ('P');
uart_putchar (':');
uart_putchar ('V');
uart_putchar (0x30 + VERSION_MAJOR);
uart_putchar ('.');uart_putchar (0x30 + VERSION_MINOR/10);
uart_putchar (0x30 + VERSION_MINOR%10);
uart_putchar ('\n');
}
 
// ---------------------------------------------------------------------------------
void USART0_EnableTXD(void)
{
//if(!(UCSR0B & (1 << TXEN0))) return;
DDRD |= (1<<DDD1); // set TXD pin as output
PORTD &= ~(1 << PORTD1);
UCSR0B |= (1 << TXEN0); // enable TX in USART
UCSR0B |= (1 << TXCIE0); // disable TX-Interrupt
}
 
// ---------------------------------------------------------------------------------
void USART0_DisableTXD(void)
{
//if((UCSR0B & (1 << TXEN0))) return;
while(!txd_complete){ };
 
UCSR0B &= ~(1 << TXCIE0); // disable TX-Interrupt
UCSR0B &= ~(1 << TXEN0); // disable TXD in USART
DDRD &= ~(1<<DDD1); // set TXD pin as input
PORTD &= ~(1 << PORTD1);
}
 
/****************************************************************/