Subversion Repositories NaviCtrl

Compare Revisions

Regard whitespace Rev 488 → Rev 489

/trunk/i2c1.c
File deleted
/trunk/i2c0.c
File deleted
/trunk/i2c1.h
File deleted
/trunk/i2c0.h
File deleted
/trunk/GPS.c
74,7 → 74,7
#include "timer1.h"
#include "spi_slave.h"
#include "waypoints.h"
#include "i2c1.h"
#include "i2c.h"
 
 
#define M_PI_180 (M_PI / 180.0f)
/trunk/Navi-Ctrl.Uv2
12,10 → 12,10
File 1,1,<.\main.c><main.c>
File 1,1,<.\ramfunc.c><ramfunc.c>
File 1,1,<.\menu.c><menu.c>
File 1,1,<.\mymath.c><mymath.c>
File 1,1,<.\i2c.c><i2c.c>
File 1,1,<.\spi_slave.c><spi_slave.c>
File 1,1,<.\fat16.c><fat16.c>
File 1,1,<.\i2c0.c><i2c0.c>
File 1,1,<.\i2c1.c><i2c1.c>
File 1,1,<.\sdc.c><sdc.c>
File 1,1,<.\settings.c><settings.c>
File 1,1,<.\ftphelper.c><ftphelper.c>
43,13 → 43,11
File 1,1,<.\buffer.c><buffer.c>
File 1,1,<.\eeprom.c><eeprom.c>
File 1,1,<.\ncmag.c><ncmag.c>
File 1,1,<.\mymath.c><mymath.c>
File 2,5,<.\ramfunc.h><ramfunc.h>
File 2,5,<.\main.h><main.h>
File 2,5,<.\menu.h><menu.h>
File 2,5,<.\settings.h><settings.h>
File 2,5,<.\i2c0.h><i2c0.h>
File 2,5,<.\i2c1.h><i2c1.h>
File 2,5,<.\i2c.h><i2c.h>
File 2,5,<.\spi_slave.h><spi_slave.h>
File 2,5,<.\sdc.h><sdc.h>
File 2,5,<.\ssc.h><ssc.h>
/trunk/compass.c
68,6 → 68,7
fifo_t CompassCalcStateFiFo;
 
volatile s16vec_t MagVector; // is written by mk3mag or ncmag implementation
volatile s16vec_t AccVector; // current acceleration vector of compass, not supported by any HW version
volatile s16 Compass_Heading; // is written by mk3mag or ncmag implementation
volatile u8 Compass_CalState; // is written by mk3mag or ncmag implementation
s16 Hx = 0, Hy = 0;
84,6 → 85,7
#define COMPASS_MK3MAG 1
#define COMPASS_NCMAG 2
u8 Compass_Device = COMPASS_NONE;
I2C_TypeDef* Compass_I2CPort = I2C1;
 
void Compass_Init(void)
{
93,27 → 95,18
UART1_PutString("\r\n Looking for compass");
if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
 
/* if(Version_HW > 11)
{
if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
else if( MK3MAG_Init()) Compass_Device = COMPASS_MK3MAG;
}
else
{
if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
else if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
}
*/
break;
 
case COMPASS_NCMAG:
UART1_PutString("\r\n Re-Init compass");
if( NCMAG_Init() ) Compass_Device = COMPASS_NCMAG;
else Compass_Device = COMPASS_NONE;
break;
 
case COMPASS_MK3MAG:
if( MK3MAG_Init() ) Compass_Device = COMPASS_MK3MAG;
else Compass_Device = COMPASS_NONE;
break;
 
default:
// nothing to do
break;
123,9 → 116,29
 
if(Compass_Device == COMPASS_MK3MAG) UART_VersionInfo.Flags |= NC_VERSION_FLAG_MK3MAG_PRESENT; else UART_VersionInfo.Flags &= ~NC_VERSION_FLAG_MK3MAG_PRESENT;
 
MagVector.X = 0;
MagVector.Y = 0;
MagVector.Z = 0;
Compass_Heading = -1;
}
 
 
void Compass_Check(void)
{
switch(Compass_Device)
{
case COMPASS_NCMAG:
NCMAG_CheckOrientation();
break;
 
case COMPASS_MK3MAG:
case COMPASS_NONE:
default:
// nothing to do
break;
}
}
 
void Compass_CalcHeading(void)
{
if((UART_VersionInfo.HardwareError[0] & NC_ERROR0_SPI_RX) || Compass_CalState)
168,29 → 181,28
{
case COMPASS_MK3MAG:
MK3MAG_Update();
break;
case COMPASS_NCMAG:
NCMAG_Update(0);
break;
default:
break;
}
 
DebugOut.Analog[24] = MagVector.X;
DebugOut.Analog[25] = MagVector.Y;
DebugOut.Analog[26] = MagVector.Z;
break;
case COMPASS_NCMAG:
 
if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normaly changing
 
if(check_value_counter == 2000)
{
UART1_PutString("\n\r Init Mag.-Sensor");
InitNC_MagnetSensor(); // 2 seconds no change of the compass value
Compass_Init(); // 2 seconds no change of the compass value
}
else NCMAG_Update(0);
DebugOut.Analog[24] = MagRawVector.X;
DebugOut.Analog[25] = MagRawVector.Y;
DebugOut.Analog[26] = MagRawVector.Z;
default:
break;
}
if(!((old.X == MagVector.X) || (old.Y == MagVector.Y) || (old.Z == MagVector.Z))) check_value_counter = 0; // Values are normally changing
 
if(check_value_counter > 5000)
else if(check_value_counter > 5000)
{
Compass_Heading = -1; // values didn't change for 5 seconds -> probably a compass-fault
// CompassValueErrorCount = 35;
}
else check_value_counter++;
 
/trunk/compass.h
1,6 → 1,9
#ifndef _COMPASS_H
#define _COMPASS_H
 
// this is the abstract interface for the magnetometer/compass sensor
// below this there are different special sensors that are supported
 
typedef struct
{
s16 X;
9,6 → 12,7
} __attribute__((packed)) s16vec_t;
 
extern volatile s16vec_t MagVector; // current magnetic field vector
extern volatile s16vec_t AccVector; // current acceleration vector of compass, not supported by any HW version
extern volatile s16 Compass_Heading; // current heading direction
extern volatile u8 Compass_CalState; // current calibration state
extern s32 EarthMagneticField;
20,17 → 24,18
extern s16 EarthMagneticStrengthTheoretic;
extern s16 Hx, Hy;
 
extern u32 CompassValueErrorCount;
 
#define COMPASS_NONE 0
#define COMPASS_MK3MAG 1
#define COMPASS_NCMAG 2
extern u8 Compass_Device;
extern I2C_TypeDef* Compass_I2CPort;
extern u32 CompassValueErrorCount;
 
void Compass_Init(void);
void Compass_Update(void);
void Compass_CalcHeading(void);
void Compass_SetCalState(u8 CalState);
void Compass_UpdateCalState(void);
void Compass_Init(void); // initialization for compass sensor
void Compass_Check(void); // possible check for compass plausibility, called when FC gets calibrates
void Compass_Update(void); // updates sensor values, cyclic called
void Compass_CalcHeading(void); // calculates heading from magent vector componentes
void Compass_SetCalState(u8 CalState); // append to CalState queue
void Compass_UpdateCalState(void); // process CalState queue
 
#endif // _COMPASS_H
/trunk/config.h
64,8 → 64,8
#define UART2_BAUD_RATE 57600 //Baud Rate for the serial interfaces
 
//<o> I2C-Bus1 <22000=> 20 kBit <50000=> 50 kBit <100000=> 100 kBit <200000=> 200 kBit <400000=> 400 kBit
#define I2C0_CLOCK 100000 // Bit Rate for I2C
#define I2C1_CLOCK 50000 // Bit Rate for I2C
#define I2C0_CLOCK 100000 // Bit Rate for I2C
 
//</h>
//<<< end of configuration section >>>
/trunk/eeprom.c
56,7 → 56,7
#include <string.h>
#include <stdio.h>
#include "91x_lib.h"
#include "i2c1.h"
#include "i2c.h"
#include "uart1.h"
#include "timer1.h"
#include "eeprom.h"
63,7 → 63,10
#include "led.h"
 
 
 
// The EEPROM M24C64 (64k) ist connected via I2C1 interface to the controller.
#define EEPROM_I2C I2C1
 
// The E0, E2, E3 pins are set to low. Therefore the slave adressbits b3, b2, b1 are 0.
 
#define EEPROM_I2C_ADDRESS 0xA0
92,7 → 95,7
// ----------------------------------------------------------------------------------------
EEPROM_Result_t EEPROM_Transfer(u8 Direction, u16 Address, u8 *pData, u16 DataLen)
{
u16 i, TxBytes = 0;
u16 i;
u8 retry = 0;
EEPROM_Result_t retval = EEPROM_ERROR_UNKNOWN;
 
101,7 → 104,7
retval = EEPROM_ERROR_OUT_OF_ADDRESS_RANGE;
return(retval);
}
if((DataLen+2) > I2C1_BUFFER_LEN)
if((DataLen+2) > I2C_BUFFER_LEN)
{
retval = EEPROM_I2C_BUFFER_OVERRUN;
return(retval);
108,23 → 111,24
}
do
{
if(!I2C1_LockBuffer(I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
if(!I2CBus_LockBuffer(EEPROM_I2C, I2C_IDLE_TIMEOUT)) return EEPROM_ERROR_I2C_IDLE_TIMEOUT;
// buffer is now locked
TxBytes = 0;
u8 TxBytes = 0;
u8 TxData[255];
// transmitt address
I2C1_Buffer[TxBytes++] = (u8)(0x00FF & (Address>>8));
I2C1_Buffer[TxBytes++] = (u8)(0x00FF & Address);
TxData[TxBytes++] = (u8)(0x00FF & (Address>>8));
TxData[TxBytes++] = (u8)(0x00FF & Address);
if(Direction == EEPROM_WRITE)
{ // copy data to i2c transfer buffer
for(i = 0; i<DataLen;i++)
{
I2C1_Buffer[TxBytes++] = pData[i];
TxData[TxBytes++] = pData[i];
}
// prepare pointer to rx data
EEPROM_pData = 0;
EEPROM_DataLen = 0;
// start transmission
if(!I2C1_Transmission(EEPROM_I2C_ADDRESS, TxBytes, 0, 0))
if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, 0, 0))
{
return(retval);
}
135,15 → 139,15
EEPROM_pData = pData;
EEPROM_DataLen = DataLen;
// start transmission
if(!I2C1_Transmission(EEPROM_I2C_ADDRESS, TxBytes, &EEPROM_RxDataHandler, DataLen))
if(!I2CBus_Transmission(EEPROM_I2C, EEPROM_I2C_ADDRESS, TxData, TxBytes, &EEPROM_RxDataHandler, DataLen))
{
return(retval);
}
}
//wait for end of this transfer
if(I2C1_WaitForEndOfTransmission(I2C_TRANSFER_TIMEOUT))
if(I2CBus_WaitForEndOfTransmission(EEPROM_I2C, I2C_TRANSFER_TIMEOUT))
{
if(I2C1_Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
if(I2CBus(EEPROM_I2C)->Error == I2C_ERROR_NONE) return(EEPROM_SUCCESS);
else retval = EEPROM_DATA_TRANSFER_INCOMPLETE;
}
else// i2c transfer timed out
190,7 → 194,7
 
while(DataLen > 0)
{
if(DataLen > I2C1_BUFFER_LEN) RxLen = I2C1_BUFFER_LEN;
if(DataLen > I2C_BUFFER_LEN) RxLen = I2C_BUFFER_LEN;
else RxLen = DataLen;
retval = EEPROM_Transfer(EEPROM_READ, Address+AdrOffset, &(pData[AdrOffset]), RxLen);
/trunk/gpx.c
110,7 → 110,7
retvalue = 1; // the document could be created on the drive.
doc->state = GPX_DOC_OPENED; // change document state to opened. At next a placemark has to be opened.
fwrite_((void*)GPX_DOCUMENT_HEADER1, sizeof(GPX_DOCUMENT_HEADER1)-1,1,doc->file);// write the gpx-header to the document.
sprintf(string, "<desc>FC HW:%d.%d SW:%d.%02d%c + NC HW:%d.%d SW:%d.%02d%c</desc>\r\n", (FC_Version.Hardware&0x7f) /10,(FC_Version.Hardware&0x7f)%10, FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, Version_HW/10, Version_HW%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH);
sprintf(string, "<desc>FC HW:%d.%d SW:%d.%02d%c + NC HW:%d.%d SW:%d.%02d%c</desc>\r\n", (FC_Version.Hardware & 0x7F)/10,(FC_Version.Hardware & 0x7F)%10, FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, Version_HW/10, Version_HW%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH);
fputs_(string, doc->file);
fwrite_((void*)GPX_DOCUMENT_HEADER2, sizeof(GPX_DOCUMENT_HEADER2)-1,1,doc->file);// write the gpx-header to the document.
}
273,20 → 273,6
return(retvalue);
}
 
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
unsigned int BL3_Current(unsigned char who) // in 0,1A
{
if(Motor[who].Current <= 200) return((unsigned int) Motor[who].Current);
else
{
if(Motor_Version[who] & MOTOR_STATE_BL30) return(200 + 10 * (unsigned int) (Motor[who].Current-200));
else return((unsigned int) Motor[who].Current);
}
}
 
 
//________________________________________________________________________________________________________________________________________
// Function: u8 GPX_TrackSegementAddPoint(GPS_Pos_t * pGPS_Position ,GPX_Document_t *doc);
//
/trunk/gpx.h
23,7 → 23,7
File_t *file; // filepointer to the file where the data should be saved.
} GPX_Document_t;
 
u8 GPX_LoggGPSCoordinates(GPX_Document_t *,unsigned char part); // intializes the gpx-document with standard filename and adds points to the file
u8 GPX_LoggGPSCoordinates(GPX_Document_t *,u8 part); // intializes the gpx-document with standard filename and adds points to the file
u8 GPX_DocumentInit(GPX_Document_t *); // Init the new gpx-document
u8 GPX_DocumentOpen(s8 *, GPX_Document_t *); // opens a new gpx-document. a new file is created on the sd-memorycard
u8 GPX_DocumentClose(GPX_Document_t *doc); // closes the specified document saving remaining data to the file.
31,7 → 31,6
u8 GPX_TrackEnd(GPX_Document_t *doc); // ends the actual track
u8 GPX_TrackSegmentBegin(GPX_Document_t *doc); // begins a new tracksegment within the actual track
u8 GPX_TrackSegmentEnd(GPX_Document_t *doc); // ends the actual track segment within the actual track
u8 GPX_TrackSegmentAddPoint(GPX_Document_t *,unsigned char part); // adds a point to the tracksegment
extern unsigned int BL3_Current(unsigned char who); // in 0,1A
u8 GPX_TrackSegmentAddPoint(GPX_Document_t *,u8 part); // adds a point to the tracksegment
 
#endif //_GPX_H
/trunk/led.h
8,15 → 8,12
#define LED_GRN_OFF {if(Led_Grn_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_6, Bit_SET); else GPIO_WriteBit(GPIO5, GPIO_Pin_6, Bit_RESET);}
#define LED_GRN_TOGGLE if (GPIO_ReadBit(GPIO5, GPIO_Pin_6)) GPIO_WriteBit(GPIO5, GPIO_Pin_6, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_6, Bit_SET);
 
 
#define LED_RED_ON_T // {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET);}
#define LED_RED_OFF_T // {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET);}
 
#define LED_RED_ON_Q {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET);}
#define LED_RED_OFF_Q {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET);}
 
 
#define LED_RED_ON // {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET);}
#define LED_RED_OFF// {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET);}
#define LED_RED_ON {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET);}
#define LED_RED_OFF {if(Led_Red_Inv) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET);}
#define LED_RED_TOGGLE if (GPIO_ReadBit(GPIO5, GPIO_Pin_7)) GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_RESET); else GPIO_WriteBit(GPIO5, GPIO_Pin_7, Bit_SET);
 
extern u8 Version_HW;
/trunk/main.c
62,8 → 62,7
#include "uart1.h"
#include "uart2.h"
#include "gps.h"
#include "i2c0.h"
#include "i2c1.h"
#include "i2c.h"
#include "compass.h"
#include "ncmag.h"
#include "timer1.h"
82,9 → 81,9
#include "ssc.h"
#include "sdc.h"
#include "uart1.h"
#include "ncmag.h"
 
 
 
#ifdef FOLLOW_ME
u8 TransmitAlsoToFC = 0;
#endif
146,9 → 145,7
s32 newErrorCode = 0;
UART_VersionInfo.HardwareError[0] = 0;
 
if((I2C_CompassPort == I2C_INTERN_1 && CheckDelay(I2C1_Timeout)) || (I2C_CompassPort == I2C_EXTERN_0 && CheckDelay(I2C0_Timeout)) || (Compass_Heading < 0))
DebugOut.StatusRed |= AMPEL_COMPASS;
if(CheckDelay(I2CBus(Compass_I2CPort)->Timeout) || (Compass_Heading < 0)) DebugOut.StatusRed |= AMPEL_COMPASS;
else DebugOut.StatusRed &= ~AMPEL_COMPASS; // MK3Mag green status
 
if((FC.Error[1] & FC_ERROR1_I2C) || (FC.Error[1] & FC_ERROR1_BL_MISSING)) DebugOut.StatusRed |= AMPEL_BL;
157,34 → 154,18
if(UART_VersionInfo.HardwareError[0] || UART_VersionInfo.HardwareError[1]) DebugOut.StatusRed |= AMPEL_NC;
else DebugOut.StatusRed &= ~AMPEL_NC;
 
//if(I2C_CompassPort == I2C_EXTERN_0) LED_RED_OFF_T;
 
if(CheckDelay(I2C1_Timeout) && (I2C_CompassPort == I2C_INTERN_1))
if(CheckDelay(I2CBus(Compass_I2CPort)->Timeout))
{
LED_RED_ON;
sprintf(ErrorMSG,"no compass communica");
//Reset I2CBus
I2C1_Deinit();
I2C1_Init();
//Reset Compass communication
Compass_Init();
newErrorCode = 4;
StopNavigation = 1;
UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
DebugOut.StatusRed |= AMPEL_COMPASS;
}
else if(CheckDelay(I2C0_Timeout) && (I2C_CompassPort == I2C_EXTERN_0))
{
LED_RED_ON;
//LED_RED_ON_T;
sprintf(ErrorMSG,"no ext. compass ");
//Reset I2CBus
I2C0_Deinit();
I2C0_Init();
NCMAG_Update(1);
newErrorCode = 33;
StopNavigation = 1;
UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_RX;
DebugOut.StatusRed |= AMPEL_COMPASS;
}
else if(CompassValueErrorCount > 30)
{
LED_RED_ON;
192,27 → 173,16
newErrorCode = 34;
StopNavigation = 1;
UART_VersionInfo.HardwareError[0] |= NC_ERROR0_COMPASS_VALUE;
if(I2C_CompassPort == I2C_INTERN_1)
{
I2C1_Deinit();
I2C1_Init();
Compass_Init();
}
else
else if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
{
I2C0_Deinit();
I2C0_Init();
}
}
else
if((FCCalibActive || CompassCalState) && FC_Version.Hardware)
{
sprintf(ErrorMSG,"Calibrate... ");
newErrorCode = 0;
ErrorCode = 0;
no_error_delay = 1;
}
else
if(CheckDelay(SPI0_Timeout))
else if(CheckDelay(SPI0_Timeout))
{
LED_RED_ON;
sprintf(ErrorMSG,"no FC communication ");
235,7 → 205,6
UART_VersionInfo.HardwareError[0] |= NC_ERROR0_FC_INCOMPATIBLE;
DebugOut.StatusRed |= AMPEL_NC;
}
 
else if(FC.Error[0] & FC_ERROR0_GYRO_NICK)
{
LED_RED_ON;
374,9 → 343,8
}
else if(BL_MinOfMaxPWM < 30 && !ErrorCode)
{
unsigned int i;
u16 i;
for(i = 0; i < 12; i++) if(Motor[i].MaxPWM == BL_MinOfMaxPWM) break;
 
LED_RED_ON;
sprintf(ErrorMSG,"ERR:BL%2d Test:%2d ",i+1,BL_MinOfMaxPWM);
newErrorCode = 32;
455,8 → 423,10
{
static u8 running = 0, oldFcFlags = 0, count5sec;
static u32 old_ms = 0;
 
if(running) {/*DebugOut.Analog[18]++;*/ return(1);};
running = 1;
 
if(CountMilliseconds != old_ms) // 1 ms
{
old_ms = CountMilliseconds;
464,6 → 434,7
Analog_Update(); // get new ADC values
CalcHeadFree();
}
 
SPI0_UpdateBuffer(); // also calls the GPS-functions
UART0_ProcessRxData(); // GPS process request
UART0_TransmitTxData(); // GPS send answer
470,9 → 441,8
UART1_ProcessRxData(); // PC process request
UART1_TransmitTxData(); // PC send answer
UART2_TransmitTxData(); // FC send answer
 
// ---------------- Error Check Timing ----------------------------
 
//DebugOut.Analog[16] = CompassValueErrorCount;
if(CheckDelay(TimerCheckError) || (FC.StatusFlags & FC_STATUS_START && !(oldFcFlags & FC_STATUS_START))) // Timer or FY wants to start
{
if(CheckDelay(TimerCheckError))
513,6 → 483,7
VIC_ITCmd(EXTIT3_ITLine,DISABLE); // disable irq
// Compass_Update(); // update compass communication
// Analog_Update(); // get new ADC values
 
if(PollingTimeout == 0)
{
PollingTimeout = 5;
530,8 → 501,8
int main(void)
{
static u32 ftimer =0;
static u8 fstate = 0;
// static u32 ftimer =0;
// static u8 fstate = 0;
// static File_t* f = NULL;
561,8 → 532,8
// initialize SPI0 to FC
SPI0_Init();
// initialize i2c busses (needs Timer 1)
I2C0_Init();
I2C1_Init();
I2CBus_Init(I2C0);
I2CBus_Init(I2C1);
 
// initialize fat16 partition on sd card (needs Timer 1)
Fat16_Init();
619,8 → 590,8
{
PollingTimeout = 5;
Polling();
 
// ---------------- Logging ---------------------------------------
 
if(SD_WatchDog)
{
SD_WatchDog = 30000;
628,6 → 599,7
else if(FC.StatusFlags & FC_STATUS_START) SD_LoggingError = 100;
if(!SD_WatchDog) UART1_PutString("\n\rSD-Watchdog - Logging aborted\n\r");
}
 
/*
if(CheckDelay(ftimer))
{
/trunk/main.h
176,6 → 176,7
u8 GlobalConfig3;
u8 NaviOut1Parameter; // Distance between Photo releases
u8 FromFC_LandingSpeed;
u8 FromFC_LowVoltageHomeActive;
} __attribute__((packed)) Param_t;
 
typedef struct
/trunk/menu.c
68,7 → 68,6
#include "ncmag.h"
#include "logging.h"
#include "settings.h"
#include "gpx.h"
 
u8 DispPtr = 0;
s8 DisplayBuff[DISPLAYBUFFSIZE];
88,7 → 87,6
for( i = 0; i < DISPLAYBUFFSIZE; i++) DisplayBuff[i] = ' ';
}
 
 
// Display with 20 characters in 4 lines
void Menu_Update(u8 Keys)
{
115,7 → 113,7
// Version Info
case 0:
LCD_printfxy(0,0,"++ Navi-Ctrl ++");
LCD_printfxy(0,1,"HW V%d.%d SW V%d.%02d%c", (Version_HW & 0x7f)/10, (Version_HW & 0x7f)%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH);
LCD_printfxy(0,1,"HW V%d.%d SW V%d.%02d%c", (Version_HW & 0x7F)/10, (Version_HW & 0x7F)%10, VERSION_MAJOR, VERSION_MINOR, 'a'+ VERSION_PATCH);
 
if(ErrorCode)
{
358,7 → 356,8
LCD_printfxy(0,0,"GyroNick: %4i", FromFlightCtrl.GyroNick);
LCD_printfxy(0,1,"GyroRoll: %4i", FromFlightCtrl.GyroRoll);
LCD_printfxy(0,2,"GyroYaw: %4i", FromFlightCtrl.GyroYaw);
if(FC_is_Calibrated) LCD_printfxy(0,3,"Calibrated ") else LCD_printfxy(0,3,"not calibrated");
if(FC_is_Calibrated) LCD_printfxy(0,3,"Calibrated ")
else LCD_printfxy(0,3,"not calibrated");
break;
case 15:
// LCD_printfxy(0,0,"Ubat: %2i.%1i V", FC.BAT_Voltage/10, FC.BAT_Voltage%10);
427,7 → 426,8
LCD_printfxy(0,2,"Y:%5i",MagVector.Y);
LCD_printfxy(0,3,"Z:%5i",MagVector.Z);
LCD_printfxy(8,1,"Field:%3i",EarthMagneticField/5);
if(I2C_CompassPort == I2C_EXTERN_0) LCD_printfxy(11,2,"Extern %d",ExtCompassOrientation) else LCD_printfxy(11,2,"Intern");
if(Compass_I2CPort == NCMAG_PORT_EXTERN) LCD_printfxy(11,2,"Extern")
else LCD_printfxy(11,2,"Intern");
// LCD_printfxy(8,2,"Dec:%c%i.%1i", sign, abs(GeoMagDec)/10,abs(GeoMagDec)%10);
// LCD_printfxy(8,3,"Inc:%2i", EarthMagneticInclination);
LCD_printfxy(15,3,"(CAL)");
467,17 → 467,18
if(Motor[4 + i1 * 4].State == 0) break;
}
break;
 
case 23:
LCD_printfxy(0,0,"Ext. Compass" );
if(I2C_CompassPort == I2C_EXTERN_0)
if(Compass_I2CPort == NCMAG_PORT_EXTERN)
{
u8 tmp;
LCD_printfxy(0,1,"ACC X Y Z");
LCD_printfxy(0,2," %5d %5d %5d",AccRawVector.X/40,AccRawVector.Y/40,AccRawVector.Z/40);
tmp = GetExtCompassOrientation();
LCD_printfxy(0,2," %5d %5d %5d", AccVector.X/40, AccVector.Y/40, AccVector.Z/40);
tmp = NCMAG_GetOrientationFromAcc();
LCD_printfxy(0,3,"Orientat.: ");
if(!tmp) LCD_printfxy(11,3,"??") else LCD_printfxy(11,3,"%2d",tmp);
LCD_printfxy(15,3,"(%d)",ExtCompassOrientation);
LCD_printfxy(15,3,"(%d)",NCMAG_Orientation);
}
else
{
/trunk/mk3mag.c
57,8 → 57,7
#include <string.h>
#include "91x_lib.h"
#include "mk3mag.h"
#include "i2c1.h"
#include "i2c0.h"
#include "i2c.h"
#include "timer1.h"
#include "led.h"
#include "main.h"
174,14 → 173,15
void MK3MAG_SendCommand(u8 command)
{
// try to catch the I2C buffer
if(I2C1_LockBuffer(0))
if(I2CBus_LockBuffer(I2C1, 0))
{
u8 TxData[100];
u16 TxBytes = 0;
u16 RxBytes = 0;
I2C_pRxHandler_t pRxHandlerFunc = NULL;
// update current command id
I2C1_Buffer[TxBytes++] = command;
TxData[TxBytes++] = command;
 
// set pointers to data area with respect to the command id
switch (command)
193,7 → 193,7
case MK3MAG_CMD_WRITE_CAL:
RxBytes = sizeof(MK3MAG_ReadCal)+1;
pRxHandlerFunc = &MK3MAG_UpdateCalibration;
memcpy((u8*)I2C1_Buffer+1, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
memcpy(TxData+TxBytes, (u8*)&MK3MAG_WriteCal, sizeof(MK3MAG_WriteCal));
TxBytes += sizeof(MK3MAG_WriteCal);
break;
case MK3MAG_CMD_READ_MAGVECT:
206,10 → 206,10
break;
}
// update packet checksum
I2C1_Buffer[TxBytes] = MK3MAG_CalcCRC((u8*)I2C1_Buffer, TxBytes);
TxData[TxBytes] = MK3MAG_CalcCRC(TxData, TxBytes);
TxBytes++;
// initiate I2C transmission
I2C1_Transmission(MK3MAG_SLAVE_ADDRESS, TxBytes, pRxHandlerFunc, RxBytes);
I2CBus_Transmission(I2C1, MK3MAG_SLAVE_ADDRESS, TxData, TxBytes, pRxHandlerFunc, RxBytes);
} // EOF I2C_State == I2C_IDLE
}
 
217,10 → 217,19
//----------------------------------------------------------------
u8 MK3MAG_Init(void)
{
if(MK3MAG_Present) // do only short init ! , full init was called before
{
// try reconnect by reseting the I2C bus
I2CBus_Deinit(I2C1);
I2CBus_Init(I2C1);
}
else // full init
{
u8 msg[64];
u8 repeat;
u32 timeout;
Compass_I2CPort = I2C1;
MK3MAG_Present = 0;
 
MK3MAG_Version.Major = 0xFF;
228,14 → 237,12
MK3MAG_Version.Patch = 0xFF;
MK3MAG_Version.Compatible = 0xFF;
 
Compass_Heading = -1;
 
// polling of version info
repeat = 0;
do
{
MK3MAG_SendCommand(MK3MAG_CMD_VERSION);
if((Version_HW & 0x7f)> 11) timeout = SetDelay(100);
if((Version_HW & 0x7F) > 11) timeout = SetDelay(100);
else timeout = SetDelay(250);
 
do
261,6 → 268,7
MK3MAG_Present = 1;
}
}
}
return(MK3MAG_Present);
}
 
273,7 → 281,7
u8 msg[64];
u16 MinCaclibration = 500;
 
if( (I2C1_State == I2C_STATE_OFF) || !MK3MAG_Present ) return;
if( (I2CBus(I2C1)->State == I2C_STATE_UNDEF) || !MK3MAG_Present ) return;
if(CheckDelay(TimerUpdate))
{
/trunk/mkprotocol.c
54,10 → 54,10
// + Note: For information on license extensions (e.g. commercial use), please contact us at info(@)hisystems.de.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <stdarg.h>
#include <stdio.h>
#include "91x_lib.h"
#include "mkprotocol.h"
#include "ramfunc.h"
#include "usb.h"
#include "uart1.h"
#include "main.h"
 
/trunk/ncmag.c
55,12 → 55,11
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "91x_lib.h"
#include "ncmag.h"
#include "i2c1.h"
#include "i2c0.h"
 
#include "i2c.h"
#include "timer1.h"
#include "led.h"
#include "uart1.h"
72,16 → 71,7
u8 NCMAG_Present = 0;
u8 NCMAG_IsCalibrated = 0;
 
u8 I2C_CompassPort = 1;
u8 ExtCompassOrientation = 0;
 
u8 *I2C_BufferPnt;
u8 *I2C_ErrorPnt;
I2C_TransmissionFunc_t I2C_TransmissionFunc;
I2C_LockBufferFunc_t I2C_LockBufferFunc;
I2C_WaitForEndOfTransmissionFunc_t I2C_WaitForEndOfTransmissionFunc;
 
 
// supported magnetic sensor types
#define TYPE_NONE 0
#define TYPE_HMC5843 1
89,7 → 79,34
#define TYPE_LSM303DLM 3
 
u8 NCMAG_SensorType = TYPE_NONE;
u8 NCMAG_Orientation = 0; // 0 means unknown!
 
// two calibrtion sets for extern and intern sensor
#define EEPROM_ADR_MAG_CALIBRATION_INTERN 50
#define EEPROM_ADR_MAG_CALIBRATION_EXTERN 70
 
#define CALIBRATION_VERSION 1
#define MAG_CALIBRATION_COMPATIBLE 0xA2
 
#define NCMAG_MIN_RAWVALUE -2047
#define NCMAG_MAX_RAWVALUE 2047
#define NCMAG_INVALID_DATA -4096
 
typedef struct
{
s16 Range;
s16 Offset;
} __attribute__((packed)) Scaling_t;
 
typedef struct
{
Scaling_t MagX;
Scaling_t MagY;
Scaling_t MagZ;
u8 Version;
u8 crc;
} __attribute__((packed)) Calibration_t;
 
Calibration_t Calibration; // calibration data in RAM
volatile s16vec_t AccRawVector;
volatile s16vec_t MagRawVector;
208,10 → 225,6
 
volatile MagConfig_t MagConfig;
 
 
 
 
 
// self test value
#define LSM303DLH_TEST_XSCALE 495
#define LSM303DLH_TEST_YSCALE 495
286,20 → 299,26
 
volatile AccConfig_t AccConfig;
 
u8 NCMag_CalibrationWrite(u8 intern) // two calibrtion sets for extern and intern sensor
// write calibration data for external and internal sensor seperately
u8 NCMag_CalibrationWrite(I2C_TypeDef* I2Cx)
{
u16 address;
u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE;
EEPROM_Result_t eres;
u8 *pBuff = (u8*)&Calibration;
Calibration.Version = CALIBRATION_VERSION;
 
if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
else
if (I2Cx == NCMAG_PORT_EXTERN)
{
address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
Calibration.Version = CALIBRATION_VERSION + ExtCompassOrientation * 16;
Calibration.Version = CALIBRATION_VERSION + (NCMAG_Orientation<<4);;
}
else if (I2Cx == NCMAG_PORT_INTERN)
{
address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
Calibration.Version = CALIBRATION_VERSION;
}
else return(i);
 
for(i = 0; i<(sizeof(Calibration)-1); i++)
{
crc += pBuff[i];
311,14 → 330,16
return(i);
}
 
u8 NCMag_CalibrationRead(u8 intern) // two calibrtion sets for extern and intern sensor
// read calibration data for external and internal sensor seperately
u8 NCMag_CalibrationRead(I2C_TypeDef* I2Cx)
{
u8 i, crc = MAG_CALIBRATION_COMPATIBLE;
u8 address;
u8 i = 0, crc = MAG_CALIBRATION_COMPATIBLE;
u8 *pBuff = (u8*)&Calibration;
u16 address;
 
if(intern == I2C_INTERN_1) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
else address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
if (I2Cx == NCMAG_PORT_EXTERN) address = EEPROM_ADR_MAG_CALIBRATION_EXTERN;
else if (I2Cx == NCMAG_PORT_INTERN) address = EEPROM_ADR_MAG_CALIBRATION_INTERN;
else return(0);
 
if(EEPROM_SUCCESS == EEPROM_ReadBlock(address, pBuff, sizeof(Calibration)))
{
328,7 → 349,7
}
crc = ~crc;
if(Calibration.crc != crc) return(0); // crc mismatch
if((Calibration.Version & 0x0f) == CALIBRATION_VERSION) return(1);
if((Calibration.Version & 0x0F) == CALIBRATION_VERSION) return(1);
}
return(0);
}
338,7 → 359,7
{
u8 msg[64];
static u8 speak = 0;
static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0, Zmin2 = 0, Zmax2 = 0;
static s16 Xmin = 0, Xmax = 0, Ymin = 0, Ymax = 0, Zmin = 0, Zmax = 0, Zmin2 = 0, Zmax2 = 0;;
static s16 X = 0, Y = 0, Z = 0;
static u8 OldCalState = 0;
s16 MinCalibration = 450;
361,19 → 382,20
Zmax = -10000;
speak = 1;
CompassValueErrorCount = 0;
if(Compass_CalState != OldCalState) // avoid continously writing of eeprom!
if(Compass_CalState != OldCalState) // only once per state
{
UART1_PutString("\r\nStarting compass calibration");
if(I2C_CompassPort == I2C_EXTERN_0)
if(Compass_I2CPort == NCMAG_PORT_EXTERN)
{
if(!ExtCompassOrientation) ExtCompassOrientation = GetExtCompassOrientation();
if(!NCMAG_Orientation) NCMAG_Orientation = NCMAG_GetOrientationFromAcc();
UART1_PutString(" - External sensor ");
sprintf(msg, "with orientation: %d ",ExtCompassOrientation);
sprintf(msg, "with orientation: %d ", NCMAG_Orientation);
UART1_PutString(msg);
}
else UART1_PutString(" - Internal sensor ");
}
break;
 
case 2: // 2nd step of calibration
// find Min and Max of the X- and Y-Sensors during rotation in the horizontal plane
if(X < Xmin) { Xmin = X; BeepTime = 20;}
382,7 → 404,6
else if(Y > Ymax) { Ymax = Y; BeepTime = 60;}
if(Z < Zmin) { Zmin = Z; } // silent
else if(Z > Zmax) { Zmax = Z; }
 
if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
break;
 
395,12 → 416,10
// find Min and Max of the Z-Sensor
if(Z < Zmin2) { Zmin2 = Z; BeepTime = 80;}
else if(Z > Zmax2) { Zmax2 = Z; BeepTime = 80;}
if(X < Xmin) { Xmin = X; BeepTime = 20;}
else if(X > Xmax) { Xmax = X; BeepTime = 20;}
if(Y < Ymin) { Ymin = Y; BeepTime = 60;}
else if(Y > Ymax) { Ymax = Y; BeepTime = 60;}
 
if(speak) SpeakHoTT = SPEAK_CALIBRATE; speak = 0;
break;
429,9 → 448,8
}
else UART1_PutString("without GPS\n\r");
 
if(Zmin2 < Zmin) { Zmin = Zmin2; }
else if(Zmax2 > Zmax) { Zmax = Zmax2; }
 
if(Zmin2 < Zmin) Zmin = Zmin2;
if(Zmax2 > Zmax) Zmax = Zmax2;
Calibration.MagX.Range = Xmax - Xmin;
Calibration.MagX.Offset = (Xmin + Xmax) / 2;
Calibration.MagY.Range = Ymax - Ymin;
447,7 → 465,7
else
if((Calibration.MagX.Range > MinCalibration) && (Calibration.MagY.Range > MinCalibration) && (Calibration.MagZ.Range > MinCalibration))
{
NCMAG_IsCalibrated = NCMag_CalibrationWrite(I2C_CompassPort);
NCMAG_IsCalibrated = NCMag_CalibrationWrite(Compass_I2CPort);
BeepTime = 2500;
UART1_PutString("\r\n-> Calibration okay <-\n\r");
SpeakHoTT = SPEAK_MIKROKOPTER;
462,7 → 480,7
UART1_PutString("\r\n");
 
// restore old calibration data from eeprom
NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort);
}
sprintf(msg, "X: (%i - %i = %i)\r\n",Xmax,Xmin,Xmax - Xmin);
UART1_PutString(msg);
529,11 → 547,13
}
else if(CompassValueErrorCount < 35) CompassValueErrorCount++; // invalid data
 
switch(ExtCompassOrientation)
// correct compass orientation
switch(NCMAG_Orientation)
{
case 0:
case 1:
default:
// 1:1 Mapping
MagRawVector.X = X;
MagRawVector.Y = Y;
MagRawVector.Z = Z;
583,22 → 603,44
}
// rx data handler for acceleration raw data
void NCMAG_UpdateAccVector(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
{ // if number of bytes are matching
if(RxBufferSize == sizeof(AccRawVector) )
{
// copy from I2C buffer
memcpy((u8*)&AccRawVector, pRxBuffer,sizeof(AccRawVector));
// scale and update Acc Vector, at the moment simply 1:1
memcpy((u8*)&AccVector, (u8*)&AccRawVector,sizeof(AccRawVector));
}
}
// rx data handler for reading magnetic sensor configuration
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
if(RxBufferSize == sizeof(MagConfig) )
{
memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
}
}
// rx data handler for reading acceleration sensor configuration
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
if(RxBufferSize == sizeof(AccConfig) )
{
memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
}
}
//----------------------------------------------------------------------
 
u8 GetExtCompassOrientation(void)
u8 NCMAG_GetOrientationFromAcc(void)
{
if(I2C_CompassPort != I2C_EXTERN_0) return(0);
 
// only if external compass connected
if(Compass_I2CPort != NCMAG_PORT_EXTERN) return(0);
// MK must not be tilted
if((abs(FromFlightCtrl.AngleNick) > 300) || (abs(FromFlightCtrl.AngleRoll) > 300))
{
// UART1_PutString("\r\nTilted");
return(0);
}
// select orientation
if(AccRawVector.Z > 3300) return(1); // Flach - Bestückung oben - Pfeil nach vorn
else
if(AccRawVector.Z < -3300) return(2); // Flach - Bestückung unten - Pfeil nach vorn
613,41 → 655,25
return(0);
}
 
// rx data handler for reading magnetic sensor configuration
void NCMAG_UpdateMagConfig(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
if(RxBufferSize == sizeof(MagConfig) )
{
memcpy((u8*)(&MagConfig), pRxBuffer, sizeof(MagConfig));
}
}
// rx data handler for reading acceleration sensor configuration
void NCMAG_UpdateAccConfig(u8* pRxBuffer, u8 RxBufferSize)
{ // if number of byte are matching
if(RxBufferSize == sizeof(AccConfig) )
{
memcpy((u8*)&AccConfig, pRxBuffer, sizeof(AccConfig));
}
}
//----------------------------------------------------------------------
 
 
// ---------------------------------------------------------------------
u8 NCMAG_SetMagConfig(void)
{
u8 retval = 0;
 
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2CBus_LockBuffer(Compass_I2CPort, 100))
{
u8 TxBytes = 0;
I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&MagConfig, sizeof(MagConfig));
u8 TxData[sizeof(MagConfig) + 3];
 
TxData[TxBytes++] = REG_MAG_CRA;
memcpy(&TxData[TxBytes], (u8*)&MagConfig, sizeof(MagConfig));
TxBytes += sizeof(MagConfig);
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, 0, 0))
if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, 0, 0))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
659,15 → 685,16
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2CBus_LockBuffer(Compass_I2CPort, 100))
{
u8 TxBytes = 0;
I2C_BufferPnt[TxBytes++] = REG_MAG_CRA;
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
u8 TxData[3];
TxData[TxBytes++] = REG_MAG_CRA;
if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagConfig, sizeof(MagConfig)))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
678,18 → 705,19
u8 NCMAG_SetAccConfig(void)
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(50))
// try to catch the i2c buffer within 50 ms timeout
if(I2CBus_LockBuffer(Compass_I2CPort, 50))
{
u8 TxBytes = 0;
I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
memcpy((u8*)(&I2C_BufferPnt[TxBytes]), (u8*)&AccConfig, sizeof(AccConfig));
u8 TxData[sizeof(AccConfig) + 3];
TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
memcpy(&TxData[TxBytes], (u8*)&AccConfig, sizeof(AccConfig));
TxBytes += sizeof(AccConfig);
if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, 0, 0))
if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, 0, 0))
{
if(I2C_WaitForEndOfTransmissionFunc(50))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 50))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
701,15 → 729,16
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2CBus_LockBuffer(Compass_I2CPort, 100))
{
u8 TxBytes = 0;
I2C_BufferPnt[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
if(I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
u8 TxData[3];
TxData[TxBytes++] = REG_ACC_CTRL1|REG_ACC_MASK_AUTOINCREMENT;
if(I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccConfig, sizeof(AccConfig)))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
721,19 → 750,20
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2CBus_LockBuffer(Compass_I2CPort, 100))
{
u16 TxBytes = 0;
u8 TxBytes = 0;
u8 TxData[3];
NCMAG_Identification.A = 0xFF;
NCMAG_Identification.B = 0xFF;
NCMAG_Identification.C = 0xFF;
I2C_BufferPnt[TxBytes++] = REG_MAG_IDA;
TxData[TxBytes++] = REG_MAG_IDA;
// initiate transmission
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification, sizeof(NCMAG_Identification)))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
744,17 → 774,18
{
u8 retval = 0;
// try to catch the i2c buffer within 100 ms timeout
if(I2C_LockBufferFunc(100))
if(I2CBus_LockBuffer(Compass_I2CPort, 100))
{
u16 TxBytes = 0;
u8 TxBytes = 0;
u8 TxData[3];
NCMAG_Identification2.Sub = 0xFF;
I2C_BufferPnt[TxBytes++] = REG_MAG_IDF;
TxData[TxBytes++] = REG_MAG_IDF;
// initiate transmission
if(I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
if(I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateIdentification_Sub, sizeof(NCMAG_Identification2)))
{
if(I2C_WaitForEndOfTransmissionFunc(100))
if(I2CBus_WaitForEndOfTransmission(Compass_I2CPort, 100))
{
if(*I2C_ErrorPnt == I2C_ERROR_NONE) retval = 1;
if(I2CBus(Compass_I2CPort)->Error == I2C_ERROR_NONE) retval = 1;
}
}
}
763,16 → 794,17
 
 
// ----------------------------------------------------------------------------------------
void NCMAG_GetMagVector(void)
void NCMAG_GetMagVector(u8 timeout)
{
// try to catch the I2C buffer within 0 ms
if(I2C_LockBufferFunc(5))
// try to catch the I2C buffer within timeout ms
if(I2CBus_LockBuffer(Compass_I2CPort, timeout))
{
u16 TxBytes = 0;
u8 TxBytes = 0;
u8 TxData[3];
// set register pointer
I2C_BufferPnt[TxBytes++] = REG_MAG_DATAX_MSB;
TxData[TxBytes++] = REG_MAG_DATAX_MSB;
// initiate transmission
I2C_TransmissionFunc(MAG_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
I2CBus_Transmission(Compass_I2CPort, MAG_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateMagVector, sizeof(MagVector));
}
}
 
779,22 → 811,20
//----------------------------------------------------------------
void NCMAG_GetAccVector(u8 timeout)
{
// try to catch the I2C buffer within 0 ms
if(I2C_LockBufferFunc(timeout))
// try to catch the I2C buffer within timeout ms
if(I2CBus_LockBuffer(Compass_I2CPort, timeout))
{
u16 TxBytes = 0;
u8 TxBytes = 0;
u8 TxData[3];
// set register pointer
I2C_BufferPnt[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
TxData[TxBytes++] = REG_ACC_X_LSB|REG_ACC_MASK_AUTOINCREMENT;
// initiate transmission
I2C_TransmissionFunc(ACC_SLAVE_ADDRESS, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
//DebugOut.Analog[16] = AccRawVector.X;
//DebugOut.Analog[17] = AccRawVector.Y;
//DebugOut.Analog[18] = AccRawVector.Z;
I2CBus_Transmission(Compass_I2CPort, ACC_SLAVE_ADDRESS, TxData, TxBytes, &NCMAG_UpdateAccVector, sizeof(AccRawVector));
}
}
 
//----------------------------------------------------------------
u8 InitNC_MagnetSensor(void)
u8 NCMAG_ConfigureSensor(void)
{
u8 crb_gain, cra_rate;
 
838,10 → 868,10
static u32 TimerUpdate = 0;
static s8 send_config = 0;
u32 delay = 20;
 
if(init) TimerUpdate = SetDelay(10);
 
// todo State Handling for both busses !!
if((I2C1_State == I2C_STATE_OFF) || (I2C_CompassPort == 0 && I2C0_State == I2C_STATE_OFF)/* || !NCMAG_Present*/ )
if( (I2CBus(Compass_I2CPort)->State == I2C_STATE_UNDEF) /*|| !NCMAG_Present*/ )
{
Compass_Heading = -1;
DebugOut.Analog[14]++; // count I2C error
848,13 → 878,13
TimerUpdate = SetDelay(10);
return;
}
if(CheckDelay(TimerUpdate))// && I2C0_State == I2C_STATE_IDLE && I2C1_State == I2C_STATE_IDLE)
if(CheckDelay(TimerUpdate))
{
if(Compass_Heading != -1) send_config = 0; // no re-configuration if value is valid
if(++send_config == 25) // 500ms
{
send_config = -25; // next try after 1 second
InitNC_MagnetSensor();
NCMAG_ConfigureSensor();
TimerUpdate = SetDelay(20); // back into the old time-slot
}
else
868,23 → 898,25
switch(NCMAG_SensorType)
{
case TYPE_HMC5843:
NCMAG_GetMagVector();
delay = 20;
delay = 20; // next cycle after 20 ms
NCMAG_GetMagVector(5);
break;
case TYPE_LSM303DLH:
case TYPE_LSM303DLM:
delay = 20;
//delay = 2;
if(s-- || (I2C_CompassPort == I2C_INTERN_1)) NCMAG_GetMagVector();
else
 
if(s-- || (Compass_I2CPort == NCMAG_PORT_INTERN))
{
if(AccRawVector.X + AccRawVector.Y + AccRawVector.Z == 0) NCMAG_Init_ACCSensor();
delay = 20; // next cycle after 20 ms
NCMAG_GetMagVector(5);
}
else // having an external compass, read every 50th cycle the ACC vec
{ // try to initialize if no data are there
if((AccRawVector.X + AccRawVector.Y + AccRawVector.Z) == 0) NCMAG_Init_ACCSensor();
// get new data
NCMAG_GetAccVector(5);
delay = 10;
s = 40; // about 0,8 sec
};
if(!s) delay = 10; // ACC-Reading in the next step after 10ms
//if(!s) delay = 2; // ACC-Reading in the next step after 10ms
delay = 10; // next cycle after 10 ms
s = 40; //reset downconter about 0,8 sec
}
break;
}
if(send_config == 24) TimerUpdate = SetDelay(15); // next event is the re-configuration
929,8 → 961,8
 
case TYPE_LSM303DLM:
// does not support self test feature
done = retval;
return(retval);
done = 1;
return(1); // always return success
break;
 
default:
949,7 → 981,7
#define AVERAGE 20
for(i = 0; i<AVERAGE; i++)
{
NCMAG_GetMagVector();
NCMAG_GetMagVector(5);
time = SetDelay(20);
while(!CheckDelay(time));
XMax += MagRawVector.X;
965,7 → 997,7
// averaging
for(i = 0; i < AVERAGE; i++)
{
NCMAG_GetMagVector();
NCMAG_GetMagVector(5);
time = SetDelay(20);
while(!CheckDelay(time));
XMin += MagRawVector.X;
1007,90 → 1039,71
}
 
 
//----------------------------------------------------------------
void NCMAG_SelectI2CBus(u8 busno)
void NCMAG_CheckOrientation(void)
{ // only for external sensor
if(Compass_I2CPort == NCMAG_PORT_EXTERN)
{
if (busno == 0)
{
I2C_WaitForEndOfTransmissionFunc = &I2C0_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C0_LockBuffer;
I2C_TransmissionFunc = &I2C0_Transmission;
I2C_BufferPnt = I2C0_Buffer;
I2C_ErrorPnt = &I2C0_Error;
NCMAG_Orientation = NCMAG_GetOrientationFromAcc();
if(NCMAG_Orientation != (Calibration.Version>>4)) NCMAG_IsCalibrated = 0;
else NCMAG_IsCalibrated = 1;
}
else
{
I2C_WaitForEndOfTransmissionFunc = &I2C1_WaitForEndOfTransmission;
I2C_LockBufferFunc = &I2C1_LockBuffer;
I2C_TransmissionFunc = &I2C1_Transmission;
I2C_BufferPnt = I2C1_Buffer;
I2C_ErrorPnt = &I2C1_Error;
}
}
 
//----------------------------------------------------------------
u8 NCMAG_Init(void)
{
MagRawVector.X = 0;
MagRawVector.Y = 0;
MagRawVector.Z = 0;
AccRawVector.X = 0;
AccRawVector.Y = 0;
AccRawVector.Z = 0;
 
if(NCMAG_Present) // do only short init ! , full init was called before
{
// reset I2C Bus
I2CBus_Deinit(Compass_I2CPort);
I2CBus_Init(Compass_I2CPort);
// try to reconfigure senor
NCMAG_ConfigureSensor();
NCMAG_Update(1);
}
else // full init
{
u8 msg[64];
u8 retval = 0;
u8 repeat = 0;
 
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// Search external sensor
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
I2C_CompassPort = I2C_EXTERN_0;
NCMAG_SelectI2CBus(I2C_CompassPort);
 
//--------------------------------------------
// search external sensor first
//--------------------------------------------
Compass_I2CPort = NCMAG_PORT_EXTERN;
// get id bytes
retval = 0;
for(repeat = 0; repeat < 5; repeat++)
{
// retval = NCMAG_GetIdentification();
retval = NCMAG_GetAccConfig(); // only the sensor with ACC is supported
retval = NCMAG_GetAccConfig(); // only the external sensor with ACC is supported
if(retval) break; // break loop on success
UART1_PutString("_");
}
//retval = 1;
// Extenal sensor not found?
if(!retval)
{
// search internal sensor afterwards
UART1_PutString(" internal sensor ");
I2C_CompassPort = I2C_INTERN_1;
NCMAG_SelectI2CBus(I2C_CompassPort);
Compass_I2CPort = NCMAG_PORT_INTERN;
}
else
{
UART1_PutString(" external sensor ");
NCMAG_Init_ACCSensor();
 
for(repeat = 0; repeat < 100; repeat++)
{
NCMAG_GetAccVector(10); // only the sensor with ACC is supported
ExtCompassOrientation = GetExtCompassOrientation();
if(ExtCompassOrientation && (ExtCompassOrientation == Calibration.Version / 16)) break;
//UART1_Putchar('-');
Compass_I2CPort = NCMAG_PORT_EXTERN;
}
//DebugOut.Analog[19] = repeat;
//-------------------------------------------
 
if(!ExtCompassOrientation) UART1_PutString(" (Orientation unknown!)");
else
{
NCMag_CalibrationRead(I2C_CompassPort);
sprintf(msg, "with orientation: %d ",ExtCompassOrientation );
UART1_PutString(msg);
if(ExtCompassOrientation != Calibration.Version / 16)
{
sprintf(msg, "\n\r! Warning: calibrated orientation was %d !",Calibration.Version / 16);
UART1_PutString(msg);
}
else UART1_PutString("ok ");
}
 
}
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
NCMAG_Present = 0;
NCMAG_SensorType = TYPE_HMC5843; // assuming having an HMC5843
// polling for LSM302DLH/DLM option by ACC address ack
repeat = 0;
for(repeat = 0; repeat < 3; repeat++)
{
retval = NCMAG_GetAccConfig();
1103,6 → 1116,7
 
NCMAG_SensorType = TYPE_LSM303DLH;
// polling of sub identification
repeat = 0;
for(repeat = 0; repeat < 12; repeat++)
{
retval = NCMAG_GetIdentification_Sub();
1114,6 → 1128,7
}
}
// get id bytes
retval = 0;
for(repeat = 0; repeat < 3; repeat++)
{
retval = NCMAG_GetIdentification();
1151,10 → 1166,44
 
if(EEPROM_Init())
{
NCMAG_IsCalibrated = NCMag_CalibrationRead(I2C_CompassPort);
NCMAG_IsCalibrated = NCMag_CalibrationRead(Compass_I2CPort);
if(!NCMAG_IsCalibrated) UART1_PutString("\r\n Not calibrated!");
}
else UART1_PutString("\r\n EEPROM data not available!!!!!!!!!!!!!!!");
 
// in case of an external sensor, try to get the orientation by acc readings
if(Compass_I2CPort == NCMAG_PORT_EXTERN)
{
// try to get orientation by acc sensor values
for(repeat = 0; repeat < 100; repeat++)
{
NCMAG_GetAccVector(10); // only the sensor with ACC is supported
NCMAG_Orientation = NCMAG_GetOrientationFromAcc();
if(NCMAG_Orientation && (NCMAG_Orientation == Calibration.Version >> 4)) break;
}
// check orientation result if available
sprintf(msg, "\r\n Orientation: ");
UART1_PutString(msg);
if(NCMAG_Orientation)
{
sprintf(msg, "%d ", NCMAG_Orientation);
UART1_PutString(msg);
if(NCMAG_IsCalibrated) // check against calibration data orientation
{
if(NCMAG_Orientation != Calibration.Version >> 4)
{
sprintf(msg, "\n\r Warning: calibrated orientation was %d !",Calibration.Version >> 4);
UART1_PutString(msg);
}
}
}
else
{
UART1_PutString("unknown!");
}
}
 
 
// perform self test
if(!NCMAG_SelfTest())
{
1165,7 → 1214,7
else UART1_PutString("\r\n Selftest ok");
 
// initialize magnetic sensor configuration
InitNC_MagnetSensor();
NCMAG_ConfigureSensor();
}
else
{
1179,6 → 1228,7
NCMAG_SensorType = TYPE_NONE;
UART1_PutString("not found!");
}
}
return(NCMAG_Present);
}
 
/trunk/ncmag.h
2,56 → 2,19
#define __NCMAG_H
 
#include "compass.h"
#include "i2c1.h"
#include "i2c.h"
 
extern s16vec_t ACC_Vector;
extern volatile s16vec_t MagRawVector;
extern volatile s16vec_t AccRawVector;
#define NCMAG_PORT_EXTERN I2C0
#define NCMAG_PORT_INTERN I2C1
 
#define EEPROM_ADR_MAG_CALIBRATION_INTERN 50 // two calibrtion sets for extern and intern sensor
#define EEPROM_ADR_MAG_CALIBRATION_EXTERN 70
 
#define CALIBRATION_VERSION 1
#define MAG_CALIBRATION_COMPATIBLE 0xA2
 
#define NCMAG_MIN_RAWVALUE -2047
#define NCMAG_MAX_RAWVALUE 2047
#define NCMAG_INVALID_DATA -4096
 
typedef struct
{
s16 Range;
s16 Offset;
} __attribute__((packed)) Scaling_t;
 
typedef struct
{
Scaling_t MagX;
Scaling_t MagY;
Scaling_t MagZ;
u8 Version;
u8 crc;
} __attribute__((packed)) Calibration_t;
 
extern Calibration_t Calibration; // calibration data in RAM
 
#define I2C_EXTERN_0 0
#define I2C_INTERN_1 1
 
extern u8 NCMAG_Init(void);
extern void NCMAG_Update(u8);
extern u8 NCMAG_Present;
extern u8 NCMAG_IsCalibrated;
extern u8 I2C_CompassPort;
extern u8 ExtCompassOrientation;
extern u8 NCMAG_Orientation;
 
u8 NCMAG_Init(void);
void NCMAG_Update(u8 init);
u8 NCMAG_GetOrientationFromAcc(void);
void NCMAG_CheckOrientation(void);
 
extern u8 *I2C_BufferPnt;
extern u8 *I2C_ErrorPnt;
 
typedef u8 (*I2C_TransmissionFunc_t) (u8 SlaveAddr, u8 TxBytes, I2C_pRxHandler_t pRxHandler, u8 RxBytes);
typedef u8 (*I2C_LockBufferFunc_t) (u32 timeout);
typedef u8 (*I2C_WaitForEndOfTransmissionFunc_t) (u32 timeout);
 
#endif // __NCMAG_H
 
/trunk/settings.c
92,7 → 92,7
{PID_MIN_EVENT_TIME , "MIN_EVENT_TIME \0" ,"minimum time of the Waypoint-Event value (seconds) ", 1, 2, 2, 0, 600}, // in seconds
{PID_WP_ACCELERATE , "WAYPOINT DYNAMIC\0" ,"dynamic for flying waypoints in percent (0-200) ", 1, 100, 100, 0, 255}, // in percent or Poti
{PID_WP_WAIT_FOR_LED , "WAIT_FOR_OUT1 \0" ,"Wait on Waypoint until Out-Pattern is finished (1=on 0=off) ", 1, 1, 1, 0, 1},
{PID_SEND_NMEA , "NMEA_INTERVAL \0" ,"NMEA Output interval in ms (0 = disabled) ", 1, 0, 0, 0, 60000},
{PID_SEND_NMEA , "NMEA_INTERVAL \0" ,"NMEA Output interval in ms (0 = disabled) ", 1, 0, 0, 0, 60000}, // the log interval for NMEA output, 0 = off
{PID_CH_SPEED , "COMINGHOME_SPEED\0" ,"Maximum speed for coming home in 0,1m/sec (80 = 8,0 m/sec) ", 1, 80, 80, 10, 150},
{PID_GPS_AUTOCONFIG , "GPSAUTOCONFIG \0" ,"GPS configmode (0 = off, 1 = on) ", 1, 1, 1, 0, 1}
};
/trunk/spi_slave.c
56,6 → 56,7
 
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include "91x_lib.h"
#include "led.h"
#include "gps.h"
66,11 → 67,8
#include "timer2.h"
#include "config.h"
#include "main.h"
#include "compass.h"
#include "params.h"
#include "stdlib.h"
#include "settings.h"
#include "ncmag.h"
 
#define SPI_RXSYNCBYTE1 0xAA
#define SPI_RXSYNCBYTE2 0x83
126,7 → 124,6
s16 POI_KameraNick = 0;
u8 NC_Wait_for_LED = 0;
s16 GyroCompassCorrected = 0; // corrected with the magnetic declination
u8 FromFC_LowVoltageHomeActive = 0;
 
//--------------------------------------------------------------
void SSP0_IRQHandler(void)
592,8 → 589,7
if(--FCCalibActive == 0)
{
FC_is_Calibrated = 1;
ExtCompassOrientation = GetExtCompassOrientation();
if(ExtCompassOrientation != Calibration.Version / 16) NCMAG_IsCalibrated = 0;
Compass_Check();
}
}
}
615,6 → 611,7
HeadFreeStartAngle = GyroCompassCorrected; // in 0.1°
}
}
 
Parameter.ActiveSetting = FromFlightCtrl.Param.Byte[9];
FC.BAT_Voltage = FromFlightCtrl.Param.Byte[10];
DebugOut.Analog[7] = FC.BAT_Voltage;
674,7 → 671,7
if(FromFlightCtrl.Param.Byte[1]) FC.FromFC_SpeakHoTT = FromFlightCtrl.Param.Byte[1]; // will be cleared in the SD-Logging
Parameter.FromFC_LandingSpeed = FromFlightCtrl.Param.Byte[2];
Parameter.ComingHomeAltitude = FromFlightCtrl.Param.Byte[3];
FromFC_LowVoltageHomeActive = FromFlightCtrl.Param.Byte[4];
Parameter.FromFC_LowVoltageHomeActive = FromFlightCtrl.Param.Byte[4];
break;
case SPI_FCCMD_STICK:
FC.StickGas = FromFlightCtrl.Param.sByte[0];
744,13 → 741,13
if(FromFlightCtrl.Param.Byte[7] >= 68 && FromFlightCtrl.Param.Byte[7] <= 188)
{
FC.FromFC_DisableDeclination = 1;
FC.FromFC_CompassOffset = 10 * (signed char) ((unsigned char) FromFlightCtrl.Param.Byte[7] - 128);
FC.FromFC_CompassOffset = 10 * (s8) (FromFlightCtrl.Param.Byte[7] - 128);
GeoMagDec = 0;
}
else
{
FC.FromFC_DisableDeclination = 0;
FC.FromFC_CompassOffset = 10 * (signed char) FromFlightCtrl.Param.Byte[7];
FC.FromFC_CompassOffset = 10 * FromFlightCtrl.Param.sByte[7];
}
Parameter.GlobalConfig = FromFlightCtrl.Param.Byte[8];
Parameter.ExtraConfig = FromFlightCtrl.Param.Byte[9];
820,15 → 817,27
}while (!CheckDelay(timeout));
UART1_PutString(".");
repeat++;
// FCCalibActive = 1;
FCCalibActive = 1;
}while((FC_Version.Major == 0xFF) && (repeat < 40)); // 40*250ms = 10s
// if we got it
if (FC_Version.Major != 0xFF)
{
sprintf(msg, " FC V%d.%02d%c HW:%d.%d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
sprintf(msg, " FC V%d.%d%c HW:%d.%02d", FC_Version.Major, FC_Version.Minor, 'a'+FC_Version.Patch, FC_Version.Hardware/10,FC_Version.Hardware%10);
UART1_PutString(msg);
}
else UART1_PutString("\n\r not found!");
}
 
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55)
// +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
u16 BL3_Current(u8 who) // in 0,1A
{
if(Motor[who].Current <= 200) return((u16) Motor[who].Current);
else
{
if(Motor_Version[who] & MOTOR_STATE_BL30) return(200 + 10 * (u16) (Motor[who].Current - 200));
else return((u16) Motor[who].Current);
}
}
 
/trunk/spi_slave.h
74,11 → 74,7
extern u8 NC_Wait_for_LED;
extern s16 GyroCompassCorrected;
 
/*extern u8 MotorCurrent[12];
extern u8 MotorTemperature[12];
extern u8 MotorState[12];
extern u8 MotorMaxPwm[12];
*/
 
extern u8 BL_MinOfMaxPWM; // indication if all BL-controllers run on full power
extern u32 FC_I2C_ErrorConter;
extern u8 FromFC_VarioCharacter;
166,14 → 162,13
 
extern Motor_t Motor[12];
 
 
//Motor_Version[12]
#define MOTOR_STATE_NEW_PROTOCOL_MASK 0x01
#define MOTOR_STATE_FAST_MODE 0x02
#define MOTOR_STATE_BL30 0x04 // extended Current measurement -> 200 = 20A 201 = 21A 255 = 75A (20+55)
extern unsigned char Motor_Version[12]; // das kann nicht in die struct, weil der PC die Struktur bekommt
extern u8 Motor_Version[12]; // das kann nicht in die struct, weil der PC die Struktur bekommt
 
 
 
typedef struct
{
u8 Major;
189,10 → 184,10
extern SPI_Version_t FC_Version;
extern u8 GPS_Aid_StickMultiplikator;
extern u8 CompassCalState;
extern u8 FromFC_LowVoltageHomeActive;
 
void SPI0_Init(void);
void SPI0_GetFlightCtrlVersion(void);
void SPI0_UpdateBuffer(void);
u16 BL3_Current(u8 who); // in 0.1A
 
#endif //_SPI_SLAVE_H
/trunk/timer1.c
75,9 → 75,10
if(SD_WatchDog) SD_WatchDog--;
if(SPIWatchDog) SPIWatchDog--;
if(PollingTimeout) PollingTimeout--;
 
// generate SW Interrupt to make a regular timing
// independent from the mainloop at the lowest IRQ priority
VIC_SWITCmd(EXTIT3_ITLine, ENABLE); // Start this Interrupt
VIC_SWITCmd(EXTIT3_ITLine, ENABLE);
}
// IDISABLE;
/trunk/uart1.c
63,8 → 63,7
#include "config.h"
#include "menu.h"
#include "GPS.h"
#include "i2c0.h"
#include "i2c1.h"
#include "i2c.h"
#include "uart0.h"
#include "uart1.h"
#include "uart2.h"
250,8 → 249,6
UART_InitStructure.UART_BaudRate = UART1_BAUD_RATE;
UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
UART_InitStructure.UART_Mode = UART_Mode_Tx_Rx;
// AVOID_I2C_COLLISION UART_InitStructure.UART_FIFO = UART_FIFO_Disable;
// AVOID_I2C_COLLISION UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_8; // FIFO size 16 bytes, FIFO level 2 bytes
UART_InitStructure.UART_FIFO = UART_FIFO_Enable;
UART_InitStructure.UART_TxFIFOLevel = UART_FIFOLevel_1_2;
UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2; // FIFO size 16 bytes, FIFO level 8 bytes
277,7 → 274,7
UART_VersionInfo.SWPatch = VERSION_PATCH;
UART_VersionInfo.ProtoMajor = VERSION_SERIAL_MAJOR;
UART_VersionInfo.ProtoMinor = VERSION_SERIAL_MINOR;
UART_VersionInfo.HWMajor = Version_HW & 0x7f;
UART_VersionInfo.HWMajor = Version_HW & 0x7F;
UART_VersionInfo.reserved2 = 0;
UART_VersionInfo.Flags = 0;
NaviData.Version = NAVIDATA_VERSION;
655,7 → 652,6
{
u8 tmp_tx;
if(DebugUART != UART1) return;
// AVOID_I2C_COLLISION if(I2C0_State != I2C_STATE_IDLE) return;
// if something has to be send and the txd fifo is not full
if(UART1_tx_buffer.Locked == TRUE)
{
689,8 → 685,9
//http://www.kowoma.de/gps/zusatzerklaerungen/NMEA.htm
void CreateNmeaGGA(void)
{
unsigned char array[200], i = 0, crc = 0, x;
long tmp1, tmp2;
u8 array[200], i = 0, crc = 0, x;
s32 tmp1, tmp2;
 
i += sprintf(array, "$GPGGA,");
// +++++++++++++++++++++++++++++++++++++++++++
if(SystemTime.Valid)
705,15 → 702,15
if(GPSData.Flags & FLAG_GPSFIXOK)
{
tmp1 = abs(GPSData.Position.Latitude)/10000000L;
i += sprintf(&array[i],"%02d",(int)tmp1);
i += sprintf(&array[i],"%02d",(s16)tmp1);
 
tmp1 = abs(GPSData.Position.Latitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",(int)tmp2);
i += sprintf(&array[i],"%02d", (s16)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",(int)tmp2);
i += sprintf(&array[i],".%05d,", (s16)tmp2);
 
if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
else i += sprintf(&array[i],"S,");
720,15 → 717,15
// +++++++++++++++++++++++++++++++++++++++++++
 
tmp1 = abs(GPSData.Position.Longitude)/10000000L;
i += sprintf(&array[i],"%03d",(int)tmp1);
i += sprintf(&array[i],"%03d", (s16)tmp1);
 
tmp1 = abs(GPSData.Position.Longitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",(int)tmp2);
i += sprintf(&array[i],"%02d", (s16)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",(int)tmp2);
i += sprintf(&array[i],".%05d,", (s16)tmp2);
 
 
if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
735,10 → 732,10
else i += sprintf(&array[i],"W,");
i += sprintf(&array[i],"%d,",GPSData.SatFix);
i += sprintf(&array[i],"%d,",GPSData.NumOfSats);
i += sprintf(&array[i],"%d.%d,",GPSData.Position_Accuracy/100,abs(GPSData.Position_Accuracy%100));
i += sprintf(&array[i],"%d.%d,",(s16)(GPSData.Position_Accuracy/100),abs(GPSData.Position_Accuracy%100));
// i += sprintf(&array[i],"%d.%d,M,",GPSData.Position.Altitude/1000,abs(GPSData.Position.Altitude%1000)/100);
tmp1 = NaviData.Altimeter / 2; // in dm
i += sprintf(&array[i],"%d.%d,M,",tmp1 / 10,abs(tmp1 % 10));
i += sprintf(&array[i],"%d.%d,M,",(s16)tmp1 / 10,abs((s16)tmp1 % 10));
i += sprintf(&array[i],",,,*");
}
else
760,9 → 757,9
 
void CreateNmeaRMC(void)
{
unsigned char array[200], i = 0, crc = 0, x;
int tmp_int;
long tmp1, tmp2;
u8 array[200], i = 0, crc = 0, x;
s16 tmp_int;
s32 tmp1, tmp2;
// +++++++++++++++++++++++++++++++++++++++++++
i += sprintf(array, "$GPRMC,");
// +++++++++++++++++++++++++++++++++++++++++++
778,28 → 775,28
{
// +++++++++++++++++++++++++++++++++++++++++++
tmp1 = abs(GPSData.Position.Latitude)/10000000L;
i += sprintf(&array[i],"A,%02d",(int)tmp1); // Status: A = Okay V = Warnung
i += sprintf(&array[i],"A,%02d", (s16)tmp1); // Status: A = Okay V = Warnung
 
tmp1 = abs(GPSData.Position.Latitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",(int)tmp2);
i += sprintf(&array[i],"%02d", (s16)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",(int)tmp2);
i += sprintf(&array[i],".%05d,", (s16)tmp2);
if(GPSData.Position.Latitude >= 0) i += sprintf(&array[i],"N,");
else i += sprintf(&array[i],"S,");
// +++++++++++++++++++++++++++++++++++++++++++
tmp1 = abs(GPSData.Position.Longitude)/10000000L;
i += sprintf(&array[i],"%03d",(int)tmp1);
i += sprintf(&array[i],"%03d", (s16)tmp1);
 
tmp1 = abs(GPSData.Position.Longitude)%10000000L;
tmp1 *= 6; // in Minuten
tmp2 = tmp1 / 1000000L;
i += sprintf(&array[i],"%02d",(int)tmp2);
i += sprintf(&array[i],"%02d", (s16)tmp2);
tmp2 = tmp1 % 1000000L;
tmp2 /= 10; // eine Stelle zu viel
i += sprintf(&array[i],".%05d,",(int)tmp2);
i += sprintf(&array[i],".%05d,", (s16)tmp2);
if(GPSData.Position.Longitude >= 0) i += sprintf(&array[i],"E,");
else i += sprintf(&array[i],"W,");
// +++++++++++++++++++++++++++++++++++++++++++