-
Notifications
You must be signed in to change notification settings - Fork 3k
NCS36510: Fix the sporadic semaphore timing issue #3779
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1f9e239
events: Added equeue platform timing tests
geky c0951c9
NCS36510: Fixed drift in ticker interrupt
geky 0949164
events: Added better handling of desynchronized timers in platform layer
geky 6e920fd
events: Increased test tolerance to +-5ms
geky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
#include "mbed_events.h" | ||
#include "mbed.h" | ||
#include "rtos.h" | ||
#include "greentea-client/test_env.h" | ||
#include "unity.h" | ||
#include "utest.h" | ||
#include <cstdlib> | ||
#include <cmath> | ||
|
||
using namespace utest::v1; | ||
|
||
|
||
// Test delay | ||
#ifndef TEST_EVENTS_TIMING_TIME | ||
#define TEST_EVENTS_TIMING_TIME 20000 | ||
#endif | ||
|
||
#ifndef TEST_EVENTS_TIMING_MEAN | ||
#define TEST_EVENTS_TIMING_MEAN 25 | ||
#endif | ||
|
||
#ifndef M_PI | ||
#define M_PI 3.14159265358979323846264338327950288 | ||
#endif | ||
|
||
// Random number generation to skew timing values | ||
float gauss(float mu, float sigma) { | ||
float x = (float)rand() / ((float)RAND_MAX+1); | ||
float y = (float)rand() / ((float)RAND_MAX+1); | ||
float x2pi = x*2.0*M_PI; | ||
float g2rad = sqrt(-2.0 * log(1.0-y)); | ||
float z = cos(x2pi) * g2rad; | ||
return mu + z*sigma; | ||
} | ||
|
||
float chisq(float sigma) { | ||
return pow(gauss(0, sqrt(sigma)), 2); | ||
} | ||
|
||
|
||
Timer timer; | ||
DigitalOut led(LED1); | ||
|
||
equeue_sema_t sema; | ||
|
||
// Timer timing test | ||
void timer_timing_test() { | ||
timer.reset(); | ||
timer.start(); | ||
int prev = timer.read_us(); | ||
|
||
while (prev < TEST_EVENTS_TIMING_TIME*1000) { | ||
int next = timer.read_us(); | ||
if (next < prev) { | ||
printf("backwards drift %d -> %d (%08x -> %08x)\r\n", | ||
prev, next, prev, next); | ||
} | ||
TEST_ASSERT(next >= prev); | ||
prev = next; | ||
} | ||
} | ||
|
||
// equeue tick timing test | ||
void tick_timing_test() { | ||
unsigned start = equeue_tick(); | ||
int prev = 0; | ||
|
||
while (prev < TEST_EVENTS_TIMING_TIME) { | ||
int next = equeue_tick() - start; | ||
if (next < prev) { | ||
printf("backwards drift %d -> %d (%08x -> %08x)\r\n", | ||
prev, next, prev, next); | ||
} | ||
TEST_ASSERT(next >= prev); | ||
prev = next; | ||
} | ||
} | ||
|
||
// equeue semaphore timing test | ||
void semaphore_timing_test() { | ||
srand(0); | ||
timer.reset(); | ||
timer.start(); | ||
|
||
int err = equeue_sema_create(&sema); | ||
TEST_ASSERT_EQUAL(0, err); | ||
|
||
while (timer.read_ms() < TEST_EVENTS_TIMING_TIME) { | ||
int delay = chisq(TEST_EVENTS_TIMING_MEAN); | ||
|
||
int start = timer.read_us(); | ||
equeue_sema_wait(&sema, delay); | ||
int taken = timer.read_us() - start; | ||
|
||
printf("delay %dms => error %dus\r\n", delay, abs(1000*delay - taken)); | ||
TEST_ASSERT_INT_WITHIN(5000, taken, delay * 1000); | ||
|
||
led = !led; | ||
} | ||
|
||
equeue_sema_destroy(&sema); | ||
} | ||
|
||
|
||
// Test setup | ||
utest::v1::status_t test_setup(const size_t number_of_cases) { | ||
GREENTEA_SETUP((number_of_cases+1)*TEST_EVENTS_TIMING_TIME, "default_auto"); | ||
return verbose_test_setup_handler(number_of_cases); | ||
} | ||
|
||
const Case cases[] = { | ||
Case("Testing accuracy of timer", timer_timing_test), | ||
Case("Testing accuracy of equeue tick", tick_timing_test), | ||
Case("Testing accuracy of equeue semaphore", semaphore_timing_test), | ||
}; | ||
|
||
Specification specification(test_setup, cases); | ||
|
||
int main() { | ||
return !Harness::run(specification); | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
license missing here
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.
Not a blocker, we should definitely add licenses to our tests (more files are without as I noticed).
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.
Ah sorry, not sure how I ended up with a lot of licenseless test files.