contiki/cpu/arm/stm32f1x_cl/clock.c
Jeff Ciesielski 5c6018f0e2 stm32f1x_cl: Add initial support for STM32F1x connectivity line
This currently supports the stm32f107, but support will be added for
the F105 shortly (missing a linker script and I can't properly test
without a devkit).
2013-02-06 15:41:50 -08:00

79 lines
1.5 KiB
C

#include <sys/clock.h>
#include <sys/cc.h>
#include <sys/etimer.h>
#include <debug-uart.h>
#include <libopencm3/cm3/systick.h>
static volatile clock_time_t current_clock = 0;
static volatile unsigned long current_seconds = 0;
static unsigned int second_countdown = CLOCK_SECOND;
void sys_tick_handler(void) __attribute__ ((interrupt));
void
sys_tick_handler(void)
{
current_clock++;
if(etimer_pending() && etimer_next_expiration_time() <= current_clock) {
etimer_request_poll();
/* printf("%d,%d\n", clock_time(),etimer_next_expiration_time ()); */
}
if(--second_countdown == 0) {
current_seconds++;
second_countdown = CLOCK_SECOND;
}
}
void
clock_init()
{
systick_set_clocksource(STK_CTRL_CLKSOURCE_AHB_DIV8);
/*72mhz / 8 / 1000 */
systick_set_reload(MCK / 8 / CLOCK_SECOND);
systick_interrupt_enable();
systick_counter_enable();
}
clock_time_t
clock_time(void)
{
return current_clock;
}
unsigned long
clock_seconds(void)
{
return current_seconds;
}
/* TODO: This code needs to be evaluated for the stm32f107 and
* implemented
*/
#if 0
/* The inner loop takes 4 cycles. The outer 5+SPIN_COUNT*4. */
#define SPIN_TIME 2 /* us */
#define SPIN_COUNT (((MCK*SPIN_TIME/1000000)-5)/4)
#ifndef __MAKING_DEPS__
void
clock_delay(unsigned int t)
{
#ifdef __THUMBEL__
asm
volatile
("1: mov r1,%2\n2:\tsub r1,#1\n\tbne 2b\n\tsub %0,#1\n\tbne 1b\n":"=l"
(t):"0"(t), "l"(SPIN_COUNT));
#else
#error Must be compiled in thumb mode
#endif
}
#endif
#endif /* __MAKING_DEPS__ */