1 /*
2  * Copyright 2024 Microchip Technology Inc. and its subsidiaries.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef _MEC_HTIMER_API_H
7 #define _MEC_HTIMER_API_H
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "device_mec5.h"
14 #include "mec_defs.h"
15 #include "mec_retval.h"
16 
17 /* Microchip MEC5 32KHz Hibernation Timer. 16-bit down counter */
18 
19 /* Interfaces to any C modules */
20 #ifdef __cplusplus
21 extern "C"
22 {
23 #endif
24 
25 #define MEC_HTIMER_MAIN_CLK_FREQ    32768u
26 #define MEC_HTIMER_COUNT_SIZE_BITS  16
27 #define MEC_HTIMER_COUNT_MAX        0xffffu
28 
29 enum rtimer_config {
30     MEC_HTMR_CFG_RESOLUTION_125MS_POS = 0,
31     MEC_HTMR_CFG_IEN_POS,
32 };
33 
34 enum htimer_status {
35     MEC_HTMR_STATUS_TERM_POS = 0, /* timer reached terminal value of 0 */
36 };
37 
38 struct mec_htimer_context {
39     struct mec_htmr_regs *regs;
40     uint32_t devi;
41     uint16_t preload;
42     uint16_t count;
43 };
44 
45 int mec_hal_htimer_init(struct mec_htmr_regs *regs, struct mec_htimer_context *ctx,
46                         uint8_t cfg_flags);
47 
48 void mec_hal_htimer_intr_ctrl(struct mec_htimer_context *ctx, uint8_t enable);
49 
50 uint32_t mec_hal_htimer_status(struct mec_htimer_context *ctx);
51 void mec_hal_htimer_status_clear(struct mec_htimer_context *ctx);
52 
53 void mec_hal_htimer_stop(struct mec_htmr_regs *regs);
54 
55 void mec_hal_htimer_halt(struct mec_htimer_context *ctx);
56 void mec_hal_htimer_unhalt(struct mec_htimer_context *ctx);
57 
58 void mec_hal_htimer_restart(struct mec_htimer_context *ctx, uint16_t new_count);
59 
60 /* Get read-only 16-bit counter value */
mec_hal_htimer_count(struct mec_htmr_regs * regs)61 static inline uint32_t mec_hal_htimer_count(struct mec_htmr_regs *regs)
62 {
63     return regs->COUNT;
64 }
65 
66 /* Get 16-bit preload value */
mec_hal_htimer_preload(struct mec_htmr_regs * regs)67 static inline uint32_t mec_hal_htimer_preload(struct mec_htmr_regs *regs)
68 {
69     return regs->PRELOAD;
70 }
71 
72 /* Write 16-bit preload value.
73  * Setting preload to a non-zero value causes the timer to copy
74  * preload into the count register and begin counting down.
75  * Writing 0 stops the counter and copies 0 into the count register.
76  */
mec_hal_htimer_preload_set(struct mec_htmr_regs * regs,uint16_t preload)77 static inline void mec_hal_htimer_preload_set(struct mec_htmr_regs *regs, uint16_t preload)
78 {
79     regs->PRELOAD = preload;
80 }
81 
82 
83 #ifdef __cplusplus
84 }
85 #endif
86 
87 #endif /* #ifndef _MEC_HTIMER_API_H */
88