Start implementing timer

This commit is contained in:
Ralf Schlatterbeck 2016-04-15 09:54:44 +02:00
parent c49d4a0b36
commit 08fb461f43
9 changed files with 120 additions and 629 deletions

View file

@ -29,17 +29,17 @@
* This file is part of the Contiki operating system.
*
*/
/**
/**
* \brief This module contains PicoRV32-specific code to implement
* the Contiki core clock functions.
*
* \author Ralf Schlatterbeck <rsc@runtux.com>.
*
*/
*/
/** \addtogroup picorv32
* @{
*/
/**
/**
* \defgroup picorv32clock PicoRV32 clock implementation
* @{
*/
@ -65,32 +65,73 @@
#include "sys/clock.h"
#include "sys/etimer.h"
#include <stdio.h> // FIXME
static volatile clock_time_t count;
volatile unsigned long seconds;
long sleepseconds;
/*---------------------------------------------------------------------------*/
/**
* Start the clock by enabling the timer comparison interrupts.
* No need to init
*/
void
void
clock_init(void)
{
//cli ();
//OCRSetup();
//sei ();
}
/*---------------------------------------------------------------------------*/
/**
* Return the tick counter. When 16 bit it typically wraps every 10 minutes.
* The comparison avoids the need to disable clock interrupts for an atomic
* read of the multi-byte variable.
* Return the tick counter. We use the full 64bit counter which makes
* computation of seconds etc. easier later.
*/
clock_time_t
#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)
{
static clock_time_t counter = 0;
return counter++;
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"
);
if (i < MAX_MEASURE) {
measure [i++] = tmp;
counter = i;
}
return tmp;
}
/*---------------------------------------------------------------------------*/
/**
@ -98,42 +139,37 @@ clock_time(void)
* The comparison avoids the need to disable clock interrupts for an atomic
* read of the four-byte variable.
*/
unsigned long
unsigned long
clock_seconds(void)
{
unsigned long tmp;
do {
tmp = seconds;
} while(tmp != seconds);
return tmp;
unsigned long tmp;
do {
tmp = seconds;
} while(tmp != seconds);
return tmp;
}
/*---------------------------------------------------------------------------*/
/**
* Set seconds, e.g. to a standard epoch for an absolute date/time.
*/
void
void
clock_set_seconds(unsigned long sec)
{
seconds = sec;
seconds = sec;
}
/*---------------------------------------------------------------------------*/
/*
* Wait for a number of clock ticks.
*/
void
void
clock_wait(clock_time_t t)
{
clock_time_t endticks = clock_time() + t;
if (sizeof(clock_time_t) == 1) {
while ((signed char )(clock_time() - endticks) < 0) {;}
} else if (sizeof(clock_time_t) == 2) {
while ((signed short)(clock_time() - endticks) < 0) {;}
} else {
while ((signed long )(clock_time() - endticks) < 0) {;}
}
clock_time_t endticks = clock_time() + t;
while (clock_time () < endticks)
{;}
}
/*---------------------------------------------------------------------------*/
void
void
clock_delay_usec(uint16_t dt)
{
}
@ -146,24 +182,24 @@ clock_delay_usec(uint16_t dt)
* Platforms are not required to implement this call.
* \note This will break for CPUs clocked above 260 MHz.
*/
void
void
clock_delay_msec(uint16_t howlong)
{
#if F_CPU>=16000000
while(howlong--) clock_delay_usec(1000);
while(howlong--) clock_delay_usec(1000);
#elif F_CPU>=8000000
uint16_t i=996;
while(howlong--) {clock_delay_usec(i);i=999;}
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;}
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;}
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;}
uint16_t i=983;
while(howlong--) {clock_delay_usec(i);i=999;}
#endif
}
/*---------------------------------------------------------------------------*/
@ -173,12 +209,12 @@ clock_delay_msec(uint16_t howlong)
*
* Typically used to add ticks after an MCU sleep
* clock_seconds will increment if necessary to reflect the tick addition.
* Leap ticks or seconds can (rarely) be introduced if the ISR is not blocked.
* Leap ticks or seconds can (rarely) be introduced if the ISR is not blocked.
*/
void
void
clock_adjust_ticks(clock_time_t howmany)
{
}
/** @} */
/** @} */

View file

@ -52,6 +52,7 @@ mtarch_start(struct mtarch_thread *t,
* measuring stack usage */
uint8_t i;
printf ("mtarch_start called\n");
for(i = 0; i < MTARCH_STACKSIZE; ++i) {
t->stack[i] = i;
}
@ -60,7 +61,7 @@ mtarch_start(struct mtarch_thread *t,
* Push pointer to mt_exit and the thread onto our stack:
* Caveats:
* - The stack is defined as an array of bytes, but pointers are 32 bit wide
* - Function pointers are 32-bit addresses in flash ROM
* - Function pointers are 32-bit addresses
*/
/* Initialize stack. This is done in reverse order ("pushing") the
@ -84,31 +85,45 @@ mtarch_start(struct mtarch_thread *t,
}
/*--------------------------------------------------------------------------*/
static unsigned char *sptmp;
static struct mtarch_thread *running;
static void
sw(void)
{
/* Disable interrupts while we perform the context switch */
//cli ();
printf ("sw called\n");
/* FIXME: Disable interrupts while we perform the context switch */
/* Needs to be in separate asm statement, we don't want to be
* interrupted while the C-Compiler-generated wrapper-code pushes
* registers on the stack.
*/
/* Push 32 general purpuse registers */
/*
* Need to save ra, s0/fp, s1-s11, we make the C-compiler do it by
* specifying these registers as clobber.
* For now we leave MT threads alone -- the stack management is too
* unstable in the currently-used gcc port, in our example for storing
* 13 4-byte variables on the stack the compiler allocates 64 bytes on
* the stack (52 would be ok, 64 is not even explained if the stack is
* kept 8-byte aligned (maybe 16?)). Also the normal function wrapper
* code is not called if we have a single asm statement in a function
* (the normal wrapper code already saves ra, s0/fp on the stack).
*/
/* Switch stack pointer */
sptmp = running->sp;
//running->sp = (unsigned char*)SP;
//SP = (unsigned short)sptmp;
/* Pop 32 general purpose registers */
/* Renable interrupts */
//sei ();
asm (
"mv t0, sp\n"
"mv sp, %0\n"
"mv %0, t0\n"
: "+r" (running->sp)
:
: "t0","ra","s0","s1","s2","s3","s4","s5","s6","s7","s8","s9","s10","s11"
);
}
/*--------------------------------------------------------------------------*/
void
mtarch_exec(struct mtarch_thread *t)
{
printf ("mtarch_exec called\n");
running = t;
sw();
running = NULL;

View file

@ -45,7 +45,7 @@
#ifdef MTARCH_CONF_STACKSIZE
#define MTARCH_STACKSIZE MTARCH_CONF_STACKSIZE
#else
#define MTARCH_STACKSIZE 512
#define MTARCH_STACKSIZE 1024
#endif
struct mtarch_thread {

View file

@ -35,8 +35,7 @@
#include <sys/clock.h>
// FIXME
#define RTIMER_ARCH_SECOND 23
#define RTIMER_ARCH_SECOND F_CPU
#define rtimer_arch_now() (clock_time ())