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 ())

View file

@ -1,528 +0,0 @@
// #### This file is auto-generated from icosoc.py. Do not edit! ####
#include "icosoc.h"
#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <stdint.h>
void icosoc_ser0_read(void *data, int len)
{
while (len > 0) {
int n = icosoc_ser0_read_nb(data, len);
data += n, len -= n;
}
}
void icosoc_ser0_write(const void *data, int len)
{
while (len > 0) {
int n = icosoc_ser0_write_nb(data, len);
data += n, len -= n;
}
}
int icosoc_ser0_read_nb(void *data, int maxlen)
{
uint8_t *p = data;
int len = *(volatile uint32_t*)(0x20000004 + 1 * 0x10000);
if (len > maxlen) len = maxlen;
for (int i = 0; i < len; i++)
p[i] = *(volatile uint32_t*)(0x20000000 + 1 * 0x10000);
return len;
}
int icosoc_ser0_write_nb(const void *data, int maxlen)
{
const uint8_t *p = data;
int len = *(volatile uint32_t*)(0x20000008 + 1 * 0x10000);
if (len > maxlen) len = maxlen;
for (int i = 0; i < len; i++)
*(volatile uint32_t*)(0x20000000 + 1 * 0x10000) = p[i];
return len;
}
// Based on riscv newlib libgloss/riscv/machine/syscall.h
// Changes by Clifford Wolf
//========================================================================
// syscalls.c : Newlib operating system interface
//========================================================================
// This is the maven implementation of the narrow newlib operating
// system interface. It is based on the minimum stubs in the newlib
// documentation, the error stubs in libnosys, and the previous scale
// implementation. Please do not include any additional system calls or
// other functions in this file. Additional header and source files
// should be in the machine subdirectory.
//
// Here is a list of the functions which make up the operating system
// interface. The file management instructions execute syscall assembly
// instructions so that a proxy kernel (or the simulator) can marshal up
// the request to the host machine. The process management functions are
// mainly just stubs since for now maven only supports a single process.
//
// - File management functions
// + open : (v) open file
// + lseek : (v) set position in file
// + read : (v) read from file
// + write : (v) write to file
// + fstat : (z) status of an open file
// + stat : (z) status of a file by name
// + close : (z) close a file
// + link : (z) rename a file
// + unlink : (z) remote file's directory entry
//
// - Process management functions
// + execve : (z) transfer control to new proc
// + fork : (z) create a new process
// + getpid : (v) get process id
// + kill : (z) send signal to child process
// + wait : (z) wait for a child process
//
// - Misc functions
// + isatty : (v) query whether output stream is a terminal
// + times : (z) timing information for current process
// + sbrk : (v) increase program data space
// + _exit : (-) exit program without cleaning up files
//
// There are two types of system calls. Those which return a value when
// everything is okay (marked with (v) in above list) and those which
// return a zero when everything is okay (marked with (z) in above
// list). On an error (ie. when the error flag is 1) the return value is
// always an errno which should correspond to the numbers in
// newlib/libc/include/sys/errno.h
//
// Note that really I think we are supposed to define versions of these
// functions with an underscore prefix (eg. _open). This is what some of
// the newlib documentation says, and all the newlib code calls the
// underscore version. This is because technically I don't think we are
// supposed to pollute the namespace with these function names. If you
// define MISSING_SYSCALL_NAMES in xcc/src/newlib/configure.host
// then xcc/src/newlib/libc/include/_syslist.h will essentially define
// all of the underscore versions to be equal to the non-underscore
// versions. I tried not defining MISSING_SYSCALL_NAMES, and newlib
// compiled fine but libstdc++ complained about not being able to fine
// write, read, etc. So for now we do not use underscores (and we do
// define MISSING_SYSCALL_NAMES).
//
// See the newlib documentation for more information
// http://sourceware.org/newlib/libc.html#Syscalls
#include <sys/stat.h>
#include <sys/times.h>
#include <sys/time.h>
#include <sys/timeb.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <utime.h>
//------------------------------------------------------------------------
// environment
//------------------------------------------------------------------------
// A pointer to a list of environment variables and their values. For a
// minimal environment, this empty list is adequate. We used to define
// environ here but it is already defined in
// xcc/src/newlib/libc/stdlib/environ.c so to avoid multiple definition
// errors we have commented this out for now.
//
// char* __env[1] = { 0 };
// char** environ = __env;
//------------------------------------------------------------------------
// open
//------------------------------------------------------------------------
// Open a file.
int open(const char* name, int flags, int mode)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// openat
//------------------------------------------------------------------------
// Open file relative to given directory
int openat(int dirfd, const char* name, int flags, int mode)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// lseek
//------------------------------------------------------------------------
// Set position in a file.
off_t lseek(int file, off_t ptr, int dir)
{
errno = ESPIPE;
return -1;
}
//----------------------------------------------------------------------
// read
//----------------------------------------------------------------------
// Read from a file.
ssize_t read(int file, void *ptr, size_t len)
{
// All reads read from console
unsigned char *p = ptr;
for (int i = 0; i < len;) {
int v = *(volatile int*)0x30000000;
if (v >= 0)
p[i++] = v;
else if (i)
return i;
}
return len;
}
//------------------------------------------------------------------------
// write
//------------------------------------------------------------------------
// Write to a file.
ssize_t write(int file, const void *ptr, size_t len)
{
// All writes go to console
const unsigned char *p = ptr;
for (int i = 0; i < len; i++)
*(volatile int*)0x30000000 = p[i];
return len;
}
//------------------------------------------------------------------------
// fstat
//------------------------------------------------------------------------
// Status of an open file. The sys/stat.h header file required is
// distributed in the include subdirectory for this C library.
int fstat(int file, struct stat *st)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// stat
//------------------------------------------------------------------------
// Status of a file (by name).
int stat(const char *file, struct stat *st)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// lstat
//------------------------------------------------------------------------
// Status of a link (by name).
int lstat(const char *file, struct stat *st)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// fstatat
//------------------------------------------------------------------------
// Status of a file (by name) in a given directory.
int fstatat(int dirfd, const char *file, struct stat *st, int flags)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// access
//------------------------------------------------------------------------
// Permissions of a file (by name).
int access(const char *file, int mode)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// faccessat
//------------------------------------------------------------------------
// Permissions of a file (by name) in a given directory.
int faccessat(int dirfd, const char *file, int mode, int flags)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// close
//------------------------------------------------------------------------
// Close a file.
int close(int file)
{
// close is always ok
return 0;
}
//------------------------------------------------------------------------
// link
//------------------------------------------------------------------------
// Establish a new name for an existing file.
int link(const char *old_name, const char *new_name)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// unlink
//------------------------------------------------------------------------
// Remove a file's directory entry.
int unlink(const char *name)
{
errno = ENOENT;
return -1;
}
//------------------------------------------------------------------------
// execve
//------------------------------------------------------------------------
// Transfer control to a new process. Minimal implementation for a
// system without processes from newlib documentation.
int execve(const char *name, char *const argv[], char *const env[])
{
errno = ENOMEM;
return -1;
}
//------------------------------------------------------------------------
// fork
//------------------------------------------------------------------------
// Create a new process. Minimal implementation for a system without
// processes from newlib documentation.
int fork()
{
errno = EAGAIN;
return -1;
}
//------------------------------------------------------------------------
// getpid
//------------------------------------------------------------------------
// Get process id. This is sometimes used to generate strings unlikely
// to conflict with other processes. Minimal implementation for a
// system without processes just returns 1.
int getpid()
{
return 1;
}
//------------------------------------------------------------------------
// kill
//------------------------------------------------------------------------
// Send a signal. Minimal implementation for a system without processes
// just causes an error.
int kill(int pid, int sig)
{
errno = EINVAL;
return -1;
}
//------------------------------------------------------------------------
// wait
//------------------------------------------------------------------------
// Wait for a child process. Minimal implementation for a system without
// processes just causes an error.
int wait(int *status)
{
errno = ECHILD;
return -1;
}
//------------------------------------------------------------------------
// isatty
//------------------------------------------------------------------------
// Query whether output stream is a terminal. For consistency with the
// other minimal implementations, which only support output to stdout,
// this minimal implementation is suggested by the newlib docs.
int isatty(int file)
{
return 1;
}
//------------------------------------------------------------------------
// times
//------------------------------------------------------------------------
// Timing information for current process. From
// newlib/libc/include/sys/times.h the tms struct fields are as follows:
//
// - clock_t tms_utime : user clock ticks
// - clock_t tms_stime : system clock ticks
// - clock_t tms_cutime : children's user clock ticks
// - clock_t tms_cstime : children's system clock ticks
//
// Since maven does not currently support processes we set both of the
// children's times to zero. Eventually we might want to separately
// account for user vs system time, but for now we just return the total
// number of cycles since starting the program.
clock_t times(struct tms *buf)
{
// when called for the first time, initialize t0
static struct timeval t0;
if (t0.tv_sec == 0)
gettimeofday(&t0, 0);
struct timeval t;
gettimeofday(&t, 0);
long long utime = (t.tv_sec - t0.tv_sec) * 1000000 + (t.tv_usec - t0.tv_usec);
buf->tms_utime = utime * CLOCKS_PER_SEC / 1000000;
buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0;
return -1;
}
//----------------------------------------------------------------------
// gettimeofday
//----------------------------------------------------------------------
// Get the current time. Only relatively correct.
int gettimeofday(struct timeval *tp, void *tzp)
{
asm volatile ("sbreak");
__builtin_unreachable();
}
//----------------------------------------------------------------------
// ftime
//----------------------------------------------------------------------
// Get the current time. Only relatively correct.
int ftime(struct timeb *tp)
{
tp->time = tp->millitm = 0;
return 0;
}
//----------------------------------------------------------------------
// utime
//----------------------------------------------------------------------
// Stub.
int utime(const char *path, const struct utimbuf *times)
{
return -1;
}
//----------------------------------------------------------------------
// chown
//----------------------------------------------------------------------
// Stub.
int chown(const char *path, uid_t owner, gid_t group)
{
return -1;
}
//----------------------------------------------------------------------
// chmod
//----------------------------------------------------------------------
// Stub.
int chmod(const char *path, mode_t mode)
{
return -1;
}
//----------------------------------------------------------------------
// chdir
//----------------------------------------------------------------------
// Stub.
int chdir(const char *path)
{
return -1;
}
//----------------------------------------------------------------------
// getcwd
//----------------------------------------------------------------------
// Stub.
char *getcwd(char *buf, size_t size)
{
return NULL;
}
//----------------------------------------------------------------------
// sysconf
//----------------------------------------------------------------------
// Get configurable system variables
long sysconf(int name)
{
switch (name) {
case _SC_CLK_TCK:
return CLOCKS_PER_SEC;
}
return -1;
}
//----------------------------------------------------------------------
// sbrk
//----------------------------------------------------------------------
// Increase program data space. As malloc and related functions depend
// on this, it is useful to have a working implementation. The following
// is suggested by the newlib docs and suffices for a standalone
// system.
void *sbrk(ptrdiff_t incr)
{
extern unsigned char _end[]; // Defined by linker
static unsigned long heap_end;
if (heap_end == 0)
heap_end = (long)_end;
// if (syscall_errno(SYS_brk, heap_end + incr, 0, 0, 0) != heap_end + incr)
// return (void *)-1;
heap_end += incr;
return (void *)(heap_end - incr);
}
//------------------------------------------------------------------------
// _exit
//------------------------------------------------------------------------
// Exit a program without cleaning up files.
void _exit(int exit_status)
{
asm volatile ("sbreak");
__builtin_unreachable();
}

View file

@ -1,5 +1,8 @@
# hello world example
# enable compressed ISA support
compressed_isa
# Digilent PmodUSBUART on PMOD3
# http://store.digilentinc.com/pmodusbuart-usb-to-uart-interface/
mod rs232 ser0

View file

@ -1,30 +0,0 @@
// #### This file is auto-generated from icosoc.py. Do not edit! ####
#ifndef ICOSOC_H
#define ICOSOC_H
#define ICOSOC_CLOCK_FREQ_HZ 6000000
void icosoc_ser0_read(void *data, int len);
void icosoc_ser0_write(const void *data, int len);
int icosoc_ser0_read_nb(void *data, int maxlen);
int icosoc_ser0_write_nb(const void *data, int maxlen);
#include <stdint.h>
static inline void icosoc_leds_set(uint32_t bitmask) {
*(volatile uint32_t*)(0x20000000 + 2 * 0x10000) = bitmask;
}
static inline uint32_t icosoc_leds_get() {
return *(volatile uint32_t*)(0x20000000 + 2 * 0x10000);
}
static inline void icosoc_leds_dir(uint32_t bitmask) {
*(volatile uint32_t*)(0x20000004 + 2 * 0x10000) = bitmask;
}
#endif /* ICOSOC_H */

View file

@ -43,7 +43,7 @@
#include "contiki.h"
#include "contiki-net.h"
//FIXME#include "er-coap-engine.h"
//FIXME#include "time.h"
//FIXME#include "xtime.h"
//FIXME#include "cron.h"
//FIXME#include "time_resource.h"
//FIXME#include "jsonparse.h"
@ -84,6 +84,8 @@ void led_set (void *onoff)
icosoc_leds_set (0xFF * status);
}
extern void print_clocks (void);
PROCESS_THREAD(wallclock, ev, data)
{
static struct etimer loop_periodic_timer;
@ -126,6 +128,7 @@ PROCESS_THREAD(wallclock, ev, data)
etimer_set (&loop_periodic_timer, LOOP_INTERVAL);
while (1) {
printf ("In while loop\n");
print_clocks ();
PROCESS_WAIT_EVENT();
if (etimer_expired (&loop_periodic_timer)) {
//cron ();

View file

@ -50,15 +50,8 @@
#define F_CPU 8000000UL
#endif
/* The AVR tick interrupt usually is done with an 8 bit counter around 128 Hz.
* 125 Hz needs slightly more overhead during the interrupt, as does a 32 bit
* clock_time_t.
*/
/* Clock ticks per second */
#define CLOCK_CONF_SECOND 128
typedef uint32_t clock_time_t;
#define CLOCK_LT(a,b) ((int32_t)((a)-(b)) < 0)
typedef uint64_t clock_time_t;
#define CLOCK_LT(a,b) ((a)<(b))
/* RADIOSTATS is used in rf230bb, clock.c and the webserver cgi to report radio usage */
/* It has less overhead than ENERGEST */