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);
    }
  [...]
This commit is contained in:
Markus Hitter 2015-07-31 16:47:48 +02:00
parent feeb411eec
commit 137a638658
4 changed files with 49 additions and 3 deletions

View File

@ -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

View File

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

View File

@ -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__ */

View File

@ -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.