Skip to content

Commit

Permalink
Merge pull request #17654 from kfessel/p-ztimer-lptimer
Browse files Browse the repository at this point in the history
sys/ztimer: add LPTIMER auto init
  • Loading branch information
benpicco authored Sep 15, 2022
2 parents 6790167 + 81d06c8 commit 1be4da5
Showing 1 changed file with 87 additions and 34 deletions.
121 changes: 87 additions & 34 deletions sys/ztimer/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* 2.1b: else: convert from ZTIMER_USEC
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author Karl Fessel <kfessel>
*
* @}
*/
Expand Down Expand Up @@ -67,18 +68,18 @@

/* Step 0: define available ztimer-periphery by activated modules */

/* #if CONFIG_ZTIMER_USEC_TYPE_PERIPH_TIMER
* replaces #if MODULE_ZTIMER_PERIPH_TIMER
* the ztimer_periph_timer is always available
* having an (1) config defined in ztimer/config.h
*/

#if CONFIG_ZTIMER_USEC_TYPE_PERIPH_TIMER
#if MODULE_ZTIMER_PERIPH_TIMER
# define ZTIMER_TIMER _ztimer_periph_timer
# define ZTIMER_TIMER_CLK _ztimer_periph_timer.super
# define ZTIMER_TIMER_FREQ CONFIG_ZTIMER_USEC_BASE_FREQ
#endif

#if MODULE_ZTIMER_PERIPH_LPTIMER
# define ZTIMER_LPTIMER _ztimer_periph_timer_lp
# define ZTIMER_LPTIMER_CLK _ztimer_periph_timer_lp.super
# define ZTIMER_LPTIMER_FREQ CONFIG_ZTIMER_LPTIMER_FREQ
#endif

#if MODULE_ZTIMER_PERIPH_RTT
# define ZTIMER_RTT _ztimer_periph_timer_rtt
# define ZTIMER_RTT_CLK _ztimer_periph_timer_rtt
Expand All @@ -99,24 +100,39 @@
/* ZTIMER_USEC always uses the basic timer
* basic timer is available on all boards */
#if MODULE_ZTIMER_USEC
# ifndef INIT_ZTIMER_TIMER
# define INIT_ZTIMER_TIMER 1
# ifdef ZTIMER_TIMER
# define ZTIMER_USEC_TIMER 1
# ifndef INIT_ZTIMER_TIMER
# define INIT_ZTIMER_TIMER 1
# endif
# endif
#endif

/* ZTIMER_MSEC prefers ZTIMER_RTT (ztimer_periph_rtt)
* if it is available and runs at a frequency > 1kHz
* if not it falls back to use the basic timer */
/* ZTIMER_MSEC uses one of in order of preference:
* - ZTIMER_LPTIMER (ztimer_periph_lptimer)
* if it is available and running at a frequency > 1kHz
* - ZTIMER_RTT (ztimer_periph_rtt)
* if it is available and running at a frequency > 1kHz,
* if there is neither of those it falls back to use the basic timer
* if that is available (there should be a basic timer with all boards)*/
#if MODULE_ZTIMER_MSEC
# if defined(ZTIMER_RTT) && ZTIMER_RTT_FREQ >= FREQ_1KHZ
# if defined(ZTIMER_LPTIMER) && ZTIMER_LPTIMER_FREQ >= FREQ_1KHZ
# define ZTIMER_MSEC_LPTIMER 1
# ifndef INIT_ZTIMER_LPTIMER
# define INIT_ZTIMER_LPTIMER 1
# endif
# if ZTIMER_LPTIMER_FREQ != FREQ_1KHZ
# define ZTIMER_MSEC_CONVERT_LOWER_FREQ ZTIMER_LPTIMER_FREQ
# endif
# elif defined(ZTIMER_RTT) && ZTIMER_RTT_FREQ >= FREQ_1KHZ
# define ZTIMER_MSEC_RTT 1
# ifndef INIT_ZTIMER_RTT
# define INIT_ZTIMER_RTT 1
# endif
# if ZTIMER_RTT_FREQ != FREQ_1KHZ
# define ZTIMER_MSEC_CONVERT_LOWER_FREQ ZTIMER_RTT_FREQ
# endif
# else
# elif defined(ZTIMER_TIMER)
# define ZTIMER_MSEC_TIMER 1
# ifndef INIT_ZTIMER_TIMER
# define INIT_ZTIMER_TIMER 1
Expand All @@ -125,29 +141,43 @@
# endif
#endif

/* ZTIMER_SEC prefers ZTIMER_RTT (ztimer_periph_rtt) if it is available
* if not it prefers ZTIMER_RTC (ztimer_periph_rtc) if it is available
* if not it falls back to use the basic timer */
/* ZTIMER_SEC uses one of in order of preference:
* - ZTIMER_LPTIMER (ztimer_periph_lptimer)
* if it is available and running at a frequency > 1Hz
* - ZTIMER_RTT (ztimer_periph_rtt)
* if it is available and running at a frequency > 1Hz,
* - ZTIMER_RTC (ztimer_periph_rtc)
* if it is available (this should be avoided)
* if there is neither of those it falls back to use the basic timer
* if that is available (there should be a basic timer with all boards)*/
#if MODULE_ZTIMER_SEC
# ifdef ZTIMER_RTT
# if defined(ZTIMER_LPTIMER) && ZTIMER_LPTIMER_FREQ >= FREQ_1HZ
# define ZTIMER_SEC_LPTIMER 1
# ifndef INIT_ZTIMER_LPTIMER
# define INIT_ZTIMER_LPTIMER 1
# endif
# if ZTIMER_LPTIMER_FREQ != FREQ_1HZ
# define ZTIMER_SEC_CONVERT_LOWER_FREQ ZTIMER_LPTIMER_FREQ
# endif
# elif defined(ZTIMER_RTT)
# define ZTIMER_SEC_RTT
# ifndef INIT_ZTIMER_RTT
# define INIT_ZTIMER_RTT 1
# endif
# if ZTIMER_RTT_FREQ != FREQ_1HZ
# define ZTIMER_SEC_CONVERT_LOWER_FREQ ZTIMER_RTT_FREQ
# else
# ifdef ZTIMER_RTC
# define ZTIMER_SEC_RTC
# ifndef INIT_ZTIMER_RTC
# define INIT_ZTIMER_RTC 1
# endif
# else
# define ZTIMER_SEC_TIMER
# ifndef INIT_ZTIMER_TIMER
# define INIT_ZTIMER_TIMER 1
# endif
# define ZTIMER_SEC_CONVERT_LOWER_FREQ ZTIMER_TIMER_FREQ
# endif
# elif defined(ZTIMER_RTC)
# define ZTIMER_SEC_RTC
# ifndef INIT_ZTIMER_RTC
# define INIT_ZTIMER_RTC 1
# endif
# elif defined(ZTIMER_TIMER)
# define ZTIMER_SEC_TIMER
# ifndef INIT_ZTIMER_TIMER
# define INIT_ZTIMER_TIMER 1
# endif
# define ZTIMER_SEC_CONVERT_LOWER_FREQ ZTIMER_TIMER_FREQ
# endif
#endif

Expand All @@ -159,6 +189,10 @@ static ztimer_periph_timer_t ZTIMER_TIMER = {
};
#endif

#if INIT_ZTIMER_LPTIMER
static ztimer_periph_timer_t ZTIMER_LPTIMER = {};
#endif

#if INIT_ZTIMER_RTT
static ztimer_periph_rtt_t ZTIMER_RTT;
#endif
Expand All @@ -170,7 +204,7 @@ static ztimer_periph_rtc_t ZTIMER_RTC;
/* Step 3: setup constants for ztimers and memory for converters */

#if MODULE_ZTIMER_USEC
# ifdef ZTIMER_TIMER
# ifdef ZTIMER_USEC_TIMER
ztimer_clock_t *const ZTIMER_USEC_BASE = &ZTIMER_TIMER_CLK;
# else
# error No suitable ZTIMER_USEC config. Basic timer configuration missing?
Expand All @@ -191,32 +225,37 @@ ztimer_clock_t *const ZTIMER_USEC = &_ztimer_convert_frac_usec.super.super;
ztimer_clock_t *const ZTIMER_MSEC_BASE = &ZTIMER_RTT_CLK;
# elif defined(ZTIMER_MSEC_TIMER)
ztimer_clock_t *const ZTIMER_MSEC_BASE = &ZTIMER_TIMER_CLK;
# elif defined(ZTIMER_MSEC_LPTIMER)
ztimer_clock_t *const ZTIMER_MSEC_BASE = &ZTIMER_LPTIMER_CLK;
# else
# error No suitable ZTIMER_MSEC config. No rtt or basic timer?
# endif
# ifdef ZTIMER_MSEC_CONVERT_LOWER_FREQ
static ztimer_convert_frac_t _ztimer_convert_frac_msec;
ztimer_clock_t *const ZTIMER_MSEC = &_ztimer_convert_frac_msec.super.super;
# else
ztimer_clock_t *const ZTIMER_MSEC = &ZTIMER_RTT_CLK;
ztimer_clock_t *const ZTIMER_MSEC = ZTIMER_MSEC_BASE;
# endif
#endif

#if MODULE_ZTIMER_SEC
# ifdef ZTIMER_SEC_RTC
ztimer_clock_t *const ZTIMER_SEC_BASE = &ZTIMER_RTC_CLK;
ztimer_clock_t *const ZTIMER_SEC = &ZTIMER_RTC_CLK;
# elif defined(ZTIMER_SEC_RTT)
ztimer_clock_t *const ZTIMER_SEC_BASE = &ZTIMER_RTT_CLK;
# elif defined(ZTIMER_SEC_TIMER)
ztimer_clock_t *const ZTIMER_SEC_BASE = &ZTIMER_TIMER_CLK;
# elif defined(ZTIMER_SEC_LPTIMER)
ztimer_clock_t *const ZTIMER_SEC_BASE = &ZTIMER_LPTIMER_CLK;
# else
# error No suitable ZTIMER_SEC config. No rtc, rtt or basic timer?
# endif

# ifdef ZTIMER_SEC_CONVERT_LOWER_FREQ
static ztimer_convert_frac_t _ztimer_convert_frac_sec;
ztimer_clock_t *const ZTIMER_SEC = &_ztimer_convert_frac_sec.super.super;
# else
ztimer_clock_t *const ZTIMER_SEC = ZTIMER_SEC_BASE;
# endif
#endif

Expand Down Expand Up @@ -249,7 +288,7 @@ void ztimer_init(void)
#if INIT_ZTIMER_TIMER
LOG_DEBUG(
"ztimer_init(): ZTIMER_TIMER using periph timer %u, freq %lu, width %u\n",
CONFIG_ZTIMER_USEC_DEV, CONFIG_ZTIMER_USEC_BASE_FREQ,
CONFIG_ZTIMER_USEC_DEV, ZTIMER_TIMER_FREQ,
CONFIG_ZTIMER_USEC_WIDTH);
ztimer_periph_timer_init(&ZTIMER_TIMER, CONFIG_ZTIMER_USEC_DEV,
ZTIMER_TIMER_FREQ, WIDTH_TO_MAXVAL(CONFIG_ZTIMER_USEC_WIDTH));
Expand All @@ -260,6 +299,20 @@ void ztimer_init(void)
# endif
#endif

#if INIT_ZTIMER_LPTIMER
LOG_DEBUG(
"ztimer_init(): ZTIMER_LPTIMER using periph timer %u, freq %lu, width %u\n",
CONFIG_ZTIMER_LPTIMER_DEV, ZTIMER_LPTIMER_FREQ,
CONFIG_ZTIMER_LPTIMER_WIDTH);
ztimer_periph_timer_init(&ZTIMER_LPTIMER, CONFIG_ZTIMER_LPTIMER_DEV,
ZTIMER_LPTIMER_FREQ, WIDTH_TO_MAXVAL(CONFIG_ZTIMER_LPTIMER_WIDTH));
# ifdef MODULE_PM_LAYERED
LOG_DEBUG("ztimer_init(): ZTIMER_LPTIMER setting block_pm_mode to %i\n",
CONFIG_ZTIMER_LPTIMER_BLOCK_PM_MODE);
ZTIMER_LPTIMER_CLK.block_pm_mode = CONFIG_ZTIMER_LPTIMER_BLOCK_PM_MODE;
# endif
#endif

#if INIT_ZTIMER_RTT
LOG_DEBUG("ztimer_init(): initializing rtt\n");
ztimer_periph_rtt_init(&ZTIMER_RTT);
Expand Down

0 comments on commit 1be4da5

Please sign in to comment.