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);
    }
  [...]
This commit is contained in:
Nico Tonnhofer 2015-11-21 09:52:18 +01:00
parent 65dab6b180
commit a798cfac34
3 changed files with 39 additions and 0 deletions

View File

@ -20,6 +20,8 @@
#include "cmsis-stm32f4xx.h" #include "cmsis-stm32f4xx.h"
#define F_CPU __SYSTEM_CLOCK
/** Pins for UART, the serial port. /** Pins for UART, the serial port.
*/ */
#define RXD_PORT PIOA_3_PORT #define RXD_PORT PIOA_3_PORT

View File

@ -62,6 +62,7 @@
extern "C" { extern "C" {
#endif #endif
#define __SYSTEM_CLOCK 96000000
/** @addtogroup STM32F4xx_System_Includes /** @addtogroup STM32F4xx_System_Includes
* @{ * @{
*/ */

View File

@ -12,8 +12,43 @@
Initialise timer and enable system clock interrupt. Step interrupt is Initialise timer and enable system clock interrupt. Step interrupt is
enabled later, when we start using it. 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() { 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. /** 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. This means to be an emergency stop.
*/ */
void timer_stop() { void timer_stop() {
SysTick->CTRL = 0;
} }
#endif /* defined TEACUP_C_INCLUDE && defined __ARM_STM32F411__ */ #endif /* defined TEACUP_C_INCLUDE && defined __ARM_STM32F411__ */