2017-01-05 09:55:38 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include "unity.h"
|
|
|
|
#include "rom/ets_sys.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2017-08-29 21:01:37 -04:00
|
|
|
#include "freertos/semphr.h"
|
|
|
|
#include "test_utils.h"
|
2017-01-05 09:55:38 -05:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
int delay_us;
|
|
|
|
int method;
|
2017-08-29 21:01:37 -04:00
|
|
|
int result;
|
|
|
|
SemaphoreHandle_t done;
|
2017-01-05 09:55:38 -05:00
|
|
|
} delay_test_arg_t;
|
|
|
|
|
|
|
|
static void test_delay_task(void* p)
|
|
|
|
{
|
2017-08-29 21:01:37 -04:00
|
|
|
delay_test_arg_t* arg = (delay_test_arg_t*) p;
|
|
|
|
vTaskDelay(1);
|
|
|
|
uint64_t start = ref_clock_get();
|
2017-01-05 09:55:38 -05:00
|
|
|
switch (arg->method) {
|
|
|
|
case 0:
|
|
|
|
ets_delay_us(arg->delay_us);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
vTaskDelay(arg->delay_us / portTICK_PERIOD_MS / 1000);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
TEST_FAIL();
|
|
|
|
}
|
2017-08-29 21:01:37 -04:00
|
|
|
uint64_t stop = ref_clock_get();
|
|
|
|
|
|
|
|
arg->result = (int) (stop - start);
|
|
|
|
xSemaphoreGive(arg->done);
|
2017-01-05 09:55:38 -05:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
}
|
|
|
|
|
2017-08-29 21:01:37 -04:00
|
|
|
TEST_CASE("ets_delay produces correct delay on both CPUs", "[delay]")
|
2017-01-05 09:55:38 -05:00
|
|
|
{
|
|
|
|
int delay_ms = 50;
|
2017-08-29 21:01:37 -04:00
|
|
|
const delay_test_arg_t args = {
|
|
|
|
.delay_us = delay_ms * 1000,
|
|
|
|
.method = 0,
|
|
|
|
.done = xSemaphoreCreateBinary()
|
|
|
|
};
|
|
|
|
ref_clock_init();
|
2017-01-05 09:55:38 -05:00
|
|
|
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 0);
|
2017-08-29 21:01:37 -04:00
|
|
|
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 2 / portTICK_PERIOD_MS) );
|
|
|
|
TEST_ASSERT_INT32_WITHIN(1000, args.delay_us, args.result);
|
|
|
|
|
2017-01-05 09:55:38 -05:00
|
|
|
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 1);
|
2017-08-29 21:01:37 -04:00
|
|
|
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 2 / portTICK_PERIOD_MS) );
|
|
|
|
TEST_ASSERT_INT32_WITHIN(1000, args.delay_us, args.result);
|
|
|
|
|
|
|
|
ref_clock_deinit();
|
|
|
|
vSemaphoreDelete(args.done);
|
2017-01-05 09:55:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE("vTaskDelay produces correct delay on both CPUs", "[delay]")
|
|
|
|
{
|
|
|
|
int delay_ms = 50;
|
2017-08-29 21:01:37 -04:00
|
|
|
const delay_test_arg_t args = {
|
|
|
|
.delay_us = delay_ms * 1000,
|
|
|
|
.method = 1,
|
|
|
|
.done = xSemaphoreCreateBinary()
|
|
|
|
};
|
|
|
|
ref_clock_init();
|
2017-01-05 09:55:38 -05:00
|
|
|
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 0);
|
2017-08-29 21:01:37 -04:00
|
|
|
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 2 / portTICK_PERIOD_MS) );
|
|
|
|
TEST_ASSERT_INT32_WITHIN(1000, args.delay_us, args.result);
|
|
|
|
|
2017-01-05 09:55:38 -05:00
|
|
|
xTaskCreatePinnedToCore(test_delay_task, "", 2048, (void*) &args, 3, NULL, 1);
|
2017-08-29 21:01:37 -04:00
|
|
|
TEST_ASSERT( xSemaphoreTake(args.done, delay_ms * 2 / portTICK_PERIOD_MS) );
|
|
|
|
TEST_ASSERT_INT32_WITHIN(1000, args.delay_us, args.result);
|
|
|
|
|
|
|
|
ref_clock_deinit();
|
|
|
|
vSemaphoreDelete(args.done);
|
2017-01-05 09:55:38 -05:00
|
|
|
}
|