Equivalent to Arduino millis()

Viewed 4824

I am currently working on the integration of a "shunt" type sensor on an electronic board. My choice was on a Linear (LTC2947), unfortunately it only has an Arduino driver. I have to translate everything in C under Linux to be compatible with my microprocessor (APQ8009 ARM Cortex-A7). I have a small question about one of the functions:

int16_t LTC2947_wake_up() //Wake up LTC2947 from shutdown mode and measure the wakeup time
{
   byte data[1];
   unsigned long wakeupStart = millis(), wakeupTime;
   LTC2947_WR_BYTE(LTC2947_REG_OPCTL, 0);
   do
   {
      delay(1);
      LTC2947_RD_BYTE(LTC2947_REG_OPCTL, data); 
      wakeupTime = millis() - wakeupStart;
      if (data[0] == 0) //! check if we are in idle mode
      {
         return wakeupTime;
      }
     if (wakeupTime > 200)
     {
  //! failed to wake up due to timeout, return -1
        return -1;
     }
   }
   while (true);
}

After finding usleep() as equivalent for delay(), I can not find it for millis() in C. Can you help me translate this function please?

2 Answers

I hope this be useful. Works for me under Lubuntu 20.04 LTS.

#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>

struct timeval __millis_start;

void init_millis() {
    gettimeofday(&__millis_start, NULL);
};

unsigned long int millis() {
    long mtime, seconds, useconds; 
    struct timeval end;
    gettimeofday(&end, NULL);
    seconds  = end.tv_sec  - __millis_start.tv_sec;
    useconds = end.tv_usec - __millis_start.tv_usec;

    mtime = ((seconds) * 1000 + useconds/1000.0) + 0.5;
    return mtime;
};

int main()
{
    init_millis();
    printf("Elapsed time: %ld milliseconds\n", millis());

    return 0;
}
Related