Subversion Repositories NaviCtrl

Compare Revisions

Ignore whitespace Rev 152 → Rev 153

/trunk/fifo.c
1,13 → 1,11
#include "fifo.h"
 
u8 fifo_init (fifo_t *f, u8 *buffer, const u8 size)
u8 fifo_init (fifo_t *f, u8 *buffer, const u16 size)
{
if(f == NULL) return(0);
f->buffer = buffer;
f->count = 0;
f->pread = buffer;
f->pwrite = buffer;
f->size = size;
f->size = size;
fifo_purge(f);
return(1);
}
 
43,3 → 41,12
 
return fifo_get(f, pdata);
}
 
void fifo_purge(fifo_t* f)
{
if((f == NULL)) return;
f->count = 0;
f->pread = f->buffer;
f->pwrite = f->buffer;
return;
}
/trunk/fifo.h
7,9 → 7,9
// the fifo object
typedef struct
{
u8 *buffer; // pointrer to start of the ringbuffer
u8 *buffer; // pointer to start of the ringbuffer
u8 count; // number of characters in FIFO
u8 size; // buffer size
u16 size; // buffer size
u8 *pread; // read pointer
u8 *pwrite; // write pointer
} fifo_t;
19,7 → 19,7
The FIFO uses the buffer 'buf' which byte length must 'size'.
Returns 1 on success ans 0 in case of an error.
*/
u8 fifo_init (fifo_t* f, u8* buf, const u8 size);
u8 fifo_init (fifo_t* f, u8* buf, const u16 size);
 
/*
Puts a byte into the FIFO. Returns 1 on success and 0 in case of FIFO overflow.
27,7 → 27,7
u8 fifo_put (fifo_t* f, const u8 data);
 
/*
Get the next byte from the FIFO as int. Returns 0 if the FIFO is empty.
Get the next byte from the FIFO. Returns 0 if the FIFO is empty.
*/
u8 fifo_get (fifo_t* f, u8* pdata);
 
37,9 → 37,9
*/
u8 fifo_get_wait (fifo_t* f, u8* pdata);
 
/*
Purges the FIFO so that it is empty afterwards
*/
void fifo_purge (fifo_t* f);
 
 
 
 
 
#endif /* _FIFO_H_ */
/trunk/main.c
80,7 → 80,7
u32 ErrorCode = 0;
u16 BeepTime;
u8 NCFlags = 0;
s32 GeoMag = 0;
s16 GeoMagDec = 0; // local magnetic declination in 0.1 deg
 
u8 ClearMKFlags = 0;
u8 StopNavigation = 0;
/trunk/main.h
35,7 → 35,7
extern u8 NCFlags;
extern u8 ClearMKFlags;
void Interrupt_Init(void);
extern s32 GeoMag;
extern s16 GeoMagDec;
 
 
typedef struct
/trunk/menu.c
316,20 → 316,22
break;
case 12: // Remote Control Level from FC
LCD_printfxy(0,0,"RC-Level: %3i", FC.RC_Quality);
LCD_printfxy(0,1,"Ubat: %3i.%1i V", FC.UBat/10, FC.UBat%10);
LCD_printfxy(0,1,"Ubat: %2i.%1i V", FC.UBat/10, FC.UBat%10);
LCD_printfxy(0,2,"CompHeading: %3i", I2C_Heading.Heading);
LCD_printfxy(0,3,"GyroHeading: %3i", FromFlightCtrl.GyroHeading/10);
if(GeoMagDec < 0) sign = '-';
else sign = '+';
LCD_printfxy(0,3,"GeoMagDec: %c%i.%1i", sign, abs(GeoMagDec)/10,abs(GeoMagDec)%10);
break;
case 13: // User Parameter
LCD_printfxy(0,0,"UP1:%3i UP2:%3i ",Parameter.User1,Parameter.User2);
LCD_printfxy(0,1,"UP3:%3i UP4:%3i ",Parameter.User3,Parameter.User4);
LCD_printfxy(0,2,"UP5:%3i UP6:%3i ",Parameter.User5,Parameter.User6);
LCD_printfxy(0,3,"UP7:%3i UP8:%3i ",Parameter.User7,Parameter.User8);
LCD_printfxy(0,0,"UP1:%3i UP2:%3i",Parameter.User1,Parameter.User2);
LCD_printfxy(0,1,"UP3:%3i UP4:%3i",Parameter.User3,Parameter.User4);
LCD_printfxy(0,2,"UP5:%3i UP6:%3i",Parameter.User5,Parameter.User6);
LCD_printfxy(0,3,"UP7:%3i UP8:%3i",Parameter.User7,Parameter.User8);
break;
case 14: // MK3MAG
I2C1_SendCommand(I2C_CMD_READ_MAG);
LCD_printfxy(0,0,"MK3MAG V%i.%i%c",MK3MAG_Version.Major, MK3MAG_Version.Minor, 'a'+ MK3MAG_Version.Patch);
LCD_printfxy(0,1,"MagX: %4i GEO:%4i",I2C_Mag.MagX, GeoMag);
LCD_printfxy(0,1,"MagX: %4i ",I2C_Mag.MagX);
LCD_printfxy(0,2,"MagY: %4i ",I2C_Mag.MagY);
LCD_printfxy(0,3,"MagZ: %4i ",I2C_Mag.MagZ);
break;
/trunk/mkprotocol.c
171,7 → 171,7
/**************************************************************/
/* Collect serial frame */
/**************************************************************/
void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c)
{
if(pRxBuff->Locked == FALSE)
{ // rx buffer not locked
226,6 → 226,7
Buffer_Clear(pRxBuff);
} // eof rxd buffer overrrun
}
return(pRxBuff->Locked);
}
 
/**************************************************************/
254,8 → 255,9
if(len--) pRxBuff->pData[ptrOut++] = y; else break;
if(len--) pRxBuff->pData[ptrOut++] = z; else break;
}
pSerialMsg->Address = pRxBuff->pData[1] - 'a';
pSerialMsg->CmdID = pRxBuff->pData[2];
pRxBuff->pData[1] -= 'a'; // substract address offset
pSerialMsg->pAddress = &(pRxBuff->pData[1]);
pSerialMsg->pCmdID = &(pRxBuff->pData[2]);
pSerialMsg->pData = &(pRxBuff->pData[3]);
pSerialMsg->DataLen = ptrOut - 3; // return number of data in bytes
pRxBuff->Position = 0;
/trunk/mkprotocol.h
21,14 → 21,14
 
typedef struct
{
u8 Address;
u8 CmdID;
u8* pAddress;
u8* pCmdID;
u8* pData;
u16 DataLen;
} __attribute__((packed)) SerialMsg_t;
 
extern u8 MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c);
extern void MKProtocol_CreateSerialFrame(Buffer_t* pTxBuff, u8 CmdID, u8 Address, u8 numofbuffers , ...); //u8 *data, u8 len, ....;
extern void MKProtocol_CollectSerialFrame(Buffer_t* pRxBuff, u8 c);
extern void MKProtocol_DecodeSerialFrame(Buffer_t* pRxBuff, SerialMsg_t* pSerialMsg);
 
#endif // _MKPROTOCOL_H
/trunk/uart0.c
306,10 → 306,10
MKProtocol_DecodeSerialFrame(&UART0_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
 
switch(SerialMsg.Address) // check for Slave Address
switch(*SerialMsg.pAddress) // check for Slave Address
{
case MKOSD_ADDRESS: // answers from the MKOSD
switch(SerialMsg.CmdID)
switch(*SerialMsg.pCmdID)
{
case 'V':
memcpy(&MKOSD_VersionInfo, SerialMsg.pData, sizeof(MKOSD_VersionInfo)); // copy echo pattern
320,7 → 320,7
break;
 
case NC_ADDRESS: // own Slave Address
switch(SerialMsg.CmdID)
switch(*SerialMsg.pCmdID)
{
case 'e': // request for the text of the error status
UART0_Request_ErrorMessage = TRUE;
335,7 → 335,7
// "break;" is missing here to fall thru to the common commands
 
default: // and any other Slave Address
switch(SerialMsg.CmdID) // check CmdID
switch(*SerialMsg.pCmdID) // check CmdID
{
case 'v': // request for version info
UART0_Request_VersionInfo = TRUE;
/trunk/uart1.c
73,6 → 73,7
#include "main.h"
#include "waypoints.h"
#include "mkprotocol.h"
#include "fifo.h"
 
#define FALSE 0
#define TRUE 1
96,10 → 97,10
 
UART_TypeDef *DebugUART = UART1;
 
// the tx buffer
#define UART1_TX_BUFFER_LEN 150
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
Buffer_t UART1_tx_buffer;
// the primary rx fifo
#define UART1_RX_FIFO_LEN 512
u8 UART1_rxfifobuffer[UART1_RX_FIFO_LEN];
fifo_t UART1_rx_fifo;
 
// the rx buffer
#define UART1_RX_BUFFER_LEN 150
106,6 → 107,13
u8 UART1_rbuffer[UART1_RX_BUFFER_LEN];
Buffer_t UART1_rx_buffer;
 
// the tx buffer
#define UART1_TX_BUFFER_LEN 150
u8 UART1_tbuffer[UART1_TX_BUFFER_LEN];
Buffer_t UART1_tx_buffer;
 
 
 
volatile u8 SerialLinkOkay = 0;
 
u8 text[200];
170,6 → 178,15
GPIO_InitTypeDef GPIO_InitStructure;
UART_InitTypeDef UART_InitStructure;
 
// initialize txd buffer
Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
// initialize rxd buffer
Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
 
// initialize the rx fifo
fifo_init(&UART1_rx_fifo, UART1_rxfifobuffer, UART1_RX_FIFO_LEN);
 
SCU_APBPeriphClockConfig(__UART1, ENABLE); // Enable the UART1 Clock
SCU_APBPeriphClockConfig(__GPIO3, ENABLE); // Enable the GPIO3 Clock
 
211,23 → 228,18
UART_InitStructure.UART_RxFIFOLevel = UART_FIFOLevel_1_2;
 
UART_DeInit(UART1); // reset uart 1 to default
UART_Init(UART1, &UART_InitStructure); // initialize uart 1
UART_Init(UART1, &UART_InitStructure); // initialize uart 1
// enable uart 1 interrupts selective
UART_ITConfig(UART1, UART_IT_Receive | UART_IT_ReceiveTimeOut, ENABLE);
UART_Cmd(UART1, ENABLE); // enable uart 1
// configure the uart 1 interupt line
VIC_Config(UART1_ITLine, VIC_IRQ, PRIORITY_UART1);
// enable the uart 1 IRQ
// enable the uart 1 IRQ
VIC_ITCmd(UART1_ITLine, ENABLE);
 
// initialize the debug timer
UART1_DebugData_Timer = SetDelay(UART1_DebugData_Interval);
UART1_NaviData_Timer = SetDelay(UART1_NaviData_Interval)+500;
// initialize txd buffer
Buffer_Init(&UART1_tx_buffer, UART1_tbuffer, UART1_TX_BUFFER_LEN);
// initialize rxd buffer
Buffer_Init(&UART1_rx_buffer, UART1_rbuffer, UART1_RX_BUFFER_LEN);
 
// Fill Version Info Structure
UART_VersionInfo.SWMajor = VERSION_MAJOR;
252,7 → 264,7
 
if((UART_GetITStatus(UART1, UART_IT_Receive) != RESET) || (UART_GetITStatus(UART1, UART_IT_ReceiveTimeOut) != RESET) )
{
// clear the pending bits
// clear the pending bits!
UART_ClearITPendingBit(UART1, UART_IT_Receive);
UART_ClearITPendingBit(UART1, UART_IT_ReceiveTimeOut);
// if debug UART is not UART1
288,6 → 300,7
{
UART0_Connect_to_MKGPS();
TIMER2_Init(); // enbable servo outputs
fifo_purge(&UART1_rx_fifo); // flush the whole fifo init buffer
}
DebugUART = UART1;
}
308,9 → 321,13
{
while(UART_GetFlagStatus(UART1, UART_FLAG_RxFIFOEmpty) != SET)
{ // some byes in the fifo and rxd buffer not locked
// get byte from fifo
// get byte from hardware fifo
c = UART_ReceiveData(UART1);
MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c);
// put into the software fifo
if(!fifo_put(&UART1_rx_fifo, c))
{ // fifo overflow
fifo_purge(&UART1_rx_fifo); // flush the whole buffer
}
} // some byes in the fifo and rxd buffer not locked
} // eof DebugUart = UART1
}
321,17 → 338,28
/**************************************************************/
void UART1_ProcessRxData(void)
{
SerialMsg_t SerialMsg;
u8 c;
 
// return on forwarding uart
if(DebugUART != UART1) return;
// collect data from primary rx fifo
while(fifo_get(&UART1_rx_fifo, &c))
{ // break if complete frame is collected
if(MKProtocol_CollectSerialFrame(&UART1_rx_buffer, c)) break;
}
// if data in the rxd buffer are not locked immediately return
if((UART1_rx_buffer.Locked == FALSE) || (DebugUART != UART1) ) return;
if(UART1_rx_buffer.Locked == FALSE) return;
Waypoint_t * pWaypoint = NULL;
SerialMsg_t SerialMsg;
 
MKProtocol_DecodeSerialFrame(&UART1_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping"
switch(SerialMsg.Address) // check for Slave Address
if(*(SerialMsg.pCmdID) != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping"
switch(*(SerialMsg.pAddress)) // check for Slave Address
{
case NC_ADDRESS: // own Slave Address
switch(SerialMsg.CmdID)
switch(*(SerialMsg.pCmdID))
{
case 'z': // connection checker
memcpy(&Echo, SerialMsg.pData, sizeof(Echo)); // copy echo pattern
358,14 → 386,14
{
case UART_FLIGHTCTRL:
UART2_Init(); // initialize UART2 to FC pins
//TIMER2_Deinit(); // stop servo output
fifo_purge(&UART1_rx_fifo);
DebugUART = UART2;
break;
case UART_MK3MAG:
if(FC.MKFlags & MKFLAG_MOTOR_RUN) break; // not if the motors are running
UART0_Connect_to_MK3MAG(); // mux UART0 to MK3MAG pins
//TIMER2_Deinit(); // stop servo output
GPSData.Status = INVALID;
fifo_purge(&UART1_rx_fifo);
DebugUART = UART0;
break;
case UART_MKGPS:
373,8 → 401,11
TIMER2_Deinit();
UART0_Connect_to_MKGPS(); // connect UART0 to MKGPS pins
GPSData.Status = INVALID;
fifo_purge(&UART1_rx_fifo);
DebugUART = UART0;
break;
default:
break;
}
break;
 
407,7 → 438,7
 
default: // and any other Slave Address
 
switch(SerialMsg.CmdID) // check CmdID
switch(*(SerialMsg.pCmdID)) // check CmdID
{
case 'a':// request for the labels of the analog debug outputs
UART1_Request_DebugLabel = SerialMsg.pData[0];
458,7 → 489,7
}
break; // default:
}
Buffer_Clear(&UART1_rx_buffer);
Buffer_Clear(&UART1_rx_buffer); // free rc buffer for next frame
}
 
 
529,16 → 560,6
Echo = 0; // reset echo value
UART1_Request_Echo = FALSE;
}
else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
{
MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
UART1_Request_DebugLabel = 0xFF;
}
else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
{
MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
UART1_ConfirmFrame = 0;
}
else if(( ((UART1_NaviData_Interval > 0) && CheckDelay(UART1_NaviData_Timer) ) || UART1_Request_NaviData) && (UART1_tx_buffer.Locked == FALSE))
{
NaviData.Errorcode = ErrorCode;
558,7 → 579,16
UART1_Data3D_Timer = SetDelay(UART1_Data3D_Interval);
UART1_Request_Data3D = FALSE;
}
 
else if((UART1_Request_DebugLabel != 0xFF) && (UART1_tx_buffer.Locked == FALSE))
{
MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'A', NC_ADDRESS, 2, &UART1_Request_DebugLabel, sizeof(UART1_Request_DebugLabel), (u8 *) ANALOG_LABEL[UART1_Request_DebugLabel], 16);
UART1_Request_DebugLabel = 0xFF;
}
else if(UART1_ConfirmFrame && (UART1_tx_buffer.Locked == FALSE))
{
MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'B', NC_ADDRESS, 1, &UART1_ConfirmFrame, sizeof(UART1_ConfirmFrame));
UART1_ConfirmFrame = 0;
}
else if(UART1_Request_ExternalControl && (UART1_tx_buffer.Locked == FALSE))
{
MKProtocol_CreateSerialFrame(&UART1_tx_buffer, 'G', NC_ADDRESS, 1, (u8 *)&ExternControl, sizeof(ExternControl));
/trunk/usb.c
165,11 → 165,11
 
MKProtocol_DecodeSerialFrame(&USB_rx_buffer, &SerialMsg); // decode serial frame in rxd buffer
 
if(SerialMsg.CmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping"
switch(SerialMsg.Address) // check for Slave Address
if(*SerialMsg.pCmdID != 'z') SerialLinkOkay = 250; // reset SerialTimeout, but not in case of the "ping"
switch(*SerialMsg.pAddress) // check for Slave Address
{
case NC_ADDRESS: // own Slave Address
switch(SerialMsg.CmdID)
switch(*SerialMsg.pCmdID)
{
case 'e': // request for the text of the error status
USB_Request_ErrorMessage = TRUE;
237,7 → 237,7
 
default: // and any other Slave Address
 
switch(SerialMsg.CmdID) // check CmdID
switch(*SerialMsg.pCmdID) // check CmdID
{
case 'a':// request for the labels of the analog debug outputs
USB_Request_DebugLabel = SerialMsg.pData[0];