Subversion Repositories MK3Mag

Compare Revisions

Ignore whitespace Rev 22 → Rev 23

/branches/MK3Mag V0.14 Code Redesign Killagreg/main.c
73,21 → 73,21
 
uint16_t Led_Timer = 0;
 
typedef struct
struct Scaling_t
{
int16_t Range;
int16_t Offset;
} Scaling_t;
} ;
 
typedef struct
struct Calibration_t
{
Scaling_t X;
Scaling_t Y;
Scaling_t Z;
} Calibration_t;
struct Scaling_t X;
struct Scaling_t Y;
struct Scaling_t Z;
} ;
 
Calibration_t eeCalibration EEMEM; // calibration data in EEProm
Calibration_t Calibration; // calibration data in RAM
struct Calibration_t eeCalibration EEMEM; // calibration data in EEProm
struct Calibration_t Calibration; // calibration data in RAM
 
 
int16_t UncalMagnetX, UncalMagnetY, UncalMagnetZ; // sensor signal difference without Scaling
125,7 → 125,6
Led_Timer = SetDelay(500);
}
 
 
Cx = MagnetX;
Cy = MagnetY;
Cz = MagnetZ;
167,7 → 166,8
if(I2C_WriteCal.CalByte) cal = I2C_WriteCal.CalByte;
else cal = ExternData.CalState;
 
DebugOut.Analog[30] = cal;
 
if(cal > 5) cal = 0;
// blink code for current calibration state
if(cal)
{
194,7 → 194,6
LED_GRN_OFF;
}
 
 
// calibration state machine
switch(cal)
{
244,7 → 243,7
{
// indicate write process by setting the led
LED_GRN_ON;
eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
eeprom_write_block(&Calibration, &eeCalibration, sizeof(Calibration));
Delay_ms(2000);
// reset led state
LED_GRN_OFF;
282,8 → 281,6
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;
}
 
 
307,19 → 304,16
Led_Timer = SetDelay(200);
 
// read calibration info from eeprom
eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration_t));
eeprom_read_block(&Calibration, &eeCalibration, sizeof(Calibration));
 
ExternData.Orientation = 0;
ExternData.CalState = 0;
I2C_WriteCal.CalByte = 0;
 
Heading = 101;
 
// main loop
while (1)
{
 
 
FLIP_LOW;
Delay_ms(2);
RawMagnet1a = ADC_GetValue(ADC0);
342,6 → 336,7
 
// check data from USART
USART0_ProcessRxData();
USART0_TransmitTxData();
 
if(PC_Connected)
{
/branches/MK3Mag V0.14 Code Redesign Killagreg/twislave.c
64,18 → 64,17
#include "led.h"
 
 
uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0;
uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0;
uint8_t Tx_Idx = 0, Rx_Idx = 0;
volatile uint8_t I2C_RxBufferSize = 0, I2C_TxBufferSize = 0;
volatile uint8_t *I2C_TxBuffer = 0, *I2C_RxBuffer = 0;
volatile uint8_t Tx_Idx = 0, Rx_Idx = 0;
 
 
 
I2C_Heading_t I2C_Heading;
I2C_WriteAttitude_t I2C_WriteAttitude;
I2C_Mag_t I2C_Mag;
I2C_EEPROM_t I2C_ReadEEPROM, I2C_WriteEEPROM;
I2C_Version_t I2C_Version;
I2C_WriteCal_t I2C_WriteCal;
struct I2C_Heading_t I2C_Heading;
struct I2C_WriteAttitude_t I2C_WriteAttitude;
struct I2C_Mag_t I2C_Mag;
struct I2C_Version_t I2C_Version;
struct I2C_WriteCal_t I2C_WriteCal;
 
 
void I2C_Init(void)
90,9 → 89,9
 
// 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 tristate
// set PB4 (SCK) and PB5 (MISO) as input pull up
DDRB &= ~((1<<DDB4)|(1<<DDB5));
PORTB &= ~((1<<PORTB4)|(1<<PORTB5));
PORTB |= ((1<<PORTB4)|(1<<PORTB5));
 
// set PC4 (SDA) and PC5 (SCL) as input tristate
DDRC &= ~((1<<DDC4)|(1<<DDC5));
119,9 → 118,7
// disable TWI STOP Condition Bit (TWSTO = 0), SLAVE
// enable TWI (TWEN = 1)
// enable TWI Interrupt (TWIE = 1)
TWCR &= ~((1<<TWSTA)|(1<<TWSTO));
TWCR |= (1<<TWEA)|(1<<TWEN)|(1<<TWIE);
 
TWCR |= (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC);
// update version info
I2C_Version.Major = VERSION_MAJOR;
I2C_Version.Minor = VERSION_MINOR;
132,17 → 129,18
}
 
// 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)
#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)
#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)
#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)
#define TWCR_CLEARBUS TWCR = (1<<TWEN)|(1<<TWIE)|(1<<TWINT)|(1<<TWEA)|(0<<TWSTA)|(1<<TWSTO)|(0<<TWWC)
 
ISR (TWI_vect)
{
uint8_t data;
 
// check event
switch (TW_STATUS)
{
149,6 → 147,7
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
LED_GRN_TOGGLE;
return;
 
case TW_SR_DATA_ACK: // data has been received and ack has been returned
160,31 → 159,21
case I2C_CMD_VERSION:
I2C_TxBuffer = (uint8_t *)&I2C_Version;
I2C_TxBufferSize = sizeof(I2C_Version);
I2C_RxBuffer = 0;
I2C_RxBufferSize = 0;
break;
 
case I2C_CMD_WRITE_EEPROM:
I2C_TxBufferSize = 0;
I2C_RxBuffer = (uint8_t *)&I2C_WriteEEPROM;
I2C_RxBufferSize = sizeof(I2C_WriteEEPROM);
break;
 
case I2C_CMD_WRITE_CAL:
I2C_TxBuffer = 0;
I2C_TxBufferSize = 0;
I2C_RxBuffer = (uint8_t *)&I2C_WriteCal;
I2C_RxBufferSize = sizeof(I2C_WriteCal);
break;
 
case I2C_CMD_READ_EEPROM:
I2C_TxBuffer = (uint8_t *)&I2C_ReadEEPROM.Content;
I2C_TxBufferSize = 2;
I2C_RxBuffer = (uint8_t *)&I2C_ReadEEPROM;
I2C_RxBufferSize = 1;
break;
 
case I2C_CMD_READ_MAG:
I2C_TxBuffer = (uint8_t *)&I2C_Mag;
I2C_TxBufferSize = sizeof(I2C_Mag);
I2C_RxBuffer = 0;
I2C_RxBufferSize = 0;
 
I2C_Mag.MagX = MagnetX;
203,7 → 192,7
ExternData.Attitude[NICK] = I2C_WriteAttitude.Nick;
ExternData.Attitude[ROLL] = I2C_WriteAttitude.Roll;
break;
default:
default: // unknown command id
I2C_RxBuffer = 0;
I2C_RxBufferSize = 0;
I2C_TxBuffer = 0;
211,64 → 200,50
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 != 0xFF
{
// 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;
// fill receiver buffer with the byte that has been received
// if buffer exist and there is still some free space
if((I2C_RxBuffer != 0) && (Rx_Idx < I2C_RxBufferSize))
{
I2C_RxBuffer[Rx_Idx++] = data;
}
// rx buffer does not exist
else TWCR_NACK; // prepare receiving of next byte and send NACK afterwards
// else ignore the data
}
TWCR_ACK;
return;
 
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;
case TW_ST_SLA_ACK: // slave transmitter selected
// reset index to start of tx buffer
Tx_Idx = 0;
// if tx bufer exist and there is at least ine byte to transfer
if((I2C_TxBuffer != 0) && (I2C_TxBufferSize > 1))
{
TWDR = I2C_TxBuffer[Tx_Idx++];
}
TWCR_RESET; // switched to the non adressed slave mode
return;
else
{ // send 0x00 if no tx buffer exist or all bytes of the tx buffer have been transmitted
TWDR = 0x00;
}
TWCR_ACK;
return;
 
case TW_ST_SLA_ACK: // slave transmitter selected
// reset position in tx buffer
Tx_Idx = 0;
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)
// put next byte from tx buffer to the data register
if((I2C_TxBuffer != 0) && (Tx_Idx < I2C_TxBufferSize))
{
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
}
TWDR = I2C_TxBuffer[Tx_Idx++];
}
else // buffer not existent
{
else
{ // send dummy byte instead
TWDR = 0x00;
TWCR_NACK;// NACK should be received
}
TWCR_ACK;
return;
 
case TW_BUS_ERROR: // Bus-Error
TWCR_CLEARBUS; // free bus reset to nonselected slave
TWCR_CLEARBUS; // free bus, reset to nonselected slave
return;
 
case TW_ST_DATA_NACK: // data transmitted, NACK received
/branches/MK3Mag V0.14 Code Redesign Killagreg/twislave.h
14,54 → 14,46
#define I2C_CMD_READ_EEPROM 0x0B
 
 
typedef struct
struct I2C_Version_t
{
uint8_t Major;
uint8_t Minor;
uint8_t Compatible;
} I2C_Version_t;
} ;
 
 
typedef struct
struct I2C_Mag_t
{
uint8_t Adress;
uint16_t Content;
} I2C_EEPROM_t;
 
 
typedef struct
{
int16_t MagX;
int16_t MagY;
int16_t MagZ;
} I2C_Mag_t;
} ;
 
 
typedef struct
struct I2C_WriteAttitude_t
{
int16_t Nick;
int16_t Roll;
} I2C_WriteAttitude_t;
} ;
 
 
typedef struct
struct I2C_WriteCal_t
{
uint8_t CalByte;
uint8_t Dummy1;
uint8_t Dummy2;
} I2C_WriteCal_t;
uint8_t CalByte;
uint8_t Dummy1;
uint8_t Dummy2;
} ;
 
typedef struct
struct I2C_Heading_t
{
int16_t Heading;
} I2C_Heading_t;
int16_t Heading;
} ;
 
 
extern I2C_Heading_t I2C_Heading;
extern I2C_WriteAttitude_t I2C_WriteNickRoll;
extern I2C_Mag_t I2C_Mag;
extern I2C_Version_t I2C_Version;
extern I2C_WriteCal_t I2C_WriteCal;
struct I2C_Heading_t I2C_Heading;
struct I2C_WriteAttitude_t I2C_WriteAttitude;
struct I2C_Mag_t I2C_Mag;
struct I2C_Version_t I2C_Version;
struct I2C_WriteCal_t I2C_WriteCal;
 
void I2C_Init(void);
 
/branches/MK3Mag V0.14 Code Redesign Killagreg/uart.c
66,8 → 66,8
#define FALSE 0
#define TRUE 1
 
#define TXD_BUFFER_LEN 150
#define RXD_BUFFER_LEN 150
#define TXD_BUFFER_LEN 100
#define RXD_BUFFER_LEN 100
 
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
volatile uint8_t rxd_buffer_locked = FALSE;
75,21 → 75,22
volatile uint8_t txd_complete = TRUE;
volatile uint8_t ReceivedBytes = 0;
 
#define VERSION_INFO 0x01
#define DEBUG_DATA 0x02
#define DEBUG_LABEL 0x04
#define COMPASS_HEADING 0x08
 
uint8_t RequestFlags = 0x00;
uint8_t RequestDebugLabel = 0;
 
uint8_t RequestVerInfo = FALSE;
uint8_t RequestDebugData = FALSE;
uint8_t RequestDebugLabel = FALSE;
uint8_t RequestCompassHeading = FALSE;
 
uint8_t MySlaveAddr = 0;
 
//unsigned char,DebugGetAnforderung = 0,
 
DebugOut_t DebugOut;
ExternData_t ExternData;
ExternControl_t ExternControl;
VersionInfo_t VersionInfo;
struct DebugOut_t DebugOut;
struct ExternData_t ExternData;
struct ExternControl_t ExternControl;
struct VersionInfo_t VersionInfo;
 
uint16_t Debug_Timer;
 
417,7 → 418,7
{
case 'w':// Attitude
Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
RequestCompassHeading = TRUE;
RequestFlags |= COMPASS_HEADING;
break;
 
case 'b': // extern control
444,7 → 445,7
break;
 
case 'v': // get version and board release
RequestVerInfo = TRUE;
RequestFlags |= VERSION_INFO;
PC_Connected = 255;
break;
 
451,11 → 452,12
case 'a':// Labels of the Analog Debug outputs
Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes);
RequestDebugLabel = tmp_char_arr2[0];
RequestFlags |= DEBUG_LABEL;
PC_Connected = 255;
break;
 
case 'g':// get debug data
RequestDebugData = TRUE;
RequestFlags |= DEBUG_DATA;
PC_Connected = 255;
break;
}
472,7 → 474,7
 
if(!txd_complete) return;
 
if(CheckDelay(Debug_Timer) && txd_complete)
if(CheckDelay(Debug_Timer))
{
SetDebugValues();
SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
479,28 → 481,29
Debug_Timer = SetDelay(250);
}
 
if(RequestDebugLabel != 255)
if(RequestFlags & DEBUG_LABEL)
{
SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16);
RequestDebugLabel = 255;
RequestFlags &= ~DEBUG_LABEL;
}
 
if(RequestVerInfo && txd_complete)
if(RequestFlags & VERSION_INFO)
{
SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo));
RequestVerInfo = FALSE;
RequestFlags &= ~VERSION_INFO;
}
 
if(RequestDebugData && txd_complete)
if(RequestFlags & DEBUG_DATA)
{
SetDebugValues();
SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
RequestDebugData = FALSE;
RequestFlags &= ~DEBUG_DATA;
}
 
if(RequestCompassHeading && txd_complete)
if(RequestFlags & COMPASS_HEADING)
{
SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
RequestCompassHeading = FALSE;
RequestFlags &= ~COMPASS_HEADING;
}
}
/branches/MK3Mag V0.14 Code Redesign Killagreg/uart.h
22,28 → 22,28
 
 
 
typedef struct
struct DebugOut_t
{
uint8_t Digital[2];
uint16_t Analog[32]; // Debugvalues
} DebugOut_t;
} ;
 
extern DebugOut_t DebugOut;
extern struct DebugOut_t DebugOut;
 
 
 
typedef struct
struct ExternData_t
{
int16_t Attitude[2]; // nock and roll angle in 0.1 deg
uint8_t UserParam[2];
uint8_t CalState;
uint8_t Orientation;
} ExternData_t;
} ;
 
extern ExternData_t ExternData;
extern struct ExternData_t ExternData;
 
 
typedef struct
struct ExternControl_t
{
uint8_t Digital[2];
uint8_t RemoteButtons;
55,12 → 55,12
uint8_t Par1;
uint8_t Par2;
uint8_t Par3;
} ExternControl_t;
} ;
 
extern ExternControl_t ExternControl;
extern struct ExternControl_t ExternControl;
 
 
typedef struct
struct VersionInfo_t
{
uint8_t Major;
uint8_t Minor;
68,7 → 68,7
uint8_t Reserved[7];
} VersionInfo_t;
 
extern VersionInfo_t VersionInfo;
extern struct VersionInfo_t VersionInfo;
 
 
#endif //_UART_H_