Main Site Documentation

Fez Cerberus and HW Watchdog in Visual Basic


#1

I’ve spent probably upwards of 10 hours or so searching online for a way to implement the Hardware watchdog using Visual Basic Code. I’ve found several references to how to do it in C#, but I just can’t find a reference to how to do it using visual basic code.

As soon as I try to include GHI.Premium.Hardware I receive this error on compile:

“An unhandled exception of type ‘System.Exception’ occurred in Microsoft.SPOT.Hardware.dll”

Any assistance with this would be greatly appreciated.

Thanks


#2

@ michaelb - welcome to the forum
can you show us your code ?


#3

The reference above is for the GHI Premium library. The Cerberus uses the OSHW libraries. You need to use references to the OSHW libraries.


#4

Here is a basic snapshot of what I’m doing, I’ve trimmed back a lot of the extra variables ect.:

Imports GT = Gadgeteer
Imports GTM = Gadgeteer.Modules
Imports Microsoft.SPOT.Hardware
Imports GHIElectronics.Gadgeteer.FEZCerberus
Imports System
Imports Gadgeteer.Modules.GHIElectronics
Imports System.Math

Namespace GadgeteerApp2

Public Class Program
    'Public COUNT As Int32
    Dim intRecievedIndex As Int32 = 0
    Dim bytRecievedData(16) As Byte
    Dim bytrecieved(16) As Int32
    Dim Out1 As GT.Interfaces.DigitalOutput
    Dim Out2 As GT.Interfaces.DigitalOutput
    Dim Cout() As GT.Interfaces.DigitalOutput = {Out1, Out2}
    Dim RTUout() As Byte = {23, 24}

    Public Sub ProgramStarted()
        Cout(0) = breakout.SetupDigitalOutput(GT.Socket.Pin.Three, False)  
        Cout(1) = breakout.SetupDigitalOutput(GT.Socket.Pin.Four, False) 

Here is some of where I was trying to set up the Watchdog with no luck, if I ignore the “Premium” version, (and take out the comments) this compiles, but never forces a reset

        'Set up timer for Watchdog
        'Dim spTimeout As TimeSpan = New TimeSpan(0, 0, 0, 0, 1) ' Set up the Timeout to be 15 minutes (or 1 ms for testing)
        'Debug.Print(GHI.Premium.Hardware.LowLevel.Watchdog.LastResetCause.ToString())
        'Watchdog.Timeout = spTimeout ' Apply the timeout period
        'Watchdog.Behavior = WatchdogBehavior.HardReboot
        'Watchdog.Enabled() = True

        rs232.Initialize(baudRate:=9600)
        Thread.Sleep(200)           'Needed to avoid glitches of not opening correctly
        rs232.serialPort.Open()
        Thread.Sleep(200)
        AddHandler rs232.serialPort.DataReceived, AddressOf DataReceivedHandler
        Debug.Print("232 open")
        rs232.serialPort.DiscardInBuffer()
    End Sub

Please let me know if any more is required.


#5

Have you tried the code in this document? https://www.ghielectronics.com/docs/31/watchdog?

Get this working before Gadgeteer.

Also, you should never sleep in the ProgramStarted method. This causes all sorts of problems. You should do your initialization and exit.


#6

Hi Mike,
Thanks for the assistance, and yes I had looked at that code, but every time I tried to run it, I would run into issues with it not liking the premium library. That said, your initial post about the OSHW libraries was extremely helpful, and now I have code that works as such below:

Load the correct HW library

Imports GHI.OSHW.Hardware

Initialization of the Registers. This is for the IWDG, there is another at 40002C00

    Dim WDKR As GHI.OSHW.Hardware.LowLevel.Register = New GHI.OSHW.Hardware.LowLevel.Register(&H40003000) 'Correct Address of the IWDG timer for the Fez Cerberus
    Dim WDPR As GHI.OSHW.Hardware.LowLevel.Register = New GHI.OSHW.Hardware.LowLevel.Register(&H40003004) 'Prescaler, bottom 3 bits.
    Dim WDSR As GHI.OSHW.Hardware.LowLevel.Register = New GHI.OSHW.Hardware.LowLevel.Register(&H4000300C) ' // Not needed , gives status for being able to adjust the prescaler
    Dim WDLR As GHI.OSHW.Hardware.LowLevel.Register = New GHI.OSHW.Hardware.LowLevel.Register(&H40003008) '' // Watchdog Count down value, default FFF

**The next section sets up the timeout, WDPR is the prescaler from with 1= /4 & 7=/256
**WDLR is the countdown amount from 1 to FFF

   WDKR.Write(&H5555) 'Enable the changing of WDPR or WDLR 
   Thread.Sleep(1)
   WDPR.Write(&H7)   'Change the prescaler to divide by 256, currently set for 30s
   Thread.Sleep(1)

   WDKR.Write(&HCCCC) ‘Start the Watchdog Timer

**Quick Subroutine to refresh the watchdog time.

Public Sub ResetCounter()
WDKR.Write(&HAAAA)
End Sub

I have tested this, and it works great.
I also need to give credit to Sambo’s post for pointing me in the right direction.
https://www.ghielectronics.com/community/codeshare/entry/887


#7

@ michaelb - I am glad that it works…

one tip for the next post :

use the “101010” button to format the programcode