libraries / Mitov / Mitov_TeensySerial.hon commit Added link to project report (97a3ba0)
   1////////////////////////////////////////////////////////////////////////////////
   2//                                                                            //
   3//     This software is supplied under the terms of a license agreement or    //
   4//     nondisclosure agreement with Mitov Software and may not be copied      //
   5//     or disclosed except in accordance with the terms of that agreement.    //
   6//         Copyright(c) 2002-2016 Mitov Software. All Rights Reserved.        //
   7//                                                                            //
   8////////////////////////////////////////////////////////////////////////////////
   9
  10#ifndef _MITOV_TEENSY_SERIAL_h
  11#define _MITOV_TEENSY_SERIAL_h
  12
  13#include <Mitov.h>
  14
  15namespace Mitov
  16{
  17        const   long CTeensySerialInits[] = 
  18        {
  19                SERIAL_7E1,
  20                SERIAL_7E1,
  21                SERIAL_7O1,
  22                SERIAL_8N1,
  23                SERIAL_8E1,
  24                SERIAL_8O1,
  25
  26                SERIAL_8N2,
  27                SERIAL_8N2,
  28                SERIAL_8N2,
  29                SERIAL_8N2,
  30                SERIAL_8N2,
  31                SERIAL_8N2
  32/*
  33                SERIAL_7E1_RXINV,
  34                SERIAL_7E1_RXINV,
  35                SERIAL_7O1_RXINV,
  36                SERIAL_8N1_RXINV,
  37                SERIAL_8E1_RXINV,
  38                SERIAL_8O1_RXINV,
  39
  40                SERIAL_8N2_RXINV,
  41                SERIAL_8N2_RXINV,
  42                SERIAL_8N2_RXINV,
  43                SERIAL_8N2_RXINV,
  44                SERIAL_8N2_RXINV,
  45                SERIAL_8N2_RXINV,
  46
  47                SERIAL_7E1_TXINV,
  48                SERIAL_7E1_TXINV,
  49                SERIAL_7O1_TXINV,
  50                SERIAL_8N1_TXINV,
  51                SERIAL_8E1_TXINV,
  52                SERIAL_8O1_TXINV,
  53
  54                SERIAL_8N2_TXINV,
  55                SERIAL_8N2_TXINV,
  56                SERIAL_8N2_TXINV,
  57                SERIAL_8N2_TXINV,
  58                SERIAL_8N2_TXINV,
  59                SERIAL_8N2_TXINV,
  60
  61                SERIAL_7E1_RXINV_TXINV,
  62                SERIAL_7E1_RXINV_TXINV,
  63                SERIAL_7O1_RXINV_TXINV,
  64                SERIAL_8N1_RXINV_TXINV,
  65                SERIAL_8E1_RXINV_TXINV,
  66                SERIAL_8O1_RXINV_TXINV,
  67
  68                SERIAL_8N2_RXINV_TXINV,
  69                SERIAL_8N2_RXINV_TXINV,
  70                SERIAL_8N2_RXINV_TXINV,
  71                SERIAL_8N2_RXINV_TXINV,
  72                SERIAL_8N2_RXINV_TXINV,
  73                SERIAL_8N2_RXINV_TXINV
  74*/
  75/*
  76#ifdef SERIAL_9BIT_SUPPORT
  77#define SERIAL_9N1 0x84
  78#define SERIAL_9E1 0x8E
  79#define SERIAL_9O1 0x8F
  80#define SERIAL_9N1_RXINV 0x94
  81#define SERIAL_9E1_RXINV 0x9E
  82#define SERIAL_9O1_RXINV 0x9F
  83#define SERIAL_9N1_TXINV 0xA4
  84#define SERIAL_9E1_TXINV 0xAE
  85#define SERIAL_9O1_TXINV 0xAF
  86#define SERIAL_9N1_RXINV_TXINV 0xB4
  87#define SERIAL_9E1_RXINV_TXINV 0xBE
  88#define SERIAL_9O1_RXINV_TXINV 0xBF
  89#endif
  90*/
  91        };
  92//---------------------------------------------------------------------------
  93        template<typename T_SERIAL_TYPE, T_SERIAL_TYPE *T_SERIAL> class TeensySerialPort : public Mitov::SpeedSerialPort<T_SERIAL_TYPE, T_SERIAL>
  94        {
  95                typedef Mitov::SpeedSerialPort<T_SERIAL_TYPE, T_SERIAL> inherited;
  96
  97        public:
  98                TArduinoSerialParity    Parity : 3;
  99                unsigned int                    StopBits : 2;
 100                unsigned int                    DataBits : 3;
 101                bool                                    InvertedRX : 1;
 102                bool                                    InvertedTX : 1;
 103
 104        public:
 105                void SetParity( TArduinoSerialParity AValue )
 106                {
 107            if( Parity == AValue )
 108                return;
 109
 110            Parity = AValue;
 111            inherited::RestartPort();
 112                }
 113
 114                void SetStopBits( unsigned int AValue )
 115                {
 116            if( StopBits == AValue )
 117                return;
 118
 119            StopBits = AValue;
 120            inherited::RestartPort();
 121                }
 122
 123                void SetDataBits( unsigned int AValue )
 124                {
 125            if( DataBits == AValue )
 126                return;
 127
 128            DataBits = AValue;
 129            inherited::RestartPort();
 130                }
 131
 132        protected:
 133                virtual void StartPort()
 134                {
 135//                      int AIndex = ((int)Parity) * 8 + ( StopBits - 1 ) * 4 + ( DataBits - 5);
 136                        int AIndex = ( StopBits - 1 ) * 6 + ( DataBits - 5) * 3 + ((int)Parity);
 137                        long AValue = CTeensySerialInits[ AIndex ];
 138                        if( InvertedRX )
 139                                AValue |= 0x10;
 140
 141                        if( InvertedTX )
 142                                AValue |= 0x20;
 143
 144                        T_SERIAL->begin( inherited::Speed, AValue );
 145                }
 146
 147        public:
 148                TeensySerialPort() :
 149                        Parity( spNone ),
 150                        StopBits( 1 ),
 151                        DataBits( 8 ),
 152                        InvertedRX( false ),
 153                        InvertedTX( false )
 154                {
 155                }
 156        };
 157//---------------------------------------------------------------------------
 158} // Mitov
 159
 160#endif
 161