Working CPU-Timer

As long as sleep time in wallclock-example is short enough, we're
scheduled by the etimer -- since etimer callback is missing (no timer
interrupt yet) this doesn't work when time gets longer.
This commit is contained in:
Ralf Schlatterbeck 2016-04-15 20:06:28 +02:00
parent 08fb461f43
commit c043a00bb4
5 changed files with 37 additions and 88 deletions

View file

@ -68,7 +68,7 @@
#include <stdio.h> // FIXME
static volatile clock_time_t count;
volatile unsigned long seconds;
unsigned long offset;
long sleepseconds;
/*---------------------------------------------------------------------------*/
@ -84,69 +84,28 @@ clock_init(void)
* Return the tick counter. We use the full 64bit counter which makes
* computation of seconds etc. easier later.
*/
#define MAX_MEASURE 40
static clock_time_t measure [MAX_MEASURE];
static volatile int counter = 0;
void print_clocks (void)
{
static int done = 0;
int i;
clock_time_t big = 0x1234567890098765LL;
if (done || counter < MAX_MEASURE) {
return;
}
for (i=0; i<counter; i++) {
clock_time_t tmp = measure [i];
uint32_t *p = (uint32_t *)(measure + i);
printf (">%016llx< ", tmp);
printf (">>%08lx%08lx<< ", (uint32_t)(tmp>>32), (uint32_t)tmp);
printf (">>>%08lx%08lx<<<\n", p [1], p [0]); /* little endian */
}
printf ("counter: %d\n", counter);
printf ("Sizes: %u %u\n", sizeof (uint32_t), sizeof (clock_time_t));
printf ("Test: %016llx\n", big);
printf ("Test: %08lx%08lx\n", (uint32_t)(big>>32), (uint32_t)big);
if (counter == MAX_MEASURE) {
done = 1;
}
}
clock_time_t
clock_time(void)
{
clock_time_t tmp;
int i = counter;
asm ("1: rdcycleh t1\n"
"rdcycle t0\n"
"rdcycleh t2\n"
"bne t1,t2,1b\n"
"sw t0,0(%1)\n"
"sw t0,4(%1)\n"
: "=r" (tmp)
: "r" (&tmp)
: "t0", "t1", "t2"
uint32_t low, high;
asm ("1: rdcycleh %1\n"
"rdcycle %0\n"
"rdcycleh t0\n"
"bne %1,t0,1b\n"
: "=r" (low), "=r" (high)
:
: "t0"
);
if (i < MAX_MEASURE) {
measure [i++] = tmp;
counter = i;
}
return tmp;
return ((clock_time_t)high) << 32 | low;
}
/*---------------------------------------------------------------------------*/
/**
* Return seconds, default is time since startup.
* The comparison avoids the need to disable clock interrupts for an atomic
* read of the four-byte variable.
*/
unsigned long
clock_seconds(void)
{
unsigned long tmp;
do {
tmp = seconds;
} while(tmp != seconds);
return tmp;
return (unsigned long)(clock_time () / CLOCK_CONF_SECOND) + offset;
}
/*---------------------------------------------------------------------------*/
/**
@ -155,7 +114,7 @@ clock_seconds(void)
void
clock_set_seconds(unsigned long sec)
{
seconds = sec;
offset = sec - (unsigned long)(clock_time () / CLOCK_CONF_SECOND);
}
/*---------------------------------------------------------------------------*/
/*
@ -172,35 +131,7 @@ clock_wait(clock_time_t t)
void
clock_delay_usec(uint16_t dt)
{
}
/*---------------------------------------------------------------------------*/
/**
* Delay up to 65535 milliseconds.
* \param howlong How many milliseconds to delay.
*
* Neither interrupts nor the watchdog timer is disabled over the delay.
* Platforms are not required to implement this call.
* \note This will break for CPUs clocked above 260 MHz.
*/
void
clock_delay_msec(uint16_t howlong)
{
#if F_CPU>=16000000
while(howlong--) clock_delay_usec(1000);
#elif F_CPU>=8000000
uint16_t i=996;
while(howlong--) {clock_delay_usec(i);i=999;}
#elif F_CPU>=4000000
uint16_t i=992;
while(howlong--) {clock_delay_usec(i);i=999;}
#elif F_CPU>=2000000
uint16_t i=989;
while(howlong--) {clock_delay_usec(i);i=999;}
#else
uint16_t i=983;
while(howlong--) {clock_delay_usec(i);i=999;}
#endif
clock_wait (CLOCK_SECOND * dt / 1000000ULL);
}
/*---------------------------------------------------------------------------*/
/**

View file

@ -6,3 +6,5 @@ icosoc.v
icosoc.ys
wallclock.co
wallclock.pico-rv32-icoboard
icosoc.c
icosoc.h

View file

@ -72,7 +72,11 @@ PROCESS (wallclock, "Wallclock Example Server");
AUTOSTART_PROCESSES(&wallclock);
#define LOOP_INTERVAL (30 * CLOCK_SECOND)
//#define LOOP_INTERVAL (30 * CLOCK_SECOND)
//#define LOOP_INTERVAL (3 * CLOCK_SECOND)
// one more than this will currently hang, probably due to
// non-implemented etimer callback.
#define LOOP_INTERVAL (25461)
/*
* Set led to on or off, we abuse the given pointer to simply carry the
@ -84,7 +88,15 @@ void led_set (void *onoff)
icosoc_leds_set (0xFF * status);
}
extern void print_clocks (void);
#if 0
/* long long printing not working */
void test_print (void)
{
clock_time_t big = 0x1234567890098765LL;
printf ("Test: %016llx\n", big);
printf ("Test: %08lx%08lx\n", (uint32_t)(big>>32), (uint32_t)big);
}
#endif
PROCESS_THREAD(wallclock, ev, data)
{
@ -125,10 +137,11 @@ PROCESS_THREAD(wallclock, ev, data)
* We need to call cron every 30 seconds or so (at least once a
* minute)
*/
printf ("Loop-interval: %ld\n", (uint32_t)LOOP_INTERVAL);
etimer_set (&loop_periodic_timer, LOOP_INTERVAL);
while (1) {
printf ("In while loop\n");
print_clocks ();
clock_time_t cl = clock_time ();
printf ("In while loop: %08lx%08lx\n", (uint32_t)(cl>>32), (uint32_t)cl);
PROCESS_WAIT_EVENT();
if (etimer_expired (&loop_periodic_timer)) {
//cron ();

View file

@ -7,7 +7,7 @@ CONTIKI_TARGET_SOURCEFILES += contiki-main.c params.c
CONTIKIPICORV32=$(CONTIKI)/cpu/pico-rv32
CONTIKIBOARD=.
CONTIKI_PLAT_DEFS = -DF_CPU=8000000UL -DAUTO_CRC_PADDING=2
CONTIKI_PLAT_DEFS = -DAUTO_CRC_PADDING=2
MCU=pico-rv32

View file

@ -47,9 +47,12 @@
#define PLATFORM_NAME "PicoRV32-icoboard"
#define PLATFORM_TYPE PICORV32
#ifndef F_CPU
#define F_CPU 8000000UL
#define F_CPU 2000000ULL
#endif
/* Clock ticks per second, our timer runs with cpu freq */
#define CLOCK_CONF_SECOND F_CPU
typedef uint64_t clock_time_t;
#define CLOCK_LT(a,b) ((a)<(b))