summaryrefslogtreecommitdiff
path: root/test/timer_calib.c
blob: 361689f8bd67ae404fc1112f21dbb1fe6165a2e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/* Copyright 2012 The Chromium OS Authors. All rights reserved.
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Tasks for scheduling test.
 */

#include "common.h"
#include "console.h"
#include "task.h"
#include "timer.h"
#include "util.h"

uint32_t difftime(timestamp_t t0, timestamp_t t1)
{
	return (uint32_t)(t1.val-t0.val);
}

int timer_calib_task(void *data)
{
	timestamp_t t0, t1;
	unsigned d;

	while (1) {
		task_wait_event(-1);

		ccprintf("\n=== Timer calibration ===\n");

		t0 = get_time();
		t1 = get_time();
		ccprintf("- back-to-back get_time : %d us\n", difftime(t0, t1));

		/* Sleep for 5 seconds */
		ccprintf("- sleep 1s :\n  ");
		cflush();
		ccprintf("Go...");
		t0 = get_time();
		usleep(1000000);
		t1 = get_time();
		ccprintf("done. delay = %d us\n", difftime(t0, t1));

		/* try small usleep */
		ccprintf("- short sleep :\n");
		cflush();
		for (d = 128; d > 0; d = d / 2) {
			t0 = get_time();
			usleep(d);
			t1 = get_time();
			ccprintf("  %d us => %d us\n", d, difftime(t0, t1));
			cflush();
		}

		ccprintf("Done.\n");
	}

	return EC_SUCCESS;
}

void run_test(void)
{
	task_wake(TASK_ID_TESTTMR);
}