Go to most recent revision |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
Copyright (c) 2008 Stefan Engelke <stefan@tinkerer.eu>
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use, copy,
modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
$Id: rcdsl.c 62 2008-08-21 16:09:07Z tensor $
RCDSL.H and RCDSL.C is an INOFFICIAL implementation of the
communication protocol used by DSL receivers of Act Europe.
The DSL receivers have a serial communication port to connect
two receivers in diversity mode. Each receiver is sending the
received servo signals periodically over this port. This fact
can be used to connect the receiver to the control unit of the
model via UART instead of evaluating the PPM signal.
The UART parameters are
38400 Baud, 8-N-1
If you have any questions, feel free to send me an e-mail.
*/
#include "rcdsl.h"
#include "main.h"
volatile unsigned char data_counter = 0;
volatile unsigned char last_byte = 0;
unsigned char check_sum = 0;
unsigned char paket_len = 0;
#define UART1_BAUDRATE 38400
volatile uint8_t rcdsl_RSSI = 0;
uint8_t rcdsl_battery=0;
uint8_t rcdsl_allocation=0;
uint8_t data[6];
typedef union {
uint16_t pos[2];
uint8_t dat[4];
} servos_t;
void rcdsl_init(void) {
// Save state of status register and disable interrupts
uint8_t sreg = SREG;
cli();
// Set baud rate
UBRR1 = (uint16_t) ((uint32_t) F_CPU/(16*UART1_BAUDRATE) - 1);
// UART Receiver und Transmitter anschalten
// Data mode 8N1, asynchron
UCSR1B = (1 << RXEN1) | (1 << TXEN0) | (1 << RXCIE1);
UCSR1C = (1 << UCSZ11) | (1 << UCSZ10);
// Flush Receive-Buffer
do { UDR1; }
while (UCSR1A & (1 << RXC1));
// Clear rx and tx interrupt flags
UCSR1A = (1 << RXC1) | (1 << TXC1);
// Reactive interrupts, if they were active before
SREG = sreg;
}
void rcdsl_new_signal(uint8_t channel, int16_t signal)
// This function is called, when a new servo signal is properly received.
// Parameters: servo - servo number (0-9)
// signal - servo signal between 7373 (1ms) and 14745 (2ms)
{
volatile signed int tmp;
uint8_t index = channel+1; // Der MK fängt bei 1 an zu zählen
// Signal vom ACTDSL-Empfänger liegt zwischen
// 7373 (1ms) und 14745 (2ms).
signal-= 11059; // Neutralstellung zur Null verschieben
signal/= 24; // Auf MK Skalierung umrechnen
// Stabiles Signal
//if(abs(signal - PPM_in[index]) < 6) { if(SenderOkay < 200) SenderOkay += 10;}
tmp = (3 * (PPM_in[index]) + signal) / 4;
if(tmp > signal+1) tmp--; else
if(tmp < signal-1) tmp++;
if(SenderOkay >= 105) PPM_diff[index] = ((tmp - PPM_in[index]) / 3) * 3;
else PPM_diff[index] = 0;
PPM_in[index] = tmp;
//NewPpmData = 0;
//NewActData = 1;
if(index == 4) {
//NewActData = 1;
NewPpmData = 0;
//PORTC = (PORTC&(~(1<<PC4))) | ((~PORTC)&(1<<PC4));
//PINC = (1<<PC4);
}
//PPM_in[index] = signal;
}
void rcdsl_incoming_paket(void)
// This function is called within rcdsl_parse_data(), when a complete
// data paket with matching checksum has been received.
{
uint8_t i;
static servos_t servos;
// Look for status headers
if ((data[0])==0x1F) {
// Get frequency allocation
rcdsl_allocation = data[0+1];
// Get signal quality
rcdsl_RSSI = data[2+1];
// Get voltage of battery supply
rcdsl_battery = data[3+1];
}
// Look for signal headers
if ((data[0]&0xF0)==0x10) {
i = data[0]&0x0F; // Last 4 bits of the header indicates servo pair
if (i<10) {
// Convert byte array to two uint16
servos.dat[1] = data[1];
servos.dat[0] = data[2];
servos.dat[3] = data[3];
servos.dat[2] = data[4];
rcdsl_new_signal(i , (int16_t)servos.pos[0]);
rcdsl_new_signal(i+1, (int16_t)servos.pos[1]);
}
}
}
void rcdsl_parse_data(uint8_t b)
// This function should be called externaly, when a new data byte has
// been received over uart.
{
// Check for sync condition
if ((b==0xFF) && (last_byte==0xFF)) {
data_counter = 0;
check_sum = 0;
return;
}
// First byte is cmd
if (data_counter == 0) {
if (b==0x1F) paket_len = 5; else paket_len=4;
}
// Last byte is checksum
if (data_counter > paket_len) {
// Calculate checksum
check_sum = ~check_sum;
if (check_sum==0xFF) check_sum=0xFE;
// If it match the received one, then apply data
if (b==check_sum) rcdsl_incoming_paket();
// Prepare for a new data paket
data_counter = 0;
check_sum = 0;
// New byte within a paket
} else {
data[data_counter] = b;
check_sum += b;
data_counter++;
}
// Remember last byte received for detection of sync condition
last_byte = b;
}
ISR (USART1_RX_vect)
{
rcdsl_parse_data(UDR1);
}