//////////////////////////////////////////////////////////////////////////////
// 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.h: interface for the swtx module.
//
// This is a software UART transmitter.
// It uses 8 data bits, 1 stop bit, and no parity
//
// NOTE: Timer/Counter1 is used to keep time
//
//////////////////////////////////////////////////////////////////////////////
#ifndef SWTX_H
#define SWTX_H

#include <inttypes.h>

    // 1/2400 = 416.666 us
    //using a 2 MHz clock (cycletime = 0.5 us) and a clk/4 prescalar
    //NOTE: a greater prescalar may need to be used with faster clocks
#define SWTX_BAUD_2400  48  //256 - (416/4 * 2)
#define SWTX_BAUD_4800  152 //256 - (416/8 * 2)
#define SWTX_BAUD_9600  204 //256 - (416/16 * 2)

#define SWTX_PORT   PORTB   //the port to use
#define SWTX_DDR    DDRB    //data direction register for the port
#define SWTX_PINNUM PB4     //pin number for the port


//////// SW UART TX Functions ////////

void swtxInitialize(void);

void swtxSetBaud(uint8_t baudrate);
void swtxSendN(char *buffer, uint8_t nbytes);

#endif //SWTX_H
