#include <stdio.h>
#include <stdlib.h>
struct rtc_tm {
unsigned char secs;
unsigned char mins;
unsigned char hours;
unsigned char mday;
unsigned char mon;
unsigned char year; /* xx 2 digits :) */
unsigned char wday;
unsigned char vl;
};
struct rtc_alr {
unsigned char mins;
unsigned char hours;
unsigned char wday;
};
#define RTC_GETDATETIME 0
#define RTC_SETTIME 1
#define RTC_SETDATETIME 2
#define RTC_GETCTRL 3
#define RTC_SETCTRL 4
#define RTC_GET_ALR_A 5
#define RTC_SET_ALR_A 6
#define RTC_GET_ALR_B 7
#define RTC_SET_ALR_B 8
static int my_atoi(const char *name)
{
int val = 0;
for (;; name++) {
switch (*name) {
case '0'...'9':
val = 10*val+(*name-'0');
break;
default:
return val;
}
}
}
int main(int argc, char* argv[])
{
int fd;
struct rtc_tm tm;
unsigned int ctrl;
struct rtc_alr alr;
unsigned int buf[2];
//unsigned char ctrl[16];
//int i;
fd = open("/dev/misc/rtc5372",2);
if(fd < 0)
{
printf("err: open /dev/misc/rtc5372 failed\n");
return -1;
}
memset(&tm,0,sizeof(struct rtc_tm));
switch(argv[1][0])
{
case '0':
ioctl(fd,RTC_GETDATETIME,&tm);
printf("tm.secs = %d\n",tm.secs);
printf("tm.mins = %d\n",tm.mins);
printf("tm.hours = %d\n",tm.hours);
printf("tm.mday = %d\n",tm.mday);
printf("tm.mon = %d\n",tm.mon);
printf("tm.year = %d\n",tm.year);
printf("tm.wday = %d\n",tm.wday);
printf("tm.vl = %d\n",tm.vl);
break;
case '1':
if(argc < 6)
{
printf("[usage] rtc 1 secs mins hours wday\n");
break;
}
tm.secs = my_atoi(&argv[2][0]);
tm.mins = my_atoi(&argv[3][0]);
tm.hours = my_atoi(&argv[4][0]);
tm.wday = my_atoi(&argv[5][0]);
ioctl(fd,RTC_SETTIME,&tm);
break;
case '2':
if(argc < 9)
{
printf("[usage] rtc 2 secs mins hours wday mday mon year\n");
break;
}
tm.secs = my_atoi(&argv[2][0]);
tm.mins = my_atoi(&argv[3][0]);
tm.hours = my_atoi(&argv[4][0]);
tm.wday = my_atoi(&argv[5][0]);
tm.mday = my_atoi(&argv[6][0]);
tm.mon = my_atoi(&argv[7][0]);
tm.year = my_atoi(&argv[8][0]);
tm.vl = 0;
ioctl(fd,RTC_SETDATETIME,&tm);
break;
case '3':
ioctl(fd,RTC_GETCTRL,&ctrl);
printf("ctrl = %x\n",ctrl);
break;
case '4':
if(argc < 4)
{
printf("[usage] rtc 4 ctrl0 ctrl1\n");
break;
}
buf[0] = my_atoi(&argv[2][0]);
buf[1] = my_atoi(&argv[3][0]);
ctrl = buf[0] & 0xff;
ctrl |= (buf[1]<<8) & 0xff00;
ioctl(fd,RTC_SETCTRL,&ctrl);
break;
case '5':
ioctl(fd,RTC_GET_ALR_A,&alr);
printf("min=%d hours=%d week=%d\n",alr.mins,alr.hours,alr.wday);
break;
case '6':
if(argc < 5)
{
printf("[usage] rtc 6 min hours weekday\n");
break;
}
alr.mins = my_atoi(&argv[2][0]);
alr.hours = my_atoi(&argv[3][0]);
alr.wday = my_atoi(&argv[4][0]);
ioctl(fd,RTC_SET_ALR_A,&alr);
break;
case '7':
ioctl(fd,RTC_GET_ALR_B,&alr);
printf("min=%d hours=%d week=%d\n",alr.mins,alr.hours,alr.wday);
break;
case '8':
if(argc < 5)
{
printf("[usage] rtc 8 min hours weekday\n");
break;
}
alr.mins = my_atoi(&argv[2][0]);
alr.hours = my_atoi(&argv[3][0]);
alr.wday = my_atoi(&argv[4][0]);
ioctl(fd,RTC_SET_ALR_B,&alr);
break;
}
close(fd);
}