Skip to content

Commit f8e7bb0

Browse files
committed
Add cycle counter driver and test app
1 parent e5c540b commit f8e7bb0

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

examples/tests/cycle_count/Makefile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Makefile for user application
2+
3+
# Specify this directory relative to the current application.
4+
TOCK_USERLAND_BASE_DIR = ../../..
5+
6+
# Which files to compile.
7+
C_SRCS := $(wildcard *.c)
8+
9+
# Include userland master makefile. Contains rules and flags for actually
10+
# building the application.
11+
include $(TOCK_USERLAND_BASE_DIR)/AppMakefile.mk

examples/tests/cycle_count/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Test `cycle_count`
2+
====================
3+
4+
This tests the cycle counter capsule.
5+
It will print the number of cycles required to toggle an LED.

examples/tests/cycle_count/main.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdio.h>
2+
3+
#include <cycle_counter.h>
4+
#include <led.h>
5+
#include <tock.h>
6+
7+
int main(void) {
8+
uint64_t cycles = 0;
9+
10+
int rc = cycle_counter_reset();
11+
if (rc == RETURNCODE_ENOSUPPORT) {
12+
printf("Cycle counter not available\n");
13+
} else if (rc == RETURNCODE_ERESERVE) {
14+
printf("Cycle counter claimed by another app\n");
15+
} else if (rc != 0) {
16+
printf("Cycle counter: other error: %d\n", rc);
17+
}
18+
19+
if (rc != 0) {
20+
return 0;
21+
}
22+
23+
cycle_counter_start();
24+
led_toggle(0);
25+
cycle_counter_stop();
26+
cycle_counter_read(&cycles);
27+
printf("cycles to toggle led first time: %d\n", cycles);
28+
29+
cycle_counter_reset();
30+
cycle_counter_start();
31+
led_toggle(0);
32+
cycle_counter_stop();
33+
cycle_counter_read(&cycles);
34+
printf("cycles to toggle led second time: %d\n", cycles);
35+
return 0;
36+
}

libtock/cycle_counter.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include "cycle_counter.h"
2+
3+
bool cycle_counter_exists(void) {
4+
return driver_exists(DRIVER_NUM_CYCLE_COUNTER);
5+
}
6+
7+
// Start the cycle counter
8+
int cycle_counter_start(void) {
9+
syscall_return_t rval = command(DRIVER_NUM_CYCLE_COUNTER, 1, 0, 0);
10+
return tock_command_return_novalue_to_returncode(rval);
11+
}
12+
13+
// Get the current cycle count
14+
int cycle_counter_read(uint64_t* count) {
15+
syscall_return_t rval = command(DRIVER_NUM_CYCLE_COUNTER, 2, 0, 0);
16+
return tock_command_return_u64_to_returncode(rval, (uint64_t*) count);
17+
}
18+
19+
// Reset the cycle counter
20+
int cycle_counter_reset(void) {
21+
syscall_return_t rval = command(DRIVER_NUM_CYCLE_COUNTER, 3, 0, 0);
22+
return tock_command_return_novalue_to_returncode(rval);
23+
}
24+
25+
// Stop the cycle counter
26+
int cycle_counter_stop(void) {
27+
syscall_return_t rval = command(DRIVER_NUM_CYCLE_COUNTER, 4, 0, 0);
28+
return tock_command_return_novalue_to_returncode(rval);
29+
}

libtock/cycle_counter.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#pragma once
2+
3+
#include "tock.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#define DRIVER_NUM_CYCLE_COUNTER 0x00090008
10+
11+
bool cycle_counter_exists(void);
12+
int cycle_counter_start(void);
13+
int cycle_counter_stop(void);
14+
int cycle_counter_reset(void);
15+
int cycle_counter_read(int *count);
16+
17+
#ifdef __cplusplus
18+
}
19+
#endif

0 commit comments

Comments
 (0)