Subversion Repositories Projects

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

/****************************************************************************
 *   Copyright (C) 2011 by Claas Anders "CaScAdE" Rathje                    *
 *   admiralcascade@gmail.com                                               *
 *   Project-URL: http://www.mylifesucks.de/oss/c-epilepsy/                 *
 *                                                                          *
 *   This program is free software; you can redistribute it and/or modify   *
 *   it under the terms of the GNU General Public License as published by   *
 *   the Free Software Foundation; either version 2 of the License.         *
 *                                                                          *
 *   This program is distributed in the hope that it will be useful,        *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of         *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the          *
 *   GNU General Public License for more details.                           *
 *                                                                          *
 *   You should have received a copy of the GNU General Public License      *
 *   along with this program; if not, write to the                          *
 *   Free Software Foundation, Inc.,                                        *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.              *
 ****************************************************************************/



#include "usart0.h"

volatile uint8_t rxd_buffer_locked = 0;
volatile uint8_t rxd_buffer[RXD_BUFFER_LEN];
volatile uint8_t txd_buffer[TXD_BUFFER_LEN];
volatile uint8_t ReceivedBytes = 0;
volatile uint8_t *pRxData = 0;
volatile uint8_t RxDataLen = 0;

/**
 * init usart0
 */

void usart0_init() {

    UBRR0H = ((F_CPU / (16UL * USART0_BAUD)) - 1) >> 8;
    UBRR0L = (F_CPU / (16UL * USART0_BAUD)) - 1;

    // Enable receiver and transmitter; enable RX interrupt
    UCSR0B = (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);

    // set asynchronous mode
    UCSR0C &= ~(1 << UMSEL01);
    UCSR0C &= ~(1 << UMSEL00);
    // no parity
    UCSR0C &= ~(1 << UPM01);
    UCSR0C &= ~(1 << UPM00);
    // 1 stop bit
    UCSR0C &= ~(1 << USBS0);
    // 8-bit
    UCSR0B &= ~(1 << UCSZ02);
    UCSR0C |= (1 << UCSZ01);
    UCSR0C |= (1 << UCSZ00);

    // set direction of RXD0 and TXD0 pins
    // set RXD0 (PD0) as an input pin
    PORTD &= ~(1 << PORTD0);
    DDRD &= ~(1 << DDD0);
    // set TXD0 (PD1) as an output pin
    //PORTD |= (1 << PORTD1);
    //DDRD &= ~(1 << DDD1);

}

/**
 * disable the txd pin of usart0
 */

void usart0_DisableTXD(void) {
    UCSR0B &= ~(1 << TXCIE0); // disable TX-Interrupt
    UCSR0B &= ~(1 << TXEN0); // disable TX in USART
    DDRD &= ~(1 << DDD1); // set TXD pin as input
    PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin
}

/**
 * enable the txd pin of usart0
 */

void usart0_EnableTXD(void) {
    DDRD |= (1 << DDD1); // set TXD pin as output
    PORTD &= ~(1 << PORTD1); // disable pullup on TXD pin
    UCSR0B |= (1 << TXEN0); // enable TX in USART
    UCSR0B |= (1 << TXCIE0); // enable TX-Interrupt
}

/**
 * send a single <character> through usart0
 */

void usart0_putc(unsigned char character) {
    // wait until UDR ready
    while (!(UCSR0A & (1 << UDRE0)));
    UDR0 = character;
}

/**
 * send a <string> throught usart0
 */

void usart0_puts(char *s) {
    while (*s) {
        usart0_putc(*s);
        s++;
    }
}

/**
 * send a PGM<string> throught usart0
 */

void usart0_puts_pgm(const char* string) {
    while (pgm_read_byte(string) != 0x00)
        usart0_putc(pgm_read_byte(string++));
}

/**
 * transmit interrupt handler
 * unused
 */

ISR(USART0_TX_vect, ISR_BLOCK) {
}

/*
 * receive data through usart0
 * portions taken and adapted from
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Fbranches%2FV0.72p+Code+Redesign+killagreg%2Fuart0.c
 */

ISR(USART0_RX_vect, ISR_BLOCK) {
    uint8_t c;
    // catch the received byte
    c = UDR0;
    if (rxd_buffer_locked) return; // if rxd buffer is locked immediately return
    static uint16_t crc;
    static uint8_t ptr_rxd_buffer = 0;
    static uint8_t c1 = 0;
    static uint8_t c2 = 0;
    static uint8_t usart_rx_ok = 0;
    uint8_t crc1, crc2;
    // the rxd buffer is unlocked
    if (usart_rx_ok == 0) {
        // if ((c2 == '#') && (c1 == 'b' || c1 == 'c') && (c == 'D' || c == 'V' || c == 'O')) {

        if ((c2 == '#') && (c1 == 'b' || c1 == 'c') &&
#if FCONLY
            (c == 'V' || c == 'D' || c == 'Q' || c == 'L')) { // version, debug, settings, LCD
#else
            (c == 'V' || c == 'O' || c == 'Q')) { // version, OSD, settings
#endif
                usart_rx_ok = 1;
                rxd_buffer[ptr_rxd_buffer++] = c2;
                crc = c2;
                rxd_buffer[ptr_rxd_buffer++] = c1;
                crc += c1;
                rxd_buffer[ptr_rxd_buffer++] = c;
                crc += c;
                c2 = 0;
                c1 = 0;
            } else {
            c2 = c1;
            c1 = c;
        }
    } else if (ptr_rxd_buffer < RXD_BUFFER_LEN) { // collect incomming bytes
        if (c != '\r') { // no termination character
            rxd_buffer[ptr_rxd_buffer++] = c; // copy byte to rxd buffer
            crc += c; // update crc
        } else { // termination character was received
            // the last 2 bytes are no subject for checksum calculation
            // they are the checksum itself
            crc -= rxd_buffer[ptr_rxd_buffer - 2];
            crc -= rxd_buffer[ptr_rxd_buffer - 1];
            // calculate checksum from transmitted data
            crc %= 4096;
            crc1 = '=' + crc / 64;
            crc2 = '=' + crc % 64;
            // 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 = 1; // lock the rxd buffer


                if (rxd_buffer[2] == 'R') {
                    wdt_enable(WDTO_250MS); // Reset-Commando
                }
            } else { // checksum invalid
                rxd_buffer_locked = 0; // unlock rxd buffer

            }
            ptr_rxd_buffer = 0; // reset rxd buffer pointer
            usart_rx_ok = 0;
        }
    } else { // rxd buffer overrun
        ptr_rxd_buffer = 0; // reset rxd buffer
        rxd_buffer_locked = 0; // unlock rxd buffer
        usart_rx_ok = 0;

    }
}

/**
 * Decode the recevied Buffer
 * portions taken and adapted from
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c
 */

void Decode64(void) {
    uint8_t a, b, c, d;
    uint8_t x, y, z;
    uint8_t ptrIn = 3;
    uint8_t ptrOut = 3;
    uint8_t len = ReceivedBytes - 6;

    while (len) {
        a = rxd_buffer[ptrIn++] - '=';
        b = rxd_buffer[ptrIn++] - '=';
        c = rxd_buffer[ptrIn++] - '=';
        d = rxd_buffer[ptrIn++] - '=';

        x = (a << 2) | (b >> 4);
        y = ((b & 0x0f) << 4) | (c >> 2);
        z = ((c & 0x03) << 6) | d;

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

/**
 * request Data through USART in special MK format by adding checksum and
 * encode data in modified Base64
 * portions taken and adapted from
 * http://svn.mikrokopter.de/mikrowebsvn/filedetails.php?repname=FlightCtrl&path=%2Ftags%2FV0.72p%2Fuart.c
 */

void sendMKData(unsigned char cmd, unsigned char addr, unsigned char *snd, unsigned char len) {
    unsigned int pt = 0;
    unsigned char a, b, c;
    unsigned char ptr = 0;

    txd_buffer[pt++] = '#'; // Start-Byte
    txd_buffer[pt++] = 'a' + addr; // Adress
    txd_buffer[pt++] = cmd; // Command
    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;
        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);
    }

    // add crc
    unsigned int tmpCRC = 0, i;
    for (i = 0; i < pt; i++) {
        tmpCRC += txd_buffer[i];
    }
    tmpCRC %= 4096;
    txd_buffer[i++] = '=' + tmpCRC / 64;
    txd_buffer[i++] = '=' + tmpCRC % 64;
    txd_buffer[i++] = '\r';

    usart0_puts((char*)txd_buffer);
}

/**
 * short script to directly send a request thorugh usart including en- and disabling it
 * where <address> is the address of the receipient, <label> is which data set to request
 * and <ms> represents the milliseconds delay between data
 */

void usart0_request_mk_data(uint8_t address, char label, uint8_t ms) {
    // re-enable TXD pin
    usart0_EnableTXD();

    unsigned char mstenth = ms / 10;
    sendMKData(label, address, &mstenth, 1);
    // wait until UDR ready
    while (!(UCSR0A & (1 << UDRE0)));
    // disable TXD pin again
    usart0_DisableTXD();
}