-
Notifications
You must be signed in to change notification settings - Fork 1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Update MRAA timer functions #950
Conversation
TMRh20
commented
Mar 2, 2024
- Found MRAA timer functions not working properly
- Copy timing functions from SPIDEV
- Found MRAA timer functions not working properly - Copy timing functions from SPIDEV
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Verified working on my RPi4.
Its surprising that this is the solution to this. I noticed it last year, but wasn't sure what the problem actually was.
Should we also revert the timing code for SPIDEV driver? I'm curious if the char-dev API would still need the delayMicrosecond(1)
in ce()
.
Not sure what you mean. This is a straight copy of the SPIDEV timing code. |
Oops. I thought SPIDEV was using only chrono lib. |
I just tried only chrono approach, and the scanner example doesn't need the the patch that I tested with char-dev-irq branch
diff --git a/RF24.cpp b/RF24.cpp
index 13db347..fd41e17 100644
--- a/RF24.cpp
+++ b/RF24.cpp
@@ -108,7 +108,7 @@ void RF24::ce(bool level)
#endif
digitalWrite(ce_pin, level);
#ifdef RF24_LINUX
- delayMicroseconds(1); // SPIDEV driver's GPIO needs a delay here (for some reason)
+ // delayMicroseconds(1); // SPIDEV driver's GPIO needs a delay here (for some reason)
#endif
#ifndef RF24_LINUX
}
diff --git a/utility/SPIDEV/compatibility.cpp b/utility/SPIDEV/compatibility.cpp
index 6b96f65..8593fbd 100644
--- a/utility/SPIDEV/compatibility.cpp
+++ b/utility/SPIDEV/compatibility.cpp
@@ -1,10 +1,8 @@
#include "compatibility.h"
-
-long long mtime, seconds, useconds;
-//static struct timeval start, end;
-//struct timespec start, end;
-#include <time.h>
#include <chrono>
+//static struct timeval start, end;
+//static long mtime, seconds, useconds;
+
/**********************************************************************/
/**
* This function is added in order to simulate arduino delay() function
@@ -12,35 +10,33 @@ long long mtime, seconds, useconds;
*/
void __msleep(int milisec)
{
- struct timespec req; // = {0};
- req.tv_sec = (time_t)milisec / 1000;
- req.tv_nsec = (milisec % 1000) * 1000000L;
- //nanosleep(&req, (struct timespec *)NULL);
- clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
+ struct timespec req = {0};
+ req.tv_sec = 0;
+ req.tv_nsec = milisec * 1000000L;
+ nanosleep(&req, (struct timespec*)NULL);
+ //usleep(milisec*1000);
}
-void __usleep(int microsec)
+void __usleep(int milisec)
{
- struct timespec req; // = {0};
- req.tv_sec = (time_t)microsec / 1000000;
- req.tv_nsec = (microsec / 1000000) * 1000;
- //nanosleep(&req, (struct timespec *)NULL);
- clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
+ struct timespec req = {0};
+ req.tv_sec = 0;
+ req.tv_nsec = milisec * 1000L;
+ nanosleep(&req, (struct timespec*)NULL);
+ //usleep(milisec);
}
+auto start = std::chrono::steady_clock::now();
+
/**
* This function is added in order to simulate arduino millis() function
*/
-
-bool timerStarted = false;
-
void __start_timer()
{
+ //gettimeofday(&start, NULL);
}
-auto start = std::chrono::steady_clock::now();
-
-uint32_t __millis()
+long __millis()
{
auto end = std::chrono::steady_clock::now();
diff --git a/utility/SPIDEV/compatibility.h b/utility/SPIDEV/compatibility.h
index 7015323..ffc6953 100644
--- a/utility/SPIDEV/compatibility.h
+++ b/utility/SPIDEV/compatibility.h
@@ -3,17 +3,15 @@
* @author purinda
*
* Created on 24 June 2012, 3:08 PM
- * patch for safer monotonic clock & millis() correction for 64bit LDV 2018
*/
-#ifndef RF24_UTILITY_SPIDEV_COMPATIBLITY_H_
-#define RF24_UTILITY_SPIDEV_COMPATIBLITY_H_
+#ifndef RF24_UTILITY_MRAA_COMPATIBLITY_H_
+#define RF24_UTILITY_MRAA_COMPATIBLITY_H_
#ifdef __cplusplus
extern "C" {
#endif
-#include <stdint.h> // for uintXX_t types
#include <stddef.h>
#include <time.h>
#include <sys/time.h>
@@ -24,10 +22,10 @@ void __usleep(int milisec);
void __start_timer();
-uint32_t __millis();
+long __millis();
#ifdef __cplusplus
}
#endif
-#endif // RF24_UTILITY_SPIDEV_COMPATIBLITY_H_
+#endif // RF24_UTILITY_MRAA_COMPATIBLITY_H_ |
Did you test it fully, because that code didn't work properly before? That's why it was changed, the delays don't work properly. |
I just ran the scanner example, so no I didn't fully vet it. I'm currently looking into the difference between |
from hosted manpage:
|
Holy crap! It was a math error. This works without extra delay in @@ -23,7 +23,7 @@ void __usleep(int microsec)
{
struct timespec req; // = {0};
req.tv_sec = (time_t)microsec / 1000000;
- req.tv_nsec = (microsec / 1000000) * 1000;
+ req.tv_nsec = (microsec % 1000000) * 1000;
//nanosleep(&req, (struct timespec *)NULL);
clock_nanosleep(CLOCK_REALTIME, 0, &req, NULL);
}
But I might need to sleep on it and check my math when I'm more refreshed. |