Cubieboard2裸机开发之(五)看门狗操作

前言

说到看门狗,应该不会陌生,看门狗说白了就是一个定时器,但是它有一个非常重要的功能就是复位系统。在A20里,看门狗的操作非常简单,只有两个寄存器,不需要操作时钟相关的东西,系统起来后可以直接使用,它的最大定时时间为16秒。

一、目的

学习使用A20的看门狗,实现软件复位系统。

二、源代码说明

start.S文件。首先禁止CPU的IRQ和FIQ,设置为管理模式,需要注意的是,这里设置异常向量表的起始地址为start.S文件里的第一指令的地址,学过ARM的都知道,ARM的异常向量表可以设置在低地址(0地址),也可以设置在高地址(0xffff0000地址,通常在使能了MMU后使用),但是目前Cortex-A7体系结构已经可以指定异常向量表的地址,这样就省去了搬移的操作。然后设置堆栈指针,最后调用C语言的main函数。

 /*
* (C) Copyright conan liang <lknlfy@163.com>
*
*/ /* global entry point */
.global _start
_start:
b reset reset:
/* disable IRQ & FIQ, set the cpu to SVC32 mode */
mrs r0, cpsr
and r1, r0, #0x1f
teq r1, #0x1a
bicne r0, r0, #0x1f
orrne r0, r0, #0x13
orr r0, r0, #0xc0
msr cpsr, r0 /* set exception vector table */
ldr r0, =_start
mcr p15, , r0, c12, c0, /* setup stack, so we can call C code */
ldr sp, =( * ) /* jump to main function */
bl main
loop:
b loop

main.c文件。首先初始化看门狗,设置它在3秒后执行系统复位操作(如果参数值小于0则表示关闭看门狗),然后初始化LED所在IO管脚,设置为输出功能,并且输出低电平,即一开始两个LED是熄灭的,接着是一段延时,目的是可以看到两个LED闪烁,如果观察现象时看到两个LED闪烁,那也说明了看门狗正常工作了。

 #include "io.h"
#include "watchdog.h" #define SUNXI_PIO_BASE (0x01C20800)
#define PH_CFG2 (SUNXI_PIO_BASE + 0x104)
#define PH_DAT (SUNXI_PIO_BASE + 0x10C) /* set two LEDs on */
static void set_led_on(void)
{
unsigned int tmp; /* PH20 and PH21 output 1 */
tmp = readl(PH_DAT);
tmp |= (0x1 << );
tmp |= (0x1 << );
writel(tmp, PH_DAT);
} /* init two PIOs */
static void led_init(void)
{
unsigned int tmp; /* configure PH20 and PH21 output */
tmp = readl(PH_CFG2);
tmp &= ~(0x7 << );
tmp &= ~(0x7 << );
tmp |= (0x1 << );
tmp |= (0x1 << );
writel(tmp, PH_CFG2);
/* PH20 and PH21 output 0 */
tmp = readl(PH_DAT);
tmp &= ~(0x1 << );
tmp &= ~(0x1 << );
writel(tmp, PH_DAT);
} int main(void)
{
int i; /* let watchdog reset the system after 3 second */
sunxi_watchdog_init();
/* init LEDs */
led_init();
/* some delay, so we can see two LEDs off a while */
for (i = ; i < ; i++);
/* set two LEDs on */
set_led_on();
while (); return ;
}

watchdog.c文件。看门狗的驱动程序,非常简单。

 #include "watchdog.h"
#include "io.h" /* watchdog interval value */
static unsigned int watchdog_intv_val [] = {
0b0000, // 0.5s
0b0001, // 1s
0b0010, // 2s
0b0011, // 3s
0b0100, // 4s
0b0101, // 5s
0b0110, // 6s
0b0111, // 8s
0b0111, // 8s
0b1000, // 10s
0b1000, // 10s
0b1001, // 12s
0b1001, // 12s
0b1010, // 14s
0b1010, // 14s
0b1011, // 16s
0b1011 // 16s
}; /* reg bits */
#define WDOG_OFF (-1)
#define WDOG_RST_EN (1)
#define WDOG_EN (0)
#define WDOG_RSTART (0) static void sunxi_watchdog_set(int time)
{
if (time >= ) {
/* should not larger than 16 seconds */
if(time > )
time = ;
writel((watchdog_intv_val[time] << ) | ( << WDOG_RST_EN) | ( << WDOG_EN), SUNXI_WDOG_MODE_REG);
} else {
/* disable watchdog */
writel(, SUNXI_WDOG_MODE_REG);
}
/* restart watchdog */
writel( << WDOG_RSTART, SUNXI_WDOG_CTRL_REG);
} int sunxi_watchdog_init(int val)
{
sunxi_watchdog_set(val); return ;
}

三、验证

使用arm-linux-gnueabihf工具编译后生成watchdog.b文件,再使用mksunxiboot工具在watchdog.b文件前面加上一个头部,最终生成watchdog.bin文件,使用以下命令将watchdog.bin文件烧写到TF中:

#sudo dd if=./watchdog.bin of=/dev/sdb bs=1024 seek=8

将TF卡插入Cubieboard2,上电即可看到两个LED出现短时的周期闪烁,效果不好用图片展示,因此就不上图了。

上一篇:ESP32-任务看门狗笔记


下一篇:iOS- Exception Type: 00000020:什么是看门狗机制(转)