Subversion Repositories MK3Mag

Compare Revisions

Ignore whitespace Rev 40 → Rev 41

/trunk/main.c
432,7 → 432,6
 
LED_GRN_ON;
 
Debug_Timer = SetDelay(200);
Led_Timer = SetDelay(200);
 
// read calibration info from eeprom
/trunk/makefile
5,10 → 5,12
#-------------------------------------------------------------------
BOARD = 10
#BOARD = 11
VERSION_MAJOR = 0
VERSION_MAJOR = 0
VERSION_MINOR = 19
VERSION_PATCH = 1
 
VERSION_PCCOMPATIBLE = 8 # PC-Kompatibilität
VERSION_SERIAL_COMPATIBLE = 10 # Serial Protocol Version
NC_I2C_COMPATIBLE = 2 # I2C Protocol Version
#-------------------------------------------------------------------
 
# get SVN revision
18,8 → 20,39
FORMAT = ihex
 
# Target file name (without extension).
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)_SVN$(REV)
 
ifeq ($(VERSION_PATCH), 0)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)a_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 1)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)b_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 2)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)c_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 3)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)d_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 4)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)e_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 5)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)f_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 6)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)g_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 7)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)h_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 8)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)i_SVN$(REV)
endif
ifeq ($(VERSION_PATCH), 9)
TARGET = MK3Mag_MEGA168_V$(VERSION_MAJOR)_$(VERSION_MINOR)j_SVN$(REV)
endif
 
 
# Optimization level, can be [0, 1, 2, 3, s]. 0 turns off optimization.
# (Note: 3 is not always the best optimization level. See avr-libc FAQ.)
OPT = s
69,7 → 102,7
#CFLAGS += -std=c99
CFLAGS += -std=gnu99
CFLAGS += $(CDEFS)
CFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_PCCOMPATIBLE=$(VERSION_PCCOMPATIBLE) -DBOARD=$(BOARD)
CFLAGS += -DVERSION_MAJOR=$(VERSION_MAJOR) -DVERSION_MINOR=$(VERSION_MINOR) -DVERSION_PATCH=$(VERSION_PATCH) -DVERSION_SERIAL_COMPATIBLE=$(VERSION_SERIAL_COMPATIBLE) -DBOARD=$(BOARD) -DNC_I2C_COMPATIBLE=$(NC_I2C_COMPATIBLE)
 
ifeq ($(AVR_CTRL_PLATINE), 1)
CFLAGS += -DAVR_CTRL_PLATINE=$(AVR_CTRL_PLATINE)
/trunk/twislave.c
123,7 → 123,8
// update version info
I2C_Version.Major = VERSION_MAJOR;
I2C_Version.Minor = VERSION_MINOR;
I2C_Version.Compatible = I2C_PROTOCOL_COMP;
I2C_Version.Patch = VERSION_PATCH;
I2C_Version.Compatible = NC_I2C_COMPATIBLE;
 
// resore status register
SREG = sreg;
/trunk/twislave.h
4,7 → 4,6
#include <inttypes.h>
 
#define I2C_SLAVE_ADDRESS 0x50
#define I2C_PROTOCOL_COMP 2
 
#define I2C_CMD_VERSION 0x01
#define I2C_CMD_READ_MAG 0x02
18,6 → 17,7
{
uint8_t Major;
uint8_t Minor;
uint8_t Patch;
uint8_t Compatible;
} I2C_Version_t;
 
/trunk/uart.c
57,6 → 57,8
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/wdt.h>
#include <stdarg.h>
#include <string.h>
#include "main.h"
#include "uart.h"
#include "timer0.h"
79,6 → 81,8
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
volatile uint8_t txd_complete = TRUE;
volatile uint8_t ReceivedBytes = 0;
volatile uint8_t *pRxData = 0;
volatile uint8_t RxDataLen = 0;
 
// send flags
#define RQST_VERSION_INFO 0x01
90,6 → 94,7
 
uint8_t RequestFlags = 0x00;
uint8_t RequestDebugLabel = 0;
uint8_t ConfirmFrame = 0;
 
uint8_t PC_Connected = 0;
uint8_t FC_Connected = 0;
100,9 → 105,10
DebugOut_t DebugOut;
ExternData_t ExternData;
ExternControl_t ExternControl;
VersionInfo_t VersionInfo;
UART_VersionInfo_t UART_VersionInfo;
 
uint16_t Debug_Timer;
uint16_t DebugData_Timer;
uint16_t DebugData_Interval = 500;
 
const uint8_t ANALOG_LABEL[32][16] =
{
147,6 → 153,7
/****************************************************************/
void USART0_Init (void)
{
uint8_t sreg = SREG;
uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1);
 
// disable all interrupts before configuration
202,13 → 209,22
// enable TX-Interrupt
UCSR0B |= (1 << TXCIE0);
 
// initialize the debug timer
DebugData_Timer = SetDelay(DebugData_Interval);
 
// unlock rxd_buffer
rxd_buffer_locked = FALSE;
pRxData = 0;
RxDataLen = 0;
 
// no bytes to send
txd_complete = TRUE;
 
 
VersionInfo.Major = VERSION_MAJOR;
VersionInfo.Minor = VERSION_MINOR;
VersionInfo.PCCompatible = VERSION_PCCOMPATIBLE;
UART_VersionInfo.Major = VERSION_MAJOR;
UART_VersionInfo.Minor = VERSION_MINOR;
UART_VersionInfo.Patch = VERSION_PATCH;
UART_VersionInfo.Compatible = VERSION_SERIAL_COMPATIBLE;
 
MySlaveAddr = MK3MAG_ADDRESS;
 
222,7 → 238,11
USART0_putchar ('.');
USART0_putchar (0x30 + VERSION_MINOR/10);
USART0_putchar (0x30 + VERSION_MINOR%10);
USART0_putchar ('a' + VERSION_PATCH);
USART0_putchar ('\n');
 
// restore global interrupt flags
SREG = sreg;
}
 
// ---------------------------------------------------------------------------------
315,9 → 335,9
// compare checksum to transmitted checksum bytes
if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1]))
{ // checksum valid
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes
rxd_buffer_locked = TRUE; // lock the rxd buffer
ReceivedBytes = ptr_rxd_buffer; // store number of received bytes
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character
// if 2nd byte is an 'R' enable watchdog that will result in an reset
if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando
}
355,36 → 375,88
 
 
// --------------------------------------------------------------------------
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len)
void SendOutData(uint8_t cmd,uint8_t module, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ...
{
va_list ap;
uint16_t pt = 0;
uint8_t a,b,c;
uint8_t ptr = 0;
 
uint8_t *pdata = 0;
int len = 0;
 
txd_buffer[pt++] = '#'; // Start character
txd_buffer[pt++] = module; // Address (a=0; b=1,...)
txd_buffer[pt++] = cmd; // Command
 
va_start(ap, numofbuffers);
if(numofbuffers--)
{
pdata = va_arg(ap, uint8_t*);
len = va_arg(ap, int);
ptr = 0;
}
 
while(len)
{
if(len) { a = snd[ptr++]; len--;} else a = 0;
if(len) { b = snd[ptr++]; len--;} else b = 0;
if(len) { c = snd[ptr++]; len--;} else c = 0;
if(len)
{
a = pdata[ptr++];
len--;
if((!len) && numofbuffers)
{
pdata = va_arg(ap, uint8_t*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else a = 0;
if(len)
{
b = pdata[ptr++];
len--;
if((!len) && numofbuffers)
{
pdata = va_arg(ap, uint8_t*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else b = 0;
if(len)
{
c = pdata[ptr++];
len--;
if((!len) && numofbuffers)
{
pdata = va_arg(ap, uint8_t*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else c = 0;
txd_buffer[pt++] = '=' + (a >> 2);
txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
txd_buffer[pt++] = '=' + ( c & 0x3f);
}
va_end(ap);
AddCRC(pt); // add checksum after data block and initates the transmission
}
 
 
// --------------------------------------------------------------------------
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max)
void Decode64(void)
{
uint8_t a,b,c,d;
uint8_t ptr = 0;
uint8_t x,y,z;
uint8_t ptrIn = 3;
uint8_t ptrOut = 3;
uint8_t len = ReceivedBytes - 6;
 
while(len)
{
a = rxd_buffer[ptrIn++] - '=';
391,19 → 463,22
b = rxd_buffer[ptrIn++] - '=';
c = rxd_buffer[ptrIn++] - '=';
d = rxd_buffer[ptrIn++] - '=';
if(ptrIn > max - 2) break;
//if(ptrIn > ReceivedBytes - 3) break;
 
x = (a << 2) | (b >> 4);
y = ((b & 0x0f) << 4) | (c >> 2);
z = ((c & 0x03) << 6) | d;
 
if(len--) ptrOut[ptr++] = x; else break;
if(len--) ptrOut[ptr++] = y; else break;
if(len--) ptrOut[ptr++] = z; else break;
if(len--) rxd_buffer[ptrOut++] = x; else break;
if(len--) rxd_buffer[ptrOut++] = y; else break;
if(len--) rxd_buffer[ptrOut++] = z; else break;
}
pRxData = &rxd_buffer[3];
RxDataLen = ptrOut - 3;
}
 
 
 
// --------------------------------------------------------------------------
int16_t USART0_putchar (int8_t c)
{
424,12 → 499,12
// if data in the rxd buffer are not locked immediately return
if(!rxd_buffer_locked) return;
 
uint8_t tmp_char_arr2[2]; // local char buffer
Decode64(); // decode data block in rxd_buffer
 
switch(rxd_buffer[2])
{
case 'w':// Attitude info from FC
Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes);
case 'k':// Attitude info from FC
memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData));
RequestFlags |= RQST_COMPASS_HEADING;
AttitudeSource = ATTITUDE_SOURCE_UART;
Orientation = ExternData.Orientation;
438,8 → 513,7
 
// used only for debug proposes at serial port
case 'b': // extern control
case 'c': // extern control with debug request
Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes);
memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl));
#define KEY1 0x01
#define KEY2 0x02
#define KEY3 0x04
451,28 → 525,22
ExternData.CalState++;
if(ExternData.CalState == 6) ExternData.CalState = 0;
}
ConfirmFrame = ExternControl.Frame;
PC_Connected = 255;
break;
 
// get debug values
case 'f':
RequestFlags |= RQST_DEBUG_DATA;
PC_Connected = 255;
case 'd': // request for the debug data
DebugData_Interval = (uint16_t) pRxData[0] * 10;
if(DebugData_Interval) RequestFlags |= RQST_DEBUG_DATA;
break;
 
// no reaction for display line request
case 'h':// x-1 display columns
PC_Connected = 255;
break;
 
case 'v': // get version and board release
case 'v': // request version and board release
RequestFlags |= RQST_VERSION_INFO;
PC_Connected = 255;
break;
 
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];
RequestDebugLabel = pRxData[0];
RequestFlags |= RQST_DEBUG_LABEL;
PC_Connected = 255;
break;
483,6 → 551,8
break;
}
// unlock the rxd buffer after processing
pRxData = 0;
RxDataLen = 0;
rxd_buffer_locked = FALSE;
}
 
495,40 → 565,45
 
if(!txd_complete) return;
 
if(CheckDelay(Debug_Timer) || (RequestFlags & RQST_DEBUG_DATA))
if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete)
{
SetDebugValues();
SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut));
Debug_Timer = SetDelay(250);
RequestFlags &= ~RQST_DEBUG_DATA;
SendOutData('A', MySlaveAddr, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), (uint8_t *) ANALOG_LABEL[RequestDebugLabel], 16);
RequestDebugLabel = 0xFF;
RequestFlags &= ~RQST_DEBUG_LABEL;
}
 
if(RequestFlags & RQST_DEBUG_LABEL)
if(ConfirmFrame && txd_complete)
{
SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16);
RequestDebugLabel = 255;
RequestFlags &= ~RQST_DEBUG_LABEL;
Debug_Timer = SetDelay(500);
SendOutData('B',MySlaveAddr, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame));
ConfirmFrame = 0;
}
 
if(RequestFlags & RQST_VERSION_INFO)
if(( (DebugData_Interval && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete)
{
SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo));
RequestFlags &= ~RQST_VERSION_INFO;
SetDebugValues();
SendOutData('D', MySlaveAddr, 1, (uint8_t *) &DebugOut, sizeof(DebugOut));
DebugData_Timer = SetDelay(DebugData_Interval);
RequestFlags &= ~RQST_DEBUG_DATA;
}
 
if(RequestFlags & RQST_EXTERN_CTRL)
 
if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete)
{
SetDebugValues();
SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl));
SendOutData('G',MySlaveAddr, 1, (uint8_t *) &ExternControl,sizeof(ExternControl));
RequestFlags &= ~RQST_EXTERN_CTRL;
}
 
if(RequestFlags & RQST_COMPASS_HEADING)
if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete)
{
SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
SendOutData('K',MySlaveAddr, 1, (uint8_t *) &I2C_Heading, sizeof(I2C_Heading));
RequestFlags &= ~RQST_COMPASS_HEADING;
}
 
if((RequestFlags & RQST_VERSION_INFO) && txd_complete)
{
SendOutData('V',MySlaveAddr, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo));
RequestFlags &= ~RQST_VERSION_INFO;
}
}
 
 
/trunk/uart.h
10,7 → 10,7
#define BAUD_RATE 57600
 
 
extern uint16_t Debug_Timer;
extern uint16_t DebugData_Timer;
 
 
void USART0_Init (void);
43,7 → 43,6
 
extern ExternData_t ExternData;
 
 
typedef struct
{
uint8_t Digital[2];
53,9 → 52,9
int8_t Yaw;
uint8_t Gas;
int8_t Height;
uint8_t Par1;
uint8_t Par2;
uint8_t Par3;
uint8_t free;
uint8_t Frame;
uint8_t Config;
} ExternControl_t;
 
extern ExternControl_t ExternControl;
65,12 → 64,11
{
uint8_t Major;
uint8_t Minor;
uint8_t PCCompatible;
uint8_t Reserved[7];
} VersionInfo_t;
uint8_t Patch;
uint8_t Compatible;
uint8_t Reserved[6];
} UART_VersionInfo_t;
 
extern VersionInfo_t VersionInfo;
 
extern uint8_t PC_Connected;
extern uint8_t FC_Connected;