MHVLib  20111011
An efficiency oriented runtime library for AVR microcontrollers
A:/eclipse/mhvlib/MHV_HardwareSerial.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2011, Make, Hack, Void Inc
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *  * Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  *  * Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  *  * Neither the name of the Make, Hack, Void nor the
00013  *    names of its contributors may be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
00017  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00018  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019  * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY
00020  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00021  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00022  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00023  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00025  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 
00028 
00029 #ifndef MHV_HardwareSerial_h
00030 #define MHV_HardwareSerial_h
00031 
00032 #include <inttypes.h>
00033 #include <avr/interrupt.h>
00034 #include <MHV_io.h>
00035 #include <stdio.h>
00036 #include <MHV_RingBuffer.h>
00037 #include <avr/pgmspace.h>
00038 #include <MHV_Device_TX.h>
00039 #include <MHV_Device_RX.h>
00040 
00041 #define MHV_HARDWARESERIAL_ASSIGN_INTERRUPTS(mhvHardwareSerial, mhvHardwareSerialInterrupts) \
00042         _MHV_HARDWARESERIAL_ASSIGN_INTERRUPTS(mhvHardwareSerial, mhvHardwareSerialInterrupts)
00043 
00044 #define _MHV_HARDWARESERIAL_ASSIGN_INTERRUPTS(mhvHardwareSerial, mhvRxVect, mhvTxVect) \
00045 ISR(mhvRxVect) { \
00046         mhvHardwareSerial.rx(); \
00047 } \
00048 ISR(mhvTxVect) { \
00049         mhvHardwareSerial.tx(); \
00050 }
00051 
00060 #define MHV_HARDWARESERIAL_CREATE(_mhvObjectName, _mhvRXBUFLEN, _mhvTXBUFCOUNT, _mhvSERIAL, _mhvBAUD) \
00061                 MHV_RX_BUFFER_CREATE(_mhvObjectName ## RX, _mhvRXBUFLEN); \
00062                 MHV_TX_BUFFER_CREATE(_mhvObjectName ## TX, _mhvTXBUFCOUNT); \
00063                 MHV_HardwareSerial _mhvObjectName(&_mhvObjectName ## RX, &_mhvObjectName ## RX, _mhvSERIAL, _mhvBAUD); \
00064                 MHV_HARDWARESERIAL_ASSIGN_INTERRUPTS(_mhvObjectName, _mhvSERIAL ## _INTERRUPTS);
00065 
00066 #define MHV_HARDWARESERIAL_DEBUG(__dbg_serial, __dbg_format, __dbg_args...) \
00067 do {\
00068         __dbg_serial.debug(__FILE__, __LINE__, __FUNCTION__, PSTR(__dbg_format), ## __dbg_args); \
00069 } while (0)
00070 
00071 class MHV_HardwareSerial : public MHV_Device_TX, public MHV_Device_RX {
00072 private:
00073         volatile uint16_t *_ubrr;
00074         volatile uint8_t *_ucsra;
00075         volatile uint8_t *_ucsrb;
00076         volatile uint8_t *_udr;
00077         uint8_t _rxen;
00078         uint8_t _txen;
00079         uint8_t _rxcie;
00080         uint8_t _txcie;
00081         uint8_t _udre;
00082         uint8_t _u2x;
00083         volatile bool _echo;
00084 
00085 protected:
00086         void runTxBuffers();
00087 
00088 public:
00089         MHV_HardwareSerial(MHV_RingBuffer *rxBuffer, MHV_RingBuffer *txBuffer, volatile uint16_t *ubrr,
00090                         volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *udr, uint8_t rxen,
00091                         uint8_t txen, uint8_t rxcie, uint8_t txcie, uint8_t udre, uint8_t u2x,
00092                         unsigned long baud);
00093         void setSpeed(unsigned long baud);
00094         void end();
00095         void busyWrite(char c);
00096         void busyWrite(const char *buffer);
00097         void busyWrite(const char *buffer, uint16_t length);
00098         void busyWrite_P(PGM_P buffer);
00099         void busyWrite_P(PGM_P buffer, uint16_t length);
00100         bool canSendBusy();
00101         void rx();
00102         void tx();
00103         void echo(bool echoOn);
00104         bool busy();
00105         void debug(const char *file, int line, const char *function,
00106                         PGM_P format, ...);
00107 
00108 };
00109 
00110 
00111 #endif