代码仓库-linux常用函数汇集

/*********************************************************************
* Author          :     lile
* Modified        :     2020年4月23日星期三  16:56:13
* Email           :     [email protected]
* HomePage        :     lile777.blog.csdn.net
* CopyRight       :     该文章版权由lile所有。
*                       保留原文出处链接和本声明的前提下,可在非商业目的下任意传播和复制。
*                       对于商业目的下对本文的任何行为需经作者同意。
*********************************************************************/

0. 系统时间获取(毫秒/微妙)

// ref: DM8148 SDK

Uint32 OSA_getCurTimeInMsec(void)
{
  static int isInit = FALSE;
  static Uint32 initTime=0;
  struct timeval tv;

  if(isInit==FALSE)
  {
      isInit = TRUE;

      if (gettimeofday(&tv, NULL) < 0)
        return 0;

      initTime = (Uint32)(tv.tv_sec * 1000u + tv.tv_usec/1000u);
  }

  if (gettimeofday(&tv, NULL) < 0)
    return 0;

  return (Uint32)(tv.tv_sec * 1000u + tv.tv_usec/1000u)-initTime;
}

void   OSA_waitMsecs(Uint32 msecs)
{
  #if 1
  struct timespec delayTime, elaspedTime;

  delayTime.tv_sec  = msecs/1000;
  delayTime.tv_nsec = (msecs%1000)*1000000;

  nanosleep(&delayTime, &elaspedTime);
  #else
  usleep(msecs*1000);
  #endif
}


UInt32 Utils_getCurTimeInMsec()
{
    #if 1
    static UInt32 cpuKhz = 500*1000; // default
    static Bool isInit = FALSE;

    Types_Timestamp64 ts64;
    UInt64 curTs;

    if(!isInit)
    {
        /* do this only once */

        Types_FreqHz cpuHz;

        isInit = TRUE;

        Timestamp_getFreq(&cpuHz);

        cpuKhz = cpuHz.lo / 1000; /* convert to Khz */

        Vps_printf(" \n");
        Vps_printf(" *** UTILS: CPU KHz = %d Khz ***\n", cpuKhz);
        Vps_printf(" \n");

    }

    Timestamp_get64(&ts64);

    curTs = ((UInt64) ts64.hi << 32) | ts64.lo;

    return (UInt32)(curTs/(UInt64)cpuKhz);
    #else
    return Clock_getTicks();
    #endif
}

UInt64 Utils_getCurTimeInUsec()
{
    static UInt32 cpuMhz = 500; // default
    static Bool isInit = FALSE;

    Types_Timestamp64 ts64;
    UInt64 curTs;

    if(!isInit)
    {
        /* do this only once */

        Types_FreqHz cpuHz;

        isInit = TRUE;

        Timestamp_getFreq(&cpuHz);

        cpuMhz = cpuHz.lo / (1000*1000); /* convert to Mhz */

        Vps_printf(" \n");
        Vps_printf(" *** UTILS: CPU MHz = %d Mhz ***\n", cpuMhz);
        Vps_printf(" \n");


    }

    Timestamp_get64(&ts64);

    curTs = ((UInt64) ts64.hi << 32) | ts64.lo;

    return (curTs/cpuMhz);

}

 

1. 进制转换

static char xtod(char c) {
  if (c>='0' && c<='9') return c-'0';
  if (c>='A' && c<='F') return c-'A'+10;
  if (c>='a' && c<='f') return c-'a'+10;
  return c=0;        // not Hex digit
}

static int HextoDec(char *hex, int l)
{
  if (*hex==0)
    return(l);

  return HextoDec(hex+1, l*16+xtod(*hex)); // hex+1?
}

int xstrtoi(char *hex)      // hex string to integer
{
  return HextoDec(hex,0);
}

你可能感兴趣的:(C基础)