📄 dog.c
字号:
#include <stdio.h>#include <fcntl.h>#include <unistd.h>#include <asm-arm/arch-s3c2410/S3C2410.h>#include "directio.h"#define PCLK (50*1024*1024)static int fd = -1;int port_init(){ fd = open("/dev/directio",O_NONBLOCK,0); if(fd<0) { printf("Can't open directio device\n"); return -1; } return 0;}unsigned char port_inb(unsigned int port){ struct port_data data; //端口结构的定义 data.cmd = CMD_PORT; data.startaddr = port; data.nums = 1; data.opts = PORT_BYTE; ioctl(fd, IOCTL_GET_MSG, &data); //调用ioctl()访问内核 return data.value;}unsigned short port_inw(unsigned int port){ struct port_data data; data.cmd = CMD_PORT; data.startaddr = port; data.nums = 2; data.opts = PORT_HWORD; ioctl(fd, IOCTL_GET_MSG, &data); return data.value;}unsigned int port_inl(unsigned int port){ struct port_data data; data.cmd = CMD_PORT; data.startaddr = port; data.nums = 4; data.opts = PORT_WORD; ioctl(fd, IOCTL_GET_MSG, &data); return data.value;}int port_outb(unsigned int port, unsigned char value){ struct port_data data; data.cmd = CMD_PORT; data.startaddr = port; data.nums = 1; data.opts = PORT_BYTE; data.value = value; ioctl(fd, IOCTL_SET_MSG, &data);}int port_outw(unsigned int port, unsigned short value){ struct port_data data; data.cmd = CMD_PORT; data.startaddr = port; data.nums = 2; data.opts = PORT_HWORD; data.value = value; ioctl(fd, IOCTL_SET_MSG, &data);}int port_outl(unsigned int port, unsigned int value){ struct port_data data; data.cmd = CMD_PORT; data.startaddr = port; data.nums = 4; data.opts = PORT_WORD; data.value = value; ioctl(fd, IOCTL_SET_MSG, &data);}void port_uninit(){ if(fd >= 0) close(fd);}int main(int argc, char **argv){ /* * 2410看门狗测试程序,定时器时钟周期的计算: * t_watchdog = 1 / (PCLK / (Prescaler value + 1 ) / Division_factor) * = 0.000001*Division_factor(秒) * 显然: WTCNT寄存器每减一个数相当于0.000001*Division_factor(秒) * 则:减到来则需要 WTCNT*0.000001*Division_factor秒 * WTCNT寄存器为零时将会产生一个复位信号 */ printf("WatchDog TimerTest!\n"); printf("t_watchdog = 1/(PCLK*1000000/(Prescaler value + 1)/Division_factor)\n"); if(port_init() != 0) //如果端口初始化不成功 { printf("port open error\n"); printf("Please follow me:\n"); printf(" #insmod directio\n"); printf(" #mknod /dev/directio c 199 0\n"); printf(" #./watchdog\n"); return -1; } port_outl(rWTCON, ((PCLK/1000000-1)<<8) | (3<<3) | (1)); //Prescaler=0x31(49),Clock division 128,Interrupt disable,Watch-dog timer enable printf(" PCLK = %dMHz\n", PCLK/1024/1024); printf(" Prescaler value = %d\n", (PCLK/1000000-1)); printf(" Clock division = %d\n", 128); printf("t_watchdog = %f\n", 1.0 / (PCLK / (((PCLK/1000000-1)) + 1) / (128))); port_outl(rWTCNT, 16896); //计数值大概相当于16896(或0x4200)*0.000001*128 = 2.16秒 printf("Please feed the dog every %f sec, if not System will restart.\n", (8448 * 2) * 1.0 / (PCLK / (((PCLK/1000000-1)) + 1) / (128))); port_outl(rWTCON, port_inl(rWTCON) | (1 << 5));//打开看门狗 //port_outl(0xf3000000, port_inl(rWTCON) | (1 << 5));//直接用虚拟地址也可以 //用ioremap() while(1) { printf("Feed the dog\n"); port_outl(rWTCNT,16896); //计数值大概相当于16896*0.000001*128 = 2.16秒 sleep(2); //2秒钟休眠 } port_uninit(); //移除端口}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -