/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Copyright (c) 2008 Ingo Busker, Holger Buss
// + Nur für den privaten Gebrauch
// + FOR NON COMMERCIAL USE ONLY
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Es gilt für das gesamte Projekt (Hardware, Software, Binärfiles, Sourcecode und Dokumentation),
// + dass eine Nutzung (auch auszugsweise) nur für den privaten (nicht-kommerziellen) Gebrauch zulässig ist.
// + Sollten direkte oder indirekte kommerzielle Absichten verfolgt werden, ist mit uns (info@mikrokopter.de) Kontakt
// + bzgl. der Nutzungsbedingungen aufzunehmen.
// + Eine kommerzielle Nutzung ist z.B.Verkauf von MikroKoptern, Bestückung und Verkauf von Platinen oder Bausätzen,
// + Verkauf von Luftbildaufnahmen, usw.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Werden Teile des Quellcodes (mit oder ohne Modifikation) weiterverwendet oder veröffentlicht,
// + unterliegen sie auch diesen Nutzungsbedingungen und diese Nutzungsbedingungen incl. Copyright müssen dann beiliegen
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Sollte die Software (auch auszugesweise) oder sonstige Informationen des MikroKopter-Projekts
// + auf anderen Webseiten oder sonstigen Medien veröffentlicht werden, muss unsere Webseite "http://www.mikrokopter.de"
// + eindeutig als Ursprung verlinkt werden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Keine Gewähr auf Fehlerfreiheit, Vollständigkeit oder Funktion
// + Benutzung auf eigene Gefahr
// + Wir übernehmen keinerlei Haftung für direkte oder indirekte Personen- oder Sachschäden
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die PORTIERUNG der Software (oder Teile davon) auf andere Systeme (ausser der Hardware von www.mikrokopter.de) ist nur
// + mit unserer Zustimmung zulässig
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Teile, die nicht eindeutig mit unserer Lizenzvereinbarung gekennzeichnet sind, unterliegen u.U. eigenen Vereinbarungen:
// + z.B. Die Funktion printf_P() unterliegt ihrer eigenen Lizenz
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Redistributions of source code (with or without modifications) must retain the above copyright notice,
// + this list of conditions and the following disclaimer.
// + * Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived
// + from this software without specific prior written permission.
// + * The use of this project (hardware, software, binary files, sources and documentation) is only permitted
// + for non-commercial use (directly or indirectly)
// + Commercial use (for excample: selling of MikroKopters, selling of PCBs, assembly, ...) is only permitted
// + with our written permission
// + * If sources or documentations are redistributet on other webpages, out webpage (http://www.MikroKopter.de) must be
// + clearly linked as origin
// + * PORTING this software (or part of it) to systems (other than hardware from www.mikrokopter.de) is NOT allowed
//
// + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// + POSSIBILITY OF SUCH DAMAGE.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
const unsigned long _Main_Crystal
= 25000;
#include "main.h"
#include "kml.h"
u32 TimerCompassUpdate
;
u32 TimerI2CReadDelay
;
u32 TimerDebugDataDelay
;
u32 KmlAddPointDelay
;
u8 BeepTime
;
void Init_Undef
(void);
u8 Parameter_UserParam1
;
u8 Parameter_UserParam2
;
u8 Parameter_UserParam3
;
u8 Parameter_UserParam4
;
u8 Parameter_UserParam5
;
u8 Parameter_UserParam6
;
u8 Parameter_UserParam7
;
u8 Parameter_UserParam8
;
//++++++++++++++++++++++++++++++++++++++++++++++++++
s32 FC_StickNick
;
s32 FC_StickRoll
;
s32 FC_StickGier
;
s32 FC_StickGas
;
s32 FC_Poti1
;
s32 FC_Poti2
;
s32 FC_Poti3
;
s32 FC_Poti4
;
//++++++++++++++++++++++++++++++++++++++++++++++++++
s32 SenderOkay
= 0;
s32 UBat
= 0;
s32 UndervoltageLevel
= 0;
s32 ActiveSetting
= 1;
//++++++++++++++++++++++++++++++++++++++++++++++++++
s32 Parameter_NaviGpsModeControl
= 128;
s32 Parameter_NaviGpsGain
= 0;
s32 Parameter_NaviGpsP
= 90;
s32 Parameter_NaviGpsI
= 0;
s32 Parameter_NaviGpsD
= 90;
s32 Parameter_NaviGpsACC
= 0;
s32 Parameter_NaviGpsMinSat
= 3;
s32 Parameter_NaviStickThreshold
= 8;
s32 Parameter_RadiusAlert
= 15;
//++++++++++++++++++++++++++++++++++++++++++++++++++
#define FLAG_MOTOR_RUN 1
#define FLAG_FLY 2
#define FLAG_CALIBRATE 4
#define FLAG_START 8
u32 MikroKopterFlags
;
u32 ClearFlags
= 1;
//++++++++++++++++++++++++++++++++++++++++++++++++++
u8 text
[20];
//----------------------------------------------------------------------------------------------------
void Leds_Init
(void)
{
GPIO_InitTypeDef GPIO_InitStructure
;
SCU_APBPeriphClockConfig
(__GPIO6
, ENABLE
);
SCU_APBPeriphClockConfig
(__GPIO5
, ENABLE
);
//GPIO_DeInit(GPIO5);
GPIO_InitStructure.
GPIO_Direction = GPIO_PinOutput
;
GPIO_InitStructure.
GPIO_Pin = GPIO_Pin_6
| GPIO_Pin_7
;
GPIO_InitStructure.
GPIO_Type = GPIO_Type_PushPull
;
GPIO_InitStructure.
GPIO_Alternate = GPIO_OutputAlt1
;
GPIO_Init
(GPIO5
, &GPIO_InitStructure
);
//SD_SWITCH
GPIO_InitStructure.
GPIO_Direction = GPIO_PinInput
;
GPIO_InitStructure.
GPIO_Pin = GPIO_Pin_3
;
GPIO_InitStructure.
GPIO_Type = GPIO_Type_PushPull
;
GPIO_InitStructure.
GPIO_Alternate = GPIO_InputAlt1
;
GPIO_Init
(GPIO5
, &GPIO_InitStructure
);
}
//
void OutputStartupData
(void)
{
char tex
[20];
SerialPutString
("\n\r--------------\n\r");
sprintf(text
,"NaviCtrl V%d.%d\n\r", VersionInfo.
Hauptversion, VersionInfo.
Nebenversion); SerialPutString
(text
);
}
//----------------------------------------------------------------------------------------------------
int main
(void)
{
static u8 oldCompassCalState
= 0;
static u8 kml_state
= 0;
u8 kml_cnt
= 0;
KML_Document_t mydocument
;
File
*file1
;
u8 str
[80];
KML_DocumentInit
(&mydocument
); // Initialise the new kml-document for further use.
// SCU_MCLKSourceConfig(SCU_MCLK_OSC);
// SCU_PCLKDivisorConfig(SCU_PCLK_Div2);
// SCU_MCLKSourceConfig(SCU_MCLK_PLL);
SCU_MCLKSourceConfig
(SCU_MCLK_OSC
);
SCU_PLLCmd
(DISABLE
); // PLL aus
SCU_RCLKDivisorConfig
(SCU_RCLK_Div1
);
SCU_HCLKDivisorConfig
(SCU_HCLK_Div1
);
SCU_PCLKDivisorConfig
(SCU_PCLK_Div2
);
// SCU_FMICLKDivisorConfig(SCU_FMICLK_Div1);
// FMI_Config(FMI_READ_WAIT_STATE_3,FMI_WRITE_WAIT_STATE_0, FMI_PWD_ENABLE, FMI_LVD_ENABLE,FMI_FREQ_HIGH);
SCU_PLLFactorsConfig
(192,25,3); // PLL = 48 MHz
SCU_PLLCmd
(ENABLE
); // PLL Enabled
SCU_MCLKSourceConfig
(SCU_MCLK_PLL
); // MCLK = PLL
VersionInfo.
Hauptversion = VERSION_HAUPTVERSION
;
VersionInfo.
Nebenversion = VERSION_NEBENVERSION
;
VersionInfo.
PCKompatibel = VERSION_KOMPATIBEL
;
SCU_AHBPeriphClockConfig
(__VIC
,ENABLE
);
SCU_AHBPeriphReset
(__VIC
,DISABLE
);
VIC_DeInit
();
InitInterrupt
();
Leds_Init
();
Debug_UART1_Init
();
OutputStartupData
();
GPS_UART0_Init
();
USB_ConfigInit
();
SPI0_Init
();
TIMER1_Init
();
I2C1_Init
();
I2C_Version.
Hauptversion = 0xff;
SendI2C_Command
(I2C_CMD_VERSION
);
TimerCompassUpdate
= SetDelay
(50);
while (!CheckDelay
(TimerCompassUpdate
));
TimerCompassUpdate
= SetDelay
(5);
TimerI2CReadDelay
= SetDelay
(5);
KmlAddPointDelay
= SetDelay
(250);
SerialPutString
("\r\n---------------------------------------------");
// ReadSetting(1);
SerialPutString
("\r\n---------------------------------------------\r\n\r\n");
InitFat16
();
for (;;)
{
if(NeuerDatensatzEmpfangen
)
{
BearbeiteRxDaten
();
DatenUebertragung
();
// if (GPIO_ReadBit(GPIO5, GPIO_Pin_3)) SerialPutString("keine SD\n\r"); else SerialPutString("SD eingesteckt!\n\r");
}
UpdateSPI_Buffer
();
SPI_CheckSlaveSelect
();
UART1_Transmit
();
// ----------- I2C Timing -------------------------
if (CheckDelay
(TimerCompassUpdate
))
{
if(oldCompassCalState
!= CompassCalState
)
{
oldCompassCalState
= CompassCalState
;
TimerCompassUpdate
= SetDelay
(25);
SendI2C_Command
(I2C_CMD_WRITE_CAL
);
}
else
{
GPIO_ToggleBit
(GPIO5
, GPIO_Pin_6
);
GPIO_ToggleBit
(GPIO5
, GPIO_Pin_7
);
TimerCompassUpdate
= SetDelay
(25);
SendI2C_Command
(I2C_CMD_READ_HEADING
);
}
}
if (CheckDelay
(TimerI2CReadDelay
))
{
if (I2C_ReadRequest
)
{
I2C_ReadRequest
= 0;
I2C_Direction
= I2C_MODE_RECEIVER
;
I2C_GenerateStart
(I2C1
, ENABLE
);
}
TimerI2CReadDelay
= SetDelay
(1000);
}
if (CheckDelay
(TimerDebugDataDelay
))
{ SendGPSPosAnforderung
= 1;
DatenUebertragung
();
TimerDebugDataDelay
= SetDelay
(500);
}
// ----------- KML Timing -------------------------
if(CheckDelay
(KmlAddPointDelay
) && 0)
{
KmlAddPointDelay
= SetDelay
(500);
switch(kml_state
)
{
case 0:
if((GPS_Data.
Flags & GPS_FIX
) && (GPS_Data.
Used_Sat >= GPS_SAT_MIN
))
{
if(str_Volume.
state == INITIALIZED
) // has the volume and filesystem been initialized?
{
if(KML_DocumentOpen
("flight01.kml",&mydocument
)) // create a new kml-document on the memorycard.
{
SerialPutString
("\r\nopening kml-file\r\nadding data");
kml_state
= 1;
}
}
else
{
InitFat16
();
if(str_Volume.
state != INITIALIZED
) KmlAddPointDelay
= SetDelay
(5000); // try again in 5 sek
}
}
break; // document.state will be changed to DOC_OPENED automatic.
case 1: // linestring opened, so datapoint (gps_coordinates) can be added to the document.
if(kml_cnt
++ < 20) // add 10 datapoints to the document.
{
KML_LoggGPSCoordinates
(GPS_Data
,&mydocument
);
}
else // after the datapoints have been written to the document close the document.
{
KML_DocumentClose
(&mydocument
);
SerialPutString
("\r\nfile closed");
kml_state
= 2;
}
break;
default: // all data has been written to card. open new document to add new data.
}
}
}
}