//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright ?2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 08/10/2003 // PROJECT: // SUBSYSTEM: // FILE: Main.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of Main.cpp //---------------------------------------------------------------------------- #include #include #include #include #include /*! \arg \c idx number of 32x port. \details Reads North bridge register. The \a idx is port number to read 32 bit value from. */ unsigned long read_nb(unsigned char idx){ unsigned long retval; _asm mov dx, 0cf8h __emit__(0x66); __emit__(0xb8); __emit__(0x00); __emit__(0x00); __emit__(0x00); __emit__(0x80); _asm mov al, idx __emit__(0x66); _asm out dx, ax _asm mov dx, 0cfch __emit__(0x66); _asm in ax, dx __emit__(0x66); _asm mov WORD PTR retval, ax return retval; }; // Write north bridge register void write_nb(unsigned char idx, unsigned long val){ _asm mov dx, 0cf8h __emit__(0x66); __emit__(0xb8); __emit__(0x00); __emit__(0x00); __emit__(0x00); __emit__(0x80); _asm mov al, idx __emit__(0x66); __emit__(0xef); _asm mov dx, 0cfch __emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08); __emit__(0x66); __emit__(0xef); }; // Read south bridge register unsigned long read_sb(unsigned char idx){ unsigned long retval; _asm mov dx, 0cf8h //80009000h // mov eax, 80003800h __emit__(0x66); __emit__(0xb8); //__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80); __emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80); _asm mov al, idx // out edx, eax __emit__(0x66); _asm out dx, ax _asm mov dx, 0cfch // in eax, edx __emit__(0x66); _asm in ax, dx // mov retval, eax __emit__(0x66); _asm mov WORD PTR retval, ax return retval; }; // Write south bridge register /*void write_sb(unsigned char idx, unsigned long val){ __asm{ mov dx, 0cf8h mov eax, 80009000h mov al, idx out dx, eax mov dx, 0cfch mov eax, DWORD PTR val out dx, eax } };*/ void make_ws8( void ){ unsigned long retval; _asm mov dx, 0cf8h __emit__(0x66); __emit__(0xb8); __emit__(0x50); __emit__(0x90); __emit__(0x00); __emit__(0x80); __emit__(0x66); _asm out dx, ax _asm mov dx, 0cfch __emit__(0x66); _asm in ax, dx __emit__(0x66); _asm mov WORD PTR retval, ax retval = retval | 0x0000F000UL; _asm mov dx, 0cf8h __emit__(0x66); __emit__(0xb8); __emit__(0x50); __emit__(0x90); __emit__(0x00); __emit__(0x80); __emit__(0x66); _asm out dx, ax _asm mov dx, 0cfch __emit__(0x66); _asm mov ax, WORD PTR retval __emit__(0x66); _asm out dx, ax }; // Write south bridge register void write_sb( unsigned char idx, unsigned long val ){ _asm mov dx, 0cf8h //80009000h // mov eax, 80003800h __emit__(0x66); __emit__(0xb8); //__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80); __emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80); _asm mov al, idx // out dx, eax __emit__(0x66); __emit__(0xef); _asm mov dx, 0cfch // mov eax, val __emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08); // out dx, eax __emit__(0x66); __emit__(0xef); }; unsigned char read_sbb( unsigned char idx ){ unsigned char retval; _asm mov dx, 0cf8h // mov eax, 80009000h __emit__(0x66); __emit__(0xb8); __emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80); _asm mov al, idx // out edx, eax //__emit__(0x66); __emit__(0xef); __emit__(0x66); _asm out dx, ax /*_asm mov dx, 0cfch // in eax, edx __emit__(0x66); _asm in al, dx // mov retval, eax __emit__(0x66); _asm mov BYTE PTR retval, al */ retval = inportb( 0x0CFC ); return retval; }; unsigned int read_sbw( unsigned char idx ){ unsigned int retval; _asm mov dx, 0cf8h // mov eax, 80009000h __emit__(0x66); __emit__(0xb8); __emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80); _asm mov al, idx // out edx, eax //__emit__(0x66); __emit__(0xef); __emit__(0x66); _asm out dx, ax /*_asm mov dx, 0cfch // in eax, edx __emit__(0x66); _asm in al, dx // mov retval, eax __emit__(0x66); _asm mov BYTE PTR retval, al */ retval = inport( 0x0CFC ); return retval; }; // Write south bridge register void write_sbb( unsigned char idx, unsigned char val ){ _asm mov dx, 0cf8h //80009000h // mov eax, 80003800h __emit__(0x66); __emit__(0xb8); //__emit__(0x00); __emit__(0x38); __emit__(0x00); __emit__(0x80); __emit__(0x00); __emit__(0x90); __emit__(0x00); __emit__(0x80); _asm mov al, idx // out dx, eax __emit__(0x66); __emit__(0xef); outportb( 0x0CFC, val ); /*_asm mov dx, 0cfch // mov eax, val __emit__(0x66); __emit__(0x8b); __emit__(0x46); __emit__(0x08); // out dx, eax __emit__(0x66); __emit__(0xef);*/ }; #ifndef NO_WDT2_INIT #include "clock.h" #endif #include "Board.h" #include "Comm485.h" #include "Kernel.h" #include "Shadow.h" #include "Screen.h" #include "..\lglib\version.h" DECLARE_VARIABLE( CBoard, g_Board ); DECLARE_VARIABLE( CComm485, g_LgsComm485 ); DECLARE_VARIABLE( CComm485, g_CameraComm485 ); DECLARE_VARIABLE( CKernel, g_Kernel ); //CKernel g_Kernel; CShadow g_Shadow; #ifdef ALS_300 // Shadow for second LRF CShadow g_ShadowS( TRUE ); #endif void StartAfterExit( PCHAR ); #ifdef COMPILE_AS_ANGLES void AnglesProc(); #endif #if 0 void CheckHeap( LPCTSTR pText ) { printf( "%s\n", pText ); if( farheapcheck() < 0 ) { printf( "far heap is corrupted\n" ); return; } unsigned long free_size = 0; unsigned long free_count = 0; unsigned long in_use_size = 0; unsigned long in_use_count = 0; struct farheapinfo fhi; fhi.ptr = NULL; while( farheapwalk( &fhi ) == _HEAPOK ) { if( fhi.in_use ) { in_use_count ++; in_use_size += (unsigned long)fhi.size; } else { free_count ++; free_size += (unsigned long)fhi.size; } } printf( "far heap:\n" "free %ld blocks, size %ld\n" "in-use %ld blocks, size %ld\n", free_count, free_size, in_use_count, in_use_size ); } #else #define CheckHeap #endif /*! Checks type of PC-104 Board Reading BSR into \a CHID then if \a CHID one of following: \b 0x31504d44 then board is \b Vortex86SX \b 0x32504d44 then board is \b Vortex86DX \b 0x33504d44 then board is \b Vortex86MX in any match returning \b TRUE */ BOOL IsVortex( void ){ unsigned long CHID; unsigned int CC; CHID = read_nb(0x90); if( CHID == 0x31504d44L ) return TRUE; if( CHID == 0x32504d44L ) return TRUE; if( CHID == 0x33504d44L ) return TRUE; /*! Returnung \b FALSE if no Board ID is matched */ // Old PC - Wait State long //make_ws8(); return FALSE; }; BOOL g_bShowErrors = TRUE; BOOL g_bDelayErrors = TRUE; BOOL g_bHideFogError = TRUE; BOOL g_bIsVortex = FALSE; BOOL g_UI_Debug = FALSE; BOOL g_bWorkEnable = FALSE; BOOL g_bIsDMABoard = FALSE; #if defined( COMPILE_3DSCANER ) | defined( COMPILE_RTSCANER ) BOOL g_bBoardReceiverMode = TRUE; #else BOOL g_bBoardReceiverMode = FALSE; #endif #ifdef COMPILE_AS_GETFILE BOOL g_bWatchDogEnable = FALSE; #else #ifdef ALS_300 BOOL g_bWatchDogEnable = TRUE; // ALS 300 is too slow for board reset TO DO: change Watchdog init for ALS_300 #else #ifdef NO_WDT2_INIT #ifdef WATCHDOG_ENABLED BOOL g_bWatchDogEnable = FALSE; #else BOOL g_bWatchDogEnable = FALSE; #endif #else #ifdef WATCHDOG_ENABLED BOOL g_bWatchDogEnable = TRUE; #else BOOL g_bWatchDogEnable = FALSE; #endif #endif #endif #endif BYTE g_bNoBlackTargets = 0; DWORD g_dwStartupTime = 0; #ifdef LGS_REDUCED_PRF INT LGS_PRF_REDUCE_RATIO = 1; INT LGS_MAX_DETECTOR_DISTANCE = 30; #endif /*! \arg argc Passed agruments count. \arg argv Array of strings vith arguments passed to the Application. \details The parameters that may be passed: \li \b h, \b h, \b v, \b V or \b ? Version output and return from Application \li \b u or \b U Hide all errors \li \b f or \b F UnHide Fog errors \li \b z or \b Z Disable delay error output \li \b u or \b D Debug output enabled All parameters to be passed mast have prefix \b / or \b - or \b \\ and space between combined parameters */ void main( int argc, char * argv[] ){ #ifndef NO_VORTEX_CHECK g_bIsVortex = IsVortex(); #else g_bIsVortex = FALSE; #endif #ifndef NO_WDT2_INIT if( g_bIsVortex ) init_sys_clock(); #endif CheckHeap( "Start" ); #ifdef COMPILE_AS_GETFILE // Startup delay for PLS //#ifndef LGS_MIRROR //After bootloader all version have delay BOOL BoardCommOK = FALSE; #ifdef LGS_STEPPER delay( 15000 );// Wait 15 seconds anyway #endif //printf( "SpinUp Motor " ); #ifdef LGS_PLS_1 INT nono = 600; // 1.5 minute #else // ALS LGS4S INT nono = 60; // 6 seconds #endif BYTE TestBYTE = inportb( NEW_ID_ADDRESS ); while ( nono && TestBYTE!= 0x5A && TestBYTE!= 0xA5 && TestBYTE != 0xAA ){ nono--; TestBYTE = inportb( NEW_ID_ADDRESS ); delay( 100 ); //printf( "." ); }; BoardCommOK = ( TestBYTE== 0x5A || TestBYTE== 0xA5 || TestBYTE == 0xAA ); //#endif #endif #ifdef DEBUG_PRINTOUT_MSGS textmode(64); if ( argc > 1 ) printf("Checking Command Line Parameters ..."); #endif // printf software version if ( argc > 1 ) { switch( argv[1][0] ) { case '-': case '\\': case '/': switch( argv[1][1] ) { case 'h': case 'H': case 'v': case 'V': case '?': printf( "Software version <%s>\n", g_Kernel.s_pSoftwareVersion ); return; case 'u': case 'U': g_bShowErrors = FALSE; break; case 'f': case 'F': g_bHideFogError = FALSE; break; case 'z': case 'Z': g_bShowErrors = TRUE; g_bHideFogError = FALSE; g_bDelayErrors = FALSE; break; case 'd': case 'D': g_UI_Debug = TRUE; break; case 'b': case 'B': g_bBoardReceiverMode = TRUE; break; case 'w': case 'W': g_bWatchDogEnable = FALSE; break; } break; } } #ifdef DEBUG_PRINTOUT_MSGS if ( argc > 1 ) printf("Done\n"); #endif #ifdef DEBUG_SHOW_COMMUNICATION g_LgsComm485.ShowCommunication( FALSE ); g_CameraComm485.ShowCommunication( TRUE ); #endif #ifndef COMPILE_AS_ANGLES LONG nSpeed = LGS_COMM_BAUDRATE; #ifndef COMPILE_AS_GETFILE // load comm speed FILE *pFile = fopen( "SERIAL.INI", "rt" ); if( pFile ){ fscanf( pFile, "%ld\n", &nSpeed ); fclose( pFile ); } #endif // #ifndef COMPILE_AS_GETFILE #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin( "Creating COM port"); #endif if( !g_LgsComm485.Create( CComm485::_COM2, nSpeed, 8, CComm485::_NOPARITY ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif } #endif // #ifndef COMPILE_AS_ANGLES // assume that no camera LGS::CAMERA_PARAMETERS cp; cp.eType = LGS::CAMERA_TYPE_NONE; cp.nShift = 0; cp.bAuto = FALSE; cp.nAutoDelay = 0; // assume that no relay LGS::RELAY_PARAMETERS rp; rp.bAuto = FALSE; rp.nAutoDelay = 0; rp.bNormallyClose = FALSE; // no voltage control by default LGS::VOLTAGE_PARAMETERS vp; vp.bControl = FALSE; // both lasers enabled LGS::LRF_DISABLE m_LRFDisable; m_LRFDisable.bMaster = FALSE; m_LRFDisable.bSlave = FALSE; // load settings #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin( "MAIN:Load General Settings" ); #endif BYTE *m_pSerialNumber, m_nSerialNumber, m_nAddress; g_Kernel.LoadSettings( (PCHAR)m_pSerialNumber, &m_nSerialNumber, &m_nAddress, &cp, &rp, &vp, &m_LRFDisable ); // load user settings AUTO_RESET AutoReset; BOARD_SETTINGS BoardSettings; BoardSettings.nHID = 255; SWITCHER_SETTINGS SwitcherSettings; #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin("MAIN:Loading User Settings"); #endif g_Kernel.LoadUserSettings( &AutoReset, &BoardSettings, &SwitcherSettings, &g_LgsComm485, &g_CameraComm485 ); #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin("MAIN:Loading HWR Settings"); #endif g_Kernel.LoadFSSettings( &AutoReset, &BoardSettings, &SwitcherSettings, &g_LgsComm485, &g_CameraComm485); #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); debugProcBegin("MAIN:Board Init"); #endif //Telling the board of Relay Default mode if( rp.bAuto ){ BoardSettings.RelayDefaultMode = rp.bNormallyClose; } else { BoardSettings.RelayDefaultMode = FALSE; }; //g_CameraComm485.SetRTS( TRUE ); //delay(1); //g_CameraComm485.SetRTS( FALSE ); if( !g_Board.Create( &BoardSettings ) ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif }; #ifdef COMPILE_AS_GETFILE // Startup delay for PLS //#ifndef LGS_MIRROR //After Boot-loader all version have small delay of startup if( BoardCommOK ){ // printf( "ChangeSpeed Motor" ); #ifdef LGS_PLS_1 INT nono = 600; // 1 minute #else // ALS INT nono = 60; // 6 seconds #endif BYTE TestBYTE = inportb( NEW_ID_ADDRESS ); while ( nono && TestBYTE!= 0x5A && TestBYTE!= 0xA5 && TestBYTE != 0xAA ){ nono--; TestBYTE = inportb( NEW_ID_ADDRESS ); delay( 100 ); //printf( "." ); }; }; //#endif //LGS_MIRROR #endif #ifdef COMPILE_AS_ANGLES AnglesProc(); #endif #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin("Target Shadowing"); #endif if( !g_Shadow.Create() ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif }; #ifdef ALS_300 if( !g_ShadowS.Create() ){ #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif }; #endif #ifdef DEBUG_GRAPH_SCREEN if( !g_Screen.Create() ){ printf( "Can't create screen\n" ); return; } else if (g_UI_Debug) printf("Done\n"); #endif #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin("Create Kernel"); #endif #ifdef ALS_300 if( !g_Kernel.Create( &g_Board, &g_LgsComm485, &g_Shadow, &g_ShadowS, &g_CameraComm485, CComm485::_COM1 ) ){ #else if( !g_Kernel.Create( &g_Board, &g_LgsComm485, &g_Shadow, &g_CameraComm485, CComm485::_COM1 ) ){ #endif #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd( 0 ); #endif return; } else { #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif }; #ifdef DEBUG_PRINTOUT_MSGS debugProcBegin("Working"); #endif CheckHeap( "Before run" ); BOOL bRestart = g_Kernel.Run(); CheckHeap( "After run" ); #ifdef DEBUG_PRINTOUT_MSGS debugProcEnd(); #endif //g_Board.Destroy(); g_Kernel.Destroy(); g_Shadow.Destroy(); #ifdef DEBUG_GRAPH_SCREEN g_Screen.Destroy(); #endif g_Board.Destroy(); g_LgsComm485.Destroy(); g_CameraComm485.Destroy(); //if( bRestart && argc ) // StartAfterExit( argv[0] ); CheckHeap( "Done" ); #ifndef NO_WDT2_INIT if( g_bIsVortex ) stop_sys_clock(); #endif } void WriteToKeyboardBuffer( CHAR a ); void StartAfterExit( PCHAR pFileName ) { // "type" for( INT i = 0; i < strlen( pFileName ); i++ ) WriteToKeyboardBuffer( pFileName[i] ); // "press enter" WriteToKeyboardBuffer( (char) 13 ); //'\n' ); // WriteToKeyboardBuffer( '\r' ); } void WriteToKeyboardBuffer( CHAR a ) { unsigned far *bufbegin; unsigned far *bufend; unsigned far *buftail; unsigned far *buffer; // set up buffer pointers FP_SEG(bufbegin)=FP_SEG(bufend)=FP_SEG(buftail)=FP_SEG(buffer)=0x40; FP_OFF(bufbegin)=0x80; FP_OFF(bufend)=0x82; FP_OFF(buftail)=0x1c; disable(); FP_OFF(buffer)=*buftail; *buffer++=a; if ( FP_OFF(buffer) >= *bufend ) FP_OFF(buffer)=*bufbegin; *buftail=FP_OFF(buffer); enable(); } #ifdef COMPILE_AS_ANGLES void AnglesProc() { INT nAngle = 0; BOOL bFirstAngle = TRUE; INT nZeroCount = 0; BOOL bZeroFirst = TRUE; INT nZeroErrorCount = 0; INT nAngleErrorCount = 0; while( TRUE ) { BOOL bZero; BOOL pSun[2]; INT pRange[2]; if( g_Board.GetData( &bZero, pSun, pRange ) ) { if( bZero ) { if( bFirstAngle ) bFirstAngle = FALSE; else { g_CRT.IntOut( 0, 0, nAngle + 1 ); if( (nAngle + 1) != LGS_MAX_DETECTOR_ANGLES ) g_CRT.IntOut( 10, 0, ++nAngleErrorCount ); } if( bZeroFirst ) nZeroCount = 1; else { nZeroCount++; g_CRT.IntOut( 10, 1, ++nZeroErrorCount ); } g_CRT.IntOut( 0, 1, nZeroCount ); bZeroFirst = FALSE; nAngle = 0; } else { bZeroFirst = TRUE; nAngle++; } } } } #endif