Skip to content

Commit

Permalink
drivers: timer: Add support for Apollo3 SoCs system timer (STIMER)
Browse files Browse the repository at this point in the history
This commit adds support for the system timer peripheral which
can be found in Apollo3 SoCs

Signed-off-by: Hao Luo <hluo@ambiq.com>
  • Loading branch information
AlessandroLuo authored and carlescufi committed May 7, 2024
1 parent 3c1fd19 commit 638f1d5
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion drivers/timer/Kconfig.ambiq
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ config AMBIQ_STIMER_TIMER
select TICKLESS_CAPABLE
select AMBIQ_HAL_USE_STIMER
help
Ambiq Apollo4 stimer driver
Ambiq Apollo stimer driver
26 changes: 13 additions & 13 deletions drivers/timer/ambiq_stimer.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,10 @@

#define COUNTER_MAX UINT32_MAX

#define CYC_PER_TICK (sys_clock_hw_cycles_per_sec() \
/ CONFIG_SYS_CLOCK_TICKS_PER_SEC)
#define MAX_TICKS ((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1)
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
#define MIN_DELAY 1
#define CYC_PER_TICK (sys_clock_hw_cycles_per_sec() / CONFIG_SYS_CLOCK_TICKS_PER_SEC)
#define MAX_TICKS ((k_ticks_t)(COUNTER_MAX / CYC_PER_TICK) - 1)
#define MAX_CYCLES (MAX_TICKS * CYC_PER_TICK)
#define MIN_DELAY 1

#define TIMER_IRQ (DT_INST_IRQN(0))

Expand Down Expand Up @@ -102,8 +101,7 @@ uint32_t sys_clock_elapsed(void)
}

k_spinlock_key_t key = k_spin_lock(&g_lock);
uint32_t ret = (am_hal_stimer_counter_get()
- g_last_count) / CYC_PER_TICK;
uint32_t ret = (am_hal_stimer_counter_get() - g_last_count) / CYC_PER_TICK;

k_spin_unlock(&g_lock, key);
return ret;
Expand All @@ -121,10 +119,13 @@ static int stimer_init(void)

oldCfg = am_hal_stimer_config(AM_HAL_STIMER_CFG_FREEZE);

am_hal_stimer_config((oldCfg & ~(AM_HAL_STIMER_CFG_FREEZE | STIMER_STCFG_CLKSEL_Msk))
| AM_HAL_STIMER_XTAL_32KHZ
| AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);

#if defined(CONFIG_SOC_SERIES_APOLLO3X)
am_hal_stimer_config((oldCfg & ~(AM_HAL_STIMER_CFG_FREEZE | CTIMER_STCFG_CLKSEL_Msk)) |
AM_HAL_STIMER_XTAL_32KHZ | AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
#else
am_hal_stimer_config((oldCfg & ~(AM_HAL_STIMER_CFG_FREEZE | STIMER_STCFG_CLKSEL_Msk)) |
AM_HAL_STIMER_XTAL_32KHZ | AM_HAL_STIMER_CFG_COMPARE_A_ENABLE);
#endif
g_last_count = am_hal_stimer_counter_get();

k_spin_unlock(&g_lock, key);
Expand All @@ -141,5 +142,4 @@ static int stimer_init(void)
return 0;
}

SYS_INIT(stimer_init, PRE_KERNEL_2,
CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
SYS_INIT(stimer_init, PRE_KERNEL_2, CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);

0 comments on commit 638f1d5

Please sign in to comment.