/* Name : Chat Program via a serial port */ /* Written By : ND */ #include #include #include #include #include #define COM1 0x3F8 #define THR 0 #define RBR 0 #define IER 1 #define FCR 2 #define LCR 3 #define MCR 4 #define LSR 5 #define MSR 6 #define DLL 0 #define DLM 1 #define DTR 0 #define RTS 1 #define CTS 4 //inside MSR #define DSR 5 #define DLAB 7 // LSR Register: Error types and bits offsets // Note: When the LSR is read, it is reset. #define DR 0 //indicates that a byte is ready to be read from RBR #define OVERRUN_ERROR 1 #define PARITY_ERROR 2 #define FRAMING_ERROR 3 #define BREAK_INTERRUPT 4 #define NUM_OF_ERRORS 4 #define THRE 5 //1 indicates I can write to the THR #define TSRE 6 #define ON 1 #define OFF 0 #define ODD 1 #define EVEN 2 #define NUM_OF_PARITY_OPTIONS 3 #define ONE 0 #define TWO 1 #define UART_CLOCK_MAX_FREQUENCY 1843200 #define MULTIPLIER 16 #define MAX_SPEED_IN_BPS 115200 #define DEFAULT_SPEED_IN_BPS 2400 #define DEFAULT_WORD_LENGTH 8 #define DEFAULT_PARITY_VALUE 0 #define DEFAULT_STOP_BIT_LENGTH 1 #define DEFAULT_INTERRUPT_MODE 0 #define MINIMAL_WORD_LENGTH 5 #define MAXIMAL_WORD_LENGTH 8 #define MINIMAL_STOP_BIT_LENGTH 1 #define MAXIMAL_STOP_BIT_LENGTH 2 #define NUM_OF_SPEEDS 14 #define NUM_OF_BUFFER_ELEMENTS 13 #define SPEED_INDEX 0 #define WORD_LENGTH_INDEX 1 #define PARITY_INDEX 2 #define STOP_BIT_INDEX 3 #define NO_CHAR 256 #define ESC 27 #define ENTER 13 char glbBuffer[]={"hello out there"}; // This function changes the UART's settings. // If BitNumber<0 the function sets on or off the whole register given // to the function. void SetABitOrAByteOnOrOff(int port, int OnOrOff, int RegisterOffset, int BitNumber) { int shifter; if(BitNumber>=0) { shifter=1; shifter=shifter<=0) { output=output>>BitNumber; BitNumber=1; output=output&BitNumber; } return(output); } int GetABaudRate(int SpeedInBPS) { long clock=UART_CLOCK_MAX_FREQUENCY; int multi=MULTIPLIER; unsigned int temp=0; temp=SpeedInBPS*multi; return(clock/temp); } // Inits the DLL and DLM registers with a new speed void SetABaudRate(int port, int BaudRate) { int lsb,msb; if(BaudRate<1) { BaudRate=GetABaudRate(DEFAULT_SPEED_IN_BPS); } lsb=BaudRate%0xFF; msb=BaudRate/0xFF; SetABitOrAByteOnOrOff(port,ON,LCR,DLAB); outp(DLL,lsb); outp(DLM,msb); SetABitOrAByteOnOrOff(port,OFF,LCR,DLAB); } int ReadABaudRate(int port) { int lsb,msb; SetABitOrAByteOnOrOff(port,ON,LCR,DLAB); lsb=inp(DLL); msb=inp(DLM); SetABitOrAByteOnOrOff(port,OFF,LCR,DLAB); msb=msb<<8; return (lsb+msb); } void SetWordLength(int port, int NewLength) { int first=OFF,second=OFF; switch (NewLength) { case 6: first=OFF; second=ON; break; case 7: first=ON; second=OFF; break; case 8: first=ON; second=ON; break; default: first=ON; second=ON; } SetABitOrAByteOnOrOff(port,first,LCR,0); SetABitOrAByteOnOrOff(port,second,LCR,1); } void SetParity(int port, int NewType) { int parity=OFF; int type=OFF; if(NewType!=OFF) { if(NewType==ODD) { parity=ON; type=ODD-1; } else { parity=ON; type=EVEN-1; } } SetABitOrAByteOnOrOff(port,parity,LCR,3); SetABitOrAByteOnOrOff(port,type,LCR,4); } void SetStopBitLength(int port, int NewLength) { if(NewLength==TWO) { SetABitOrAByteOnOrOff(port,ON,LCR,2); } else { SetABitOrAByteOnOrOff(port,OFF,LCR,2); } } void ChangeCommunicationSettings(int port, int SpeedInBPS, int WordLength, int parity, int StopBitLength, int InterruptMode) { if(InterruptMode==OFF) { SetABitOrAByteOnOrOff(port,OFF,IER,-1); /* Turn off interrupts */ } SetABaudRate(port,GetABaudRate(SpeedInBPS)); SetWordLength(port,WordLength); SetParity(port,parity); SetStopBitLength(port,StopBitLength); } void StartOrStopTheControlSystem(int port, int OnOrOff) { if(OnOrOff==ON) { SetABitOrAByteOnOrOff(port,ON,MCR,RTS); } else { SetABitOrAByteOnOrOff(port,OFF,MCR,RTS); } } void KillTheConnection(int port) { StartOrStopTheControlSystem(port,OFF); SetABitOrAByteOnOrOff(port,OFF,MCR,DTR); // Kill the connection } void SendAChar(int port, int aChar) { outp(port+THR,aChar); } int GetAChar(int port) { int aChar=NO_CHAR; aChar=inp(port+RBR); return(aChar); } void SendDataBufferToPort(int port, int index_of_buffer) { SendAChar(port,glbBuffer[index_of_buffer]); printf("sent one char \n"); } void SaveInfoToBuffer(int port) { int i=0; // while(i