guowenxue
2020-08-21 02f4d9518378031c63df7a36c49d8b2eabdaab90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/********************************************************************************
 *      Copyright:  (C) 2012 CoherentPlus Sdn. Bhd.
 *                  All rights reserved.
 *
 *       Filename:  cp_time.h
 *    Description:  This head file 
 *
 *        Version:  1.0.0(02/23/2012~)
 *         Author:  Guo Wenxue <guowenxue@gmail.com>
 *      ChangeLog:  1, Release initial version on "02/23/2012 07:46:37 AM"
 *                 
 ********************************************************************************/
#ifndef __CP_TIME_H
#define __CP_TIME_H
 
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
 
#include <linux/rtc.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
 
typedef struct __DATE_TIME
{
    int iYear;
    int iMonth;
    int iDay;
    int iHour;
    int iMinute;
    int iSecond;
    int iDayOfWeek;
} DATE_TIME;
 
static inline void micro_second_sleep(unsigned long ms)
{
    struct timespec cSleep;
    unsigned long ulTmp;
    cSleep.tv_sec = ms / 1000;
    if (cSleep.tv_sec == 0)
    {
        ulTmp = ms * 10000;
        cSleep.tv_nsec = ulTmp * 100;
    }
    else
    {
        cSleep.tv_nsec = 0;
    } 
    
    nanosleep(&cSleep, 0);
}
 
/*UNIT: micro second*/
static inline unsigned long time_now()
{
    struct timeval now; 
    gettimeofday(&now, 0);
    return (now.tv_sec * 1000) + (now.tv_usec / 1000);
}
 
/*UNIT: micro second*/
static inline unsigned long time_elapsed(unsigned long start)
{
    unsigned long current = time_now(); 
    
    if (current < start)
    {
        return (0xFFFFFFFF - start) + current;
    }
 
    //printf("time_elapsed: %ld\n", current-start);
    return current - start;
}
               
static inline void get_current_time(DATE_TIME * date)
{
    time_t now = time(NULL);
    struct tm *tnow = localtime(&now); 
    
    memset(date, 0, sizeof(DATE_TIME)); 
    date->iYear = 1900 + tnow->tm_year;
    date->iMonth = 1 + tnow->tm_mon;
    date->iDay = tnow->tm_mday;
 
    date->iHour = tnow->tm_hour;
    date->iMinute = tnow->tm_min;
    date->iSecond = tnow->tm_sec; 
    date->iDayOfWeek = tnow->tm_wday; 
    return;
}
#define get_sys_time(date)   get_current_time(date)
 
static inline int get_rtc_time(DATE_TIME *date)
{
    int                 rv, fd = -1;
    struct rtc_time     rtc_tm;  
 
    memset(date, 0, sizeof(DATE_TIME));
 
    if ((fd=open("/dev/rtc0", O_RDONLY)) < 0)
        return -1;
 
    if((rv=ioctl(fd, RTC_RD_TIME, &rtc_tm)) < 0)
        return -2;
 
    date->iYear = 1900 + rtc_tm.tm_year;
    date->iMonth = 1 + rtc_tm.tm_mon;
    date->iDay = rtc_tm.tm_mday;
 
    date->iHour = rtc_tm.tm_hour;
    date->iMinute = rtc_tm.tm_min;
    date->iSecond = rtc_tm.tm_sec;
    date->iDayOfWeek = rtc_tm.tm_wday;
 
    close(fd);
 
    return 0;
}
 
#endif