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)); |