57,6 → 57,8 |
#include <avr/io.h> |
#include <avr/interrupt.h> |
#include <avr/wdt.h> |
#include <stdarg.h> |
#include <string.h> |
#include "main.h" |
#include "uart.h" |
#include "timer0.h" |
79,6 → 81,8 |
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN]; |
volatile uint8_t txd_complete = TRUE; |
volatile uint8_t ReceivedBytes = 0; |
volatile uint8_t *pRxData = 0; |
volatile uint8_t RxDataLen = 0; |
|
// send flags |
#define RQST_VERSION_INFO 0x01 |
90,6 → 94,7 |
|
uint8_t RequestFlags = 0x00; |
uint8_t RequestDebugLabel = 0; |
uint8_t ConfirmFrame = 0; |
|
uint8_t PC_Connected = 0; |
uint8_t FC_Connected = 0; |
100,9 → 105,10 |
DebugOut_t DebugOut; |
ExternData_t ExternData; |
ExternControl_t ExternControl; |
VersionInfo_t VersionInfo; |
UART_VersionInfo_t UART_VersionInfo; |
|
uint16_t Debug_Timer; |
uint16_t DebugData_Timer; |
uint16_t DebugData_Interval = 500; |
|
const uint8_t ANALOG_LABEL[32][16] = |
{ |
147,6 → 153,7 |
/****************************************************************/ |
void USART0_Init (void) |
{ |
uint8_t sreg = SREG; |
uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(8 * BAUD_RATE) - 1); |
|
// disable all interrupts before configuration |
202,13 → 209,22 |
// enable TX-Interrupt |
UCSR0B |= (1 << TXCIE0); |
|
// initialize the debug timer |
DebugData_Timer = SetDelay(DebugData_Interval); |
|
// unlock rxd_buffer |
rxd_buffer_locked = FALSE; |
pRxData = 0; |
RxDataLen = 0; |
|
// no bytes to send |
txd_complete = TRUE; |
|
|
VersionInfo.Major = VERSION_MAJOR; |
VersionInfo.Minor = VERSION_MINOR; |
VersionInfo.PCCompatible = VERSION_PCCOMPATIBLE; |
UART_VersionInfo.Major = VERSION_MAJOR; |
UART_VersionInfo.Minor = VERSION_MINOR; |
UART_VersionInfo.Patch = VERSION_PATCH; |
UART_VersionInfo.Compatible = VERSION_SERIAL_COMPATIBLE; |
|
MySlaveAddr = MK3MAG_ADDRESS; |
|
222,7 → 238,11 |
USART0_putchar ('.'); |
USART0_putchar (0x30 + VERSION_MINOR/10); |
USART0_putchar (0x30 + VERSION_MINOR%10); |
USART0_putchar ('a' + VERSION_PATCH); |
USART0_putchar ('\n'); |
|
// restore global interrupt flags |
SREG = sreg; |
} |
|
// --------------------------------------------------------------------------------- |
315,9 → 335,9 |
// compare checksum to transmitted checksum bytes |
if((crc1 == rxd_buffer[ptr_rxd_buffer-2]) && (crc2 == rxd_buffer[ptr_rxd_buffer-1])) |
{ // checksum valid |
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
ReceivedBytes = ptr_rxd_buffer + 1;// store number of received bytes |
rxd_buffer_locked = TRUE; // lock the rxd buffer |
ReceivedBytes = ptr_rxd_buffer; // store number of received bytes |
rxd_buffer[ptr_rxd_buffer] = '\r'; // set termination character |
// if 2nd byte is an 'R' enable watchdog that will result in an reset |
if(rxd_buffer[2] == 'R') {wdt_enable(WDTO_250MS);} // Reset-Commando |
} |
355,36 → 375,88 |
|
|
// -------------------------------------------------------------------------- |
void SendOutData(uint8_t cmd,uint8_t module, uint8_t *snd, uint8_t len) |
void SendOutData(uint8_t cmd,uint8_t module, uint8_t numofbuffers, ...) // uint8_t *pdata, uint8_t len, ... |
{ |
va_list ap; |
uint16_t pt = 0; |
uint8_t a,b,c; |
uint8_t ptr = 0; |
|
uint8_t *pdata = 0; |
int len = 0; |
|
txd_buffer[pt++] = '#'; // Start character |
txd_buffer[pt++] = module; // Address (a=0; b=1,...) |
txd_buffer[pt++] = cmd; // Command |
|
va_start(ap, numofbuffers); |
if(numofbuffers--) |
{ |
pdata = va_arg(ap, uint8_t*); |
len = va_arg(ap, int); |
ptr = 0; |
} |
|
while(len) |
{ |
if(len) { a = snd[ptr++]; len--;} else a = 0; |
if(len) { b = snd[ptr++]; len--;} else b = 0; |
if(len) { c = snd[ptr++]; len--;} else c = 0; |
if(len) |
{ |
a = pdata[ptr++]; |
len--; |
if((!len) && numofbuffers) |
{ |
pdata = va_arg(ap, uint8_t*); |
len = va_arg(ap, int); |
ptr = 0; |
numofbuffers--; |
} |
} |
else a = 0; |
if(len) |
{ |
b = pdata[ptr++]; |
len--; |
if((!len) && numofbuffers) |
{ |
pdata = va_arg(ap, uint8_t*); |
len = va_arg(ap, int); |
ptr = 0; |
numofbuffers--; |
} |
} |
else b = 0; |
if(len) |
{ |
c = pdata[ptr++]; |
len--; |
if((!len) && numofbuffers) |
{ |
pdata = va_arg(ap, uint8_t*); |
len = va_arg(ap, int); |
ptr = 0; |
numofbuffers--; |
} |
} |
else c = 0; |
txd_buffer[pt++] = '=' + (a >> 2); |
txd_buffer[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
txd_buffer[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
txd_buffer[pt++] = '=' + ( c & 0x3f); |
} |
va_end(ap); |
AddCRC(pt); // add checksum after data block and initates the transmission |
} |
|
|
// -------------------------------------------------------------------------- |
void Decode64(uint8_t *ptrOut, uint8_t len, uint8_t ptrIn, uint8_t max) |
void Decode64(void) |
{ |
uint8_t a,b,c,d; |
uint8_t ptr = 0; |
uint8_t x,y,z; |
uint8_t ptrIn = 3; |
uint8_t ptrOut = 3; |
uint8_t len = ReceivedBytes - 6; |
|
while(len) |
{ |
a = rxd_buffer[ptrIn++] - '='; |
391,19 → 463,22 |
b = rxd_buffer[ptrIn++] - '='; |
c = rxd_buffer[ptrIn++] - '='; |
d = rxd_buffer[ptrIn++] - '='; |
if(ptrIn > max - 2) break; |
//if(ptrIn > ReceivedBytes - 3) break; |
|
x = (a << 2) | (b >> 4); |
y = ((b & 0x0f) << 4) | (c >> 2); |
z = ((c & 0x03) << 6) | d; |
|
if(len--) ptrOut[ptr++] = x; else break; |
if(len--) ptrOut[ptr++] = y; else break; |
if(len--) ptrOut[ptr++] = z; else break; |
if(len--) rxd_buffer[ptrOut++] = x; else break; |
if(len--) rxd_buffer[ptrOut++] = y; else break; |
if(len--) rxd_buffer[ptrOut++] = z; else break; |
} |
pRxData = &rxd_buffer[3]; |
RxDataLen = ptrOut - 3; |
} |
|
|
|
// -------------------------------------------------------------------------- |
int16_t USART0_putchar (int8_t c) |
{ |
424,12 → 499,12 |
// if data in the rxd buffer are not locked immediately return |
if(!rxd_buffer_locked) return; |
|
uint8_t tmp_char_arr2[2]; // local char buffer |
Decode64(); // decode data block in rxd_buffer |
|
switch(rxd_buffer[2]) |
{ |
case 'w':// Attitude info from FC |
Decode64((uint8_t *) &ExternData, sizeof(ExternData), 3, ReceivedBytes); |
case 'k':// Attitude info from FC |
memcpy(&ExternData, (uint8_t*)pRxData, sizeof(ExternData)); |
RequestFlags |= RQST_COMPASS_HEADING; |
AttitudeSource = ATTITUDE_SOURCE_UART; |
Orientation = ExternData.Orientation; |
438,8 → 513,7 |
|
// used only for debug proposes at serial port |
case 'b': // extern control |
case 'c': // extern control with debug request |
Decode64((uint8_t *) &ExternControl,sizeof(ExternControl), 3,ReceivedBytes); |
memcpy(&ExternControl, (uint8_t*)pRxData, sizeof(ExternControl)); |
#define KEY1 0x01 |
#define KEY2 0x02 |
#define KEY3 0x04 |
451,28 → 525,22 |
ExternData.CalState++; |
if(ExternData.CalState == 6) ExternData.CalState = 0; |
} |
ConfirmFrame = ExternControl.Frame; |
PC_Connected = 255; |
break; |
|
// get debug values |
case 'f': |
RequestFlags |= RQST_DEBUG_DATA; |
PC_Connected = 255; |
case 'd': // request for the debug data |
DebugData_Interval = (uint16_t) pRxData[0] * 10; |
if(DebugData_Interval) RequestFlags |= RQST_DEBUG_DATA; |
break; |
|
// no reaction for display line request |
case 'h':// x-1 display columns |
PC_Connected = 255; |
break; |
|
case 'v': // get version and board release |
case 'v': // request version and board release |
RequestFlags |= RQST_VERSION_INFO; |
PC_Connected = 255; |
break; |
|
case 'a':// Labels of the Analog Debug outputs |
Decode64((uint8_t *) &tmp_char_arr2[0], sizeof(tmp_char_arr2), 3, ReceivedBytes); |
RequestDebugLabel = tmp_char_arr2[0]; |
RequestDebugLabel = pRxData[0]; |
RequestFlags |= RQST_DEBUG_LABEL; |
PC_Connected = 255; |
break; |
483,6 → 551,8 |
break; |
} |
// unlock the rxd buffer after processing |
pRxData = 0; |
RxDataLen = 0; |
rxd_buffer_locked = FALSE; |
} |
|
495,41 → 565,46 |
|
if(!txd_complete) return; |
|
if(CheckDelay(Debug_Timer) || (RequestFlags & RQST_DEBUG_DATA)) |
if((RequestFlags & RQST_DEBUG_LABEL) && txd_complete) |
{ |
SetDebugValues(); |
SendOutData('D',MySlaveAddr,(uint8_t *) &DebugOut,sizeof(DebugOut)); |
Debug_Timer = SetDelay(250); |
RequestFlags &= ~RQST_DEBUG_DATA; |
SendOutData('A', MySlaveAddr, 2, (uint8_t *)&RequestDebugLabel, sizeof(RequestDebugLabel), (uint8_t *) ANALOG_LABEL[RequestDebugLabel], 16); |
RequestDebugLabel = 0xFF; |
RequestFlags &= ~RQST_DEBUG_LABEL; |
} |
|
if(RequestFlags & RQST_DEBUG_LABEL) |
if(ConfirmFrame && txd_complete) |
{ |
SendOutData('A',RequestDebugLabel + '0',(uint8_t *) ANALOG_LABEL[RequestDebugLabel],16); |
RequestDebugLabel = 255; |
RequestFlags &= ~RQST_DEBUG_LABEL; |
Debug_Timer = SetDelay(500); |
SendOutData('B',MySlaveAddr, 1, (uint8_t *) &ConfirmFrame, sizeof(ConfirmFrame)); |
ConfirmFrame = 0; |
} |
|
if(RequestFlags & RQST_VERSION_INFO) |
if(( (DebugData_Interval && CheckDelay(DebugData_Timer)) || (RequestFlags & RQST_DEBUG_DATA)) && txd_complete) |
{ |
SendOutData('V',MySlaveAddr,(uint8_t *) &VersionInfo, sizeof(VersionInfo)); |
RequestFlags &= ~RQST_VERSION_INFO; |
SetDebugValues(); |
SendOutData('D', MySlaveAddr, 1, (uint8_t *) &DebugOut, sizeof(DebugOut)); |
DebugData_Timer = SetDelay(DebugData_Interval); |
RequestFlags &= ~RQST_DEBUG_DATA; |
} |
|
if(RequestFlags & RQST_EXTERN_CTRL) |
|
if((RequestFlags & RQST_EXTERN_CTRL) && txd_complete) |
{ |
SetDebugValues(); |
SendOutData('G',MySlaveAddr,(uint8_t *) &ExternControl,sizeof(ExternControl)); |
SendOutData('G',MySlaveAddr, 1, (uint8_t *) &ExternControl,sizeof(ExternControl)); |
RequestFlags &= ~RQST_EXTERN_CTRL; |
} |
|
if(RequestFlags & RQST_COMPASS_HEADING) |
if((RequestFlags & RQST_COMPASS_HEADING) && txd_complete) |
{ |
SendOutData('K',MySlaveAddr,(uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); |
SendOutData('K',MySlaveAddr, 1, (uint8_t *) &I2C_Heading, sizeof(I2C_Heading)); |
RequestFlags &= ~RQST_COMPASS_HEADING; |
} |
|
if((RequestFlags & RQST_VERSION_INFO) && txd_complete) |
{ |
SendOutData('V',MySlaveAddr, 1, (uint8_t *) &UART_VersionInfo, sizeof(UART_VersionInfo)); |
RequestFlags &= ~RQST_VERSION_INFO; |
} |
} |
|
|
void USART0_Print(int8_t *msg) |