Skip to content

Commit

Permalink
Update MRAA timer functions (#950)
Browse files Browse the repository at this point in the history
* Update MRAA timer functions

- Found MRAA timer functions not working properly
- Copy timing functions from SPIDEV
  • Loading branch information
TMRh20 authored Mar 2, 2024
1 parent efdc40a commit 6d0f1b1
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 18 deletions.
30 changes: 13 additions & 17 deletions utility/MRAA/compatibility.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include "compatibility.h"
#include <time.h>
#include <chrono>
//static struct timeval start, end;
//static long mtime, seconds, useconds;

/**********************************************************************/
/**
Expand All @@ -10,33 +9,30 @@
*/
void __msleep(int milisec)
{
struct timespec req = {0};
req.tv_sec = 0;
req.tv_nsec = milisec * 1000000L;
nanosleep(&req, (struct timespec*)NULL);
//usleep(milisec*1000);
struct timespec req; // = {0};
req.tv_sec = (time_t)milisec / 1000;
req.tv_nsec = (milisec % 1000) * 1000000L;
clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
}

void __usleep(int milisec)
void __usleep(int microsec)
{
struct timespec req = {0};
req.tv_sec = 0;
req.tv_nsec = milisec * 1000L;
nanosleep(&req, (struct timespec*)NULL);
//usleep(milisec);
struct timespec req; // = {0};
req.tv_sec = (time_t)microsec / 1000000;
req.tv_nsec = (microsec / 1000000) * 1000;
clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
}

auto start = std::chrono::steady_clock::now();

/**
* This function is added in order to simulate arduino millis() function
*/
void __start_timer()
{
//gettimeofday(&start, NULL);
}

long __millis()
auto start = std::chrono::steady_clock::now();

uint32_t __millis()
{
auto end = std::chrono::steady_clock::now();

Expand Down
3 changes: 2 additions & 1 deletion utility/MRAA/compatibility.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ extern "C" {
#include <stddef.h>
#include <time.h>
#include <sys/time.h>
#include <stdint.h>

void __msleep(int milisec);

void __usleep(int milisec);

void __start_timer();

long __millis();
uint32_t __millis();

#ifdef __cplusplus
}
Expand Down

0 comments on commit 6d0f1b1

Please sign in to comment.