home *** CD-ROM | disk | FTP | other *** search
/ World of Shareware - Software Farm 2 / wosw_2.zip / wosw_2 / CPROG / CPPCOM17.ZIP / UART.HPP < prev   
C/C++ Source or Header  |  1991-02-27  |  6KB  |  228 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7. ***************************************************************************/
  8.  
  9. // file uart.hpp, class declaration for the uart class.
  10. // see your modem manual (I used MultiTech's) or the IBM Technical
  11. // reference manual for more information on the 8250 UART used in the PC.
  12.  
  13.  
  14. #ifndef UART_HPP
  15. #define UART_HPP 1
  16.  
  17. #include <dos.h>
  18. #include <bool.h>    // enum boolean { false, true }; if you don't have
  19.  
  20.  
  21. #ifdef __ZTC__
  22.  
  23. #include <int.h>
  24.  
  25. #ifndef inportb
  26. #define inportb inp
  27. #define outportb outp
  28. #define inportw inpw
  29. #define outportw outpw
  30. #define disable int_off
  31. #define enable int_on
  32. #endif
  33.  
  34. #endif
  35.  
  36.  
  37. // N. B.: Be VERY careful when using some PC hardware with COM1
  38. // or COM2 at the same time this class or its derivative classes are set
  39. // to use COM3 or COM4! This includes hardware incompatibilities with 
  40. // certain serial rodents.  On the IBM PC, the interrupts for 
  41. // COM1 & COM4 and for COM2 & COM3 are the same and tend to collide.
  42.  
  43. const int NUM_PORTS = 4;
  44. // COM1 thru COM4, but see above.
  45.  
  46.  
  47.  
  48. // The 8250 UART uses some truly weird methods of signaling interrupt
  49. // type.  The enum type below tries to make this into a hopefully more
  50. // comprehensible enum set.
  51.  
  52. enum com_interrupt_t { 
  53.         NONE_PENDING,
  54.         RING,
  55.         CARRIER, 
  56.         NO_CARRIER,
  57.         TRANSMIT_READY,
  58.         TRANSMIT_FALSE_ALARM,
  59.         RECEIVE_READY,
  60.         OVERRUN_ERROR,
  61.         PARITY_ERROR,
  62.         FRAMING_ERROR,
  63.         BREAK_RECEIVED,
  64.         UNKNOWN_ERROR
  65.     };                           
  66.  
  67.  
  68. // Parity is a concept which is hard to represent as a set
  69. // of integers.  The enum set below takes its values for the
  70. // convenience of setting parity on the 8250 UART line control
  71. // register (LCR).
  72.  
  73. enum parity_t
  74. {
  75.     NOPAR =  0x00,                // No parity 
  76.     ODDPAR = 0x08,                // Odd parity 
  77.     EVNPAR = 0x18,                // Even parity 
  78.     STKPAR = 0x28,                // Stick parity 
  79.     ZROPAR = 0x38                 // Zero parity; not well documented
  80. };
  81.  
  82. // This typedef helps the compiler sort out parameter types.
  83. #ifdef __TURBOC__
  84. typedef void interrupt (* DRIVER)(...);
  85. #else ifdef __ZTC__
  86. extern "C" {
  87. typedef int (*DRIVER)(INT_DATA *);
  88. };
  89. #endif
  90.  
  91.  
  92. // Here is the uart class itself.  It uses a method of calling derived
  93. // class methods for base class functions which is occasionally buggy
  94. // on TC++ 1.0 when using inline functions.  Take care, therefore, in
  95. // any attempt to inline the member functions below, since they call
  96. // virtual functions.
  97.  
  98. class uart
  99. {
  100. protected:
  101.      static unsigned int io_address[NUM_PORTS];
  102.     static int portvector_num[NUM_PORTS];
  103.     static char intmaskbit[NUM_PORTS];
  104.     static char old_intmask[NUM_PORTS];
  105.     static char old_MCR[NUM_PORTS];
  106.     static char old_IER[NUM_PORTS];
  107.  
  108.     // Here is the lone non-static variable for the class.
  109.      unsigned int base_address;    // base address for this instance.
  110.  
  111. #ifdef __TURBOC__
  112.     static DRIVER old_driver[NUM_PORTS];
  113. #else ifdef __ZTC__
  114.     static void far * old_driver[NUM_PORTS];
  115. #endif
  116.  
  117. public:
  118.     uart();
  119.     virtual ~uart();
  120.     // note that there is a static virtual destructor "heap is corrupted" 
  121.     // bug in certain ZTC versions of this code which may bite here.
  122.  
  123.      static void SetPortAddresses(int portnum, unsigned int io_add,
  124.          int portvec, char imask);
  125.  
  126.     int RegisterDriver(int portnum, DRIVER driv);
  127.     int RestoreDriver(int portnum);
  128.     
  129.     // These are constants of the PC 8250 UART.
  130.     // From the MultiTech Systems Owners' Manual 
  131.     // and the IBM Technical Reference Manual.
  132.      unsigned int LCR() { return base_address + 3; }   
  133.      unsigned int DLL() { return base_address; }       
  134.      unsigned int DLM() { return base_address + 1; }        
  135.      unsigned int LSR() { return base_address + 5; }        
  136.      unsigned int MCR() { return base_address + 4; }           
  137.      unsigned int MSR() { return base_address + 6; }           
  138.      unsigned int THR() { return base_address; }               
  139.      unsigned int RBR() { return base_address; }               
  140.      unsigned int IER() { return base_address + 1; }           
  141.      unsigned int IIR() { return base_address + 2; }           
  142.  
  143.     char GetLCR();
  144.     char GetDLL();
  145.     char GetDLM();
  146.     char GetLSR();
  147.     char GetMCR();
  148.     char GetMSR();
  149.     char GetRBR();
  150.     char GetIER();
  151.     char GetIIR();
  152.  
  153.     boolean GetLSR_THRE();
  154.  
  155.     void SetLCR(char byte);
  156.     void SetDLL(char byte);
  157.     void SetDLM(char byte);
  158.     void SetMCR(char byte);
  159.     void SetMSR(char byte);
  160.     void SetLSR(char byte);
  161.     void SetTHR(char byte);
  162.     void SetIER(char byte);
  163.  
  164.     void SetIER_Recieve(boolean bit);
  165.     void SetIER_Transmit(boolean bit);
  166.     void SetIER_Line(boolean bit);
  167.     void SetIER_Modem(boolean bit);
  168.  
  169.     void SetLCR_DLAB(boolean bit);
  170.  
  171.     void SetLSR_DR(boolean bit);
  172.     
  173.     void SetBaudRate(long speed);        
  174.     long GetBaudRate();
  175.     void SetSpeed(long speed) { SetBaudRate(speed); }
  176.      long GetSpeed() { return GetBaudRate(); }
  177.  
  178.     void SetParity(parity_t p);
  179.     parity_t GetParity();
  180.  
  181.     void SetWordLength(int len);
  182.     int GetWordLength();
  183.     void SetDataBits(int num) { SetWordLength(num); }
  184.     int GetDataBits() { return GetWordLength(); }
  185.  
  186.     void SetStopBits(int num);
  187.     int GetStopBits();
  188.  
  189.  
  190.     void SetBreak();
  191.     void StopBreak();
  192.     void Break(int msec = 500);
  193.     void Pause(int msec = 500);
  194.  
  195.     void SetCTS(boolean bit);
  196.     void SetDSR(boolean bit);
  197.  
  198.     boolean CarrierPresent();
  199.  
  200.     boolean GetDTR();
  201.     void SetDTR(boolean bit);
  202.  
  203.     com_interrupt_t GetIntrType();
  204.  
  205.     virtual void SetDoIfNoCarrier(void (*f)()) = 0;
  206.  
  207.     virtual void PurgeInput() = 0;
  208.     virtual void FlushOutput() = 0;
  209.     virtual void PurgeOutput() = 0;
  210.     virtual boolean InputEmpty() = 0;
  211.     virtual boolean OutputEmpty() = 0;
  212.     virtual boolean OutputReady() = 0;
  213.  
  214.     // Note: the functions below do NO error checking.
  215.     // You must do your error checking in your driver routine.
  216.  
  217.     virtual int GetChar();
  218.     virtual void SendChar(char ch);
  219.     virtual void TransmitChar(char ch);
  220.     virtual int ReceiveChar();
  221. };
  222.  
  223.  
  224. #endif
  225.  
  226. // end of file uart.hpp
  227.  
  228.