如题。还有
struct timespec tp;
clock_gettime(CLOCK_REALTIME,&tp);
printf("current time : %x s %x ns\n",tp.tv_sec,tp.tv_nsec);
结果怎么会是“current time :0s,0ns”啊?
共4条
1/1 1 跳转至页
非常感谢~Sophia~
我找到的一段代码:
/* includes */
#include "vxWorks.h"
#include "time.h"
#include "intLib.h"
#include "C:\Tornado2.2\target\config\pcPentium4\pc.h" /* macro to check that <v> is between <l> and <h> inclusive range */
#define CHECK_RANGE(v,l,h) (((v)>=(l))&&((v)<=(h))) /* Macros to convert 2-digit BCD into binary and vice versa */
#define BIN2BCD(b) (((b)%10) | (((b)/10)<<4))
#define BCD2BIN(b) (((b)&0xf) + ((b)>>4)*10) /* RTC register access, macros should be defined for a particular BSP */
#if CPU_FAMILY==I80X86 #define RTC_REG_SET(reg,val) \
(sysOutByte(RTC_INDEX,(reg)),sysOutByte(RTC_DATA,(val)))
#define RTC_REG_GET(reg) (sysOutByte(RTC_INDEX,(reg)),sysInByte(RTC_DATA)) #else
#ifndef RTC_JMP
#define RTC_JMP 1
#endif #ifndef RTC_BASE
#define RTC_BASE 0x800000
#endif #define RTC_REG(o) ((volatile u_char *)(RTC_BASE+(o)*RTC_JMP))
#define RTC_REG_SET(reg,val) (*RTC_REG(reg) = (val))
#define RTC_REG_GET(reg) (*RTC_REG(reg))
#endif /******************************************************************************
*
* sysRtcGet - get the current date abd time from a Real Time Clock chip
*
* The values are returned in a ANSI time structure.
* During initialization phase, the POSIX clock of the system is
* set according to the Real Time Clock time and date, thus
* it is recommended to use the POSIX functions and avoid calling
* this function from the application software,
* to acheive similar results but with greater portability.
*
* NOTE: Interrupts are locked during reading of the values from the chip.
*
* RETURNS: OK or ERROR
*
* SEE ALSO
* clockLib(), ansiTime
*
*/ STATUS sysRtcGet
(
struct tm *tm
)
{
FAST int ipl ;
FAST count = 50; ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80)); tm->tm_hour = RTC_REG_GET( 0x04); /* get BCD regs as is */
tm->tm_min = RTC_REG_GET( 0x02); /* while ints are off, */
tm->tm_sec = RTC_REG_GET( 0x00); /* and decode later */
tm->tm_mon = RTC_REG_GET( 0x08);
tm->tm_mday = RTC_REG_GET( 0x07);
tm->tm_year = RTC_REG_GET( 0x09);
tm->tm_wday = RTC_REG_GET( 0x06); intUnlock(ipl);
/* corrections - all registers are BCD, we need them in binary */
tm->tm_hour = BCD2BIN( tm->tm_hour );
tm->tm_min = BCD2BIN( tm->tm_min );
tm->tm_sec = BCD2BIN( tm->tm_sec );
tm->tm_mon = BCD2BIN( tm->tm_mon );
tm->tm_mday = BCD2BIN( tm->tm_mday );
tm->tm_year = BCD2BIN( tm->tm_year );
tm->tm_wday = BCD2BIN( tm->tm_wday ); /* corrections - some fields range is defined differently */
tm->tm_mon -- ; /* chip does 1-12, POSIX needs 0-11 */
tm->tm_wday -- ; /* chip does 1-7, POSIX needs 0-6 */ /* corrections - handle year after y2k */
if( tm->tm_year < 80 )
tm->tm_year += 100 ; /* These fields are unknown, filled with 0 */
tm->tm_yday = 0; /* days since January 1 - [0, 365] */
tm->tm_isdst= 0; /* Daylight Saving Time flag */ return OK ;
} /******************************************************************************
*
* sysRtcSet - Set the time and date into the RTC chip
*
* NOTE
* Setting the time is done with interrupts locked, but it is expected
* to be called rarely.
*
* RETURNS: OK or ERROR if values are out of range.
*/ STATUS sysRtcSet
(
const struct tm *timedate
)
{
struct tm t1 = * timedate ;
FAST struct tm *tm = &t1 ; /* make a local copy of the argument */
FAST count = 50;
FAST int ipl ; /* Check value ranges */ if( !CHECK_RANGE( tm->tm_sec, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_min, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_hour, 0, 23)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mday, 1, 31)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mon , 0, 11)) return ERROR ; /* correction - for y2k */
if( tm->tm_year > 99 )
tm->tm_year -= 100 ; if( !CHECK_RANGE( tm->tm_year, 0, 99)) return ERROR ; /* corrections - convert to BSD and add offset where needed. */
tm->tm_hour = BIN2BCD( tm->tm_hour );
tm->tm_min = BIN2BCD( tm->tm_min );
tm->tm_sec = BIN2BCD( tm->tm_sec );
tm->tm_mon = BIN2BCD( tm->tm_mon+1 );
tm->tm_mday = BIN2BCD( tm->tm_mday );
tm->tm_year = BIN2BCD( tm->tm_year );
tm->tm_wday = BIN2BCD( tm->tm_wday+1 ); ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80));
RTC_REG_SET( 0x04, tm->tm_hour );
RTC_REG_SET( 0x02, tm->tm_min );
RTC_REG_SET( 0x00, tm->tm_sec );
RTC_REG_SET( 0x08, tm->tm_mon );
RTC_REG_SET( 0x07, tm->tm_mday);
RTC_REG_SET( 0x09, tm->tm_year);
RTC_REG_SET( 0x06, tm->tm_wday); intUnlock(ipl); return OK ;
} /******************************************************************************
*
* sysRtcInit - initialize the RTC chip
*
* This function should called from sysHwInit2(). With this particular
* device, there is nothing we need to do here.
*
*/
STATUS sysRtcInit ( void)
{
/* turn the oscilator on, just in case */
RTC_REG_SET( 0x0a, 0x20 );
RTC_REG_SET( 0x0b, 0x02 ); /* set 24-hr & BCD modes */
return OK ;
} /******************************************************************************
*
* sysRtcShutdown - put the RTC chip in to sleep mode
*
* The sleep mode is designed to save on battery life during inactive
* storage of the equipment. During this time the date & time do not
* progress.
*
*/ void sysRtcShutdown(void)
{
RTC_REG_SET( 0x0a, 0x00 );
} /* End Of File */
#include "vxWorks.h"
#include "time.h"
#include "intLib.h"
#include "C:\Tornado2.2\target\config\pcPentium4\pc.h" /* macro to check that <v> is between <l> and <h> inclusive range */
#define CHECK_RANGE(v,l,h) (((v)>=(l))&&((v)<=(h))) /* Macros to convert 2-digit BCD into binary and vice versa */
#define BIN2BCD(b) (((b)%10) | (((b)/10)<<4))
#define BCD2BIN(b) (((b)&0xf) + ((b)>>4)*10) /* RTC register access, macros should be defined for a particular BSP */
#if CPU_FAMILY==I80X86 #define RTC_REG_SET(reg,val) \
(sysOutByte(RTC_INDEX,(reg)),sysOutByte(RTC_DATA,(val)))
#define RTC_REG_GET(reg) (sysOutByte(RTC_INDEX,(reg)),sysInByte(RTC_DATA)) #else
#ifndef RTC_JMP
#define RTC_JMP 1
#endif #ifndef RTC_BASE
#define RTC_BASE 0x800000
#endif #define RTC_REG(o) ((volatile u_char *)(RTC_BASE+(o)*RTC_JMP))
#define RTC_REG_SET(reg,val) (*RTC_REG(reg) = (val))
#define RTC_REG_GET(reg) (*RTC_REG(reg))
#endif /******************************************************************************
*
* sysRtcGet - get the current date abd time from a Real Time Clock chip
*
* The values are returned in a ANSI time structure.
* During initialization phase, the POSIX clock of the system is
* set according to the Real Time Clock time and date, thus
* it is recommended to use the POSIX functions and avoid calling
* this function from the application software,
* to acheive similar results but with greater portability.
*
* NOTE: Interrupts are locked during reading of the values from the chip.
*
* RETURNS: OK or ERROR
*
* SEE ALSO
* clockLib(), ansiTime
*
*/ STATUS sysRtcGet
(
struct tm *tm
)
{
FAST int ipl ;
FAST count = 50; ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80)); tm->tm_hour = RTC_REG_GET( 0x04); /* get BCD regs as is */
tm->tm_min = RTC_REG_GET( 0x02); /* while ints are off, */
tm->tm_sec = RTC_REG_GET( 0x00); /* and decode later */
tm->tm_mon = RTC_REG_GET( 0x08);
tm->tm_mday = RTC_REG_GET( 0x07);
tm->tm_year = RTC_REG_GET( 0x09);
tm->tm_wday = RTC_REG_GET( 0x06); intUnlock(ipl);
/* corrections - all registers are BCD, we need them in binary */
tm->tm_hour = BCD2BIN( tm->tm_hour );
tm->tm_min = BCD2BIN( tm->tm_min );
tm->tm_sec = BCD2BIN( tm->tm_sec );
tm->tm_mon = BCD2BIN( tm->tm_mon );
tm->tm_mday = BCD2BIN( tm->tm_mday );
tm->tm_year = BCD2BIN( tm->tm_year );
tm->tm_wday = BCD2BIN( tm->tm_wday ); /* corrections - some fields range is defined differently */
tm->tm_mon -- ; /* chip does 1-12, POSIX needs 0-11 */
tm->tm_wday -- ; /* chip does 1-7, POSIX needs 0-6 */ /* corrections - handle year after y2k */
if( tm->tm_year < 80 )
tm->tm_year += 100 ; /* These fields are unknown, filled with 0 */
tm->tm_yday = 0; /* days since January 1 - [0, 365] */
tm->tm_isdst= 0; /* Daylight Saving Time flag */ return OK ;
} /******************************************************************************
*
* sysRtcSet - Set the time and date into the RTC chip
*
* NOTE
* Setting the time is done with interrupts locked, but it is expected
* to be called rarely.
*
* RETURNS: OK or ERROR if values are out of range.
*/ STATUS sysRtcSet
(
const struct tm *timedate
)
{
struct tm t1 = * timedate ;
FAST struct tm *tm = &t1 ; /* make a local copy of the argument */
FAST count = 50;
FAST int ipl ; /* Check value ranges */ if( !CHECK_RANGE( tm->tm_sec, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_min, 0, 59)) return ERROR ;
if( !CHECK_RANGE( tm->tm_hour, 0, 23)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mday, 1, 31)) return ERROR ;
if( !CHECK_RANGE( tm->tm_mon , 0, 11)) return ERROR ; /* correction - for y2k */
if( tm->tm_year > 99 )
tm->tm_year -= 100 ; if( !CHECK_RANGE( tm->tm_year, 0, 99)) return ERROR ; /* corrections - convert to BSD and add offset where needed. */
tm->tm_hour = BIN2BCD( tm->tm_hour );
tm->tm_min = BIN2BCD( tm->tm_min );
tm->tm_sec = BIN2BCD( tm->tm_sec );
tm->tm_mon = BIN2BCD( tm->tm_mon+1 );
tm->tm_mday = BIN2BCD( tm->tm_mday );
tm->tm_year = BIN2BCD( tm->tm_year );
tm->tm_wday = BIN2BCD( tm->tm_wday+1 ); ipl = intLock(); /* wait until registers update is done, then we got 244 us
* to read the regs without loosing sanity
*/
while((count--) && (RTC_REG_GET(0x0a) & 0x80));
RTC_REG_SET( 0x04, tm->tm_hour );
RTC_REG_SET( 0x02, tm->tm_min );
RTC_REG_SET( 0x00, tm->tm_sec );
RTC_REG_SET( 0x08, tm->tm_mon );
RTC_REG_SET( 0x07, tm->tm_mday);
RTC_REG_SET( 0x09, tm->tm_year);
RTC_REG_SET( 0x06, tm->tm_wday); intUnlock(ipl); return OK ;
} /******************************************************************************
*
* sysRtcInit - initialize the RTC chip
*
* This function should called from sysHwInit2(). With this particular
* device, there is nothing we need to do here.
*
*/
STATUS sysRtcInit ( void)
{
/* turn the oscilator on, just in case */
RTC_REG_SET( 0x0a, 0x20 );
RTC_REG_SET( 0x0b, 0x02 ); /* set 24-hr & BCD modes */
return OK ;
} /******************************************************************************
*
* sysRtcShutdown - put the RTC chip in to sleep mode
*
* The sleep mode is designed to save on battery life during inactive
* storage of the equipment. During this time the date & time do not
* progress.
*
*/ void sysRtcShutdown(void)
{
RTC_REG_SET( 0x0a, 0x00 );
} /* End Of File */
共4条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 | |
【FRDM-MCXN947评测】核间通信MUTEX被打赏50分 |