2021-07-07 20:50:43 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021 Vestas Wind Systems A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define DT_DRV_COMPAT nxp_kinetis_lptmr
|
|
|
|
|
2023-08-28 13:15:43 +02:00
|
|
|
#include <zephyr/init.h>
|
2022-05-06 10:25:46 +02:00
|
|
|
#include <zephyr/drivers/timer/system_timer.h>
|
2022-10-04 16:34:24 +02:00
|
|
|
#include <zephyr/kernel.h>
|
2022-10-05 13:53:46 +02:00
|
|
|
#include <zephyr/sys/time_units.h>
|
2021-07-07 20:50:43 +02:00
|
|
|
#include <fsl_lptmr.h>
|
2022-10-17 10:24:11 +02:00
|
|
|
#include <zephyr/irq.h>
|
2021-07-07 20:50:43 +02:00
|
|
|
|
|
|
|
BUILD_ASSERT(DT_NUM_INST_STATUS_OKAY(DT_DRV_COMPAT) == 1,
|
|
|
|
"No LPTMR instance enabled in devicetree");
|
|
|
|
|
|
|
|
/* Prescaler mapping */
|
|
|
|
#define LPTMR_PRESCALER_2 kLPTMR_Prescale_Glitch_0
|
|
|
|
#define LPTMR_PRESCALER_4 kLPTMR_Prescale_Glitch_1
|
|
|
|
#define LPTMR_PRESCALER_8 kLPTMR_Prescale_Glitch_2
|
|
|
|
#define LPTMR_PRESCALER_16 kLPTMR_Prescale_Glitch_3
|
|
|
|
#define LPTMR_PRESCALER_32 kLPTMR_Prescale_Glitch_4
|
|
|
|
#define LPTMR_PRESCALER_64 kLPTMR_Prescale_Glitch_5
|
|
|
|
#define LPTMR_PRESCALER_128 kLPTMR_Prescale_Glitch_6
|
|
|
|
#define LPTMR_PRESCALER_256 kLPTMR_Prescale_Glitch_7
|
|
|
|
#define LPTMR_PRESCALER_512 kLPTMR_Prescale_Glitch_8
|
|
|
|
#define LPTMR_PRESCALER_1024 kLPTMR_Prescale_Glitch_9
|
|
|
|
#define LPTMR_PRESCALER_2048 kLPTMR_Prescale_Glitch_10
|
|
|
|
#define LPTMR_PRESCALER_4096 kLPTMR_Prescale_Glitch_11
|
|
|
|
#define LPTMR_PRESCALER_8192 kLPTMR_Prescale_Glitch_12
|
|
|
|
#define LPTMR_PRESCALER_16384 kLPTMR_Prescale_Glitch_13
|
|
|
|
#define LPTMR_PRESCALER_32768 kLPTMR_Prescale_Glitch_14
|
|
|
|
#define LPTMR_PRESCALER_65536 kLPTMR_Prescale_Glitch_15
|
|
|
|
#define TO_LPTMR_PRESCALER(val) _DO_CONCAT(LPTMR_PRESCALER_, val)
|
|
|
|
|
|
|
|
/* Prescaler clock mapping */
|
|
|
|
#define TO_LPTMR_CLK_SEL(val) _DO_CONCAT(kLPTMR_PrescalerClock_, val)
|
|
|
|
|
|
|
|
/* Devicetree properties */
|
|
|
|
#define LPTMR_BASE ((LPTMR_Type *)(DT_INST_REG_ADDR(0)))
|
|
|
|
#define LPTMR_CLK_SOURCE TO_LPTMR_CLK_SEL(DT_INST_PROP(0, clk_source));
|
|
|
|
#define LPTMR_PRESCALER TO_LPTMR_PRESCALER(DT_INST_PROP(0, prescaler));
|
|
|
|
#define LPTMR_BYPASS_PRESCALER DT_INST_PROP(0, prescaler) == 1
|
|
|
|
#define LPTMR_IRQN DT_INST_IRQN(0)
|
|
|
|
#define LPTMR_IRQ_PRIORITY DT_INST_IRQ(0, priority)
|
|
|
|
|
|
|
|
/* Timer cycles per tick */
|
|
|
|
#define CYCLES_PER_TICK ((uint32_t)((uint64_t)sys_clock_hw_cycles_per_sec() \
|
|
|
|
/ (uint64_t)CONFIG_SYS_CLOCK_TICKS_PER_SEC))
|
|
|
|
|
|
|
|
/* 32 bit cycle counter */
|
|
|
|
static volatile uint32_t cycles;
|
|
|
|
|
|
|
|
void sys_clock_set_timeout(int32_t ticks, bool idle)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(idle);
|
|
|
|
|
|
|
|
if (idle && (ticks == K_TICKS_FOREVER)) {
|
|
|
|
LPTMR_DisableInterrupts(LPTMR_BASE, kLPTMR_TimerInterruptEnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_clock_idle_exit(void)
|
|
|
|
{
|
|
|
|
if (LPTMR_GetEnabledInterrupts(LPTMR_BASE) != kLPTMR_TimerInterruptEnable) {
|
|
|
|
LPTMR_EnableInterrupts(LPTMR_BASE, kLPTMR_TimerInterruptEnable);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void sys_clock_disable(void)
|
|
|
|
{
|
|
|
|
LPTMR_DisableInterrupts(LPTMR_BASE, kLPTMR_TimerInterruptEnable);
|
|
|
|
LPTMR_StopTimer(LPTMR_BASE);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sys_clock_elapsed(void)
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t sys_clock_cycle_get_32(void)
|
|
|
|
{
|
|
|
|
return LPTMR_GetCurrentTimerCount(LPTMR_BASE) + cycles;
|
|
|
|
}
|
|
|
|
|
2022-01-06 11:27:07 +01:00
|
|
|
static void mcux_lptmr_timer_isr(const void *arg)
|
2021-07-07 20:50:43 +02:00
|
|
|
{
|
|
|
|
ARG_UNUSED(arg);
|
|
|
|
|
|
|
|
cycles += CYCLES_PER_TICK;
|
|
|
|
|
|
|
|
sys_clock_announce(1);
|
|
|
|
LPTMR_ClearStatusFlags(LPTMR_BASE, kLPTMR_TimerCompareFlag);
|
|
|
|
}
|
|
|
|
|
init: remove the need for a dummy device pointer in SYS_INIT functions
The init infrastructure, found in `init.h`, is currently used by:
- `SYS_INIT`: to call functions before `main`
- `DEVICE_*`: to initialize devices
They are all sorted according to an initialization level + a priority.
`SYS_INIT` calls are really orthogonal to devices, however, the required
function signature requires a `const struct device *dev` as a first
argument. The only reason for that is because the same init machinery is
used by devices, so we have something like:
```c
struct init_entry {
int (*init)(const struct device *dev);
/* only set by DEVICE_*, otherwise NULL */
const struct device *dev;
}
```
As a result, we end up with such weird/ugly pattern:
```c
static int my_init(const struct device *dev)
{
/* always NULL! add ARG_UNUSED to avoid compiler warning */
ARG_UNUSED(dev);
...
}
```
This is really a result of poor internals isolation. This patch proposes
a to make init entries more flexible so that they can accept sytem
initialization calls like this:
```c
static int my_init(void)
{
...
}
```
This is achieved using a union:
```c
union init_function {
/* for SYS_INIT, used when init_entry.dev == NULL */
int (*sys)(void);
/* for DEVICE*, used when init_entry.dev != NULL */
int (*dev)(const struct device *dev);
};
struct init_entry {
/* stores init function (either for SYS_INIT or DEVICE*)
union init_function init_fn;
/* stores device pointer for DEVICE*, NULL for SYS_INIT. Allows
* to know which union entry to call.
*/
const struct device *dev;
}
```
This solution **does not increase ROM usage**, and allows to offer clean
public APIs for both SYS_INIT and DEVICE*. Note that however, init
machinery keeps a coupling with devices.
**NOTE**: This is a breaking change! All `SYS_INIT` functions will need
to be converted to the new signature. See the script offered in the
following commit.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
init: convert SYS_INIT functions to the new signature
Conversion scripted using scripts/utils/migrate_sys_init.py.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
manifest: update projects for SYS_INIT changes
Update modules with updated SYS_INIT calls:
- hal_ti
- lvgl
- sof
- TraceRecorderSource
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: devicetree: devices: adjust test
Adjust test according to the recently introduced SYS_INIT
infrastructure.
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
tests: kernel: threads: adjust SYS_INIT call
Adjust to the new signature: int (*init_fn)(void);
Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-10-19 09:33:44 +02:00
|
|
|
static int sys_clock_driver_init(void)
|
2021-07-07 20:50:43 +02:00
|
|
|
{
|
|
|
|
lptmr_config_t config;
|
|
|
|
|
|
|
|
|
|
|
|
LPTMR_GetDefaultConfig(&config);
|
|
|
|
config.timerMode = kLPTMR_TimerModeTimeCounter;
|
|
|
|
config.enableFreeRunning = false;
|
|
|
|
config.prescalerClockSource = LPTMR_CLK_SOURCE;
|
|
|
|
|
|
|
|
#if LPTMR_BYPASS_PRESCALER
|
|
|
|
config.bypassPrescaler = true;
|
|
|
|
#else /* LPTMR_BYPASS_PRESCALER */
|
|
|
|
config.bypassPrescaler = false;
|
|
|
|
config.value = LPTMR_PRESCALER;
|
|
|
|
#endif /* !LPTMR_BYPASS_PRESCALER */
|
|
|
|
|
|
|
|
LPTMR_Init(LPTMR_BASE, &config);
|
|
|
|
|
|
|
|
IRQ_CONNECT(LPTMR_IRQN, LPTMR_IRQ_PRIORITY, mcux_lptmr_timer_isr, NULL, 0);
|
|
|
|
irq_enable(LPTMR_IRQN);
|
|
|
|
|
|
|
|
LPTMR_EnableInterrupts(LPTMR_BASE, kLPTMR_TimerInterruptEnable);
|
|
|
|
LPTMR_SetTimerPeriod(LPTMR_BASE, CYCLES_PER_TICK);
|
|
|
|
LPTMR_StartTimer(LPTMR_BASE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2021-11-04 12:51:39 +01:00
|
|
|
|
|
|
|
SYS_INIT(sys_clock_driver_init, PRE_KERNEL_2,
|
|
|
|
CONFIG_SYSTEM_CLOCK_INIT_PRIORITY);
|