//--------------------------------------------------------------------------- #include #include "registry.hpp" #pragma hdrstop #include "Sample1u.h" #include "acces32.h" #include "wdg.h" #include "WDGThrd1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; //--------------------------------------------------------------------------- const char *myKey = "Software\\PCIFIND\\NTioPCI\\Parameters"; WDGThread *Thread; unsigned int BaseAddr; unsigned int IRQ; unsigned int Running; unsigned int found; unsigned char State = 0; int Loop = 0; TPCI_COMMON_CONFIG buf[64]; //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void CtrMode(unsigned char cntr, unsigned char mode) { unsigned char ctrl; ctrl = (cntr << 6) | 0x30 | (mode << 1); OutPortB(BaseAddr+3, ctrl); } void LoadCtr(int c,int val) { OutPortB(BaseAddr+c, (0x00FF & val)); OutPortB(BaseAddr+c, (0xFF00 & val)>>8); } void SetCounter() { InPortB(BaseAddr+7); //disable counter functions CtrMode(2,0); //set counter 2 to mode 0 OutPortB(BaseAddr+7,0); //enable counters InPortB(BaseAddr+7); //disable counter functions OutPortB(BaseAddr+6,0); //enable buzzer OutPortB(BaseAddr+0x0C,0); //select low clock rate InPortB(BaseAddr+0x0D); //disable opto reset InPortB(BaseAddr+0x0E); //disable opto reset InPortB(BaseAddr+0x0F); //disable program outputs //program the counters for the REQUIRED modes CtrMode(0,3); //set counter 0 to mode 3 LoadCtr(0,0xFFFF); //full load value CtrMode(1,2); //set counter 1 to mode 2 LoadCtr(1,0x10); //load counter 1 with 10 hex CtrMode(2,1); //set counter 2 to mode 1 //Note:Not loading counter 2 creates an infinite reset duration //Set the reset duration by loading a value in counter 2 //Change counter 2 to mode 2 to set duration } void TForm1::SetHandler() { unsigned int HandleCOS, HandleTimeout; unsigned int TimeoutOp; short BusType; unsigned char BusNumber; HandleCOS = CheckBox1->Checked; HandleTimeout = CheckBox2->Checked; TimeoutOp = IGNORE_TIMEOUT; if (HandleTimeout) { if (RadioButton1->Checked) TimeoutOp = IGNORE_TIMEOUT; else if (RadioButton2->Checked) TimeoutOp = DISABLE_TIMEOUT; else if (RadioButton3->Checked) TimeoutOp = SHUTDOWN_TIMEOUT; } try { BusNumber = StrToInt(BusNumberEdit->Text); } catch (const EConvertError &E) { BusNumber = 0; }; if (BusTypeCombo->Text == "PCI") BusType = PCIBus; else if (BusTypeCombo->Text == "ISA") BusType = Isa; else BusType = InterfaceTypeUndefined; WDGSetHandler(BaseAddr, IRQ, BusType, BusNumber, HandleCOS, HandleTimeout, TimeoutOp); } //--------------------------------------------------------------------------- void __fastcall TForm1::Timer1Timer(TObject *Sender) { unsigned int readback; switch (State) { case 0: InPortB(BaseAddr+4); //clear any pending interrupts OutPortB(BaseAddr+7,0); //start counters counting State++; break; case 1: CtrMode(1,2); LoadCtr(1,0x10); Loop++; if(Loop == 100) State++; LoopLabel->Caption = IntToStr(Loop); break; case 2: readback = InPortB(BaseAddr+4); if ((readback & 1) == 0) { StatusLabel->Caption = "Watchdog Timed Out"; TestButton->Caption = "&Test"; State++; Loop = 0; } else StatusLabel->Caption = "Waiting for Timeout"; break; case 3: Loop++; if (Loop == 5) { InPortB(BaseAddr + 6); Timer1->Enabled = false; State = 0; Loop = 0; Thread->Terminate(); } break; } } //--------------------------------------------------------------------------- void __fastcall TForm1::TestButtonClick(TObject *Sender) { if(!found){ BaseAddr = StrToInt("0x"+ISAEdit->Text); IRQ = StrToInt("0x"+IRQEdit->Text); } if (TestButton->Caption == "&Test") { SetHandler(); Thread = new WDGThread(false); UpdateLabel->Visible = true; LoopLabel->Visible = true; TestButton->Caption = "&Abort"; StatusLabel->Caption = "Watchdog Okay"; SetCounter(); Timer1->Enabled = true; } else { InPortB(BaseAddr + 6); InPortB(BaseAddr + 7); Timer1->Enabled = false; TestButton->Caption = "&Test"; State = 0; Loop = 0; Thread->Terminate(); } } //--------------------------------------------------------------------------- void __fastcall TForm1::FormClose(TObject *Sender, TCloseAction &Action) { InPortB(BaseAddr + 6); InPortB(BaseAddr + 7); } //--------------------------------------------------------------------------- void __fastcall TForm1::FormCreate(TObject *Sender) { int num, i; if (InPortB(0x61) == 0xAA55) { Application->MessageBox("ACCESNT.SYS not detected. Please copy ACCESNT.SYS into [NT]/system32/drivers and re-run this sample.", "Warning", IDOK); } TRegistry *DriverRegistry = new TRegistry; found = false; DriverRegistry->RootKey = HKEY_LOCAL_MACHINE; DriverRegistry->OpenKey(myKey, true); num = DriverRegistry->ReadInteger("NumDevices"); if (num > 0) DriverRegistry->ReadBinaryData("PCICommonConfig", buf, (sizeof(TPCI_COMMON_CONFIG)*num)); for (i = 0; i<= num-1; i++) { if ((buf[i].DeviceID == 0x22C0) && (buf[i].VendorID == 0x494F)) { BaseAddr = buf[i].BaseAddresses[2] & 0xFFF8; IRQ = buf[i].InterruptLine; BaseLabel->Caption = IntToHex(int(BaseAddr),4); found = true; break; } }; if (!found) { ISAEdit->Visible = true; IRQEdit->Visible = true; Label11->Visible = true; Label1->Caption = "Windows 95/NT WDG-CSM Sample 1"; } DriverRegistry->Free(); } //--------------------------------------------------------------------------- void __fastcall TForm1::ExitButtonClick(TObject *Sender) { Close(); } //--------------------------------------------------------------------------- void __fastcall TForm1::CheckBox2Click(TObject *Sender) { RadioButton1->Enabled = CheckBox2->Checked; RadioButton2->Enabled = CheckBox2->Checked; RadioButton3->Enabled = CheckBox2->Checked; } //---------------------------------------------------------------------------