From a798cfac34d7265136964d01781231e8e9a10161 Mon Sep 17 00:00:00 2001 From: Nico Tonnhofer Date: Sat, 21 Nov 2015 09:52:18 +0100 Subject: [PATCH] STM32F411: get timer.c in, so far only with the system clock. This test code in SysTickHandler() 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); } [...] --- arduino_stm32f411.h | 2 ++ cmsis-system_stm32f4xx.h | 1 + timer-stm32.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/arduino_stm32f411.h b/arduino_stm32f411.h index 10848fd..b942c45 100644 --- a/arduino_stm32f411.h +++ b/arduino_stm32f411.h @@ -20,6 +20,8 @@ #include "cmsis-stm32f4xx.h" +#define F_CPU __SYSTEM_CLOCK + /** Pins for UART, the serial port. */ #define RXD_PORT PIOA_3_PORT diff --git a/cmsis-system_stm32f4xx.h b/cmsis-system_stm32f4xx.h index 5f1ea2c..acceb80 100644 --- a/cmsis-system_stm32f4xx.h +++ b/cmsis-system_stm32f4xx.h @@ -62,6 +62,7 @@ extern "C" { #endif +#define __SYSTEM_CLOCK 96000000 /** @addtogroup STM32F4xx_System_Includes * @{ */ diff --git a/timer-stm32.c b/timer-stm32.c index c17e06c..bf5845a 100644 --- a/timer-stm32.c +++ b/timer-stm32.c @@ -12,8 +12,43 @@ 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_cm4.h + + Register name mapping from STM32F4xx + +*/ + NVIC_SetPriority(SysTick_IRQn, 0); // Highest priority. + + // SysTick defined in cmsis-core_cm4.h. + SysTick->LOAD = TICK_TIME - 1; // set reload register + SysTick->VAL = 0; // Load the SysTick Counter Value + + 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 +93,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 __ARM_STM32F411__ */