//---------------------------------------------------------------------------
#include <vcl.h>
#include <stdio.h>
#include <dos.h>
#include <windows.h>
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HINSTANCE hinstLib;
typedef int (*GETINT2PROC)(int, int*);
typedef int (*SETINT2PROC)(int, int);
typedef int (*GETUINTPROC)(unsigned int*);
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
AnsiString sVal;
unsigned int IcId;
GETUINTPROC ProcAdd;
char *endptr;
OpenDriverFail = 0;
int ResetTime = 0,Unit = 0;
TIniFile *ini;
ini = new TIniFile( ChangeFileExt( Application->ExeName, ".INI"));
ResetTime = ini->ReadInteger("Setting", "Reset_Time" , 0);
Unit = ini->ReadInteger("Setting", "Unit" , 0);
TimeValEdit->Text = ResetTime;
UnitRadioGroup->ItemIndex = Unit;
hinstLib = LoadLibrary("Fintek.dll");
if (hinstLib == NULL)
{
ShowMessage("Load fail Fintek.dll");
OpenDriverFail = 1;
}
delete ini;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormShow(TObject *Sender)
{
if( OpenDriverFail == 1 )
Close();
}
//---------------------------------------------------------------------------
void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action)
{
if (hinstLib != NULL)
{
FreeLibrary(hinstLib);
}
}
//---------------------------------------------------------------------------
int __fastcall TForm1::READ_PORT(int readport)
{
int data;
GETINT2PROC ProcAdd;
char *endptr;
ProcAdd = (GETINT2PROC) GetProcAddress(hinstLib, "PORT_LPC_R");
if (NULL != ProcAdd)
{
if (! (*ProcAdd)(readport, &data))
{
ShowMessage("Read Fail");
}
}
return data;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::WRITE_PORT(int writeport, int writevalue)
{
SETINT2PROC ProcAdd;
char *endptr;
ProcAdd = (SETINT2PROC) GetProcAddress(hinstLib, "PORT_LPC_W");
if (NULL != ProcAdd)
{
if (! (*ProcAdd)(writeport, writevalue))
{
ShowMessage("Write Fail");
}
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SetButtonClick(TObject *Sender)
{
int iRegE8, iRegED, iRegE0, iUnit, ResetTime, Unit;
int IsEnabled = 1; //0 disable 1 enable
int temp=0;
//ini save----------
TIniFile *ini;
ini = new TIniFile( ChangeFileExt( Application->ExeName, ".INI"));
ResetTime = StrToInt(TimeValEdit->Text);
Unit = StrToInt(UnitRadioGroup->ItemIndex);
ini->WriteString("Setting", "Reset_Time" , ResetTime);
ini->WriteString("Setting", "Unit" , Unit);
//-------------------
WRITE_PORT(0x4E, 0x87);
WRITE_PORT(0x4E, 0x87);
WRITE_PORT(0x4E, 0x07);//set ldn 0x07
WRITE_PORT(0x4F, 0x07);//ldn
Sleep(1);
//enable watchdog function
WRITE_PORT(0x4E, 0xF0); //baseaddress 0xF0
temp=(int)READ_PORT(0x4F);
WRITE_PORT(0x4F, (temp&(~0x81))|(IsEnabled<<7));//set bit7=IsEnable and bit0=0
/*
// Set this bit to enable Timeout event to wakeup system
WRITE_PORT(0x4E, 0xe8);
iRegE8 = READ_PORT(0x4E + 1);
WRITE_PORT(0x4E + 1, (iRegE8 | 0x04));
Sleep(1);
// Watchdog timer timeout status. Write 1 to clear.
WRITE_PORT(0x4E, 0xed);
WRITE_PORT(0x4E + 1, 0x10);
Sleep(1);
// enable EuP function
WRITE_PORT(0x4E, 0xe0);
iRegE0 = READ_PORT(0x4E + 1);
WRITE_PORT(0x4E + 1, (iRegE0 | 0x81));
Sleep(1);
*/
// Watchdog timer count timer
WRITE_PORT(0x4E, 0xF0+6);
temp= TimeValEdit->Text.ToInt();
temp = temp&0xff;
WRITE_PORT(0x4F, temp);
Sleep(1);
// UNIT
/*
if ( UnitRadioGroup->ItemIndex == 0 )
iUnit = 0x00;
else
iUnit = 0x02;
*/
iUnit = (int)UnitRadioGroup->ItemIndex;
iUnit = iUnit&1;
// Enable EuP watchdog timer
WRITE_PORT(0x4E, 0xF0+5);
temp=(int)READ_PORT(0x4F);
WRITE_PORT(0x4F, (temp&(~0x28))|0x11|(iUnit<<3)|(IsEnabled<<5));
Sleep(1);
// exit superio
WRITE_PORT(0x4E, 0xaa);
Sleep(1);
delete ini;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::TimeValEditExit(TObject *Sender)
{
if ( !TimeValEdit->Text.IsEmpty())
{
if ( (TimeValEdit->Text.ToInt() < 1) || (TimeValEdit->Text.ToInt() > 255) )
TimeValEdit->SetFocus();
}
else
TimeValEdit->SetFocus();
}
//---------------------------------------------------------------------------