//////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2003, Oren Avissar
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
//   notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in
//   the documentation and/or other materials provided with the
//   distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// swtx.c: implementation of the swtx module.
//
//////////////////////////////////////////////////////////////////////////////

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/signal.h>

#include "swtx.h"


//////// Internal Global Variable Declarations ////////

    //counter value to generate the proper baud rate
static volatile uint8_t _tcnt1val = 0;

    //pointer to the current byte to transmit
static volatile char *_byteptr = 0;
    //number of bytes left to send
static volatile uint8_t _nleft = 0;
    //used to keep track of the last bit that was sent
static volatile uint8_t _bitnumber = 0;

//////// Internal Function Declarations ////////

    //ISR for Timer/Counter1 Overflow
void SIG_OVERFLOW1 (void) __attribute__ ((signal));


//////////////////////////////////////////////////////////////////////
// Timer 1 ISR
//////////////////////////////////////////////////////////////////////

    //set the interrupt for Timer/Counter1 Overflow
void SIG_OVERFLOW1 (void)
{

        //reload the timer value
    TCNT1 = _tcnt1val;

    if (_nleft == 0)
        return; //nothing left to send

    if (_bitnumber == 0) {
            //send the start bit
        SWTX_PORT &= ~(1 << SWTX_PINNUM);  //send a 0
            //increment the bit number
        _bitnumber++;
    } else if (_bitnumber <= 8) {
            //send the data
        if (((*_byteptr) >> (_bitnumber-1)) & 0x01)
            SWTX_PORT |= (1 << SWTX_PINNUM);   //send a 1
        else
            SWTX_PORT &= ~(1 << SWTX_PINNUM);  //send a 0
            //increment the bit number
        _bitnumber++;
    } else if (_bitnumber > 8) {
            //send the stop bit
        SWTX_PORT |= (1 << SWTX_PINNUM);   //send a 1
            //reset the bit number
        _bitnumber = 0;
            //decrement the number of bytes left to send
        _nleft--;
            //move to the next byte to send
        if (_nleft > 0)
            _byteptr++;
    }
}

//////////////////////////////////////////////////////////////////////
// swtx functions
//////////////////////////////////////////////////////////////////////

void swtxInitialize(void)
{

        //initialize the pin to output a 1
    SWTX_PORT |= (1 << SWTX_PINNUM);

       //set the pin to output
    SWTX_DDR |= (1 << SWTX_PINNUM);

        //initialize the baud rate to 2400
    _tcnt1val = SWTX_BAUD_2400;
}

void swtxSetBaud(uint8_t baudrate)
{

    _tcnt1val = baudrate;
}

void swtxSendN(char *buffer, uint8_t nbytes)
{

        //set the pointer to the buffer to send
    _byteptr = buffer;
        //set the number of bytes to send
    _nleft = nbytes;

        //initialize the Timer/Counter1 control register
    TCCR1B = (1 << CS10) | (1 << CS11) | (1 << PSR1);  //set prescaler to clk/4
 
         //initialize the timer counter
    TCNT1 = _tcnt1val;

        //set the Timer/Counter1 Overflow Interrupt Enable
    TIMSK |= (1 << TOIE1);

        //wait for the transmission to finish
    while (_nleft > 0);

        //stop the Timer/Counter1
    TCCR1B = 0;
}
