Main Page   Packages   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members   Search  

C:/temp/src/j2k/QNX4/BoardThread.cpp

Go to the documentation of this file.
00001 #include <j2k/Fred/Basic.hpp>
00002 #include <j2k/Fred/QNX/System.hpp>
00003 
00004 #define   INPUT_BASE_ADDR    0x188 
00005 #define   OUTPUT_BASE_ADDR   0x180 
00006 #define   ENABLE_OFFSET      2 
00007 #define   VAL_MAX            0xFF    // 255 (%1111 1111)
00008 
00009 #define   LOCAL_KEYBOARD   "/dev/con1"
00010 #define   ELAN104_SERIAL  "//2/dev/ser1"
00011 
00012 /**********************************************************
00013  * Basic_Thread should have a  ' virtual Run() = 0; '
00014  * function that is implemented here.
00015  * The keyboard is used here to DEBUG and TEST
00016  * it should be replace by a message call from 
00017  * your application that sends the proper bytes, 
00018  * so it make sense.
00019  **********************************************************/      
00020 
00021 class Board   // : public Basic_Thread
00022 {
00023 private:
00024    // In16 and Out16 Stuff
00025    int input_base_addr;
00026    int output_base_addr;
00027    int enable_addr;
00028 
00029    // Serial Port Stuff
00030    unsigned state;
00031    pid_t kb_proxy, mdm_proxy, pid;
00032    int kb_fd, mdm_fd, n;
00033    char buf[80];
00034 
00035 public:
00036     Board( int in_addr  = INPUT_BASE_ADDR, int out_addr  = OUTPUT_BASE_ADDR )
00037     {
00038       // Initialize variables for In16 and Out16
00039       input_base_addr  = in_addr;
00040       output_base_addr = out_addr;
00041       enable_addr     = out_addr + ENABLE_OFFSET;
00042 
00043       outp( enable_addr, 3 );   // Enable Output Port 0 & 1 (%0000 0011)
00044 
00045 
00046       // Initialize and setup the Serial Port
00047 
00048       kb_fd  = open( LOCAL_KEYBOARD, O_RDWR );  // Local Keyboard
00049       mdm_fd = open( ELAN104_SERIAL, O_RDWR );  // QNX-2 Elan104 Board
00050 
00051       kb_proxy  = qnx_proxy_attach( 0, 0, 0, -1 );
00052       mdm_proxy = qnx_proxy_attach( 0, 0, 0, -1 );
00053 
00054       dev_mode( kb_fd,  0, _DEV_MODES );
00055       dev_mode( mdm_fd, 0, _DEV_MODES );
00056 
00057       // Clear, then arm the proxies for future events
00058 
00059       dev_state( kb_fd,  0, _DEV_EVENT_INPUT );
00060       dev_arm(   kb_fd,  kb_proxy,  _DEV_EVENT_INPUT );
00061       
00062       dev_state( mdm_fd, 0, _DEV_EVENT_INPUT );
00063       dev_arm(   mdm_fd, mdm_proxy, _DEV_EVENT_INPUT );
00064     }
00065     
00066     ~Board()
00067     {
00068       outp( output_base_addr, 0 );    // Send 0x00 to Port 0 & 1
00069       outp( output_base_addr + 1, 0 );
00070       outp( enable_addr, 0 );         // Disable All Output Port  
00071     }
00072 
00073     void Write( int port, int val)
00074     {
00075       port = ( port >= 1 ) ? ( 1 ) : ( 0 );
00076        val  = ( val > VAL_MAX ) ? ( VAL_MAX ) : ( val );
00077 
00078         outp( output_base_addr + port, val);
00079     }
00080    
00081    void Write( int val )
00082    {
00083       int port = ( val > VAL_MAX ) ? ( 1 ) : ( 0 );
00084           val  = ( val > VAL_MAX ) ? ( val % VAL_MAX ) : ( val );
00085 
00086        outp( output_base_addr + port, val);
00087    }
00088 
00089     int Read( int port )
00090     {
00091       port = ( port >= 1 ) ? ( 1 ) : ( 0 );
00092 
00093         return ( inp( input_base_addr + port ) );
00094     }
00095 
00096     int Read()
00097     {
00098       int r1 = inp( input_base_addr     );
00099       int r2 = inp( input_base_addr + 1 );
00100 
00101         return ( ( r2 << 8 ) + r1 );   // Shift Left 8 bits and Add.
00102     }
00103 
00104    // This is a bidirectionnal CHAT program 
00105    // between the ELAN104 and the HP Dump Terminal
00106    void Run()  
00107    {
00108       for(;;) {   // Infinite Loop, until CTRL-BREAK
00109       
00110          // Wait for either keyboard or modem to send an input.
00111          pid = Receive( 0, 0, 0 ); 
00112       
00113          // Waiting for an event, then check the Process ID,
00114          // Read the message and send it back to the other machine.
00115       
00116          if ( pid == kb_proxy ) {
00117       
00118             state = dev_state( kb_fd, 0, _DEV_EVENT_INPUT );
00119    
00120             // If there is an Input from Keyboard Console
00121             // then Read it and Send it Back to the Modem
00122       
00123             if ( state & _DEV_EVENT_INPUT ) {
00124                n = read( kb_fd, &buf, sizeof( buf ) );
00125                write( mdm_fd, &buf, n );
00126                dev_arm( kb_fd, kb_proxy, _DEV_EVENT_INPUT );
00127             }
00128       
00129          } else if ( pid == mdm_proxy ) {
00130 
00131             state = dev_state( mdm_fd, 0, _DEV_EVENT_INPUT );
00132    
00133       
00134             // If there is an Input from Modem
00135             // then Read it and Send it Back to the Keyboard Console
00136          
00137             if ( state & _DEV_EVENT_INPUT ) {
00138                n = read( mdm_fd, &buf, sizeof( buf ) );
00139                write( kb_fd, &buf, n );
00140                dev_arm( mdm_fd, mdm_proxy, _DEV_EVENT_INPUT );
00141             }
00142       
00143          }
00144       }
00145    }
00146 
00147 }; 
00148 
00149 int main() {
00150   Board* b = new Board();
00151   printf("READ()      [%d] \n\r", b->Read()     );
00152   printf("READ(0)     [%d] \n\r", b->Read(0)    );
00153   printf("READ(1)     [%d] \n\r", b->Read(1)    );
00154   printf("READ(5555)  [%d] \n\r", b->Read(5555) );
00155 
00156   b->Write( 0 );
00157   b->Write( 10 );
00158   b->Write( 255 );
00159   b->Write( 0xA5 );
00160   b->Write( 511 );
00161   b->Write( 12120 );
00162   b->Write( 0x1A0 );
00163 
00164   printf("\n\rBasic QNX Serial Port CHAT program: \n\r\n\n");
00165   b->Run();
00166 
00167   printf("\nDone!\n");
00168 
00169   delete b;
00170 
00171   return 0;
00172 }

Generated on Sun Oct 14 18:46:40 2001 for Standard J2K Library by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001