From 137a6386581ab2da75fda5f08d797e339a6de4b7 Mon Sep 17 00:00:00 2001 From: Markus Hitter Date: Fri, 31 Jul 2015 16:47:48 +0200 Subject: [PATCH] ARM: get timer.c in, so far only with the system clock. This test code in SysTickHanlder() should give you a rather accurate clock with only a few seconds deviation per hour: #include "serial.h" #include "sersendf.h" void SysTick_Handler(void) { static uint32_t count = 0; static uint8_t minutes = 0, seconds = 0; count++; if ( ! (count % 500)) { // A full second. seconds++; if ( ! (seconds % 60)) { seconds = 0; minutes++; } sersendf_P(PSTR("%su:"), minutes); if (seconds < 10) serial_writechar('0'); sersendf_P(PSTR("%su\n"), seconds); } [...] --- Makefile-ARM | 2 +- mendel.c | 4 +++- timer-arm.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ timer.h | 2 +- 4 files changed, 49 insertions(+), 3 deletions(-) diff --git a/Makefile-ARM b/Makefile-ARM index 78ec3d1..dd735f0 100644 --- a/Makefile-ARM +++ b/Makefile-ARM @@ -98,7 +98,7 @@ TARGET = $(PROGRAM).hex # Until the generic ARM port is completed, we'd have to wrap all sources # in #ifdef __AVR__. To avoid this, build only a selection for now: SOURCES = mendel.c cpu.c serial.c sermsg.c sersendf.c delay.c -SOURCES += gcode_parse.c gcode_process.c pinio.c +SOURCES += gcode_parse.c gcode_process.c pinio.c timer.c ifeq ($(MCU), lpc1114) SOURCES += cmsis-system_lpc11xx.c endif diff --git a/mendel.c b/mendel.c index 84f4e3c..05e3a22 100644 --- a/mendel.c +++ b/mendel.c @@ -35,8 +35,8 @@ #include "dda_queue.h" #endif /* __ARMEL_NOTYET__ */ #include "gcode_parse.h" -#ifndef __ARMEL_NOTYET__ #include "timer.h" +#ifndef __ARMEL_NOTYET__ #include "temp.h" #include "watchdog.h" #include "debug.h" @@ -94,10 +94,12 @@ void init(void) { #if defined TEMP_MAX6675 || defined SD spi_init(); #endif + #endif /* __ARMEL_NOTYET__ */ // set up timers timer_init(); + #ifndef __ARMEL_NOTYET__ // read PID settings from EEPROM heater_init(); diff --git a/timer-arm.c b/timer-arm.c index e867de0..39158ca 100644 --- a/timer-arm.c +++ b/timer-arm.c @@ -7,13 +7,56 @@ #if defined TEACUP_C_INCLUDE && defined __ARMEL__ +#include "cmsis-core_cm0.h" + /** Timer initialisation. Initialise timer and enable system clock interrupt. Step interrupt is enabled later, when we start using it. + + For the system clock, we use SysTickTimer. This timer is made for exactly + such purposes. */ void timer_init() { + + /** + Initialise the system tick timer. + + We enable the system tick timer with interrupts. A similar function is + SysTick_Config(uint32_t ticks) in cmsis-core_cm0.h + + Register name mapping from LPC111x User Manual to CMSIS headers: + + chap. 24.5 cmsis-core_cm0.h description + + SYST_CSR SysTick->CTRL System Timer Control and status register. + SYST_RVR SysTick->LOAD System Timer Reload value register. + SYST_CVR SysTick->VAL System Timer Current value register. + SYST_CALIB SysTick->CALIB System Timer Calibration value register. + */ + NVIC_SetPriority(SysTick_IRQn, 0); // Highest priority. + + // SysTick defined in cmsis-core_cm0.h. + SysTick->LOAD = TICK_TIME - 1; + SysTick->VAL = 0; // Actually load LOAD. + + SysTick->CTRL = SysTick_CTRL_ENABLE_Msk // Enable the ticker. + | SysTick_CTRL_TICKINT_Msk // Enable interrupt. + | SysTick_CTRL_CLKSOURCE_Msk; // Run at full CPU clock. +} + +/** System clock interrupt. + + Happens every TICK_TIME. Must have the same name as in + cmsis-startup_lpc11xx.s +*/ +void SysTick_Handler(void) { + + #ifndef __ARMEL_NOTYET__ + clock_tick(); + dda_clock(); + #endif /* __ARMEL_NOTYET__ */ } /** Specify how long until the step timer should fire. @@ -58,6 +101,7 @@ uint8_t timer_set(int32_t delay, uint8_t check_short) { This means to be an emergency stop. */ void timer_stop() { + SysTick->CTRL = 0; } #endif /* defined TEACUP_C_INCLUDE && defined __ARMEL__ */ diff --git a/timer.h b/timer.h index 619de6a..0cd6ac7 100644 --- a/timer.h +++ b/timer.h @@ -10,7 +10,7 @@ /// How often we overflow and update our clock. /// With F_CPU = 16MHz, max is < 4.096ms (TICK_TIME = 65535). -#define TICK_TIME 2 MS +#define TICK_TIME (2 MS) /// Convert back to ms from cpu ticks so our system clock runs /// properly if you change TICK_TIME.