代碼倉庫-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);
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章