For DOS Protected Mode version of sensor core software
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

1212 lines
32 KiB

//----------------------------------------------------------------------------
// Laser Guard Ltd.
// Copyright © 2000-2002. All Rights Reserved.
// Version 1.0 01/01/2000
// Last modification: 08/10/2003
// PROJECT:
// SUBSYSTEM:
// FILE: board_.cpp
// AUTHOR: Sergey Chizhevskiy
// OVERVIEW
// ~~~~~~~~
// Source file of board_.cpp
//----------------------------------------------------------------------------
#include "board.h"
#ifdef WATCHDOG_ENABLED
#include "watchdog.h"
#endif
CBoard::CBoard( PISRPROC pIsrProc ) : CIsr( pIsrProc ){
m_pOldIsrProc = NULL;
mOld_TV = clock();
m_wTimer = 0;
mwBoardID=0;
mwSoftwareID=0;
g_bIsDMABoard = FALSE;
//printf("Board's Constructor\n");
#ifndef DEFAULT_DISC_C
if ( g_bIsVortex ){
nHID = 0;
} else {
nHID = 2;
};
#else
nHID = 2;
#endif
g_bFlashDriveID = nHID;
BSNS = NULL;
BFCD = NULL;
BSCD = NULL;
bl_day = 0;
bl_month = 0;
bl_year = 0;
fw_day = 0;
fw_month = 0;
fw_year = 0;
#ifndef BOARD_DEFFERED_PROCESSING
m_bReady = FALSE;
memset( &m_BoardData, 0, sizeof(BOARDDATA) );
#endif
// unknown type of board
m_nBoardType = BT_NONE;
m_bBoardMCU = FALSE;
m_bBoardBLO = FALSE;
m_bOutputDirty = FALSE;
m_Settings.M_LRFDisable = TRUE;
m_Settings.S_LRFDisable = TRUE;
#ifdef DEBUG_ENABLE_OLD_BOARD
// used for A/D conversion
s_pDataArray[0] = s_pDataArray[1] = 0;
#endif
// reset mask
m_bOutputMask = 0;
}
CBoard::~CBoard(){
if( BSNS != NULL ){
free( BSNS );
};
if( BFCD != NULL ){
free( BFCD );
};
if( BSCD != NULL ){
free( BSCD );
};
UpdateLicensing();
Destroy();
}
#ifdef DEBUG_ENABLE_OLD_BOARD
BOOL CBoard::InitOldBoard(){
g_bWorkEnable=TRUE;
// try to initialize old board
static short infoArray[11];// = { 0, 0x300, 5, 3, 12, 20, 0, 0, 0, 0, 0 };
infoArray[0] = 0; // result
infoArray[1] = 0x300; // base address
infoArray[2] = 5; // interrupt level
infoArray[3] = 3; // DMA level
infoArray[4] = 12; // A/D resolution
infoArray[5] = 20; // A/D scan interval
infoArray[6] = 0; // reserved
infoArray[7] = 0; // reserved
infoArray[8] = 0; // reserved
infoArray[9] = 0; // reserved
infoArray[10] = 0; // reserved
// initialize board
SMM( 0, infoArray );
if( infoArray[0] )
return FALSE;
infoArray[1] = 1; // Number of conversions
infoArray[2] = FP_SEG(s_pDataArray);
infoArray[3] = FP_OFF(s_pDataArray);
infoArray[4] = 1; // Operation type
infoArray[5] = 0; // Gain setting
infoArray[6] = 0; // Low channel
infoArray[7] = 1; // High channel
infoArray[8] = 0; // reserved
infoArray[9] = 0; // reserved
infoArray[10] = 0; // reserved
// Install ISR for Sapphire
SMM(4, infoArray);
if( infoArray[0] )
return FALSE;
// fix irq 7
asm
{
mov AL, 0xDE
out 0xF2, AL
in AL, 0xF3
and AL, 0xF3
out 0xF3, AL
}
// set type of board
m_nBoardType = BT_OLD;
// install interrupt handler
_disable();
m_pOldIsrProc = _dos_getvect( OLD_INTERRUPT_BOARD );
_dos_setvect( OLD_INTERRUPT_BOARD, m_pIsrProc );
#ifndef BOARD_BIND_IRQ_TO_SAPHIR
outportb(PICIMR, inportb(PICIMR) & OLD_INTERRUPT_MASK );
inportb( OLD_BASE_ADDRESS ); // Reset interrupt trigger
// outportb( OLD_BASE_ADDRESS, 0x01 );
#endif
_enable();
#ifdef RELAY_ACTIVATE_BY_ZERO
m_bOutputMask = 0x0;
#else
m_bOutputMask = 0x0;
#endif
// Enable two lasers
outportb( OLD_BASE_ADDRESS, m_bOutputMask |= 0x07 );
return TRUE;
}
#endif
BYTE validateHexImage( char *FileName, BYTE MSB, BYTE LSB ){
FILE *fileID = NULL;
BYTE RetValue = 0;
fileID = fopen( FileName, "r" );
char Sig;
BYTE InpB;
BOOL inLine = FALSE;
BYTE Stage = 0;
BYTE RecLen = 0;
BYTE CRec = 0;
WORD OffSet = 0;
WORD RecType = 0;
BYTE RCheck = 0;
WORD CCheck = 0;
char InpC[3];
char InpW[5];
if( fileID ){
RetValue = 1;
while( !feof( fileID ) && RetValue == 1 ){
if( !inLine ){
fread( &Sig, 1, 1, fileID );
if( Sig == ':' ) inLine = TRUE;
CCheck = 0;
Stage = 0;
} else {
switch( Stage ){
case 0:{
fread( &InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &RecLen ) == 0 ) RetValue = 0;
CCheck = RecLen;
Stage = 1;
break;
};
case 1:{
fread( &InpW, 4, 1, fileID );
InpW[4]='\0';
if( sscanf( InpW, "%x", &OffSet )== 0 ) RetValue = 0;
CCheck = ( CCheck + (OffSet >> 8 ) + (OffSet & 0xFF ) ) & 0xFF ;
CRec = 0;
Stage = 2;
break;
};
case 2:{
fread( &InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &RecType ) == 0 ) RetValue = 0;
CCheck = ( CCheck + RecType ) & 0xFF ;
Stage = 3;
break;
};
default:{
fread( &InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &InpB ) == 0 ) RetValue = 0;
CCheck = ( CCheck + InpB ) & 0xFF ;
if( CRec == RecLen ){
inLine = FALSE;
if( CCheck != 0 ) RetValue = 0;
} else CRec++;
};
};
};
};
fclose( fileID );
};
return RetValue;
};
FILE *FirmwarefileID = NULL;
volatile BOOL FirmwareUpgradeDone = FALSE;
void interrupt FirmwareUpdaterISR( ... ){
BOOL LineIsSent;
BYTE S_BUFFER[21];
char Sig;
BYTE InpB;
BOOL inLine = FALSE;
BYTE Stage = 0;
BYTE RecLen = 0;
BYTE CRec = 0;
WORD OffSet = 0;
WORD RecType = 0;
BYTE RCheck = 0;
WORD CCheck = 0;
char InpC[3];
char InpW[5];
if( !feof( FirmwarefileID ) ){
LineIsSent = FALSE;
inLine = FALSE;
while( !feof( FirmwarefileID ) && !LineIsSent ){
if( !inLine ){
fread( &Sig, 1, 1, FirmwarefileID );
if( Sig == ':' ) inLine = TRUE;
CCheck = 0;
Stage = 0;
} else {
switch( Stage ){
case 0:{
fread( &InpC, 2, 1, FirmwarefileID );
InpC[2]='\0';
sscanf( InpC, "%x", &RecLen );
CCheck = RecLen;
S_BUFFER[0] = RecLen;
//outportb(NEW_BASE_ADDRESS + 0x05, RecLen );
Stage = 1;
break;
};
case 1:{
fread( &InpW, 4, 1, FirmwarefileID );
InpW[4]='\0';
sscanf( InpW, "%x", &OffSet );
CCheck = ( CCheck + (OffSet >> 8 ) + (OffSet & 0xFF ) ) & 0xFF ;
S_BUFFER[2] = (OffSet >> 8 ) & 0xFF;
S_BUFFER[3] = OffSet & 0xFF;
CRec = 0;
Stage = 2;
break;
};
case 2:{
fread( &InpC, 2, 1, FirmwarefileID );
InpC[2]='\0';
sscanf( InpC, "%x", &RecType );
CCheck = ( CCheck + RecType ) & 0xFF ;
S_BUFFER[1] = RecType;
Stage = 3;
break;
};
default:{
fread( &InpC, 2, 1, FirmwarefileID );
InpC[2]='\0';
sscanf( InpC, "%x", &InpB );
CCheck = ( CCheck + InpB ) & 0xFF ;
S_BUFFER[ 4 + CRec ] = InpB;
if( CRec == RecLen ){ //Send To the board
outp( PICICR, EOI );
inLine = FALSE;
outportb( NEW_RST_ADDRESS, S_BUFFER[ 0 ] );
CRec = 1;
while( CRec < ( RecLen + 5 ) ){
outportb( NEW_DTA_ADDRESS, S_BUFFER[ CRec ] );
CRec++;
};
LineIsSent = TRUE;
//Confirm Record
outportb( NEW_BASE_ADDRESS + 0x05, 0xFF );
} else CRec++;
};
};
};
};
};
if( RecType == 0x01 ){
FirmwareUpgradeDone = TRUE;
};
};
BOOL StartDownloadHexImage( char *FileName, BYTE MSB, BYTE LSB ){
FirmwarefileID = fopen( FileName, "r" );
if( FirmwarefileID ){
//Disable Watchdog
unsigned char valb;
outp(0x22, 0x13); // lock register
outp(0x23, 0xc5); // unlock config
outp(0x22, 0x37);
valb = inp(0x23);
outp(0x22, 0x37);
outp( 0x23, valb & 0xBF);
outp(0x22,0x13); //Lock reg
outp(0x22,0x00); // Lock config
// Instal IRQ Handler
// m_pOldIsrProc = _dos_getvect( NEW_INTERRUPT_BOARD );
outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
delay( 5000 );
int nono = 50;
unsigned char TestBYTE = inportb( NEW_BL_ADDRESS );
while ( nono && ( TestBYTE!= 0x5A || TestBYTE!= 0xA5 || TestBYTE == 0xAA ) ){
nono--;
TestBYTE = inportb( NEW_BL_ADDRESS );
delay(100);
};
if( !( TestBYTE == 0xAA || TestBYTE == 0x55 ) ) return FALSE;
_disable();
_dos_setvect( NEW_INTERRUPT_BOARD, FirmwareUpdaterISR );
outp( PICIMR, inportb(PICIMR) & NEW_INTERRUPT_MASK );
_enable();
FirmwareUpgradeDone = FALSE;
FirmwareUpdaterISR();
while( !FirmwareUpgradeDone ) delay( 1 );
fclose( FirmwarefileID );
FirmwarefileID = NULL;
};
return TRUE;
};
#ifdef LGS_MIR22ROR
BOOL CBoard::InitNewBoard(){
//#ifdef RELAY_ACTIVATE_BY_ZERO
// m_bOutputMask = BE_NEW_DC0;
//#else
// m_bOutputMask = 0;
//#endif
// disable laser devices
//outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
#ifdef BOARD_LAZY_INIT
delay(2000); // Wait 2 seconds
#endif
// check if this is a new board
unsigned char TestBYTE;
int nono = 50;
TestBYTE = inportb( NEW_ID_ADDRESS );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Checking Board Type");
#endif
while ( nono && TestBYTE!= 0x5A && TestBYTE!= 0xA5 && TestBYTE != 0xAA ){
nono--;
TestBYTE = inportb( NEW_ID_ADDRESS );
delay(1);
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
if( TestBYTE == 0x5A ){
m_bBoardMCU = TRUE;
m_bBoardBLO = FALSE;
g_bIsDMABoard = FALSE;
#ifdef DEBUG_PRINTOUT_MSGS
debug( "Board in IRQ mode" );
#endif
};
if( TestBYTE == 0xA5 ){
m_bBoardMCU = TRUE;
m_bBoardBLO = FALSE;
g_bIsDMABoard = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board in DMA mode");
#endif
};
if( TestBYTE == 0xAA ){
m_bBoardBLO = TRUE;
m_bBoardMCU = TRUE;
g_bIsDMABoard = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board in BootLoader mode");
#endif
};
if( TestBYTE != 0xA5 && TestBYTE != 0x5A && TestBYTE != 0xAA ){
m_bBoardBLO = FALSE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Oy Vey! Board is Old!");
#endif
#ifdef RESTART_PC_BY_WATCHDOG
//No Board Init -> Restart WDT
outportb( NEW_WDT_ADDRESS, 0xAA ); // Try To restart Board
#ifdef WATCHDOG_ENABLED
CWatchDog m_WatchDog;
m_WatchDog.Create();
m_WatchDog.Update( 255 );
while(1);
#endif
#endif
};
BSNLB=0;
BSNHB=0;
// if not MCU board
if( m_bBoardMCU ){
if( g_bIsDMABoard ){
outportb( NEW_RST_ADDRESS, 5 ); // Load 2 Bytes:
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Status - LRF Off
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
// nessesary to make delay
#ifdef DEBUG_PRINTOUT_MSGS
debug("Stoped DMA Transfers (if Any )");
#endif
if( g_bWorkEnable == FALSE ){
delay(1);
outportb( NEW_RST_ADDRESS, 0 ); // Load Full Data
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Read BSN");
#endif
delay(1);
BSNLB = inportb( NEW_DTA_ADDRESS ); // 1
delay(1);
BSNHB = inportb( NEW_DTA_ADDRESS ); // 2
delay(1);
fw_year = inportb( NEW_DTA_ADDRESS ); // 3
delay(1);
fw_month = inportb( NEW_DTA_ADDRESS ); // 4
delay(1);
fw_day = inportb( NEW_DTA_ADDRESS ); // 5
delay(1);
bl_year = inportb( NEW_DTA_ADDRESS ); // 6
delay(1);
bl_month = inportb( NEW_DTA_ADDRESS ); // 7
delay(1);
bl_day = inportb( NEW_DTA_ADDRESS ); // 8
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ~( mwBoardID ^ mwSoftwareID ) == 0 ){
g_bWorkEnable = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd( 0 );
#endif
};
};
ffblk buffer[1];
if ( findfirst("firmware", buffer,0)==0 ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Upgrading Board Firmware");
debugProcBegin("Checking Firmware Integrity");
#endif
if( validateHexImage( "firmware", BSNHB, BSNLB ) ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
debugProcBegin("Sending Firmware to Board");
#endif
if( StartDownloadHexImage( "firmware", BSNHB, BSNLB ) ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
#ifndef LGS_PLS_1
delay( 15000 );
#else
delay( 5000 );
#endif
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
};
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd( 0 );
#endif
};
unlink("firmware");
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
return FALSE;
} else {
if( m_bBoardBLO ) return TRUE; // Don't do Init to the board if in BootLoader Mode
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Downloading Board Parameters");
#endif
delay(1);
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
delay(1);
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // 1 Serial Number
delay(1);
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF ); // 2
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift & 0xFF ); // 3
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );//( m_Settings.nZeroShift >> 8 ) & 0xFF ); // 4 Zero offset
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // 5 Temp
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // 6 Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // 7 Reset DMA
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
debugProcBegin("Wait Till Board INIT done");
#endif
delay(1);
//Wait till speed/reset is done
TestBYTE = inportb( NEW_ID_ADDRESS );
nono = 15000;
while ( nono && TestBYTE!= 0x5A && TestBYTE!= 0xA5 && TestBYTE != 0xAA ){
nono--;
TestBYTE = inportb( NEW_ID_ADDRESS );
delay(1);
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
debugProcBegin("Alloc DMA Buffer");
#endif
Assign_DMA_Buffer ( DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
m_nbWriteChunk = 0;
m_nbReadChunk = 0;
m_nwChunkInterator = 0;
InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
} else { // IRQ MCU Board
// disable interrupts
disable();
delay(1);
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
// nessesary to make delay
delay(100);
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ~( mwBoardID ^ mwSoftwareID ) == 0 ){
g_bWorkEnable = TRUE;
};
outportb( NEW_BASE_ADDRESS + 6, ( unsigned char )( mwSoftwareID & 0xFF )); // Sending SoftwareID to the Board
outportb( NEW_BASE_ADDRESS + 7, ( unsigned char )(( mwSoftwareID >> 8 ) & 0xFF));
//outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
//outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
//outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
//outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
delay( 10 );
outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
delay( 10 );
outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
//outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
//outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
};
} else { // No MCU Board
g_bWorkEnable = TRUE; // Old Board = Work Always
// write to CR0
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 1st word
outportb(NEW_BASE_ADDRESS, 0x01);
// write to CR1
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 2nd word
outportb(NEW_BASE_ADDRESS, 0x00);
// write to CR0
outportb(NEW_BASE_ADDRESS + 1, 0x00); // 3th word
outportb(NEW_BASE_ADDRESS, 0x8A); // 10001010
// 10001--- autoscan two single-ended channels,
// -----0-- ADC is active,
// ------1- single conversation mode
// -------0 internal reference
// outportb(BASE_ADDRESS, 0x02); // One channel, internal reference
// write to CR1
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 4th word
#ifdef BOARD_A2D_FOUR_SAMPLES
outportb(NEW_BASE_ADDRESS, 0x8A); //(10011010)
// 1------- output in binary format
// -0------ RD: read input, WR: write input
// --00---- DATA_AV: active low level
// ----10-- trigger level 8
// ------1- FIFO reset
// -------0 no reset
#else
outportb(NEW_BASE_ADDRESS, 0x92); // 10010010
// 1------- output in binary format
// -0------ RD: read input, WR: write input
// --01---- DATA_AV: active low pulse
// ----00-- trigger level 1
// ------1- FIFO reset
// -------0 no reset
#endif
}
// set type of board
m_nBoardType = BT_NEW;
if( g_bIsDMABoard ){ // DMA Mode
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board DMA Transfers Started");
#endif
m_bResetIssued = FALSE;
// install handler
_disable();
m_pOldIsrProc = _dos_getvect( NEW_INTERRUPT_BOARD );
_dos_setvect( NEW_INTERRUPT_BOARD, m_pIsrProc );
//outportb( PICICR, PICROT | 0x04 ); // Prioritize IRQ5 as most VII
outportb( PICIMR, inportb(PICIMR) & NEW_INTERRUPT_MASK );
_enable();
m_bDMA_CRASH = FALSE;
m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1); // Set LRF ON
SetRelayBit( m_Settings.RelayDefaultMode );
delay(1);
outportb( NEW_RST_ADDRESS, 5 ); // Set 2 Bytes:
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // 1 Status
delay(1);
outportb( NEW_DTA_ADDRESS, 1 ); // 2 Start DMA
delay(1);
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
} else { // IRQ Mode
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board IRQ Transfers Started");
#endif
// install handler
m_pOldIsrProc = _dos_getvect( NEW_INTERRUPT_BOARD );
_disable();
_dos_setvect( NEW_INTERRUPT_BOARD, m_pIsrProc );
outportb( PICIMR, inportb(PICIMR) & NEW_INTERRUPT_MASK );
_enable();
// enable devices
delay( 10 );
m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
};
m_nOldAngle = 0;
return TRUE;
}
#else //ALL Other(ALL) Devices
BOOL CBoard::InitNewBoard(){
//printf("Sending to Board Temp Settings: %d\n", m_Settings.nTemperature );
// control register 0
// 0 - VREF (0=internal, 1=external)
// 1 - MODE (0-continues conversations, 1-single conversation )
// 2 - POWER_DOWN (0-active, 1-power down)
// 3,4 - CHANAL SELECT
// 5,6 - NUMBER OF DIFFERENTION CHANALS
// 7 - AUTOSCAN ENABLE
// 8,9 - TEST INPUT ENABLE
// control register 1
// 0 - RESET
// 1 - FIFO_RESET
// 2 - FIFO_LEVEL
// 3 - FIFO_LEVEL
// 4 - DATA_AVALIBLE TYPE (pulse = 1, static = 0)
// 5 - DATA_AVALIBLE POLARITY (hi = 1,0 = low)
// 6 - R/W, RD/WR SELECTION
// 7 - BIN/2s DATA FORMAT (binary = 1, 2s = 0 )
// 8 - OFFSET (0=normal, 1=offset calibration)
// 9 - RBACK (0=normal, 1=debug mode)
//#ifdef RELAY_ACTIVATE_BY_ZERO
// m_bOutputMask = BE_NEW_DC0;
//#else
// m_bOutputMask = 0;
//#endif
// disable laser devices
//outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
#ifdef BOARD_LAZY_INIT
//delay(2000); // Wait 2 seconds
#endif
/*
* Cecking Before All if Firmware must be upgraded
*/
ffblk buffer[1];
if( findfirst("firmware", buffer, 0 )==0 ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Upgrading Board Firmware");
debugProcBegin("Checking Firmware Integrity");
#endif
if( validateHexImage( "firmware", BSNHB, BSNLB ) ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
debugProcBegin("Sending Firmware to Board");
#endif
if( StartDownloadHexImage( "firmware", BSNHB, BSNLB ) ){
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
#ifndef LGS_PLS_1
delay( 15000 );
#else
delay( 5000 );
#endif
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
};
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd( 0 );
#endif
};
unlink("firmware");
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
return FALSE;
} else {
if( m_bBoardBLO ) return TRUE; // Don't do Init to the board if in BootLoader Mode
};
/*
* After BootLoader - Firmware checking do regular INIT
*/
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
// check if this is a new board
unsigned char TestBYTE;
#ifdef BOARD_LAZY_INIT
int nono = 150;
#else
int nono = 50;
#endif
TestBYTE = inportb( NEW_ID_ADDRESS );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Checking Board Type");
#endif
while ( nono && TestBYTE!= 0x5A && TestBYTE!= 0xA5 && TestBYTE != 0xAA ){
nono--;
TestBYTE = inportb( NEW_ID_ADDRESS ); //+5 ??
#ifdef DEBUG_PRINTOUT_MSGS
//putch(176);
#endif
delay(1);
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
// default Board Props
m_bBoardMCU = TRUE;
g_bIsDMABoard = TRUE;
m_bBoardBLO = FALSE;
#if !( defined(LGS_PLS_1) || defined( LGS_STEPPER ) || defined( LGS_MND ) )
if( TestBYTE == 0x5A ){
m_bBoardMCU = TRUE;
m_bBoardBLO = FALSE;
g_bIsDMABoard = FALSE;
#ifdef DEBUG_PRINTOUT_MSGS
debug( "Board in IRQ mode" );
#endif
};
if( TestBYTE == 0xA5 ){
m_bBoardMCU = TRUE;
m_bBoardBLO = FALSE;
g_bIsDMABoard = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board in DMA mode");
#endif
};
if( TestBYTE == 0xAA ){
m_bBoardBLO = TRUE;
m_bBoardMCU = TRUE;
g_bIsDMABoard = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board in BootLoader mode");
#endif
};
if( TestBYTE != 0xA5 && TestBYTE != 0x5A && TestBYTE != 0xAA ){
m_bBoardBLO = FALSE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Oy Vey! Board is Old!");
#endif
#ifdef RESTART_PC_BY_WATCHDOG
//No Board Init -> Try Restart WDT
outportb( NEW_WDT_ADDRESS, 0xAA ); // Try To restart Board
#ifdef WATCHDOG_ENABLED
CWatchDog m_WatchDog;
m_WatchDog.Create();
m_WatchDog.Update( 255 );
while(1);
#endif
#else
return FALSE;
#endif
};
#else // Not Step Motor board
if( TestBYTE == 0xAA ){
m_bBoardBLO = TRUE;
} else {
m_bBoardBLO = FALSE;
};
m_bBoardMCU = TRUE;
g_bIsDMABoard = TRUE;
if( TestBYTE != 0xA5 && TestBYTE != 0x5A && TestBYTE != 0xAA ){
m_bBoardBLO = FALSE;
#ifdef DEBUG_PRINTOUT_MSGS
debug("Oy Vey! Board is Old!");
#endif
#ifdef RESTART_PC_BY_WATCHDOG
//No Bard Init -> Restart WDT
outportb( NEW_WDT_ADDRESS, 0xAA ); // Try To restart Board
#ifdef WATCHDOG_ENABLED
CWatchDog m_WatchDog;
m_WatchDog.Create();
m_WatchDog.Update( 255 );
while(1);
#endif
#else
return FALSE;
#endif
};
#endif // Not a Step Motor Board
BSNLB=0;
BSNHB=0;
// if not MCU board
if( m_bBoardMCU ){
if( g_bIsDMABoard ){
#if !( defined(LGS_PLS_1) || defined( LGS_STEPPER ) || defined( LGS_MND ) )
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
#else
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
#endif
// delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
//delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
// nessesary to make delay
#ifdef DEBUG_PRINTOUT_MSGS
debug("Stoped DMA Transfers (if Any )");
#endif
if( g_bWorkEnable == FALSE ){
delay(10);
int nono = 10;
while ( nono && inportb( NEW_RST_ADDRESS )!= 0x5A ){
nono--; // Full Upload
delay(1);
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Read BSN");
#endif
delay(1);
BSNLB = inportb( NEW_DTA_ADDRESS );
delay(1);
BSNHB = inportb( NEW_DTA_ADDRESS );
delay(1);
fw_year = inportb( NEW_DTA_ADDRESS );
delay(1);
fw_month = inportb( NEW_DTA_ADDRESS );
delay(1);
fw_day = inportb( NEW_DTA_ADDRESS );
delay(1);
bl_year = inportb( NEW_DTA_ADDRESS );
delay(1);
bl_month = inportb( NEW_DTA_ADDRESS );
delay(1);
bl_day = inportb( NEW_DTA_ADDRESS );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ~( mwBoardID ^ mwSoftwareID ) == 0 ){
g_bWorkEnable = TRUE;
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd( 0 );
#endif
};
};
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("Downloading Board Parameters");
#endif
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND )
#ifdef LGS_PLS_200
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
#else
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
#endif
#endif
outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift & 0xFF );
outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift >> 8 ); // Zero offset
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
debugProcBegin("Alloc DMA Buffer");
#endif
Assign_DMA_Buffer ( DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
m_nbWriteChunk = 0;
m_nbReadChunk = 0;
m_nwChunkInterator = 0;
InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
} else {
// disable interrupts
disable();
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
// nessesary to make delay
delay(100);
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ~( mwBoardID ^ mwSoftwareID ) == 0 ){
g_bWorkEnable = TRUE;
};
outportb( NEW_BASE_ADDRESS + 6, ( unsigned char )( mwSoftwareID & 0xFF )); // Sending SoftwareID to the Board
outportb( NEW_BASE_ADDRESS + 7, ( unsigned char )(( mwSoftwareID >> 8 ) & 0xFF));
//outportb( NEW_BASE_ADDRESS + 3, 0 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio >> 8 ); // motor ratio
//outportb( NEW_BASE_ADDRESS + 3, 1 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nGearRatio & 0xFF );
//outportb( NEW_BASE_ADDRESS + 3, 2 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps >> 8 ); // encoder count
//outportb( NEW_BASE_ADDRESS + 3, 3 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nEncoderSteps & 0xFF );
outportb( NEW_BASE_ADDRESS + 3, 4 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nTemperature ); // temperature
delay( 10 );
outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
delay( 10 );
outportb( NEW_BASE_ADDRESS + 3, 6 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift & 0xFF );
// outportb( NEW_BASE_ADDRESS + 3, 7 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange >> 8 );
// outportb( NEW_BASE_ADDRESS + 3, 8 ); outportb( NEW_BASE_ADDRESS + 4, m_Settings.nMaxRange & 0xFF ); // max range
};
} else {
g_bWorkEnable = TRUE; // Old Board = Work Always
// write to CR0
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 1st word
outportb(NEW_BASE_ADDRESS, 0x01);
// write to CR1
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 2nd word
outportb(NEW_BASE_ADDRESS, 0x00);
// write to CR0
outportb(NEW_BASE_ADDRESS + 1, 0x00); // 3th word
outportb(NEW_BASE_ADDRESS, 0x8A); // 10001010
// 10001--- autoscan two single-ended channels,
// -----0-- ADC is active,
// ------1- single conversation mode
// -------0 internal reference
// outportb(BASE_ADDRESS, 0x02); // One channel, internal reference
// write to CR1
outportb(NEW_BASE_ADDRESS + 1, 0x04); // 4th word
#ifdef BOARD_A2D_FOUR_SAMPLES
outportb(NEW_BASE_ADDRESS, 0x8A); //(10011010)
// 1------- output in binary format
// -0------ RD: read input, WR: write input
// --00---- DATA_AV: active low level
// ----10-- trigger level 8
// ------1- FIFO reset
// -------0 no reset
#else
outportb(NEW_BASE_ADDRESS, 0x92); // 10010010
// 1------- output in binary format
// -0------ RD: read input, WR: write input
// --01---- DATA_AV: active low pulse
// ----00-- trigger level 1
// ------1- FIFO reset
// -------0 no reset
#endif
}
// set type of board
m_nBoardType = BT_NEW;
if( g_bIsDMABoard ){
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board DMA Transfers Started");
#endif
m_bResetIssued = FALSE;
// install handler
m_pOldIsrProc = _dos_getvect( NEW_INTERRUPT_BOARD );
_disable();
_dos_setvect( NEW_INTERRUPT_BOARD, m_pIsrProc );
outportb( PICIMR, inportb(PICIMR) & NEW_INTERRUPT_MASK );
_enable();
//SignalToBoard( 0x301 ); // Init DMA
m_bDMA_CRASH = FALSE;
m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
#if !( defined(LGS_PLS_1) || defined( LGS_STEPPER ) || defined( LGS_MND ) )
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
#else
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
#endif
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
} else {
#ifdef DEBUG_PRINTOUT_MSGS
debug("Board IRQ Transfers Started");
#endif
// install handler
m_pOldIsrProc = _dos_getvect( NEW_INTERRUPT_BOARD );
_disable();
_dos_setvect( NEW_INTERRUPT_BOARD, m_pIsrProc );
outportb( PICIMR, inportb(PICIMR) & NEW_INTERRUPT_MASK );
_enable();
// enable devices
delay( 10 );
m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
};
m_nRealAngle = -1;
m_nErrorAngle = -1;
return TRUE;
}
#endif // LGS_MIRROR
BOOL CBoard::Create( BOARD_SETTINGS* pSettings ){
#ifndef DEFAULT_DISC_C
if ( g_bIsVortex ){
nHID = 0;
} else {
nHID = 2;
};
#else
nHID = 2;
#endif
if( pSettings != NULL ){
m_Settings = *pSettings;
};
Destroy();
if ( m_Settings.nHID < 255 ){
nHID = m_Settings.nHID;
g_bFlashDriveID = nHID;
};
#ifdef DEBUG_PRINTOUT_MSGS
debug( "SetUp Settings : Disk: %d",nHID );
debugProcBegin( "License Check ");
#endif
SetupLicense();
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
SetRelayBit( m_Settings.RelayDefaultMode );
#ifndef BOARD_SIMULATION
if( InitNewBoard() )
return TRUE;
#ifdef DEBUG_ENABLE_OLD_BOARD
if( InitOldBoard() )
return TRUE;
#endif
return FALSE;
#else
return TRUE;
#endif // BOARD_SIMULATION
}
void CBoard::Destroy(){
#ifndef BOARD_SIMULATION
SetRelayBit( m_Settings.RelayDefaultMode );
if( m_pOldIsrProc )
{
if( m_nBoardType == BT_NEW )
{
if( g_bIsDMABoard ){
m_bResetIssued = TRUE;
int nono = 10;
while ( nono && m_bResetIssued ){
nono--;
delay(100);
};
};
_disable();
outportb( PICIMR, inportb(PICIMR) | !NEW_INTERRUPT_MASK );
_dos_setvect( NEW_INTERRUPT_BOARD, m_pOldIsrProc );
_enable();
}
#ifdef DEBUG_ENABLE_OLD_BOARD
else
if( m_nBoardType == BT_OLD )
{
#ifndef BOARD_BIND_IRQ_TO_SAPHIR
outportb( PICIMR, inportb(PICIMR) | !OLD_INTERRUPT_MASK );
m_bOutputMask &= ~0x07;
SetRelayBit( m_Settings.RelayDefaultMode );
outportb( OLD_BASE_ADDRESS, m_bOutputMask ); // Disable interrupt
#endif
_disable();
_dos_setvect( OLD_INTERRUPT_BOARD, m_pOldIsrProc );
_enable();
}
#endif
m_pOldIsrProc = NULL;
#ifndef BOARD_DEFFERED_PROCESSING
m_bReady = FALSE;
memset( &m_BoardData, 0, sizeof(BOARDDATA) );
#endif
}
// unknown type of board
m_nBoardType = BT_NONE;
// used for A/D conversion
#ifdef DEBUG_ENABLE_OLD_BOARD
s_pDataArray[0] = s_pDataArray[1] = 0;
#endif
#endif // BOARD_SIMULATION
}