Zephyr API Documentation  2.7.0-rc2
A Scalable Open Source RTOS
timer.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2021 IoT.bzh
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7#ifndef ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_CORTEX_A_R_TIMER_H_
8#define ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_CORTEX_A_R_TIMER_H_
9
10#ifdef CONFIG_ARM_ARCH_TIMER
11
12#ifndef _ASMLANGUAGE
13
15#include <sys/device_mmio.h>
16#include <zephyr/types.h>
17
18#ifdef __cplusplus
19extern "C" {
20#endif
21
22#define ARM_ARCH_TIMER_BASE DT_REG_ADDR_BY_IDX(ARM_TIMER_NODE, 0)
23#define ARM_ARCH_TIMER_IRQ ARM_TIMER_VIRTUAL_IRQ
24#define ARM_ARCH_TIMER_PRIO ARM_TIMER_VIRTUAL_PRIO
25#define ARM_ARCH_TIMER_FLAGS ARM_TIMER_VIRTUAL_FLAGS
26
27#define TIMER_CNT_LOWER 0x00
28#define TIMER_CNT_UPPER 0x04
29#define TIMER_CTRL 0x08
30#define TIMER_ISR 0x0c
31#define TIMER_CMP_LOWER 0x10
32#define TIMER_CMP_UPPER 0x14
33
34#define TIMER_IRQ_ENABLE BIT(2)
35#define TIMER_COMP_ENABLE BIT(1)
36#define TIMER_ENABLE BIT(0)
37
39
40#define TIMER_REG_GET(offs) (DEVICE_MMIO_TOPLEVEL_GET(timer_regs) + offs)
41
42static ALWAYS_INLINE void arm_arch_timer_init(void)
43{
45}
46
48{
49 uint32_t lower = (uint32_t)val;
50 uint32_t upper = (uint32_t)(val >> 32);
51 uint32_t ctrl;
52
53 /* Disable IRQ and comparator */
54 ctrl = sys_read32(TIMER_REG_GET(TIMER_CTRL));
55 ctrl &= ~(TIMER_COMP_ENABLE | TIMER_IRQ_ENABLE);
56 sys_write32(ctrl, TIMER_REG_GET(TIMER_CTRL));
57
58 sys_write32(lower, TIMER_REG_GET(TIMER_CMP_LOWER));
59 sys_write32(upper, TIMER_REG_GET(TIMER_CMP_UPPER));
60
61 /* enable comparator back, let set_irq_mask enabling the IRQ again */
62 ctrl |= TIMER_COMP_ENABLE;
63 sys_write32(ctrl, TIMER_REG_GET(TIMER_CTRL));
64}
65
66static ALWAYS_INLINE void arm_arch_timer_enable(bool enable)
67{
68 uint32_t ctrl;
69
70 ctrl = sys_read32(TIMER_REG_GET(TIMER_CTRL));
71 if (enable) {
72 ctrl |= TIMER_ENABLE;
73 } else {
74 ctrl &= ~TIMER_ENABLE;
75 }
76
77 sys_write32(ctrl, TIMER_REG_GET(TIMER_CTRL));
78}
79
80static ALWAYS_INLINE void arm_arch_timer_set_irq_mask(bool mask)
81{
82 uint32_t ctrl;
83
84 ctrl = sys_read32(TIMER_REG_GET(TIMER_CTRL));
85 if (mask) {
86 ctrl &= ~TIMER_IRQ_ENABLE;
87 } else {
88 ctrl |= TIMER_IRQ_ENABLE;
89 sys_write32(1, TIMER_REG_GET(TIMER_ISR));
90 }
91 sys_write32(ctrl, TIMER_REG_GET(TIMER_CTRL));
92}
93
95{
96 uint32_t lower;
97 uint32_t upper, upper_saved;
98
99 /* To get the value from the Global Timer Counter register proceed
100 * as follows:
101 * 1. Read the upper 32-bit timer counter register.
102 * 2. Read the lower 32-bit timer counter register.
103 * 3. Read the upper 32-bit timer counter register again. If the value
104 * is different to the 32-bit upper value read previously,
105 * go back to step 2.
106 * Otherwise the 64-bit timer counter value is correct.
107 */
108 upper = sys_read32(TIMER_REG_GET(TIMER_CNT_UPPER));
109 do {
110 upper_saved = upper;
111 lower = sys_read32(TIMER_REG_GET(TIMER_CNT_LOWER));
112 upper = sys_read32(TIMER_REG_GET(TIMER_CNT_UPPER));
113 } while (upper != upper_saved);
114
115 return ((uint64_t)upper) << 32 | lower;
116}
117
118#ifdef __cplusplus
119}
120#endif
121
122#endif /* _ASMLANGUAGE */
123
124#endif /* CONFIG_ARM_ARCH_TIMER */
125
126#endif /* ZEPHYR_INCLUDE_ARCH_ARM_AARCH32_CORTEX_A_R_TIMER_H_ */
static ALWAYS_INLINE uint64_t arm_arch_timer_count(void)
Definition: timer.h:62
static ALWAYS_INLINE void arm_arch_timer_set_irq_mask(bool mask)
Definition: timer.h:47
static ALWAYS_INLINE void arm_arch_timer_set_compare(uint64_t val)
Definition: timer.h:27
static ALWAYS_INLINE void arm_arch_timer_enable(unsigned char enable)
Definition: timer.h:32
static ALWAYS_INLINE void arm_arch_timer_init(void)
Definition: timer.h:23
#define ARM_TIMER_NODE
Definition: arm_arch_timer.h:14
#define ALWAYS_INLINE
Definition: common.h:116
#define DEVICE_MMIO_TOPLEVEL_MAP(name, flags)
Set up memory for a driver'sMMIO region.
Definition: device_mmio.h:652
#define K_MEM_CACHE_NONE
Definition: mem_manage.h:18
DEVICE_MMIO_TOPLEVEL_STATIC(foo4, DT_DRV_INST(4))
__UINT32_TYPE__ uint32_t
Definition: stdint.h:60
__UINT64_TYPE__ uint64_t
Definition: stdint.h:61
static ALWAYS_INLINE void sys_write32(uint32_t data, mem_addr_t addr)
Definition: sys-io-common.h:70
static ALWAYS_INLINE uint32_t sys_read32(mem_addr_t addr)
Definition: sys-io-common.h:59