|
MHVLib
20111109
An efficiency oriented runtime library for AVR microcontrollers
|
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 MHV_DEBUG 00067 00068 class MHV_HardwareSerial : public MHV_Device_TX, public MHV_Device_RX { 00069 private: 00070 volatile uint16_t *_ubrr; 00071 volatile uint8_t *_ucsra; 00072 volatile uint8_t *_ucsrb; 00073 volatile uint8_t *_udr; 00074 uint8_t _rxen; 00075 uint8_t _txen; 00076 uint8_t _rxcie; 00077 uint8_t _txcie; 00078 uint8_t _udre; 00079 uint8_t _u2x; 00080 volatile bool _echo; 00081 00082 protected: 00083 void runTxBuffers(); 00084 00085 public: 00086 MHV_HardwareSerial(MHV_RingBuffer *rxBuffer, MHV_RingBuffer *txBuffer, volatile uint16_t *ubrr, 00087 volatile uint8_t *ucsra, volatile uint8_t *ucsrb, volatile uint8_t *udr, uint8_t rxen, 00088 uint8_t txen, uint8_t rxcie, uint8_t txcie, uint8_t udre, uint8_t u2x, 00089 unsigned long baud); 00090 void setSpeed(unsigned long baud); 00091 void end(); 00092 void busyWrite(char c); 00093 void busyWrite(const char *buffer); 00094 void busyWrite(const char *buffer, uint16_t length); 00095 void busyWrite_P(PGM_P buffer); 00096 void busyWrite_P(PGM_P buffer, uint16_t length); 00097 bool canSendBusy(); 00098 void rx(); 00099 void tx(); 00100 void echo(bool echoOn); 00101 bool busy(); 00102 00103 }; 00104 00105 00106 #endif