Page 1 of 1

PCI Watchdog converted 'header' file.

Posted: Sun Jul 28, 2013 3:46 am
by jassing
we use their pci watchdog card in all our servers; recently I needed a very specific watchdog; so I took the opportunity to convert their example source into PB
We have been using it for almost a year...

Code: Select all

; File: pciWatchDog.pbi
; Product reference http://www.berkprod.com/Product_Web_Pages/pci_pc_watchdog.aspx
; Forum reference: http://purebasic.fr/english/viewtopic.php?f=12&t=55657

Enumeration; WDogFlags
  #WD_OK 
  #WD_DEVICE_NOT_FOUND
  #WD_DEVICE_NOT_OPENED
  #WD_INVALID_HANDLE
  #WD_FAIL_COMM_INIT
  #WD_IO_ERROR
  #WD_IO_READ_FAILED
  #WD_IO_WRITE_FAILED
  #WD_RX_CHECKSUM_ERROR           ; Not Used on PCI
  #WD_TX_CHECKSUM_ERROR           ; Not Used on PCI
  #WD_INVALID_COMMAND             ;board firmware Rev may be To old For new command?

  #WD_OPEN_EX_ERROR
  #WD_OPEN_EX_COMM_ERR            ; COM port specified was greater than MAX_COMM_PORTS
  #WD_COMM_VAL_ERR
  #WD_BD_TYPE_UNKNOWN             ; called a function where the type was unknown
  ; could be because board was not yet opened.
  #WD_USB_DISABLE_FAIL = $100     ; USB Specific
  #WD_USB_ENABLE_FAIL
  #WD_USB_FIND_COMM_ERROR         ; found a USB but errored on initial comm

  #WD_PCI_DISABLE_FAIL = $140 ; PCI Specific
  #WD_PCI_ENABLE_FAIL
  #WD_PCI_FIND_COMM_ERROR         ; found a PCI but errored on initial comm

  #WD_EUSB_DISABLE_FAIL = $180    ; ETH-USB Specific
  #WD_EUSB_ENABLE_FAIL
  #WD_EUSB_FIND_COMM_ERROR        ; found a PCI but errored on initial comm
  #WD_FTD2XX_DLL_NOT_FOUND
  #WD_FTD2XX_DLL_VER_ERR
  #WD_INVALID_INFO_STRUCTURE
  #WD_INVALID_IP_ADDRESS

  #WD_SERIAL_NO_COMM_PORT = $200 ; Could not find a comm port in the PC
  #WD_SERIAL_NO_DEVICE            ; Check all comm ports - no device found
  #WD_SERIAL_FIND_COMM_ERROR      ; found a Serial but errored on initial comm

  ; Special Flag/Status for serial WDogs - if possble it will be used as
  ; a flag - otherwise it will be a #WD_STATUS
  #WD_SERIAL_DATA_CONVERTED = $80000000  ; data saved - but not an exact converion


  ; Special for firmware - you can use upper byte to distinguish that it is firmware
  #WD_WDOG_FIRMWARE_ERR_FLAG = $FF000000  ; upper byte = 0xff indicates firmware error
  ; for any type WDog
  #WD_USB_FIRMWARE_ERROR           ; firmware rev too low to accept command
  #WD_PCI_FIRMWARE_ERROR           ; firmware rev too low to accept command
  #WD_EUSB_FIRMWARE_ERROR      ; firmware rev too low to accept command
  #WD_SERIAL_FIRMWARE_ERROR        ; firmware rev too low to accept command

  #WD_OTHER_ERROR = -1
EndEnumeration

;{ Status flags
  #WD_STAT_PCI_ENTP = $200000    ; PCI Board Only - Bit in Port #2
  #WD_STAT_PCI_WDIS = $100000    ; PCI Board Only - Bit in Port #2
  #WD_STAT_PCI_TTRP = $40000     ; PCI Board Only - Bit in Port #1
  #WD_STAT_PCI_WTRP = $10000     ; PCI Board Only - Bit in Port #1

  #WD_STAT_ETH_IP_SET = $40      ; Ethernet has a valid IP
  #WD_STAT_ETH_ENABLED = $20     ; Ehternet enabled
  #WD_STAT_RESET_PEND  = $10     ; Reset PC command issued - Pending timeout
  #WD_STAT_CMD_DISABLED = $8     ; WDog has been disabled by command
  #WD_STAT_POD_DSW_DELAY = $4    ; WDog is armed - SW set to wait for first "tickle"
  #WD_STAT_POD_ACTIVE = $2       ; WDog is still in 2.5 minute (or user) delay
  #WD_STAT_ACTIVE_ARMED = $1     ; WDog is armed and done with POD time
 ;}
  
;{ Diagnostic flags
  #WD_DIAG_ARP_ERROR = $40        ;IP address conflict
  #WD_DIAG_NV_WRITE_FAIL = $20    ; data not written to NV memory
  #WD_DIAG_MAC_INVALID = $10      ; MAC Address corrupted
  #WD_DIAG_NV_CORRUPTED = $8      ; Non-Voaltile Memory Corrupted
  #WD_DIAG_ETHER_OK = $4          ; Ethernet Chip OK
  #WD_DIAG_NVMEM_OK = $2          ; Non-Volatile Memory OK
  #WD_DIAG_TEMP_OK = $1           ; Temp Sensor IC OK
;}

;{ DIP Switch Bit Defines
    ; Common to all Boards
    #WD_DSW_OVERTEMP_RESET = $10
    #WD_DSW_POWER_ON_DELAY = $8    ; Wait for first "tickle" after POD time
    #WD_DSW_WDOG_TIME4 = $4
    #WD_DSW_WDOG_TIME2 = $2
    #WD_DSW_WDOG_TIME1 = $1
    ; USB & ETH-USB
    #WD_DSW_BUZZER_DISABLE = $20
    ; ETH-USB Switches
    #WD_DSW_ETH_OPTION1 = $80       ; Ethernet option switches
    #WD_DSW_ETH_OPTION2 = $40
    ; USB Switches
    #WD_DSW_PASSIVE_MODE = $80      ; SW1 Passive Mode
    #WD_DSW_ACTIVE_MODE = $40       ; SW2 Active Mode
    ; PCI & PCIe Switches
    #WD_DSW_RLY1_TEMP_MODE = $80    ; Activate RELAY #1 for overtemp
    #WD_DSW_RLY1_MODE = $40         ; Relay #1 Pulse or Latch
    #WD_DSW_RLY2_MODE = $20         ; Relay #2 Pulse or Latch
    ; Internal & External Serial
    #WD_DSW_IES_MONITOR = $80       ; Passive Monitor Mode
    #WD_DSW_IES_COMMAND = $40       ; Command Monitor Mode
    #WD_DSW_IES_RINGMON = $20       ; Ring RI Monitor Mode
    #WD_DSW_IES_POD = $10           ; Wait for first "tickle" after POD time
    ; Internal Serial
    #WD_DSW_IS_OVERTEMP_RST = $8
    ; External Serial
    #WD_DSW_EXS_NOTUSED = $8    ; Spare Switch - External Serial
    ;}
    
; Board types For WD_Open_Ex() Function
Enumeration ;WDogTypes
	#WD_TYPE_NONE_FOUND = 0
	#WD_TYPE_USB_V1             ; PN: 1140
	#WD_TYPE_PCI                ; PN: 1130 & 1134
	#WD_TYPE_PCI_TYP2           ; PN: 1131
	#WD_TYPE_PCI_EXP            ; PN: 1170
	#WD_TYPE_ETH_USB            ; PN: 1160
	#WD_TYPE_INT_SER            ; PN: 1120
	#WD_TYPE_EXT_SER            ; PN: 1110
EndEnumeration

;{ Constants
; ----------------------------------------------------
; Function Calling Flags
; ------------------------------------------
; Max Buffer Sizes in DLL
#INFO_ERR_BUFF_MAXSIZE = 256

; ----------------------------------------
; Flags for POD Set operations
#WD_POD_SETNVPOD = $2         ; Write new POD to non-volatile memory
#WD_POD_SETPOD = $1           ; Update POD time if WD not active
; Flags from POD Get operations
#WD_POD_SET_IGNORE = $1       ; new POD ignored - WD was already active

; ------------------------------------------
; Flags for WDog Timer Set operations
#WD_SETNV_TIMEOUT = $2         ; Write new time to nv memory
#WD_SET_TIMEOUT = $1           ; Set new time

; ------------------------------------------
; Clear Reset Count Flag
#WD_CLEAR_RST_CNT = $1         ; clear the Reset Count & LED

; ------------------------------------------
; Temp Offset Write Enable Flag
#WD_TEMP_OFF_WREN = $1         ; enable nv memory write

; ------------------------------------------
; Enable/Disable Flags for Send and Get
#WD_WDOG_ENABLE = $2           ; enable the Watchdog
#WD_WDOG_DISABLE = $1          ; disable the Watchdog

; ------------------------------------------
; Relay Pulse Write Enable Flag
#WD_RLY_PLS_WREN = $1          ; enable nv memory write

; ------------------------------------------
; Set the Buzzer Flags
#WD_BUZZ_GET_NOT_NV = $10000     ; get current buzzer time

#WD_BUZZ_NV_CNTL_EN = $8000        ; must be set for NV disable/enable operations
#WD_BUZZ_CNTL_EN = $4000        ; must be set for disable/enable operations
#WD_BUZZ_NV_DISABLE = $2000     ; save disable in non-volatile memory
#WD_BUZZ_DISABLE = $1000     ; buzzer disable

#WD_BUZZ_NV_WREN = $2          ; enable nv memory write
#WD_BUZZ_ON_EN = $1            ; turn buzzer on (off)

; ------------------------------------------
; User Code Write Enable Flag
#WD_USE_CODE_WREN = $1         ; enable nv memory write

; ------------------------------------------
; Enable - Disable PC Reset Flags
#WD_PC_RESET_NO_PEND = $80000   ; there was not a reset pending yet - issue reset command first
#WD_PC_RESET_NO_TR = $40000     ; board firmware does not allow read of time remaining
#WD_PC_RESET_NO_DIS = $20000    ; board firmware does not allow disable after enable
#WD_PC_RESET_REJECT = $10000    ; command rejected - board was not ARMed yet

#WD_PC_RESET_DIS = $2          ; stop the reset sequence if possible
#WD_PC_RESET_EN = $1           ; reset the pc w/timer

; ------------------------------------------
; Flags for Aux Get/Set Relay
#WD_AUX_RLY_INVRT_EN = $800     ; invert relay
#WD_AUX_RLY_RST_EN = $200       ; enable relay reset action
#WD_AUX_RLY_EN = $100           ; enable ON/OFF operation

#WD_AUX_RLY_INVRT = $80         ; invert relay at power-up
#WD_AUX_RLY_LATCH = $4          ; latch relay on at reboot
#WD_AUX_RLY_PULSE = $2          ; relay pulse at reboot
#WD_AUX_RLY_ON = $1             ; turn relay on

; ------------------------------------------
; Flags for Digital In
#WD_DIG_CLR_EXTCNT = $100       ; Clear External Trigger Count after read
#WD_DIG_IN_EDGE_EN = $80        ; enable edge detection command bit
#WD_DIG_IN_EDGE = $40           ; turn on edge detection
#WD_DIG_IN_NVWR_EN = $20        ; nv write enable
#WD_DIG_IN_NVEDGE = $10         ; nv turn on edge detection
; Flags returned
#WD_DIG_IN_ERROR = $8000        ; wrong firmware - too low
#WD_DIG_IN_EDGE_ACT = $80       ; edges are active for tickling
#WD_DIG_IN_NVEDGE_ACT = $40     ; edges active are stored in NV memory
#WD_DIGITAL_IN_EDGE = $2        ; edge has occurred
#WD_DIGITAL_IN_HIGH = $1        ; input is currently high


; --------------------------------------------------
; Flag defines for PCI only functions
; ------------------------------------------
; Flags for PCI Digital IO
#WD_PCI_DIG_OUT_EN = $1         ; enable writes to Digital out pins on PCI

; ------------------------------------------
; Flags for PCI Get Set Relays
; Flags for Set operations
#WD_PCI_INVRT_EN = $800        ; invert relay #1
#WD_PCI_HDW2_EN = $400         ; enable relay #2 hardware Port #1 options
#WD_PCI_RLY2_EN = $200         ; enable ON/OFF operation of Relay #2
#WD_PCI_RLY1_EN = $100         ; enable ON/OFF operation of Relay #1
; Flags for Get/Set operations
#WD_PCI_NV_INVRT = $20         ; make invert a non-volatile setting
#WD_PCI_INVRT_ON = $10         ; Invert Relay #1
#WD_PCI_HDW_EXCL_ON = $8       ; set bit R2DS exclusive Relay #2 in hardware Port #1
#WD_PCI_HDW_RLY2_ON = $4       ; turn on Relay #2 with hardware Port #1
#WD_PCI_RLY2_ON = $2           ; turn on Relay #1
#WD_PCI_RLY1_ON = $1           ; turn on Relay #1

; --------------------------------------------------
; Flag defines for External Serial only functions
; ------------------------------------------
; Flags Ext Serial Power Module
#WD_POWERMODULE_ON = $1         ; turn on the power module
#WD_POWERMODULE_OFF = $2        ; turn off

; --------------------------------------------------
; Flag defines for Ethernet-USB only functions
; --------------------------------------------------
; Get - Set Ethernet Command Allow
#WD_ETH_ALLOW_WREN = $1         ; write the codes to non-volatile

; ------------------------------------------
; Set IP Addresses Flag
#WD_NV_IPADD_WREN = $2          ; For writes - of NV Memory
#WD_IP_ACTIVATE_NOW = $1        ; restart Ethernet with new address

; ------------------------------------------
; Get IP Addresses Flag
#WD_NV_IPADD_RDEN = $1          ; Get the NV addresses

; ------------------------------------------
; Get / Set UDP Port Number
#WD_NV_UDP_PORT_WREN = $1       ; Wrtite new port number to non-volatile memory

; -------------------------------------------------------------------------
; Ethernet Reset
#WD_OVRR_DHCP_IP = $C0          ; DHCP IP override
#WD_OVRR_NVMEM_IP = $80         ; EEPROM IP override
#WD_OVRR_FIXED_IP = $40         ; fixed IP override

#WD_REBOOT_ETHERNET = $2        ; Perform soft reset
#WD_RESET_ETHERNET = $1         ; Perform Hard reset

; ----------------------------------------
; Get Set USB Suspend Mode
#WD_SUSPEND_IGNORE = $4         ; if set - then ignore suspend
#WN_NV_IGNORE_WREN = $2         ; Write status to nv memory
#WN_NV_SUSP_TIME_WREN = $1      ; Write time to NV memory

; -------------------------------
; Ethernet Commands Allowed
#E_Allow_WD_GetDeviceInfo = $1             ; Bit 0
#E_Allow_WD_GetTempTickle = $2             ; Bit 1
#E_Allow_WD_SetPowerOnDlyTimes = $4        ; Bit 2
#E_Allow_WD_GetPowerOnDlyTimes = $8        ; Bit 3
#E_Allow_WD_SetWdogTimes = $10             ; Bit 4
#E_Allow_WD_GetWdogTimes = $20             ; Bit 5
#E_Allow_WD_GetAnalogDigitalIn = $40       ; Bit 6
#E_Allow_WD_GetSetAuxRelay = $80           ; Bit 7
#E_Allow_WD_EnableDisable = $100           ; Bit 8
#E_Allow_WD_GetResetCount = $200           ; Bit 9
#E_Allow_WD_GetSetNvTempOffset = $400      ; Bit 10
#E_Allow_WD_SetBuzzer = $800               ; Bit 11
#E_Allow_WD_GetBuzzer = $1000              ; Bit 12
#E_Allow_WD_GetSetNvRelayPulse = $2000     ; Bit 13
#E_Allow_WD_GetSetNvUsbUserCode = $4000    ; Bit 14
#E_Allow_WD_EnableDisablePcReset = $8000   ; Bit 15
;}

;{ Public declarations of variables
; ----------------------------------------------------
;Global.l wdStatus
;Global.l wdHandle
;}

;{ Declarations
; --------------------------------------------------------
;   Start Function Defs
; ----------------------------------------
; Open Device - Must be called first
Prototype.l WD_Open(*pwdHandle)

; ----------------------------------------
; Open Device Extended - Must be called first
Prototype.l WD_OpenEx(*pwdHandle, iType.l)

; ----------------------------------------
; Close Device - Should be called last
Prototype.l WD_ForceCom(iCom.l)

; ----------------------------------------
; Force COM port - Call before OpenEx
Prototype.l WD_Close(wdHandle.l)

; ----------------------------------------
; Get DLL Version
;    Declare WD_GetDllVersion_
;                (*pDllVersion.l)

Prototype.l WD_GetDllVersion(*pDllVersion)


; ----------------------------------------
; Get Error / Info Messages
Prototype.l WD_GetErrorInfoMsg(*pErrorMsg, *pInfoMsg) ;(ErrorMsg.s, InfoMsg.s)

; ----------------------------------------
; Get board type
Prototype.l WD_GetWDogType(wdHandle.l, *iType)

; ----------------------------------------
; Get Com Port Open number
Prototype.l WD_GetComPortNum(wdHandle.l, *iCpn)

; ----------------------------------------
; Get Info from board
Prototype.l WD_GetDeviceInfo(wdHandle.l, *pStat, *pDipSw, *pVer, *pTick, *pDiag)

; ------------------------------------------
; Get Temp & Tickle the board
Prototype.l WD_GetTempTickle(wdHandle.l, *pTempw, *pTempf, *pTick)

; ------------------------------------------
; Set POD times
Prototype.l WD_SetPowerOnDlyTimes(wdHandle.l, iPod.l, iNvPod.l, iSetFlag.l, *pResFlag)

; ------------------------------------------
; Get POD times
Prototype.l WD_GetPowerOnDlyTimes(wdHandle.l, *pPod, *pNvPod)

; ------------------------------------------
; Set Watchdog Countdown Times
Prototype.l WD_SetWdogTimes(wdHandle.l, iWdTime.l, iNvWdTime.l, iFlag.l)

; ------------------------------------------
; Get Watchdog Countdown Times
Prototype.l WD_GetWdogTimes(wdHandle.l, *pWdTime, *pNvWdTime, *pHoldRegTime, *pDipSwTime)

; ------------------------------------------
; Get (Clear) Reset Count
Prototype.l WD_GetResetCount(wdHandle.l, iFlag.l, *pRstCnt)

; ------------------------------------------
; Get / Set NV Temp Offset
Prototype.l WD_GetSetNvTempOffset(wdHandle.l, iFlag.l, iNvOffset.l, *pCurOffset)

; ------------------------------------------
; WD Enable Disable
Prototype.l WD_EnableDisable(wdHandle.l, iFlagSet.l)

; ------------------------------------------
; Get / Set NV Relay Pulse
Prototype.l WD_GetSetNvRelayPulse(wdHandle.l, iFlag.l, iNvRelayPulse.l, *pCurRelayPulse)

; ------------------------------------------
; Set the Buzzer Times
Prototype.l WD_SetBuzzer(wdHandle.l, iBuzzTime.l, iNvBuzzTime.l, iFlags.l)

; ------------------------------------------
; Get the Buzzer Times
Prototype.l WD_GetBuzzer(wdHandle.l, *pFlag, *pBuzzTime)

; ------------------------------------------
; Get / Set NV User Code
Prototype.l WD_GetSetNvUserCode(wdHandle.l, iFlag.l, *pNvUserCode, *pCurNvUserCode) ; Last 2 are 'bytes'

; ------------------------------------------
; Enable / Disable PC Reset
Prototype.l WD_EnableDisablePcReset(wdHandle.l, iFlag.l, iResetTime.l, *pGetTime)

; ----------------------------------------
; Get & Set Aux Relay
Prototype.l WD_GetSetAuxRelay(wdHandle.l, iRelaySet.l, *pRelayGet)

; ----------------------------------------
; Get Digital In
Prototype.l WD_GetDigitalIn(wdHandle.l, *pExtCount, *pDi, iFlag.l)

; ------------------------------------------
; Get Analog Input
Prototype.l WD_GetAnalogIn(wdHandle.l, *pAi)

; ----------------------------------------
; Get / Set PCI Digital IO
Prototype.l WD_GetSetPciDigitalInOut(wdHandle.l, iDigOut.l, *pDigIn, iFlag.l)

; ------------------------------------------
; Get & set PCI Relays
Prototype.l WD_GetSetPciRelays(wdHandle.l, iRelaySet.l, *pRelayGet)

; ----------------------------------------
; Get Set External Serial Power Module
Prototype.l WD_GetSetPowerModule(wdHandle.l, iFlagSet.l, *iFlagGet)

;}

;{ Library Handling...

Global.l hPCIWDogLib 
Global.WD_Open WD_Open
Global.WD_OpenEx WD_OpenEx 
Global.WD_ForceCom WD_ForceCom 
Global.WD_Close WD_Close 
Global.WD_GetDllVersion WD_GetDllVersion 
Global.WD_GetErrorInfoMsg WD_GetErrorInfoMsg 
Global.WD_GetWDogType WD_GetWDogType 
Global.WD_GetComPortNum WD_GetComPortNum 
Global.WD_GetDeviceInfo WD_GetDeviceInfo 
Global.WD_GetTempTickle WD_GetTempTickle 
Global.WD_SetPowerOnDlyTimes WD_SetPowerOnDlyTimes 
Global.WD_GetPowerOnDlyTimes WD_GetPowerOnDlyTimes 
Global.WD_SetWdogTimes WD_SetWdogTimes 
Global.WD_GetWdogTimes WD_GetWdogTimes 
Global.WD_GetResetCount WD_GetResetCount 
Global.WD_GetSetNvTempOffset WD_GetSetNvTempOffset 
Global.WD_EnableDisable WD_EnableDisable 
Global.WD_GetSetNvRelayPulse WD_GetSetNvRelayPulse 
Global.WD_SetBuzzer WD_SetBuzzer 
Global.WD_GetBuzzer WD_GetBuzzer 
Global.WD_GetSetNvUserCode WD_GetSetNvUserCode 
Global.WD_EnableDisablePcReset WD_EnableDisablePcReset 
Global.WD_GetSetAuxRelay WD_GetSetAuxRelay 
Global.WD_GetDigitalIn WD_GetDigitalIn 
Global.WD_GetAnalogIn WD_GetAnalogIn 
Global.WD_GetSetPciDigitalInOut WD_GetSetPciDigitalInOut 
Global.WD_GetSetPciRelays WD_GetSetPciRelays 
Global.WD_GetSetPowerModule WD_GetSetPowerModule 

hPCIWDogLib = OpenLibrary(#PB_Any,"WDog_Univrsl.dll")

If hPCIWDogLib ;{
	WD_Open = GetFunction(hPCIWDogLib,"WD_Open")
	WD_OpenEx = GetFunction(hPCIWDogLib,"WD_OpenEx")
	WD_ForceCom = GetFunction(hPCIWDogLib,"WD_ForceCom")
	WD_Close = GetFunction(hPCIWDogLib,"WD_Close")
	WD_GetDllVersion = GetFunction(hPCIWDogLib,"WD_GetDllVersion")
	WD_GetErrorInfoMsg = GetFunction(hPCIWDogLib,"WD_GetErrorInfoMsg")
	WD_GetWDogType = GetFunction(hPCIWDogLib,"WD_GetWDogType")
	WD_GetComPortNum = GetFunction(hPCIWDogLib,"WD_GetComPortNum")
	WD_GetDeviceInfo = GetFunction(hPCIWDogLib,"WD_GetDeviceInfo")
	WD_GetTempTickle = GetFunction(hPCIWDogLib,"WD_GetTempTickle")
	WD_SetPowerOnDlyTimes = GetFunction(hPCIWDogLib,"WD_SetPowerOnDlyTimes")
	WD_GetPowerOnDlyTimes = GetFunction(hPCIWDogLib,"WD_GetPowerOnDlyTimes")
	WD_SetWdogTimes = GetFunction(hPCIWDogLib,"WD_SetWdogTimes")
	WD_GetWdogTimes = GetFunction(hPCIWDogLib,"WD_GetWdogTimes")
	WD_GetResetCount = GetFunction(hPCIWDogLib,"WD_GetResetCount")
	WD_GetSetNvTempOffset = GetFunction(hPCIWDogLib,"WD_GetSetNvTempOffset")
	WD_EnableDisable = GetFunction(hPCIWDogLib,"WD_EnableDisable")
	WD_GetSetNvRelayPulse = GetFunction(hPCIWDogLib,"WD_GetSetNvRelayPulse")
	WD_SetBuzzer = GetFunction(hPCIWDogLib,"WD_SetBuzzer")
	WD_GetBuzzer = GetFunction(hPCIWDogLib,"WD_GetBuzzer")
	WD_GetSetNvUserCode = GetFunction(hPCIWDogLib,"WD_GetSetNvUserCode")
	WD_EnableDisablePcReset = GetFunction(hPCIWDogLib,"WD_EnableDisablePcReset")
	WD_GetSetAuxRelay = GetFunction(hPCIWDogLib,"WD_GetSetAuxRelay")
	WD_GetDigitalIn = GetFunction(hPCIWDogLib,"WD_GetDigitalIn")
	WD_GetAnalogIn = GetFunction(hPCIWDogLib,"WD_GetAnalogIn")
	WD_GetSetPciDigitalInOut = GetFunction(hPCIWDogLib,"WD_GetSetPciDigitalInOut")
	WD_GetSetPciRelays = GetFunction(hPCIWDogLib,"WD_GetSetPciRelays")
	WD_GetSetPowerModule = GetFunction(hPCIWDogLib,"WD_GetSetPowerModule")
	
Else
	Debug "No Watchdog dll loaded"
EndIf ;}
;}

CompilerIf #PB_Compiler_IsMainFile

Procedure.s GetErrorMsg( cMsg.s = "")
	Protected *ErrBuf, *InfBuff
	
	*ErrBuf = AllocateMemory(#INFO_ERR_BUFF_MAXSIZE)
	*InfBuff = AllocateMemory(#INFO_ERR_BUFF_MAXSIZE)
	
	If *ErrBuf And *InfBuff
		If WD_GetErrorInfoMsg(*ErrBuf,*InfBuff)
			cMsg + " "+PeekS(*ErrBuf)+"/"+PeekS(*InfBuff)
		Else
			cMsg + " (Failed to get error information)"
		EndIf
	Else
		cMsg + " (Failed to allocate memory)"
	EndIf
	If *ErrBuf : FreeMemory(*ErrBuf) : EndIf
	If *InfBuff : FreeMemory(*InfBuff) : EndIf
	ProcedureReturn cMsg
EndProcedure
	;{ Macro Section
	Macro _GetDeviceInfo()
		If WD_GetDeviceInfo(wdHandle, @iStat, @iDSW, @iVer, @iTick, @iDiag) = #wd_ok
			Debug "Firmware Version "+Str ( (iVer>>8) )+"."+Str(iVer & $0ff)
			Debug "Current DIP Switch: 0x"+Hex(iDSW,#PB_Integer)
			Debug "Tickle Count: "+Str(iTick)
			
			; section 3
			Debug "Board status bits: 0x"+Hex(iStat,#PB_Integer)
			If iStat
				Debug "  Status Detail"
				
				If iStat & #WD_STAT_ACTIVE_ARMED
					Debug "    ARMED/POD completed"
				Else
					Debug "    NOT Armed"
				EndIf
				
				If iStat & #WD_STAT_POD_ACTIVE
					Debug "    Still in 2.5 minute POD delay"
				Else
					Debug "    Done with POD delay"
				EndIf
				
				If iStat & #WD_STAT_POD_DSW_DELAY
					Debug "    Finished POD/waiting for 1st tickle"
				EndIf
				
				If iStat & #WD_STAT_CMD_DISABLED
					Debug "    Card has awk'd the disable command"
				EndIf
				
				If iStat & #WD_STAT_RESET_PEND 
					Debug "    Reset is pending!"
				Else
					;Debug "    No reset pending"
				EndIf
			EndIf
			
			Debug "Board Diagnostic Bits: 0x"+Hex(iDiag,#PB_Integer)
			If iDiag
				If iDiag & #WD_DIAG_TEMP_OK
				;	Debug "    Temp sensor OK"
				Else
					Debug "    Failed to obtain temp"
				EndIf
				
				If iDiag & #WD_DIAG_NVMEM_OK
				;	Debug "    Non Vol Mem OK"
				Else
					Debug "    ERROR with Non Volatile RAM!"
				EndIf
				
				If iDiag & #WD_DIAG_NV_CORRUPTED
					Debug "    WARNING: Non Volatile RAM Corrupted"
				EndIf
				
				If iDiag & #WD_DIAG_NV_WRITE_FAIL
					Debug "    WARNING: Non volitile RAM failed a write"
				EndIf
			EndIf 
			
		Else
			Debug GetErrorMsg("Failed to get device info")
		EndIf
	EndMacro
	
	Macro _Tickle()
		If WD_GetTempTickle(wdHandle,@iTempW,@iTempF,@iTick) = #WD_OK 
			Debug "Tick #: "+Str(iTick)
			Debug "Temp C: "+Str(iTempW)+"."+Str(iTempF)
		Else
			Debug GetErrorMsg("Failed TICKLE")
		EndIf
	EndMacro
	
	Macro _GetEnabled()
		If WD_EnableDisable( wdHandle, #WD_WDOG_ENABLE)
			Debug "Card Enabled"
		Else
			Debug "Failed to enable card"
		EndIf
	EndMacro
	
	Macro _EnableReset()
		iFlag = #WD_PC_RESET_EN
		iResetTime = 90; 90 seconds to reset!
		
		If WD_EnableDisablePcReset(wdHandle, iFlag, iResetTime, @iTime) = #WD_OK
			If iTime
				If iTime & #WD_PC_RESET_REJECT
					Debug "Card did not accept command (to enable reset)"
				Else
					Debug "Card accepted command (to enable reset)"
				EndIf
				If iTime & #WD_PC_RESET_NO_DIS
					Debug "Firmware error/reset disable"
				EndIf
				If iTime & #WD_PC_RESET_NO_TR
					Debug "Reading time remainging - board too old"
				EndIf
				If iTime & #WD_PC_RESET_NO_PEND
					Debug "Tried tor ead back time remaining before reset (no reset pending)"
				EndIf
			EndIf 
		Else
			Debug GetErrorMsg("Failed to enable/disable PC reset")
		EndIf
	EndMacro
	;}
	
	If hPCIWDogLib 
		Define.i iVersion, iTempF, iTempW, iTick,iCountDownTime, iNVCountDownTime, iHoldingRegTime, iDIPSwitchTime
		Define.l wdStatus, wdHandle, nStart
		Define.i	iStat, iDSW, iVer, iTick, iDiag, iFlag, iResetCount, iNewOffset, iCurrentOffset
		Define.i iResetTime, iTime
		
		If WD_GetDllVersion( @iVersion ) = #WD_OK
			Debug "DLL Version: "+Str( (iVersion & $FF0000)>>16)+"."+ "." + Str( (iVersion&$ff00)>>8)+"."+Str( iVersion&$00ff) 
		Else
			Debug "Failed to get dll version"
		EndIf
			
		If WD_Open(@wdHandle) = #WD_OK
			Debug "Card opened!"
			
			_GetDeviceInfo()
			
			If WD_GetWdogTimes(wdHandle, @iCountDownTime, @iNVCountDownTime, @iHoldingRegTime, @iDIPSwitchTime) = #wd_ok
				Debug "Current countdown time "+Str(iCountDownTime)
				Debug "Current NV Countdown time "+Str(iNVCountDownTime)
				Debug "Current Holding Register Time "+Str(iHoldingRegTime)
				Debug "DIP switch Time "+Str(iDIPSwitchTime)
			Else
				Debug GetErrorMsg("Failed to get watchdog times")
			EndIf 
			
			iFlag = 0 ; can be: #WD_CLEAR_RST_CNT
			If WD_GetResetCount(wdHandle, iFlag, @iResetCount)  = #WD_OK
				Debug "Card reset count: "+Str(iResetCount)
			Else
				Debug GetErrorMsg("Failed to get reset count")
			EndIf
			
			iFlag = 0 ; Can be: #WD_TEMP_OFF_WREN (then set new value)
			iNewOffset=0
			
			If WD_GetSetNvTempOffset(wdHandle,iFlag, iNewOffset, @iCurrentOffset) = #WD_OK
				Debug "Current NV Temp Offset: "+Str(iCurrentOffset)
			Else
				Debug GetErrorMsg("Failed to get temp offset")
			EndIf
						
			Debug "---- PRE Tickle Info ----"
			_EnableReset()
			_GetDeviceInfo()
			_GetEnabled()
			
			
			If MessageRequester("Tickle?","Do you want to tickle? (will cause reset)",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes
				Debug "--- TICKLE -----"
				Repeat 
					_Tickle()	
					Delay(1000)
				ForEver
			Else
				Debug "--- TICKLE SKIPPED ----"
			EndIf
		  
			Debug "---- POST Tickle Info ----"
			_EnableReset()
			_GetDeviceInfo()
			_GetEnabled()
			
			Debug "Test over! Ready to reboot???"
			
			If WD_Close(wdHandle) = #wd_ok
				Debug "Card closed"
			Else
				Debug GetErrorMsg("Failed to close card")
			EndIf
			
		Else
			Debug GetErrorMsg("Failed OPEN")
		EndIf
	Else
		Debug "No Watchdog DLL Found!"
	EndIf

CompilerEndIf