Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 891 → Rev 892

/tags/V2.20h/mkprotocol.c
0,0 → 1,309
/*#######################################################################################*/
/* !!! THIS IS NOT FREE SOFTWARE !!! */
/*#######################################################################################*/
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + www.MikroKopter.com
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software Nutzungsbedingungen (english version: see below)
// + der Fa. HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland - nachfolgend Lizenzgeber genannt -
// + Der Lizenzgeber räumt dem Kunden ein nicht-ausschließliches, zeitlich und räumlich* unbeschränktes Recht ein, die im den
// + Mikrocontroller verwendete Firmware für die Hardware Flight-Ctrl, Navi-Ctrl, BL-Ctrl, MK3Mag & PC-Programm MikroKopter-Tool
// + - nachfolgend Software genannt - nur für private Zwecke zu nutzen.
// + Der Einsatz dieser Software ist nur auf oder mit Produkten des Lizenzgebers zulässig.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Die vom Lizenzgeber gelieferte Software ist urheberrechtlich geschützt. Alle Rechte an der Software sowie an sonstigen im
// + Rahmen der Vertragsanbahnung und Vertragsdurchführung überlassenen Unterlagen stehen im Verhältnis der Vertragspartner ausschließlich dem Lizenzgeber zu.
// + Die in der Software enthaltenen Copyright-Vermerke, Markenzeichen, andere Rechtsvorbehalte, Seriennummern sowie
// + sonstige der Programmidentifikation dienenden Merkmale dürfen vom Kunden nicht verändert oder unkenntlich gemacht werden.
// + Der Kunde trifft angemessene Vorkehrungen für den sicheren Einsatz der Software. Er wird die Software gründlich auf deren
// + Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Die Haftung des Lizenzgebers wird - soweit gesetzlich zulässig - begrenzt in Höhe des typischen und vorhersehbaren
// + Schadens. Die gesetzliche Haftung bei Personenschäden und nach dem Produkthaftungsgesetz bleibt unberührt. Dem Lizenzgeber steht jedoch der Einwand
// + des Mitverschuldens offen.
// + Der Kunde trifft angemessene Vorkehrungen für den Fall, dass die Software ganz oder teilweise nicht ordnungsgemäß arbeitet.
// + Er wird die Software gründlich auf deren Verwendbarkeit zu dem von ihm beabsichtigten Zweck testen, bevor er diese operativ einsetzt.
// + Der Kunde wird er seine Daten vor Einsatz der Software nach dem Stand der Technik sichern.
// + Der Kunde ist darüber unterrichtet, dass der Lizenzgeber seine Daten im zur Vertragsdurchführung erforderlichen Umfang
// + und auf Grundlage der Datenschutzvorschriften erhebt, speichert, verarbeitet und, sofern notwendig, an Dritte übermittelt.
// + *) Die räumliche Nutzung bezieht sich nur auf den Einsatzort, nicht auf die Reichweite der programmierten Software.
// + #### ENDE DER NUTZUNGSBEDINGUNGEN ####'
// + Hinweis: Informationen über erweiterte Nutzungsrechte (wie z.B. Nutzung für nicht-private Zwecke) sind auf Anfrage per Email an info(@)hisystems.de verfügbar.
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + Software LICENSING TERMS
// ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
// + of HiSystems GmbH, Flachsmeerstrasse 2, 26802 Moormerland, Germany - the Licensor -
// + The Licensor grants the customer a non-exclusive license to use the microcontroller firmware of the Flight-Ctrl, Navi-Ctrl, BL-Ctrl, and MK3Mag hardware
// + (the Software) exclusively for private purposes. The License is unrestricted with respect to time and territory*.
// + The Software may only be used with the Licensor's products.
// + The Software provided by the Licensor is protected by copyright. With respect to the relationship between the parties to this
// + agreement, all rights pertaining to the Software and other documents provided during the preparation and execution of this
// + agreement shall be the property of the Licensor.
// + The information contained in the Software copyright notices, trademarks, other legal reservations, serial numbers and other
// + features that can be used to identify the program may not be altered or defaced by the customer.
// + The customer shall be responsible for taking reasonable precautions
// + for the safe use of the Software. The customer shall test the Software thoroughly regarding its suitability for the
// + intended purpose before implementing it for actual operation. The Licensor's liability shall be limited to the extent of typical and
// + foreseeable damage to the extent permitted by law, notwithstanding statutory liability for bodily injury and product
// + liability. However, the Licensor shall be entitled to the defense of contributory negligence.
// + The customer will take adequate precautions in the case, that the software is not working properly. The customer will test
// + the software for his purpose before any operational usage. The customer will backup his data before using the software.
// + The customer understands that the Licensor collects, stores and processes, and, where required, forwards, customer data
// + to third parties to the extent necessary for executing the agreement, subject to applicable data protection and privacy regulations.
// + *) The territory aspect only refers to the place where the Software is used, not its programmed range.
// + #### END OF LICENSING TERMS ####
// + 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 "uart1.h"
#include "main.h"
#include "GPS.h"
 
/**************************************************************/
/* Create serial output frame */
/**************************************************************/
u8 AddSerialData(Buffer_t* pTxBuff, u8 *buffer, u16 size) //u8 *data, u8 len, ....
{
unsigned int i;
if(pTxBuff->Locked == TRUE) return(0);
// tx-buffer is not in use
// lock the buffer
pTxBuff->Locked = TRUE;
pTxBuff->Position = 0;
for(i=0; i<size; i++) pTxBuff->pData[pTxBuff->Position++] = buffer[i];
pTxBuff->DataBytes = pTxBuff->Position;
pTxBuff->Position = 0; // reset buffer position for transmision
return(pTxBuff->Locked);
}
 
/**************************************************************/
/* Create serial output frame */
/**************************************************************/
u16 MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...) //u8 *data, u8 len, ....
{
va_list ap;
 
u8 a,b,c;
u16 ptr = 0;
u16 tmpCRC = 0, i;
 
u8* pdata = NULL;
int len = 0;
if(pTxBuff->Locked == TRUE) return(0);
 
// tx-buffer is not in use
// lock the buffer
pTxBuff->Locked = TRUE;
pTxBuff->Position = 0;
pTxBuff->pData[pTxBuff->Position++] = '#'; // Start character
pTxBuff->pData[pTxBuff->Position++] = 'a' + Address; // Address (a=0; b=1,...)
pTxBuff->pData[pTxBuff->Position++] = CmdID; // Command
 
va_start(ap, numofbuffers);
if(numofbuffers)
{
pdata = va_arg(ap, u8*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
while(len)
{
if(len)
{
a = pdata[ptr++];
len--;
if((!len) && numofbuffers) // try to jump to next buffer
{
pdata = va_arg(ap, u8*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else a = 0;
if(len)
{
b = pdata[ptr++];
len--;
if((!len) && numofbuffers) // try to jump to next buffer
{
pdata = va_arg(ap, u8*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else b = 0;
if(len)
{
c = pdata[ptr++];
len--;
if((!len) && numofbuffers) // try to jump to next buffer
{
pdata = va_arg(ap, u8*);
len = va_arg(ap, int);
ptr = 0;
numofbuffers--;
}
}
else c = 0;
pTxBuff->pData[pTxBuff->Position++] = '=' + (a >> 2);
pTxBuff->pData[pTxBuff->Position++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4));
pTxBuff->pData[pTxBuff->Position++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
pTxBuff->pData[pTxBuff->Position++] = '=' + ( c & 0x3f);
}
va_end(ap);
// add crc
for(i = 0; i < pTxBuff->Position; i++)
{
tmpCRC += pTxBuff->pData[i];
}
tmpCRC %= 4096;
pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC / 64;
pTxBuff->pData[pTxBuff->Position++] = '=' + tmpCRC % 64;
pTxBuff->pData[pTxBuff->Position++] = '\r';
pTxBuff->DataBytes = pTxBuff->Position;
pTxBuff->Position = 0; // reset buffer position for transmision
// return(pTxBuff->Locked);
return(pTxBuff->DataBytes);
}
 
// typical called in an UART Rx ISR
/**************************************************************/
/* Collect serial frame */
/**************************************************************/
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
{
if(pRxBuff->Locked == FALSE)
{ // rx buffer not locked
if(c == '#') // if syncronisation character is received
{
pRxBuff->Position = 0; // reset buffer
pRxBuff->pData[pRxBuff->Position++] = c; // copy 1st byte to buffer
pRxBuff->DataBytes = 1;
}
else if (pRxBuff->Position < pRxBuff->Size) // rx buffer not full
{
pRxBuff->pData[pRxBuff->Position++] = c; // copy byte to rxd buffer
pRxBuff->DataBytes++;
// termination character received and sync has been established
if ((c == '\r') && (pRxBuff->pData[0]== '#'))
{
// calculate checksum from transmitted data
u16 crc = 0, i;
u8 crc1, crc2;
for(i = 0; i < (pRxBuff->Position-3); i++)
{
crc += pRxBuff->pData[i];
}
crc %= 4096;
crc1 = '=' + crc / 64;
crc2 = '=' + crc % 64;
// compare checksum to transmitted checksum bytes
if((crc1 == pRxBuff->pData[pRxBuff->Position-3]) && (crc2 == pRxBuff->pData[pRxBuff->Position-2]))
{
// checksum is valid
pRxBuff->Position = 0;
pRxBuff->Locked = TRUE; // lock the rxd buffer
// if 2nd byte is an 'R' start bootloader
if(pRxBuff->pData[2] == 'R' && !(FC.RealStatusFlags & FC_STATUS_MOTOR_RUN)) // not if the motors are running)
{
VIC_DeInit();
Execute_Bootloader(); // Reset-Commando - Bootloader starten
}
} // eof checksum valid
else
{ // checksum is invalid
Buffer_Clear(pRxBuff);
} // eof checksum invalid
} // eof termination character received
} // rxd buffer not full
else // rxd buffer overrun
{
Buffer_Clear(pRxBuff);
} // eof rxd buffer overrun
}
return(pRxBuff->Locked);
}
 
/**************************************************************/
/* Decode detination address */
/**************************************************************/
void MKProtocol_DecodeSerialFrameHeader(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
{
if(pRxBuff->Locked)
{
pSerialMsg->Address = pRxBuff->pData[1] - 'a';
pSerialMsg->CmdID = pRxBuff->pData[2];
}
else
{
pSerialMsg->Address = 0;
pSerialMsg->CmdID = ' ';
}
}
 
/**************************************************************/
/* Decode data */
/**************************************************************/
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
{
u8 a,b,c,d;
u16 ptrIn = 3; // start with first data byte in rx buffer
u16 ptrOut = 3;
u16 len = pRxBuff->DataBytes - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
 
len/=4; // number of 4 byte blocks
while(len--)
{
a = pRxBuff->pData[ptrIn++] - '=';
b = pRxBuff->pData[ptrIn++] - '=';
c = pRxBuff->pData[ptrIn++] - '=';
d = pRxBuff->pData[ptrIn++] - '=';
 
pRxBuff->pData[ptrOut++] = (a << 2) | (b >> 4);
pRxBuff->pData[ptrOut++] = ((b & 0x0f) << 4) | (c >> 2);
pRxBuff->pData[ptrOut++] = ((c & 0x03) << 6) | d;
}
pSerialMsg->pData = &(pRxBuff->pData[3]);
pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes
pRxBuff->Position = 0;
pRxBuff->DataBytes = ptrOut;
}
 
/*
void MKProtocol_DecodeSerialFrameData(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg)
{
u8 a,b,c,d;
u8 x,y,z;
u16 ptrIn = 3; // start with first data byte in rx buffer
u16 ptrOut = 3;
u16 len = pRxBuff->DataBytes - 6; // must be a multiple of 4 (3 bytes at begin and 3 bytes at end are no payload )
while(len)
{
a = pRxBuff->pData[ptrIn++] - '=';
b = pRxBuff->pData[ptrIn++] - '=';
c = pRxBuff->pData[ptrIn++] - '=';
d = pRxBuff->pData[ptrIn++] - '=';
//if(ptrIn > ReceivedBytes - 3) break;
 
x = (a << 2) | (b >> 4);
y = ((b & 0x0f) << 4) | (c >> 2);
z = ((c & 0x03) << 6) | d;
 
if(len--) pRxBuff->pData[ptrOut++] = x; else break;
if(len--) pRxBuff->pData[ptrOut++] = y; else break;
if(len--) pRxBuff->pData[ptrOut++] = z; else break;
}
pSerialMsg->pData = &(pRxBuff->pData[3]);
pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes
pRxBuff->Position = 0;
pRxBuff->DataBytes = ptrOut;
}
*/