1 /*
2  * Copyright 2024 Microchip Technology Inc. and its subsidiaries.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef _MEC_PWM_API_H
7 #define _MEC_PWM_API_H
8 
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 
13 #include "mec_defs.h"
14 #include "mec_retval.h"
15 
16 /* Interfaces to any C modules */
17 #ifdef __cplusplus
18 extern "C"
19 {
20 #endif
21 
22 /* PWM Controller */
23 
24 struct mec_pwm_regs;
25 
26 enum mec5_pwm_config {
27     MEC5_PWM_CFG_ENABLE = MEC_BIT(0),
28     MEC5_PWM_CFG_INVERT = MEC_BIT(2),
29     MEC5_PWM_CFG_RESET  = MEC_BIT(3),
30 };
31 
32 /* Return PWM frequency input for hi range and lo range */
33 uint32_t mec_hal_pwm_hi_freq_input(void);
34 uint32_t mec_hal_pwm_lo_freq_input(void);
35 
36 /* Initialze a PWM instance
37  * period_cycles is the number of PWM input frequency cycles in the desired
38  * PWM output period.
39  * pulse_cycles is the number of PMW input frequency cycles in the desired
40  * PWM active pulse width.
41  * flags - Enable after configuration, use low frequeny range instead of high
42  * range, invert output (active output is low instead of high), or reset the
43  * PWM using PCR peripheral reset before configuration.
44  */
45 int mec_hal_pwm_init(struct mec_pwm_regs *regs, uint32_t period_cycles,
46                      uint32_t pulse_cycles, uint32_t flags);
47 
48 int mec_hal_pwm_reset(struct mec_pwm_regs *regs);
49 
50 int mec_hal_pwm_set_polarity(struct mec_pwm_regs *regs, uint8_t polarity_inverted);
51 
52 int mec_hal_pwm_enable(struct mec_pwm_regs *regs, uint8_t enable);
53 int mec_hal_pwm_is_enabled(struct mec_pwm_regs *regs);
54 
55 /* set output to inactive state based upon invert bit */
56 int mec_hal_pwm_off(struct mec_pwm_regs *regs);
57 /* set output to active state based upon invert bit */
58 int mec_hal_pwm_on(struct mec_pwm_regs *regs);
59 
60 uint32_t mec_hal_pwm_get_freq_in(struct mec_pwm_regs *regs);
61 uint32_t mec_hal_pwm_get_count(struct mec_pwm_regs *regs, uint8_t on_count);
62 uint32_t mec_hal_pwm_get_freq_out(struct mec_pwm_regs *regs);
63 int mec_hal_pwm_set_freq_out(struct mec_pwm_regs *regs, uint32_t period_cycles,
64                              uint32_t pulse_cycles);
65 
66 #ifdef __cplusplus
67 }
68 #endif
69 
70 #endif /* #ifndef _MEC_PWM_API_H */
71