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.
 
 
 
 

1956 lines
73 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
#include "hardware.h"
#ifndef NO_WDT2_INIT
#include "clock.h"
#endif
#define io_delay( X ) {for( uint16_t i=0; i<(X*50); i++ ) io_wait();}
/*
bool IsVortex( void ){
#ifdef VORTEX_BRIDGES
unsigned long CHID;
unsigned int CC;
//CHID = read_nb(0x90);
outl( 0x80000090, 0xcf8 );
CHID = inl( 0xcfc );
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;
#else
return true;
#endif
};
*/
#ifdef DMA_TRANSFERS
#ifdef __DJGPP__
//int dma_buffer_selector;
//int dma_buffer_segment;
uint32_t _dma_dos_linear_address[2] = {0};
uint8_t _dma_dos_buffer_iterator = 0;
//__dpmi_raddr _dma_buffer_ra = {0};
__dpmi_paddr _dma_buffer_pa = {0};
int _dma_dos_selector[2] = {-1};
unsigned char * DMA_BUFFER = NULL;
#else
unsigned char * DMA_BUFFER = NULL;
//uint32_t DMA_BUFER_LINEAR_ADDRESS = 0;
#endif
#ifdef __DJGPP__
void KillDMA( int DMA ){
if( _dma_dos_selector[_dma_dos_buffer_iterator] == -1 ) return;
if( _dma_dos_linear_address[_dma_dos_buffer_iterator] == 0 ) return;
outportb( 0x0A, 4 | DMA ); //Mask DMA channel
};
void Free_DMA_Buffer(){
if( _dma_dos_selector[0] != -1 ) {
if( __dpmi_free_dos_memory( _dma_dos_selector[0] ) == 0 ){
_dma_dos_selector[0] = -1;
_dma_dos_linear_address[0] = 0;
};
};
if( _dma_dos_selector[1] != -1 ) {
if( __dpmi_free_dos_memory( _dma_dos_selector[1] ) == 0 ){
_dma_dos_selector[1] = -1;
_dma_dos_linear_address[1] = 0;
};
};
_dma_dos_buffer_iterator = 0;
if( DMA_BUFFER ) {
free( DMA_BUFFER );
DMA_BUFFER = NULL;
};
};
bool InitDMA( int DMA ){
#else
void InitDMA( int DMA, unsigned char *Buffer , unsigned int Size ){
#endif
#ifndef BOARD_SIMULATION
#ifdef __DJGPP__
//if no memory assigned
if( _dma_dos_selector[_dma_dos_buffer_iterator] == -1 ) return false;
if( _dma_dos_linear_address[_dma_dos_buffer_iterator] == 0 ) return false;
uint16_t Page, OffSet;
Page = _dma_dos_linear_address[_dma_dos_buffer_iterator] >> 16; //Calculate page
OffSet = _dma_dos_linear_address[_dma_dos_buffer_iterator] & 0xFFFF; //Calculate offset in the page
outportb (0x0A, 4 | DMA); //Mask DMA channel
outportb (0x0C, 0); //Clear byte pointer
outportb (0x0B, 0x14 | DMA); //Set mode
/*
The mode consists of the following:
0x14 + x = binary 00 01 01 xx
|| || || ||
|| || || ++- DMA channel
|| || |+--- Write operation (the DSP writes to memory)
|| || +---- Read operation (the DSP reads from memory)
|| |+------ Autoinit
|| +------- Increment - 1/Decrement - 0
|+--------- Single mode - 1 -+--- 00 - on Demand
+---------- Block mode - 1 -+
*/
outportb (DMA << 1, OffSet & 0xFF ); //Write the offset to the DMA controller
outportb (DMA << 1, OffSet >> 8 );
if (DMA == 0) outportw ( 0x87, Page );
if (DMA == 1) outportw ( 0x83, Page );
if (DMA == 3) outportw ( 0x82, Page );
outportb (( DMA << 1 ) + 1, ( DMA_TRANSFER_CHUNK - 1 ) & 0xFF );
outportb (( DMA << 1 ) + 1, ( ( DMA_TRANSFER_CHUNK - 1 ) >> 8 ) & 0xFF );
outportb (0x0A, DMA); //Unmask DMA channel
return true;
#else
long LinearAddress;
unsigned int Page, OffSet;
//Convert pointer to linear address
LinearAddress = FP_SEG ( Buffer );
LinearAddress = ( LinearAddress << 4 ) + FP_OFF( Buffer );
Page = LinearAddress >> 16; //Calculate page
OffSet = LinearAddress & 0xFFFF; //Calculate offset in the page
outportb (0x0A, 4 | DMA); //Mask DMA channel
outportb (0x0C, 0); //Clear byte pointer
outportb (0x0B, 0x14 | DMA); //Set mode
/*
The mode consists of the following:
0x14 + x = binary 00 01 01 xx
|| || || ||
|| || || ++- DMA channel
|| || |+--- Write operation (the DSP writes to memory)
|| || +---- Read operation (the DSP reads from memory)
|| |+------ Autoinit
|| +------- Increment - 1/Decrement - 0
|+--------- Single mode - 1 -+--- 00 - on Demand
+---------- Block mode - 1 -+
*/
outportb (DMA << 1, OffSet & 0xFF ); //Write the offset to the DMA controller
outportb (DMA << 1, OffSet >> 8 );
if (DMA == 0) outportb ( 0x87, Page );
if (DMA == 1) outportb ( 0x83, Page );
if (DMA == 3) outportb ( 0x82, Page );
outportb (( DMA << 1 ) + 1, ( Size - 1 ) & 0xFF );
outportb (( DMA << 1 ) + 1, ( ( Size - 1 ) >> 8 ) & 0xFF );
outportb (0x0A, DMA); //Unmask DMA channel
#endif
#endif // BOARD_SIMULATION
};
#ifdef __DJGPP__
bool Assign_DMA_Buffer ( unsigned int Size ){
#else
void Assign_DMA_Buffer ( unsigned int Size ){
#endif
#ifndef BOARD_SIMULATION
#ifdef __DJGPP__
if( DMA_BUFFER ) return false;
DMA_BUFFER = ( uint8_t * ) malloc( Size );
_dma_dos_buffer_iterator = 0;
if( DMA_BUFFER ) {
memset( DMA_BUFFER, 0, Size );
int paragraphs = ( ( DMA_TRANSFER_CHUNK + 15 ) >> 4 );
int rv = __dpmi_allocate_dos_memory( paragraphs, &_dma_dos_selector[0] );
if( rv == -1 ){
_dma_dos_selector[0] = -1;
_dma_dos_linear_address[0] = 0;
free( DMA_BUFFER );
DMA_BUFFER = NULL;
} else {
_dma_dos_linear_address[0] = rv << 4;
#ifdef DEBUG
printf( "Buffer[0] DOS Linear Address: 0x%08X\n", _dma_dos_linear_address[0] );
#endif
};
} else {
_dma_dos_selector[0] = -1;
_dma_dos_linear_address[0] = 0;
};
if( DMA_BUFFER ) {
int paragraphs = ( ( DMA_TRANSFER_CHUNK + 15 ) >> 4 );
int rv = __dpmi_allocate_dos_memory( paragraphs, &_dma_dos_selector[1] );
if( rv == -1 ){
_dma_dos_selector[1] = -1;
_dma_dos_linear_address[1] = 0;
// Clear First DOS Buffer
if( __dpmi_free_dos_memory( _dma_dos_selector[0] ) == 0 ){
_dma_dos_selector[0] = -1;
_dma_dos_linear_address[0] = 0;
};
free( DMA_BUFFER );
DMA_BUFFER = NULL;
} else {
_dma_dos_linear_address[1] = rv << 4;
#ifdef DEBUG
printf( "Buffer[1] DOS Linear Address: 0x%08X\n", _dma_dos_linear_address[1] );
#endif
return true;
};
} else {
_dma_dos_selector[1] = -1;
_dma_dos_linear_address[1] = 0;
};
return false;
#else // !__DJGPP__
unsigned char far *TempBuf=NULL; //Temporary pointer
long LinearAddress;
unsigned int Page1, Page2; //Words
if( DMA_BUFFER == NULL ){
do {
TempBuf = ( unsigned char far * ) farmalloc( Size );
if( DMA_BUFFER ) free( DMA_BUFFER );
LinearAddress = FP_SEG (TempBuf);
LinearAddress = (LinearAddress << 4) + FP_OFF ( TempBuf );
Page1 = LinearAddress >> 16;
Page2 = ( LinearAddress + Size ) >> 16;
DMA_BUFFER = TempBuf;
} while ( Page1 != Page2 );
}; // DMA_BUFFER == 0
memset ( DMA_BUFFER, 0, Size );
#endif // !__DJGPP__
#endif // BOARD_SIMULATION
};
#endif // DMA_TRANSFERS
CBoard *CBoard::_instance = NULL;
CBoard *CBoard::getInstance(){
if( _instance == NULL ) {
_instance = new CBoard();
};
return _instance;
};
CBoard::CBoard(): CIsr(){
m_bWM = WM_NONE;
_dma_dos_buffer_iterator = 0;
m_bWorkEnable = false;
m_bDMA_first_chunk_transfered = false;
m_bDMA_buffer_assigned = false;
m_bDMA_is_crashed = false;
m_bResetIssued = false;
m_bIRQ_handler_installed = false;
m_eBoardType = BT_UNKNOWN;
//m_pFirmwareFileID = NULL;
m_pFirmwareSentLine = 0;
m_bFirmwareUpgradeDone = false;
__dpmi_version_ret dpmi_version;
__dpmi_get_version( &dpmi_version );
#ifdef DEBUG
printf( "DPMI Major: 0x%02X Minor: 0x%02X\n\tCPU:\t[%d]\n\tHOST:\t[%d bits]\n\tMODE:\t[%s]\n\tVirtual Memory:\t[%s]\n",
dpmi_version.major,
dpmi_version.minor,
dpmi_version.cpu,
(( dpmi_version.flags & 0x01 )?32:16),
(( dpmi_version.flags & 0x02 )?_T("REAL"):_T("V86")),
(( dpmi_version.flags & 0x04 )?_T("SUPPORTED"):_T("NOT SUPPORTED"))
);
#endif
if( ( dpmi_version.flags & 0x02 ) == 0 ) m_bWM = WM_DPMI_OK;
//if ( __djgpp_nearptr_enable() == 0 ){
// printf("DPMI near pointers are not available\n");
//exit(-1);
//};
if( m_eBoardType == BT_UNKNOWN ) {
/* char *pCoreModule4DXeStr = _T( "CM/4DXi" );
if( !_fmemcmp( (void far*)0xF0000101UL, pCoreModule4DXeStr, 7 ) &&
!_fmemcmp( (void far*)0xF0002B6DUL, pCoreModule4DXeStr, 7 ) )
m_eBoardType = BT_COREMODULE_4DXE;
*/
};
if( m_eBoardType == BT_UNKNOWN ) {
uint32_t CHID;
//unsigned int CC;
CHID = read_nb( 0x90 );
if( CHID == 0x31504d44L ){
#ifdef DEBUG
printf("Board is Vortex SX\n");
#endif
m_eBoardType = BT_VORTEX_SX;
//make_ws8();
};
if( CHID == 0x32504d44L ){
#ifdef DEBUG
printf("Board is Vortex DX\n");
#endif
m_eBoardType = BT_VORTEX_DX;
//make_ws8();
};
if( CHID == 0x33504d44L ){
#ifdef DEBUG
printf("Board is Vortex MX\n");
#endif
m_eBoardType = BT_VORTEX_MX;
//make_ws8();
};
};
if( m_eBoardType == BT_UNKNOWN ) {
/*
asm
{
mov AL,0x02 // Index
mov DX,ZFLINDEX // Index Address
out DX,AL // Set Index
mov DX,ZFLDATA16 // read value:
in AX,DX // AX=1234H
}
*/
if( ZFLReadw( 0x02 ) == 0x1234 ) m_eBoardType = BT_TRI_M;
};
mOld_TV = clock();
m_wTimer = 0;
mwBoardID=0;
mwSoftwareID=0;
//g_bIsVortex = ( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX );
//g_bIsDMABoard = ( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ); //false;
#ifdef DEFAULT_DISC_C
nHID = 2;
#else
if( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ){
nHID = 2;
} else {
// Old Boards Run From A: -> Virtual Disk is C:
nHID = 0;
};
#endif
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
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;
// Moved from Destructor since CBoard now is Singletone
g_bFlashDriveID = nHID;
}
CBoard::~CBoard(){
if( BSNS != NULL ){
free( BSNS );
};
if( BFCD != NULL ){
free( BFCD );
};
if( BSCD != NULL ){
free( BSCD );
};
Destroy();
}
void CBoard::freeHexImage( FIRMWARE *ptr ){
if( ptr ) {
if( ptr->pLines ) {
free( ptr->pLines );
};
free( ptr );
};
};
FIRMWARE *CBoard::LoadHexImage( char *FileName ){
FIRMWARE *rV = (FIRMWARE *) malloc( sizeof(FIRMWARE));
if( rV ) {
memset( rV, 0, sizeof(FIRMWARE) );
FILE *fileID = fopen( FileName, "r" );
if( fileID ) {
char Sig[2];
uint8_t InpB;
bool inLine = false;
uint8_t Stage = 0;
uint8_t RecLen = 0;
uint8_t CRec = 0;
uint16_t OffSet = 0;
uint8_t RecType = 0;
uint8_t RCheck = 0;
uint8_t CCheck = 0;
char InpC[3];
char InpW[5];
while( !feof( fileID ) ){
if( !inLine ){
fread( Sig, 1, 1, fileID );
if( Sig[0] == ':' ) {
inLine = true;
rV->nLines++;
HEXLINE *tPtr = (HEXLINE *) realloc( rV->pLines, rV->nLines * sizeof( HEXLINE ));
if( tPtr == NULL ) {
freeHexImage( rV );
fclose( fileID );
return NULL;
};
rV->pLines = tPtr;
memset( &rV->pLines[rV->nLines-1], 0, sizeof(HEXLINE));
};
CCheck = 0;
RecLen = 0;
RecType = 0;
RCheck = 0;
Stage = 0;
CRec = 0;
} else {
switch( Stage ){
case 0:{
fread( InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &RecLen ) == 0 ){
freeHexImage( rV );
fclose( fileID );
return NULL;
};
rV->pLines[rV->nLines-1].len = RecLen;
CCheck = RecLen;
Stage = 1;
break;
};
case 1:{
fread( InpW, 4, 1, fileID );
InpW[4]='\0';
if( sscanf( InpW, "%x", &OffSet )== 0 ) {
freeHexImage( rV );
fclose( fileID );
return NULL;
};
rV->pLines[rV->nLines-1].offset_lb = OffSet & 0xFF;
rV->pLines[rV->nLines-1].offset_hb = (OffSet >> 8 ) & 0xFF;
CCheck = ( CCheck + rV->pLines[rV->nLines-1].offset_lb ) & 0xFF ;
CCheck = ( CCheck + rV->pLines[rV->nLines-1].offset_hb ) & 0xFF ;
CRec = 0;
Stage = 2;
break;
};
case 2:{
fread( InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &RecType ) == 0 ){
freeHexImage( rV );
fclose( fileID );
return NULL;
};
rV->pLines[rV->nLines-1].type = RecType;
CCheck = ( CCheck + RecType ) & 0xFF ;
Stage = 3;
break;
};
default:{
fread( InpC, 2, 1, fileID );
InpC[2]='\0';
if( sscanf( InpC, "%x", &InpB ) == 0 ){
freeHexImage( rV );
fclose( fileID );
return NULL;
};
CCheck = ( CCheck + InpB ) & 0xFF ;
rV->pLines[rV->nLines-1].data[CRec] = InpB;
if( CRec == rV->pLines[rV->nLines-1].len ){
if( CCheck != 0 ){
freeHexImage( rV );
fclose( fileID );
return NULL;
};
inLine = false;
} else CRec++;
break;
};
};
};
};
fclose( fileID );
} else {
freeHexImage( rV );
rV = NULL;
};
};
return rV;
};
uint8_t wait_board( uint8_t ack = 0x00, uint16_t loop = 100 );
void CBoard::BeginService(){
#ifdef DEBUG
printf("Board Service Begins!\n");
#endif
switch( m_bWM ) {
case WM_FW_UPGRADE:{
if( m_pFirmwareImage ) {
int nono = 2500;
unsigned char TestBYTE = inportb( BLO_READ_ID_PORT );
while ( nono && ( TestBYTE != 0xAA ) ){
nono--;
delay(1);
outportb( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
delay(1);
TestBYTE = inportb( BLO_READ_ID_PORT );
};
if( TestBYTE == 0xAA ) {
if(!m_bISR ) CIsr::initISR( NEW_INTERRUPT_BOARD );
#ifdef DEBUG
printf("Start FW Service!\n");
#endif
m_pFirmwareSentLine = 0;
m_bFirmwareUpgradeDone = false;
outportb( BLO_WRITE_START_TRANSFER, m_pFirmwareImage->pLines[m_pFirmwareSentLine].len );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].type );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_hb );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_lb );
for( uint8_t idx = 0; idx < (m_pFirmwareImage->pLines[m_pFirmwareSentLine].len+1); idx++ ){
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].data[idx] );
};
outportb( BLO_WRITE_END_TRANSFER, 0xFF );
m_pFirmwareSentLine++;
} else {
if( m_bISR ) CIsr::resignISR();
#ifdef DEBUG
printf("Failure To Enter Board into Bootloader Mode!\n");
#endif
m_bWM = WM_ONLY_INFO; // Not Entered into Bootloader Mode
};
};
break;
};
case WM_DMA_WORK:{
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
delay(1);
#else
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
delay(1);
#endif
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
break;
};
};
};
void CBoard::EndService( void ){
if( !m_bWorkEnable ) return;
#ifndef BOARD_SIMULATION
switch( m_eBoardType ){
case BT_VORTEX_SX:
case BT_VORTEX_DX:
case BT_VORTEX_MX:{
// disable interrupts
// disable();
// disable laser devices
// outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
//SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
switch( m_bWM ){
case WM_DMA_WORK:{
StopDMA();
if( m_bIRQ_handler_installed ) {
resignISR();
m_bDMA_first_chunk_transfered = false;
};
if( !m_bWorkEnable ){
outportb( NEW_RST_ADDRESS, 0 );// Full Upload
delay(1);
BSNLB = inportb( NEW_DTA_ADDRESS );
delay(1);
BSNHB = inportb( NEW_DTA_ADDRESS );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
} else {
outportb( NEW_RST_ADDRESS, 2 ); // Partial Upload
};
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD )
#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
#ifdef LGS_FOD
outportb( NEW_DTA_ADDRESS, 0xFF );
outportb( NEW_DTA_ADDRESS, 0xFF );
#else
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
#endif
#endif
#endif
#if defined( LGS_MND )
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 ); // Zero offset
#else
outportb( NEW_DTA_ADDRESS, 0x00 );
outportb( NEW_DTA_ADDRESS, 0x00 ); // Zero offset
#endif
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
delay(10); // To ensure stop of DMA conveyor
break;
};
case WM_IRQ_WORK:{
// disable interrupts
disable();
// disable laser devices
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
enable();
delay(100);
if( !m_bWorkEnable ){
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ){
printf("License is OK\n WORK ENABLE!\n");
m_bWorkEnable = true;
} else {
printf("License is NOK\n WORK DISABLE!\n");
}
outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
} else {
printf("WORK ALLREADY ENABLED!\n");
};
//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
m_BoardData.Clear();
break;
};
};
m_nRealAngle = -1;
m_nErrorAngle = -1;
break;
};// case BT_NEW
};// end of SWITCH board type
#endif // BOARD_SIBULATION
};
void CBoard::StopDMA(){
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
m_bResetIssued = true;
int nono = 100; // 300 mSecond
while ( nono && m_bResetIssued ){
nono--;
delay(3);
};
// NO DMA TRANSFERS ONGOING -> STOP Forced
if( m_bResetIssued ) { // No DMA stop during IRQ! so stop it anyway
m_bResetIssued = false;
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
#else
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
#endif
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // work status
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
delay(3);
};
};
uint8_t wait_board( uint8_t ack, uint16_t loop ){
uint8_t tByte = inportb( NEW_ID_ADDRESS );
if( ack ) {
while( loop && tByte!= ack ){
delay(1);
tByte = inportb( NEW_ID_ADDRESS );
loop--;
};
} else {
while( loop && tByte!= 0x5A && tByte!= 0xA5 && tByte != 0xAA ){
delay(1);
tByte = inportb( NEW_ID_ADDRESS );
loop--;
};
};
return ( loop > 0 )?tByte:0x00;
};
bool CBoard::InitBoard(){
m_bDMA_first_chunk_transfered = false;
if( m_bWM == WM_SIMULATION ) return true;
#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(_T("firmware"), buffer, 0 )==0 ){
#ifdef DEBUG
//printf("Found Board Firmware Upgrade!\n");
#endif
//if( validateHexImage( _T("firmware"), BSNHB, BSNLB ) ){
m_pFirmwareImage = LoadHexImage( _T( "firmware" ) );
if( m_pFirmwareImage ){
#ifdef DEBUG
//printf("Board Firmware Upgrade is Valid!\n");
#endif
m_bFirmwareUpgradeDone = false;
m_pFirmwareSentLine = 0;
#ifdef DEBUG
//printf("Stoping WD\n");
#endif
//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
if( m_bWM != WM_BLO_ONLY ) {
// If Board in BLO mode no need to start BootLoader manually
outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
delay( 1 );
outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
delay( 1 );
outp( NEW_BL_ADDRESS, 0xAA ); // Start BootLoader Proc
delay( 1 );
outp( NEW_BL_ADDRESS, 0x55 ); // Start BootLoader Proc
delay( 1500 ); // Wait till BL checksumm complete
};
#ifdef DEBUG
//printf("Board Mode NOW is - Firmware Upgrade!\n");
#endif
m_bWM = WM_FW_UPGRADE;
}// else {
#ifdef DEBUG
//printf("HEX File is Invalid!\n");
#endif
// Delete Firmware File in any case
// if the FW is ok it's already loadded to memory
unlink(_T("firmware"));
//};
};
/*
* After BootLoader - Firmware checking do regular INIT
*/
m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
BSNLB=0;
BSNHB=0;
switch( m_bWM ) {
case WM_FW_UPGRADE:{
/*
if( wait_board() ) {
delay(1);
BSNLB = inportb( NEW_ID_ADDRESS );
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 ) & 0xFFFF ) == 0 ){
#ifdef DEBUG
printf("The License is OK\n");
#endif
m_bWorkEnable = true;
};
CIsr::initISR( NEW_INTERRUPT_BOARD );
break;
} else m_bWM = WM_ONLY_INFO;*/
m_bIRQ_handler_installed = CIsr::initISR( NEW_INTERRUPT_BOARD );
m_bWorkEnable = true;
break;
};
case WM_BLO_ONLY:{
m_bWorkEnable = true;
break;
}
case WM_ONLY_INFO:{
if( wait_board() ) {
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 ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
};
m_bWorkEnable = true;
break;
};
case WM_DMA_WORK:{
if( wait_board() ) {
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
// nessesary to make delay
if( !m_bWorkEnable ){
BSNLB = inportb( NEW_RST_ADDRESS );
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 ) & 0xFFFF ) == 0 ) {
m_bWorkEnable = true;
} else {
m_bWM = WM_ONLY_INFO;
};
};
if( m_bWorkEnable ) {
delay(1);
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
delay(1);
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
delay(1);
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
#ifdef LGS_PLS_200
delay(1);
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
#else
#ifdef LGS_FOD
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
#else
#ifdef ALS_300_BM
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
#else
delay(1);
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
#endif
#endif
#endif
#if defined( LGS_MND )
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( m_Settings.nZeroShift >> 8 ) & 0xFF ); // Zero offset
#else
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );//m_Settings.nZeroShift >> 8 ); // Zero offset
#endif
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
delay(1);
if( !Assign_DMA_Buffer( DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES )) return false;
m_nbWriteChunk = 0;
m_nbReadChunk = 0;
m_nwChunkInterator = 0;
_dma_dos_buffer_iterator = 0;
if( !InitDMA( DMA_CHANNEL ) ) return false;
m_bResetIssued = false;
m_bIRQ_handler_installed = CIsr::initISR( NEW_INTERRUPT_BOARD );
};
if( m_bWorkEnable ) {
m_bDMA_is_crashed = false;
m_bOutputMask |= (m_Settings.M_LRFDisable?0:BE_NEW_DEVICE0) | (m_Settings.S_LRFDisable?0:BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
delay(1);
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA -> Don't Work After Init !!
};
};
break;
};
case WM_IRQ_WORK:{
// 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 ) & 0xFFFF )== 0 ) m_bWorkEnable = true;
if( m_bWorkEnable ) {
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
m_bWorkEnable = CIsr::initISR( NEW_INTERRUPT_BOARD );
// enable devices here ?? or in Reset Board?
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 );
};
break;
};
};
// no need to check m_bWorkEnable for FW upgrade?
m_nRealAngle = -1;
m_nErrorAngle = -1;
#ifdef DEBUG
printf( "Board [0x%04X] Init Done\n", mwBoardID );
#endif
if( m_bWM == WM_DMA_WORK || m_bWM == WM_IRQ_WORK ) {
return m_bWorkEnable;
};
return true;
};
bool CBoard::Create( BOARD_SETTINGS* pSettings ){
// Initial Create Always with Settings!!
if( pSettings != NULL ) memcpy( &m_Settings, pSettings, sizeof( BOARD_SETTINGS ));
#ifdef BOARD_SIMULATION
m_bWM = WM_SIMULATION;
#else // !BOARD_SIMULATION
//Destroy();
#ifndef NO_WDT2_INIT
//if( g_bIsDMABoard ) init_sys_clock();
#endif
if( !m_bWorkEnable ) {
if ( m_Settings.nHID < 255 ){
nHID = m_Settings.nHID;
g_bFlashDriveID = nHID;
};
if( !UpdateLicensing() ) SetupLicense();
};
SetRelayBit( m_Settings.RelayDefaultMode );
// Init Global VARS and Board Type!
#ifdef COMPILE_AS_GETFILE
#ifdef LGS_STEPPER
delay( 15000 );// Wait 15 seconds anyway to spinup motors.
#endif
#endif
#ifdef BOARD_LAZY_INIT
uint8_t TestBYTE = wait_board( 0x00, 150 );
#else
uint8_t TestBYTE = wait_board( 0x00, 150 );
#endif
if( TestBYTE ){
#ifdef DEBUG
//printf("Board Test Byte is: 0x%02X\n", TestBYTE );
#endif
if( TestBYTE == 0x5A ) m_bWM = WM_IRQ_WORK;
if( TestBYTE == 0xA5 ) m_bWM = WM_DMA_WORK;
if( TestBYTE == 0xAA ) m_bWM = WM_BLO_ONLY;
// First INIT
if( !m_bWorkEnable ){
if( m_bWM != WM_IRQ_WORK ) {
if( m_bWM == WM_DMA_WORK ) StopDMA();
outportb( NEW_RST_ADDRESS, 0 );// Full Upload
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 );
delay(1);
//Send SW ID in any case
// No need to send to BootLoader
if( m_bWM == WM_DMA_WORK ) {
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
delay(1);
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
delay(1);
};
} else
//if( m_bWM == WM_IRQ_WORK )
{
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
delay(100);
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
//Send SW ID in any case
outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
};
mwBoardID = (( BSNHB<<8) | BSNLB );
#ifdef DEBUG
//printf("Board ID: %04X\n", mwBoardID );
#endif
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
};
} else {
// No Board Communication!
#ifdef DEBUG
//printf( "Board Link Failure!\n" );
#endif
#ifdef COMPILE_AS_GETFILE
// Get File Mode -> Continue even No Board
m_bWM = WM_ONLY_INFO;
#else // ! COMPILE_AS_GETFILE
#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( 0xFF );
while(1); // Hangup PC -> Full Restart
#endif
delay( 3000 ); // Wait 3 Seconds anyway -> then restart App
#else
return false;
#endif
#endif // ! COMPILE_AS_GETFILE
};
/*
#ifdef COMPILE_AS_GETFILE
m_bWM = WM_ONLY_INFO;
#else
// If License is Not OK -> WM is INFO
if( !m_bWorkEnable ) m_bWM = WM_ONLY_INFO;
#endif
*/
#endif // BOARD_SIMULATION
return InitBoard();
}
void CBoard::Destroy(){
#ifndef BOARD_SIMULATION
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("BOARD:Destroy");
#endif
SetRelayBit( m_Settings.RelayDefaultMode );
#ifdef __DJGPP__
resignISR();
m_bDMA_first_chunk_transfered = false;
#else //__DJGPP__
if( m_pOldIsrProc )
{
if( m_nBoardType == BT_NEW )
{
if( g_bIsDMABoard ){
m_bResetIssued = true;
int nono = 10;
while ( nono && m_bResetIssued ){
nono--;
delay(100);
};
m_bResetIssued = false;
};
_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;
#ifdef DEBUG_PRINTOUT_MSGS
debug("BOARD:Clear Data");
#endif
#ifndef BOARD_DEFFERED_PROCESSING
m_bReady = false;
memset( &m_BoardData, 0, sizeof(BOARDDATA) );
#endif
}
#endif //__DJGPP__
// unknown type of board
// m_eBoardType = BT_UNKNOWN;
// used for A/D conversion
#ifdef DEBUG_ENABLE_OLD_BOARD
s_pDataArray[0] = s_pDataArray[1] = 0;
#endif
#ifdef DEBUG_PRINTOUT_MSGS
debugProcEnd();
#endif
#else // BOARD_SIMULATION
#ifdef DEBUG_PRINTOUT_MSGS
debugProcBegin("BOARD:Destroy SIM-Board");
debugProcEnd();
#endif
#endif // BOARD_SIMULATION
#ifndef NO_WDT2_INIT
#ifdef DEBUG
printf("Stop Sysclock\n");
#endif
//if( g_bIsVortex ) stop_sys_clock();
#ifdef DEBUG
printf("Done\n");
#endif
#endif
};
void CBoard::AsyncReset(){
#ifdef DEBUG_PRINTOUT_MSGS
debug( "AsyncReset" );
#endif
#ifndef BOARD_SIMULATION
switch( m_bWM ){
case WM_DMA_WORK:{
// disable interrupts
// disable();
// disable laser devices
// outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
//SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
// if MCU board
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
#else
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
#endif
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // work status
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
delay(10);
if( !m_bWorkEnable ){
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF ); // Serial Number
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
} else {
outportb( NEW_RST_ADDRESS, 2 ); //Params + Status + DMA Upload - SerNum
};
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
#ifdef LGS_PLS_200
delay(1);
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
#else
#ifdef LGS_FOD
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
#else
#ifdef ALS_300_BM
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
#else
delay(1);
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
#endif
#endif
#endif
#endif
#if defined( LGS_MND )
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 ); // Zero offset
#else
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 ); // Zero offset
#endif
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature ); // Temp
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 3 ); // Reset DMA
delay(10); // To ensure stop of DMA conveyor
m_nbWriteChunk = 0;
m_nbReadChunk = 0;
m_nwChunkInterator = 0;
m_bDMA_is_crashed = false;
memset ( DMA_BUFFER, 0, DMA_TRANSFER_CHUNK );
#ifdef __DJGPP__
//uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
InitDMA( DMA_CHANNEL );
#else
InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
#endif
m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
#else
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
#endif
delay(1);
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
// enable interrupts
//enable();
m_nRealAngle = -1;
m_nErrorAngle = -1;
break;
};
case WM_IRQ_WORK:{
// disable interrupts
disable();
// disable laser devices
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
enable();
delay(100);
if( !m_bWorkEnable ){
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF )== 0 ) m_bWorkEnable = true;
outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
outportb( NEW_BASE_ADDRESS + 7, ( mwSoftwareID >> 8 ) & 0xFF );
};
delay(1);
//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(1);
outportb( NEW_BASE_ADDRESS + 3, 5 ); outportb( NEW_BASE_ADDRESS + 4, 0x00 );//m_Settings.nZeroShift >> 8 ); // zero offset
delay(1);
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
// enable interrupts
delay( 10 );
m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
// enable laser devices
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
m_BoardData.Clear();
m_nRealAngle = -1;
m_nErrorAngle = -1;
break;
};
}; // end of SWITCH WM
// if( g_UI_Debug ){
#ifdef DEBUG_BOARD_PRINT_NO_IRQ
printf("Board Async Reset\n");
#endif
// };
#endif // BOARD_SIMULATION
}
void CBoard::OnISR(){
switch( m_bWM ) {
case WM_FW_UPGRADE:{
if( m_pFirmwareImage ) {
if( m_pFirmwareSentLine < m_pFirmwareImage->nLines ){
outportb( BLO_WRITE_START_TRANSFER, m_pFirmwareImage->pLines[m_pFirmwareSentLine].len );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].type );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_hb );
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].offset_lb );
for( uint8_t idx = 0; idx < (m_pFirmwareImage->pLines[m_pFirmwareSentLine].len+1); idx++ ){
outportb( BLO_WRITE_DATA_PORT, m_pFirmwareImage->pLines[m_pFirmwareSentLine].data[idx] );
};
outportb( BLO_WRITE_END_TRANSFER, 0xFF );
m_pFirmwareSentLine++;
};
if( m_pFirmwareSentLine == m_pFirmwareImage->nLines ){
m_bFirmwareUpgradeDone = true;
};
};
break;
};
case WM_IRQ_WORK:{
// Port Mode
// TestBit( true );
// update timer
m_wTimer++;
if( m_eBoardType == BT_VORTEX_SX || m_eBoardType == BT_VORTEX_DX || m_eBoardType == BT_VORTEX_MX ){
enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
// special structure for data converting
typedef union DATA {
uint16_t wValue[2];
uint8_t bValue[4];
} DATA, *PDATA;
if( m_bOutputDirty ){
m_bOutputDirty = false;
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask ); // fix of bug with write to MCU board
}
DATA data;
// ---1--- read value for first laser
data.bValue[0] = inportb(NEW_BASE_ADDRESS);
data.bValue[1] = inportb(NEW_BASE_ADDRESS + 1);
// read value for second laser
data.bValue[2] = inportb(NEW_BASE_ADDRESS);
data.bValue[3] = inportb(NEW_BASE_ADDRESS + 1);
// store flags
uint8_t bFlags = data.bValue[1];
// initialize sum
uint16_t wRange[2] = { (uint16_t)(data.wValue[0] & 0x0FFF), (uint16_t)(data.wValue[1] & 0x0FFF) };
// release interrupt controler
//outp( PICICR, EOI );
wRange[0] = 0x07FF - (wRange[0] >> 1);
wRange[1] = 0x07FF - (wRange[1] >> 1);
#ifdef BOARD_DEFFERED_PROCESSING
BOARDDATA bd;
// check for zero mark and sun bit
bd.bZero = bFlags & BF_ZERO; // read zero
bd.bPower = bFlags & BF_POWER; // read power bit
bd.bSun[0] = bFlags & BF_SUN1; // read sun from device 1
bd.bSun[1] = bFlags & BF_SUN2; // read sun from device 2
// prepare values ( convert from 4095..0 to 0..2047 )
bd.nRange[0] = wRange[0];
bd.nRange[1] = wRange[1];
// put values into conveier
m_BoardData.Put( bd );
#else // BOARD_DEFFERED_PROCESSING
// check for zero mark and sun bit
m_BoardData.bZero = bFlags & BF_ZERO; // read zero
m_BoardData.bPower = bFlags & BF_POWER; // read zero
m_BoardData.bSun[0] = bFlags & BF_SUN1; // read sun from device 1
m_BoardData.bSun[1] = bFlags & BF_SUN2; // read sun from device 2
// prepare values ( convert from 4095..0 to 0..2047 )
m_BoardData.nRange[0] = wRange[0];
m_BoardData.nRange[1] = wRange[1];
// value ready
m_bReady = true;
#endif // BOARD_DEFFERED_PROCESSING
} else {
// release interrupt controler
//outp( PICICR, EOI );
};
break;
};
case WM_DMA_WORK:{
unsigned long irqsave = save_irqdisable();
#ifndef COMM_WITHOUT_INTERRUPTS
// To Disable IRQ line
outportb( MASTER_PIC_DATA, ( inportb( MASTER_PIC_DATA ) | 0x18 ) ); /* disable IRQ3 and IRQ4 (com ports) 0x8+0x10 */
#endif
//Wait Bord to be ready for BUS cycle
//for( uint16_t i=0; i<100; i++ ) io_wait();
//io_delay( 1 );
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 );
outportb( NEW_RST_ADDRESS, 7 );
//io_delay( 1 );
m_nbWriteChunk ++;
if( m_nbWriteChunk == DMA_TRANSFER_ZONES ) m_nbWriteChunk = 0;
if( m_bDMA_first_chunk_transfered && ( m_nbWriteChunk == m_nbReadChunk ) ){
m_bDMA_is_crashed = true;
} else {
#ifdef __DJGPP__
if( DMA_BUFFER ){
uint8_t oIterator = _dma_dos_buffer_iterator;
_dma_dos_buffer_iterator = 1 - _dma_dos_buffer_iterator;
if( _dma_dos_selector[_dma_dos_buffer_iterator] != -1 ) {
InitDMA( DMA_CHANNEL );
//outportb( ICR_REGISTER, END_OF_INTERRUPT );
//io_wait();
//io_wait();
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
io_wait();
//io_delay( 1 );
//io_wait();
if( m_bDMA_is_crashed || m_bResetIssued ){
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
m_bResetIssued = false;
} else {
outportb( NEW_DTA_ADDRESS, 2 );
};
};
if( _dma_dos_selector[oIterator] != -1 ) {
// save Transfered memory to PM memory
//uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
//Get Back RM to PM
dosmemget( _dma_dos_linear_address[oIterator], DMA_TRANSFER_CHUNK, DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbWriteChunk );
if( !m_bDMA_first_chunk_transfered ) m_bDMA_first_chunk_transfered = true;
};
};
#else
InitDMA( DMA_CHANNEL , DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbWriteChunk , DMA_TRANSFER_CHUNK );
#endif
};
#ifndef COMM_WITHOUT_INTERRUPTS
/* Unmask (i.e. enable) IRQ3. This requires turning bit 3 to 0 */
/* and bits 0,1,2,4,5,6,7 to 1. Hence 11110011 binary = F3 hex */
outportb( MASTER_PIC_DATA, ( inportb( MASTER_PIC_DATA ) & 0xE7 ) ); /* Unmask (Enable) IRQ3 and IRQ4 (com ports) */
#endif
irqrestore( irqsave );
break;
};
};
};
void CBoard::Reset(){
switch( m_bWM ){
case WM_DMA_WORK:{
// disable laser devices
// outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
//SetStatus( m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1) );
m_bOutputMask &= ~(BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
StopDMA();
if( !m_bWorkEnable ){
outportb( NEW_RST_ADDRESS, 0 );// Full Upload
outportb( NEW_RST_ADDRESS, 0 );// Full Upload
outportb( NEW_RST_ADDRESS, 0 );// Full Upload
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 );
delay(1);
mwBoardID = (( BSNHB<<8) | BSNLB );
//printf("Board ser num: %04X\n", mwBoardID );
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
};
//Send SW ID in any case
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
outportb( NEW_RST_ADDRESS, 0 ); // Full Upload
delay(1);
// Serial Number
outportb( NEW_DTA_ADDRESS, mwSoftwareID & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( mwSoftwareID >> 8 ) & 0xFF );
delay(1);
// Motor Speed
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
#ifdef LGS_PLS_200
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 * 3 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 * 3 ) >> 8 ) & 0xFF );
delay(1);
#else
#ifdef LGS_FOD
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
#else
#ifdef ALS_300_BM
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, 0xFF );
delay(1);
#else
outportb( NEW_DTA_ADDRESS, ( m_Settings.nMotorSpeed * 535 ) & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, ( ( m_Settings.nMotorSpeed * 535 ) >> 8 ) & 0xFF );
delay(1);
#endif
#endif
#endif
#endif
// Zero Shift
#if defined( LGS_MND )
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift & 0xFF );
delay(1);
outportb( NEW_DTA_ADDRESS, m_Settings.nZeroShift >> 8 );
delay(1);
#else
outportb( NEW_DTA_ADDRESS, 0x00 );
delay(1);
outportb( NEW_DTA_ADDRESS, 0x00 );
delay(1);
#endif
// Temperature
outportb( NEW_DTA_ADDRESS, m_Settings.nTemperature );
delay(1);
// Work Status
outportb( NEW_DTA_ADDRESS, m_bOutputMask );
delay(1);
// DMA Status
outportb( NEW_DTA_ADDRESS, 3 ); // Stop DMA
delay(10);
m_nbWriteChunk = 0;
m_nbReadChunk = 0;
m_nwChunkInterator = 0;
m_bDMA_first_chunk_transfered = false;
m_bResetIssued = false;
m_bDMA_is_crashed = false;
memset ( DMA_BUFFER, 0, DMA_TRANSFER_CHUNK * DMA_TRANSFER_ZONES );
_dma_dos_buffer_iterator = 0;
#ifdef __DJGPP__
//uint32_t LA = _dma_buffer_ra.segment << 4 + _dma_buffer_ra.offset16;
InitDMA( DMA_CHANNEL );
#else
InitDMA( DMA_CHANNEL , DMA_BUFFER, DMA_TRANSFER_CHUNK );
#endif
m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
#if defined( LGS_PLS_1 ) || defined( LGS_STEPPER ) || defined( LGS_MND ) || defined( LGS_FOD ) || defined( ALS_300_BM )
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 7 ); // Status + DMA Upload
delay(1);
#else
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
outportb( NEW_RST_ADDRESS, 5 ); // Status + DMA Upload
delay(1);
#endif
outportb( NEW_DTA_ADDRESS, m_bOutputMask ); // Work Status
delay(1);
outportb( NEW_DTA_ADDRESS, 1 ); // Init DMA
m_nRealAngle = -1;
m_nErrorAngle = -1;
break;
}; // case WM_DMA
case WM_IRQ_WORK:{
// disable interrupts
//disable();
// disable laser devices
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
//enable();
delay(100);
if( !m_bWorkEnable ){
BSNLB=inportb( NEW_BASE_ADDRESS + 6 );
BSNHB=inportb( NEW_BASE_ADDRESS + 7 );
mwBoardID = (( BSNHB<<8) | BSNLB );
if ( ( ~( mwBoardID ^ mwSoftwareID ) & 0xFFFF ) == 0 ) m_bWorkEnable = true;
outportb( NEW_BASE_ADDRESS + 6, mwSoftwareID & 0xFF );
outportb( NEW_BASE_ADDRESS + 7, ( 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
delay( 10 );
// enable laser devices
m_bOutputMask |= (BE_NEW_DEVICE0 | BE_NEW_DEVICE1);
SetRelayBit( m_Settings.RelayDefaultMode );
outportb( NEW_BASE_ADDRESS + 2, m_bOutputMask );
m_BoardData.Clear();
m_nRealAngle = -1;
m_nErrorAngle = -1;
break;
}; // case WM_IRQ
}; // end of SWITCH WM
};
// special structure for data converting
#ifdef RANGE_16BIT
enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
#else
enum { BF_NONE = 0x00, BF_ZERO = 0x10, BF_SUN1 = 0x20, BF_SUN2 = 0x40, BF_POWER = 0x80, BF_MASK = 0xF0 };
#endif
uint8_t CBoard::GetData( PBOOL pZero, PBOOL pSun, PWORD pRange, PBOOL pPower ){
uint8_t bRetVal = 0;
switch( m_bWM ) {
case WM_DMA_WORK:{
FPDATA data;
uint8_t bFlags;
uint16_t wRange[2];
if( m_bDMA_first_chunk_transfered ){
if( m_bDMA_is_crashed ){
#ifdef DEBUG_BOARD_PRINT_NO_IRQ
printf( "DMA Overlaps [%d]=[%d] - BR\n", m_nbReadChunk , m_nbWriteChunk );
#endif
this->Reset();
bRetVal = 2;
} else {
if( m_nbWriteChunk != m_nbReadChunk ){
data = ( FPDATA ) ( DMA_BUFFER + DMA_TRANSFER_CHUNK * m_nbReadChunk );
#ifdef DEBUG_BOARD_DUMP_DATA
if( m_nwChunkInterator == 0 && m_nbReadChunk == ( DMA_TRANSFER_ZONES - 1 ) ){
FILE *FID;
FID = fopen("BUFFER.txt","w+b"); //a
fwrite( DMA_BUFFER, DMA_TRANSFER_ZONES, DMA_TRANSFER_CHUNK, FID );
fclose( FID );
};
#endif
m_wTimer++;
#ifdef RANGE_16BIT
// store flags
bFlags = data[ m_nwChunkInterator ].bValue[2] & BF_MASK;
wRange[0] = ( 0xFFFF - ( data[m_nwChunkInterator].wValue[0] ) & 0xFFFF );
#ifndef DMA_SINGLE_LRF
//wRange[1] = ( 0xFFFF - ( data[m_nwChunkInterator].bValue[4] << 8 + data[m_nwChunkInterator].bValue[3] ) & 0xFFFF );// >> 5;
wRange[1] = ( 0xFFFF - ( data[m_nwChunkInterator].wValue[1] ) & 0xFFFF );
#else // DMA_SINGLE_LRF
wRange[1] = wRange[0];
#endif // DMA_SINGLE_LRF
#else
// store flags
bFlags = data[ m_nwChunkInterator ].bValue[1] & BF_MASK;
wRange[0] = 0x07FF - (( data[m_nwChunkInterator].wValue[0] & 0x0FFF ) >> 1);
#ifndef DMA_SINGLE_LRF
wRange[1] = 0x07FF - (( data[m_nwChunkInterator].wValue[1] & 0x0FFF ) >> 1);
#else // DMA_SINGLE_LRF
wRange[1] = wRange[0];
#endif // DMA_SINGLE_LRF
#endif
#ifndef LGS_MND
if( bFlags & BF_ZERO ){
//putch( 0x2A );
if( m_nRealAngle < 0 ){
m_nRealAngle = m_Settings.nZeroShift;
} else {
m_nErrorAngle = m_Settings.nZeroShift;
};
};
*pZero = ( m_nRealAngle == 0 ) || ( m_nErrorAngle == 0 );
if( m_nRealAngle > (-1) ) m_nRealAngle--;
if( m_nErrorAngle > (-1) ) m_nErrorAngle--;
//if( *pZero ) putch( 0x3e );
#else
*pZero = ( bFlags & BF_ZERO ) > 0; // read zero
if( *pZero ) {
m_nRealAngle = 0;
}else{
m_nRealAngle++;
};
#endif
#ifdef BOARD_POWER_ENABLE
*pPower = bFlags & BF_POWER; // read power bit
#else
*pPower = true;
#endif
#ifdef BOARD_SUN_BIT_DISABLE
pSun[0] = pSun[1] = false;
#else
pSun[0] = bFlags & BF_SUN1; // read sun from device 1
#ifndef DMA_SINGLE_LRF
pSun[1] = bFlags & BF_SUN2; // read sun from device 2
#else
pSun[1] = pSun[0];
#endif
#endif // BOARD_SUN_BIT_DISABLE
// prepare values ( convert from 4095..0 to 0..2047 )
pRange[0] = wRange[0];
#ifndef DMA_SINGLE_LRF
pRange[1] = wRange[1];
#endif
m_nwChunkInterator++;
if ( m_nwChunkInterator >= ( DMA_TRANSFER_CHUNK / DMA_ANGLE_SHOT ) ){
m_nwChunkInterator = 0;
m_nbReadChunk++;
if ( m_nbReadChunk == DMA_TRANSFER_ZONES ) m_nbReadChunk = 0;
};
bRetVal = 1;
} else {
bRetVal = 0;
}
}; // DMA CRASH
} else bRetVal = 0;
break;
};
case WM_IRQ_WORK:{
#ifdef BOARD_DEFFERED_PROCESSING
// check data in conveier
BOARDDATA bd;
if( m_BoardData.Get( &bd ) ) {
// return data
*pZero = bd.bZero;
#ifdef BOARD_POWER_ENABLE
*pPower = bd.bPower;
#else
*pPower = true;
#endif
#ifdef BOARD_SUN_BIT_DISABLE
pSun[0] = pSun[1] = false;
#else
pSun[0] = bd.bSun[0];
pSun[1] = bd.bSun[1];
#endif
pRange[0] = bd.nRange[0];
pRange[1] = bd.nRange[1];
bRetVal = 1;
} else bRetVal = 0;
#else
// make sure that new data arrived
if( m_bReady ) {
// data will be readed
m_bReady = false;
// fill data fields
*pZero = m_BoardData.bZero;
#ifdef BOARD_POWER_ENABLE
*pPower = m_BoardData.bPower;
#else
*pPower = true;
#endif
#ifdef BOARD_SUN_BIT_DISABLE
pSun[0] = pSun[1] = false;
#else
pSun[0] = m_BoardData.bSun[0];
pSun[1] = m_BoardData.bSun[1];
#endif
pRange[0] = m_BoardData.nRange[0];
pRange[1] = m_BoardData.nRange[1];
bRetVal = 1;
} else bRetVal = 0;
#endif
break;
};
default:{
if( m_bWM == WM_FW_UPGRADE ) {
if( m_pFirmwareImage ) {
if( m_bFirmwareUpgradeDone ){
#ifdef DEBUG
printf("Board Firmware Transfered!\n");
#endif
freeHexImage( m_pFirmwareImage );
unlink( _T( "firmware" ) );
m_pFirmwareImage = NULL;
};
};
};
bRetVal = 1;
m_nwChunkInterator ++;
if( m_nwChunkInterator > LGS_REAL_DETECTOR_ANGLES * LGS_BOARDSAMPLE_RATIO ){
m_nwChunkInterator = 0;
};
if( ( m_nwChunkInterator % 2 ) == 0 ) bRetVal = 0;
*pZero = m_nwChunkInterator == 0;
*pSun = false;
*pRange = 1024;
*pPower = true;
break;
};
};
return bRetVal;
};
bool CBoard::getSettings( BOARD_SETTINGS *S ){
if( S == NULL ) return false;
memcpy( S, &m_Settings, sizeof(BOARD_SETTINGS));
return true;
};