px-fwlib 0.10.0
Cross-platform embedded library and documentation for 8/16/32-bit microcontrollers generated with Doxygen 1.9.2
px_sysclk.c
1/* =============================================================================
2 ____ ___ ____ ___ _ _ ___ __ __ ___ __ __ TM
3 | _ \ |_ _| / ___| / _ \ | \ | | / _ \ | \/ | |_ _| \ \/ /
4 | |_) | | | | | | | | | | \| | | | | | | |\/| | | | \ /
5 | __/ | | | |___ | |_| | | |\ | | |_| | | | | | | | / \
6 |_| |___| \____| \___/ |_| \_| \___/ |_| |_| |___| /_/\_\
7
8 Copyright (c) 2018 Pieter Conradie <https://piconomix.com>
9
10 License: MIT
11 https://github.com/piconomix/px-fwlib/blob/master/LICENSE.md
12
13 Title: px_sysclk.h : System Clock using the SysTick peripheral
14 Author(s): Pieter Conradie
15 Creation Date: 2018-03-06
16
17============================================================================= */
18
19/* _____STANDARD INCLUDES____________________________________________________ */
20
21/* _____PROJECT INCLUDES_____________________________________________________ */
22#include "px_sysclk.h"
23#include "px_board.h"
24#include "px_stm32cube.h"
25
26#if PX_SYSCLK_CFG_STMCUBE_HAL_TMR
27#include "stm32l0xx_hal.h"
28#endif
29
30/* _____LOCAL DEFINITIONS____________________________________________________ */
31
32/* _____MACROS_______________________________________________________________ */
33
34/* _____GLOBAL VARIABLES_____________________________________________________ */
35
36/* _____LOCAL VARIABLES______________________________________________________ */
37static volatile px_sysclk_ticks_t px_sysclk_tick_counter;
38
39#ifdef PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS
40static volatile px_sysclk_ticks_t px_sysclk_timeout_counter;
41#endif
42
43/* _____LOCAL FUNCTION DECLARATIONS__________________________________________ */
44
45/* _____LOCAL FUNCTIONS______________________________________________________ */
46/// SysTick interrupt handler.
47void SysTick_Handler(void)
48{
49 // Increment counter
50 px_sysclk_tick_counter++;
51
52#if PX_SYSCLK_CFG_STMCUBE_HAL_TMR
53#if (PX_SYSCLK_CFG_TICKS_PER_SEC != 1000)
54 #error "STMCube HAL expects SysTick interval to be 1 ms"
55#endif
56 // Increment HAL Tick value for timeout functionality in STMCUBE HAL library
57 HAL_IncTick();
58#endif
59
60#ifdef PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS
61 // Decrement timeout counter
62 if(--px_sysclk_timeout_counter == 0)
63 {
64 // Reset timeout counter
65 px_sysclk_timeout_counter = PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS;
66#endif
67
68#ifdef PX_SYSCLK_CFG_ON_PERIODIC_TIMEOUT
69 // Call periodic timeout function
70 PX_SYSCLK_CFG_ON_PERIODIC_TIMEOUT();
71#endif
72
73#ifdef PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS
74 }
75#endif
76}
77
78/* _____GLOBAL FUNCTIONS_____________________________________________________ */
80{
81
82 uint32_t ticks_per_period;
83
84 // Calculate number of ticks per SysTick period (SysTick is fed from AHB clock by default)
86 // Configure SysTick peripheral
87 SysTick_Config(ticks_per_period);
88 // Enable SysTick interrupt
89 NVIC_EnableIRQ(SysTick_IRQn);
90
91#ifdef PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS
92 // Reset timeout counter
93 px_sysclk_timeout_counter = PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS;
94#endif
95}
96
98{
99 return px_sysclk_tick_counter;
100}
101
103{
104 px_sysclk_tick_counter = 0;
105}
#define PX_SYSCLK_CFG_TIMEOUT_PERIOD_TICKS
Specify periodic timeout (in sysclk ticks)
#define PX_SYSCLK_CFG_TICKS_PER_SEC
The number of system clock ticks per second.
#define PX_BOARD_SYS_CLK_HZ
System clock frequency in Hz.
Definition: px_board.h:46
#define PX_UDIV_ROUND(dividend, divisor)
Calculate unsigned division with rounding to nearest integer value.
Definition: px_defs.h:284
px_sysclk_ticks_t px_sysclk_get_tick_count(void)
Return number of ticks since system clock started.
Definition: px_sysclk.c:97
uint32_t px_sysclk_ticks_t
Size definition of the tick counter.
Definition: px_sysclk.h:74
void px_sysclk_init(void)
Start system clock (one clock tick every 1/PX_SYSCLK_TICKS_PER_SEC seconds)
Definition: px_sysclk.c:79
void px_sysclk_reset_tick_count(void)
Reset tick count.
Definition: px_sysclk.c:102