Programming the Watchdog Timer of LattePanda 3 Delta

userHead Youyou 2023-12-26 19:36:14 671 Views2 Replies

Introduction

 

Lattepanda 3 Delta utilizes IT8613E Super I/O chipset which bulid in the watchdog timer. It output the reset command through the PWRGD pin to let the OS restart. 

This post will show the principles of watchdog timer initialization, setting related registers, and how to program the watchdog timer in Ubuntu OS.
 

 

Principle introduction

After the hardware reset or power-on reset, the IT8613E enters the normal mode with all logical devices disabled except KBC. The initial state (enable bit ) of this logical device (KBC) is "1".

 

Configuring Sequence

 

 

There are three steps to complete the configuration setup: 

(1) Enter the MB PnP Mode; 

(2) Modify the data of configuration registers; 

(3) Exit the MB PnP Mode. 

Note: The undesired result may occur if the MB PnP Mode is not exited properly.

 

Enter the MB PnP Mode 

To enter the MB PnP Mode, four special I/O write operations are required to be performed during Wait for Key state. To ensure the initial state of the key-check logic, it is necessary to perform four write opera-tions to the Special Address port (2EH). 

 

In Ubuntu OS, we can use the following codes:

 outb(0x87,0x2E);
 outb(0x01,0x2E);
 outb(0x55,0x2E);
 outb(0x55,0x2E);

 

Modify Data of Configuration Registers 

All configuration registers can be accessed after entering the MB PnP Mode. Before accessing a selected register, the content of Index 07h must be changed to the LDN to which the register belongs, except some Global registers.

 

In Ubuntu OS, we can use the following codes:

 outb(0x07,0x2E);
 outb(0x07,0x2F);

 

Watchdog Timer Register

 

The watchdog timer of LattePanda 3 Delta output the reset command through the PWRGD pin to let the OS restart. That is, set Bit 4 as 1. 

 

In this post, we use 60 seconds as an example. That is, set Bit 7 as 1.

10010000 BIN = 90 HEX. 

Write 0x90 to this register. 

 

In Ubuntu OS, the following code can be used:

 outb(0x72,0x2E);
 outb(0x90,0x2F);

 

 

In this post, we set the reset time to 60 seconds. 

60 DEC = 3C HEX.

Write 0x3C to the LSB register. There is no need to operate the MSB register, just keep the default value.

In Ubuntu OS, the following code can be used:

 outb(0x73,0x2E);
 outb(0x3C,0x2F);

 

Exit the MB PnP Mode 

Set Bit 1 of the configure control register (Index=02h) to 1 to exit the MB PnP Mode.

In Ubuntu OS, the following code can be used:

 outb(0x02,0x2E);
 outb(0x02,0x2F);

 

 

Note: The above content of register and  configuring sequence are excerpted from the data sheet of IT8613E. If you want to dig deeper, check out the chip's datasheet.

 

 

Program the WDT in Ubuntu

 

Preparation

LattePanda 3 Delta

ubuntu 22.04 or Later

 

Sample Code

#include<stdio.h>
#include<sys/io.h>
#include<stdlib.h>
#include<unistd.h>
#define REG        0x2E
#define VAL        0x2F
int superio_enter(void)
{
    outb(0x87, REG);
    outb(0x01, REG);
    outb(0x55, REG);
    outb(0x55, REG);
    return 0;
}
int superio_inb(int reg)
{
    int val;
    outb(reg, REG);
    val = inb(VAL);
    return val;
}
void superio_exit()
{
    outb(0x02, REG);
    outb(0x02, VAL);
}
void feed_dog(int val)
{
    outb(0x73, REG);
    outb(val, VAL);
    if(val>255)
    {
        outb(0x74, REG);
        outb(val>>8, VAL);
    }
    
}
int main()
{
    int status = iopl(3);
    if(status == -1)
    {
        printf("iopl error.\n");
        return -1;
    }
    superio_enter();
    printf("Chip Type: %02x%02x\n", superio_inb(0x20),superio_inb(0x21)); // read the superio chip id
    superio_exit();
    superio_enter();
    
    //select logic device
    outb(0x07, REG);
    outb(0x07, VAL);
    
    //configure the WDT register
    outb(0x72, REG);
    outb(0x90, VAL);//1001 0000 (second;PWRGD)
    outb(0x73, REG);
    outb(0x3C, VAL);//LSB 0x3C (set time-out value: 60 seconds)
    outb(0x74, REG);
    outb(0x00, VAL);//MSB 0x00
    superio_exit();
    
    while(1)
    {
        sleep(30);
        
        printf("start to feed the dog...    ");
        superio_enter();
        outb(0x07, REG);
        outb(0x07, VAL);
        //feed dog,reset time-out to 60s.
        //if comment this, when the time is up, WDT will cause the OS to restart        
        feed_dog(60);
        printf("finished! \n");
        superio_exit();
    }
    
    return 0;
}

 

Step

1. Download the attachment, then extract it to a folder.

 

2. Go to this folder and open the terminal.

 

3. Enter the following command to compile the .out file.

sudo gcc superio_watchdog.c

 

4. Run the compiled .out file to start the watchdog timer, and feed it every 30 seconds.

sudo ./a.out

In the sample code, the time-out is 60 seconds, which means that if the watchdog timer cannot be fed within 60 seconds, the operating system will restart.

 

5. Based on this sample, you can add it to the system startup items, or develop customized program to fit your application. 

The above method is to enable the watchdog timer in the operating system. When the watchdog timer overflows and the operating system is restarted, the watchdog will not start automatically.