|
|
//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright � 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: Comm485.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of Comm485.cpp
//----------------------------------------------------------------------------
#include <dos.h>
#include <stdio.h>
#include "..\lgs\comm485.h"
//#include "..\LgLib\version.h"
#include <dpmi.h>
#include <go32.h>
static __dpmi_regs callback_regs;static _go32_dpmi_seginfo callback_info;
int install_real_mode_service_handler( void (*func)( __dpmi_regs * ) ){ __dpmi_regs r;
callback_info.pm_offset = (long)func; callback_info.pm_selector = _my_cs();
/* Replace the next line with the equivalent *_iret function if your real-mode service requires it. */ if( _go32_dpmi_allocate_real_mode_callback_retf( &callback_info, &callback_regs ) ) return -1;
/* Fill all the required fields of the '__dpmi_regs' with appropriate values including
the interrupt function number. In the next example the ES:DX register pair was used as a pointer to the handler. Feel free to modify as necessary. */ r.x.es = callback_info.rm_segment; r.x.dx = callback_info.rm_offset;
/* Finally call the required interrupt. */ //__dpmi_int( interrupt_number, &r );
return 0;};
#include <sys/nearptr.h>
#include <crt0.h>
void * MK_FP (unsigned short seg, unsigned short ofs){ if ( !(_crt0_startup_flags & _CRT0_FLAG_NEARPTR) ) if (!__djgpp_nearptr_enable ()) return (void *)0; return (void *) (seg*16 + ofs + __djgpp_conventional_base);};
//extern bool g_bIsVortex;
/*!
Gets value of BIOS timer counter (1/18 of second)*///static DWORD far* g_Timer = (DWORD far*)0x46CL;
//#ifdef INCLUDE_WINDOWS_DEFINITIONS
// #define TIMER() *g_Timer
//#else
// #define TIMER() (GetTickCount() * 10 / 182)
//#endif
const uint8_t CComm485::c_acPort[] = {0x04, 0x03, 0x04, 0x03};//IRQ instead of ISR //{0x0C, 0x0B, 0x0C, 0x0B};
const uint8_t CComm485::c_acMask[] = {0xEF, 0xF7, 0xEF, 0xF7};
CComm485::CComm485():#ifndef COMM_WITHOUT_INTERRUPTS
CIsr(),#endif
CConnection(){ m_pBaseAddress = 0;#ifndef COMM_WITHOUT_INTERRUPTS
m_bFollowControlTimer = true; mbValidableFCT = false;#endif
m_nBase = 0;#ifdef __DJGPP__
memset( &m_OldIsrProc, 0, sizeof(__dpmi_paddr)); memset( &m_CurrentIsrProc, 0, sizeof(__dpmi_paddr));#else
m_pOldIsrProc = NULL;#endif
#ifdef COMM_RS485_PREVENT_ECHO
m_bWrite = false;#endif
m_nBaudRate = 0; m_nBitCount = 0; m_eParity = _NOPARITY;
#ifdef DEBUG_SHOW_COMMUNICATION
m_bShowCommunication = false; m_nShowCommunicationWrite = 0; m_nShowCommunicationRead = 0; m_bShowCommunicationWritten = false;#endif
}
CComm485::~CComm485(){ Destroy();}#ifndef COMM_WITHOUT_INTERRUPTS
void CComm485::SetFCT( bool bFCT ){ m_bFollowControlTimer = bFCT;};#endif
bool CComm485::SetParameters( int32_t nBaudRate, int16_t nBitCount, PARITY eParity ){ if( !m_pBaseAddress ) return false;
if( m_nBaudRate == nBaudRate && m_nBitCount == nBitCount && m_eParity == eParity ) return true;
// finish transmition!!!
Flush();
// prepare configuration
int config; switch( nBitCount ) { case 5: config = LCR_SERIALDB5; break; case 6: config = LCR_SERIALDB6; break; case 7: config = LCR_SERIALDB7; break; case 8: config = LCR_SERIALDB8; break; default: return false; }
// add parity
config |= (uint8_t)eParity;
// Setup baud rate;
outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB );
// remember baudrate for future use
m_nBaudRate = nBaudRate; m_nBitCount = nBitCount; m_eParity = eParity;
// initialize baudrate generator
outportb( m_pBaseAddress + DLL_REGISTER, static_cast<uint8_t> (115200L / nBaudRate) ); outportb( m_pBaseAddress + DLM_REGISTER, 0 );
// Setup configuration
outportb( m_pBaseAddress + LCR_REGISTER, config );
return true;}
bool CComm485::Create( PORTS nPort, int32_t nBaudRate, int16_t nBitCount, PARITY eParity ){ Destroy();
if( !m_InputStream.Create() ) return false;
if( !m_OutputStream.Create() ) return false;
m_nBase = nPort;
m_pBaseAddress = *((short far*)MK_FP(0x0040, 2 * m_nBase )); if(!m_pBaseAddress){#ifdef DEBUG
printf( "COM%d: is Inaccessable\n", nPort+1 );#endif
return false;#ifdef DEBUG
} else { printf( "COM%d: is Accessable\n", nPort+1 );#endif
}; //(_farpeekb (_dos_ds, 0x0047b) & 0x20)
_farpeekb ( _dos_ds, 0x00400 + 2*m_nBase );
/*
int config; switch( nBitCount ) { case 5: config = LCR_SERIALDB5; break; case 6: config = LCR_SERIALDB6; break; case 7: config = LCR_SERIALDB7; break; case 8: config = LCR_SERIALDB8; break; default: return false; }
// Setup baud rate;
outportb( m_pBaseAddress + LCR_REGISTER, LCR_DLAB );
// remember baudrate for future use
m_nBaudRate = nBaudRate;
outportb(m_pBaseAddress + DLL_REGISTER, static_cast<uint8_t> (115200L / nBaudRate));
outportb( m_pBaseAddress + DLM_REGISTER, 0 );
// Setup configuration
outportb( m_pBaseAddress + LCR_REGISTER, config );*/#ifdef DEBUG
printf("Set Com Parameters\n");#endif
if( !SetParameters( nBaudRate, nBitCount, eParity ) ) return false;
// Use FIFO
outportb( m_pBaseAddress + FCR_REGISTER, FCR_ITL14 | FCR_RESETTF | FCR_RESETRF | FCR_FIFO_ENABLE );
#ifndef COMM_WITHOUT_INTERRUPTS
// First thing is to store the old interrupt
//m_pOldIsrProc = _dos_getvect( c_acPort[m_nBase] );
//_dos_setvect( c_acPort[m_nBase], m_pIsrProc );
// Set OUT2 bit to enable interrupts
outportb( m_pBaseAddress + MCR_REGISTER, MCR_IE );
// Enable the interrupts we want
outportb( m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ERBFI );
// Last thing to do is unmask the int in the ICU --> moved to ISR class
//outportb( IMR_REGISTER, c_acMask[m_nBase] & inp(IMR_REGISTER) );
initISR( c_acPort[m_nBase] );#else
// Disable all interrupts
outportb( m_pBaseAddress + IER_REGISTER, 0 );#endif
SetRTS( false ); // clear RTS
// uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
// if( MCR_Status & MCR_RTSC )
// outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
#ifdef DEBUG
printf( "Done Creating COM port\n" );#endif
return true;}
void CComm485::Flush(){#ifdef COMM_WITHOUT_INTERRUPTS
while( !Validate() );#else
// output remaining bytes
while( m_OutputStream.GetCount() ); // wait until output shift register become empty
while( !(inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT) );#endif
}
void CComm485::Destroy(){ if( m_pBaseAddress ) { // finish transmition!!!
Flush();
// Disable FIFO
outportb(m_pBaseAddress + FCR_REGISTER, 0);
#ifndef COMM_WITHOUT_INTERRUPTS
// Disable interrupt
outportb(m_pBaseAddress + MCR_REGISTER, 0); outportb(m_pBaseAddress + IER_REGISTER, 0);
// disable in interrupt controller -> moved to ISR resign
//outportb(IMR_A_REGISTER, ~c_acMask[m_nBase] | inp(IMR_REGISTER));
// return old vector
//_dos_setvect(c_acPort[m_nBase], m_pOldIsrProc );
resignISR();#endif
m_pBaseAddress = 0;#ifdef COMM_RS485_PREVENT_ECHO
m_bWrite = false;#endif
m_nBase = 0; } m_nBaudRate = 0; m_nBitCount = 0; m_eParity = _NOPARITY;}#ifndef COMM_WITHOUT_INTERRUPTS
void CComm485::OnISR(){ // First thing to do in interrupt routines is tell
// the ICU about the service being done. This avoids lock-up
// outportb( ICR_REGISTER, END_OF_INTERRUPT );
while( true ){ uint8_t byte; int LSR_Status;
int IIR_Status = inportb( m_pBaseAddress + IIR_REGISTER ); if( IIR_Status & IIR_PENDING ) break;
IIR_Status &= IIR_INTERRUPT_MASK; // Equal 0x0F
switch( IIR_Status ) { case IIR_BREAK_OR_ERROR: // In order of priority
// It's just important to read the LSR_REGISTER
LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER);
if( LSR_Status & LSR_ERROR_MASK ) { // read error byte from receiver buffer
inportb(m_pBaseAddress + RBR_REGISTER); } break;
case IIR_RECEIVER_READY: case IIR_TIMEOUT: while( true ) { // read byte from receiver buffer
byte = inportb(m_pBaseAddress + RBR_REGISTER);
#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
if( !m_bWrite )#endif
m_InputStream.Put( byte );
// Check when receiver data ready
LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER); if(!(LSR_Status & LSR_DR)) break; } break; case IIR_TRANSMITER_EMPTY: // if there are not bytes for transmit
if( !m_OutputStream.GetCount() ) {#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
m_bWrite = false;#endif
/*if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) )
break; if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT ) ) break; */ // m_nBaudRate m_nBitCount m_eParity
if ( m_bFollowControlTimer ){ // if( getTimerCC() > ( 2 * 65536L/m_nBaudRate ) ){
setTimerCC( (unsigned long) ( 2 * 65536L/m_nBaudRate ) ); //( m_nBitCount+2 ) );
mbValidableFCT = true; // };
break; } else { SetRTS( false ); }; // clear RTS
// uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
// if( MCR_Status & MCR_RTSC )
// outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
// enable receiver and disable transmitter
outportb(m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ERBFI );
break; }
#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
m_bWrite = true;#endif
// while there are bytes in output stream
while(1){ // check for transmitter buffer overflow
LSR_Status = inportb(m_pBaseAddress + LSR_REGISTER); if(!(LSR_Status & LSR_THRE)) break;
// get new byte for transmit
if( !m_OutputStream.Get( &byte ) ) break;
// write byte into FIFO
outportb(m_pBaseAddress + THR_REGISTER, byte ); } break;
case IIR_MODEM_CHANGE_STATUS: // Must read the MSR_REGISTER or will lock up the system!
inportb(m_pBaseAddress + MSR_REGISTER); break; } }}#endif // ! COMM_WITHOUT_INTERRUPTS
#ifdef COMM_WITHOUT_INTERRUPTS
bool CComm485::Validate(){ uint8_t byte; if( !m_pBaseAddress ) return true;
// check errors
// while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_ERROR_MASK )
while( inportb(m_pBaseAddress + LSR_REGISTER) & ( LSR_FIFOE | LSR_FE | LSR_PE | LSR_OE ) ) { // read error byte from receiver buffer
inportb(m_pBaseAddress + RBR_REGISTER); }
// check receiver
while( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_DR ) { // read byte from receiver buffer
byte = inportb(m_pBaseAddress + RBR_REGISTER);
#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
if( !m_bWrite )#endif
m_InputStream.Put( byte ); }
// if there are not bytes for transmit
if( m_OutputStream.GetCount() ) { // set RTS
SetRTS( true );// uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
// if( !(MCR_Status & MCR_RTSC) )
// outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC );
#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
m_bWrite = false;#endif
// if FIFO empty
if( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) { // while there are bytes in output stream
for( int16_t i = 0; i < 16; i++ ) { // get new byte for transmit
if( !m_OutputStream.Get( &byte ) ) break;
// write byte into FIFO
outportb(m_pBaseAddress + THR_REGISTER, byte ); } } }
// check for transmit end
if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) ) return false; if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT ) ) return false;
SetRTS( false ); // clear RTS
// uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
// if( MCR_Status & MCR_RTSC )
// outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC );
#ifdef COMM_RS485_PREVENT_ECHO
// check for echo
m_bWrite = false;#endif
if( m_OutputStream.GetCount() ) return false; return true;}#else // COMM_WITHOUT_INTERRUPTS
bool CComm485::ValidateRTS(){ if( mbValidableFCT ){ if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_THRE ) ) return false; if( !( inportb(m_pBaseAddress + LSR_REGISTER) & LSR_TEMPT ) ) return false; SetRTS( false ); mbValidableFCT = false; }; return true;}#endif //COMM_WITHOUT_INTERRUPTS
DWORD CComm485::Write( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ){ // unused parameter
dwTimeout; int16_t nWritten = 0;
nWritten = m_OutputStream.Put( pBuffer, dwSize ); // Direct Write to the port
#ifdef DEBUG_SHOW_COMMUNICATION
if( m_bShowCommunication ) { printf( "\nW%d: ", m_nShowCommunicationWrite++ );
for( DWORD i = 0; i < nWritten; i++ ) printf( "%02X", (uint8_t)pBuffer[i] );
m_bShowCommunicationWritten = true; }#endif
#ifndef COMM_WITHOUT_INTERRUPTS
SetRTS( true ); // set RTS
// uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER );
// if( !(MCR_Status & MCR_RTSC) )
// outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC );
// enable transmitter and disable receiver
outportb(m_pBaseAddress + IER_REGISTER, IER_EDSSI | IER_ELSI | IER_ETBEI | IER_ERBFI );#endif
return nWritten;}DWORD CComm485::ReadWithWait( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ){ DWORD dwRead = 0; DWORD dwCount = 0; DWORD dwSTIME = TIMER(); while( ( dwCount < dwSize ) && ( ( TIMER() - dwSTIME ) < dwTimeout ) ){ dwCount = m_InputStream.Get( pBuffer+dwRead, dwSize-dwRead ); dwRead += dwCount; }; return dwRead;};DWORD CComm485::Read( IN PBYTE pBuffer, IN DWORD dwSize, IN DWORD dwTimeout ){ // unused parameter
dwTimeout;
DWORD dwRead = 0;
dwRead = m_InputStream.Get( pBuffer, dwSize );
#ifdef DEBUG_SHOW_COMMUNICATION
if( m_bShowCommunication ) { if( dwRead && m_bShowCommunicationWritten ) { printf( "\nR%d: ", m_nShowCommunicationRead++ ); m_bShowCommunicationWritten = false; }
for( DWORD i = 0; i < dwRead; i++ ) printf( "%02X", (uint8_t)pBuffer[i] ); }#endif
// read bytes from receive stream
return dwRead;}
bool CComm485::SetRTS( bool bOn ){ // set/clear RTS
uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); if( bOn ) { //putch(37);//%
if( !(MCR_Status & MCR_RTSC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_RTSC ); } else { //putch(36);//$
if( MCR_Status & MCR_RTSC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_RTSC ); }; return bOn;}
bool CComm485::SetDTR( bool bOn ){ // set/clear DTR
uint8_t MCR_Status = inportb( m_pBaseAddress + MCR_REGISTER ); if( bOn ) { //putch(35);//#
if( !(MCR_Status & MCR_DTRC) ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status | MCR_DTRC ); } else { //putch(33);//!
if( MCR_Status & MCR_DTRC ) outportb( m_pBaseAddress + MCR_REGISTER, MCR_Status & ~MCR_DTRC ); }; return bOn;};
|