1 /*
2  * Copyright 2018-2019 NXP
3  * All rights reserved.
4  *
5  *
6  * SPDX-License-Identifier: BSD-3-Clause
7  */
8 
9 #ifndef _PWM_H_
10 #define _PWM_H_
11 
12 /*!
13  * @addtogroup PWM_Adapter
14  * @{
15  */
16 
17 /************************************************************************************
18 *************************************************************************************
19 * Include
20 *************************************************************************************
21 ***********************************************************************************/
22 
23 /************************************************************************************
24 *************************************************************************************
25 * Public types
26 *************************************************************************************
27 ************************************************************************************/
28 /*! @brief Hal pwm mode. */
29 typedef enum _hal_pwm_mode
30 {
31     kHAL_EdgeAlignedPwm = 0U, /*!< Edge aligned PWM */
32     kHAL_CenterAlignedPwm,    /*!< Center aligned PWM */
33 } hal_pwm_mode_t;
34 
35 /*! @brief PWM output pulse level select: high-true, low-true or no output */
36 typedef enum _hal_pwm_level_select
37 {
38     kHAL_PwmNoPwmSignal = 0U, /*!< No PWM output on pin */
39     kHAL_PwmLowTrue,          /*!< Low true pulses */
40     kHAL_PwmHighTrue,         /*!< High true pulses */
41 } hal_pwm_level_select_t;
42 
43 /*! @brief Hal pwm status. */
44 typedef enum _hal_pwm_status
45 {
46     kStatus_HAL_PwmSuccess     = kStatus_Success,                      /*!< Success */
47     kStatus_HAL_PwmFail        = MAKE_STATUS(kStatusGroup_HAL_PWM, 1), /*!< Failure*/
48     kStatus_HAL_PwmNotSupport  = MAKE_STATUS(kStatusGroup_HAL_PWM, 2), /*!< Not support*/
49     kStatus_HAL_PwmOutOfRanger = MAKE_STATUS(kStatusGroup_HAL_PWM, 3), /*!< Pwm is Out Of Ranger */
50 } hal_pwm_status_t;
51 
52 /*! @brief hal pwm configuration structure for hal pwm setting. */
53 typedef struct _hal_pwm_setup_config
54 {
55     hal_pwm_level_select_t level; /*!< PWM output pulse level select */
56     hal_pwm_mode_t mode;          /*!< PWM mode select */
57     uint32_t pwmFreq_Hz;          /*!< PWM frequency */
58     uint8_t dutyCyclePercent;     /*!< PWM duty cycle percent */
59 } hal_pwm_setup_config_t;
60 
61 /*! @brief Hal pwm handle size. */
62 #define HAL_PWM_HANDLE_SIZE (8U)
63 
64 /*! @brief Hal pwm handle. */
65 typedef void *hal_pwm_handle_t;
66 
67 /*!
68  * @brief Defines the PMW handle
69  *
70  * This macro is used to define a 4 byte aligned PWM handle.
71  * Then use "(hal_pwm_handle_t)name" to get the PWM handle.
72  *
73  * The macro should be global and could be optional. You could also define PWM handle by yourself.
74  *
75  * This is an example,
76  * @code
77  *   HAL_PWM_HANDLE_DEFINE(pwmHandle);
78  * @endcode
79  *
80  * @param name The name string of the PMW handle.
81  */
82 #define HAL_PWM_HANDLE_DEFINE(name) uint32_t name[(HAL_PWM_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t)]
83 
84 /************************************************************************************
85 *************************************************************************************
86 * Public prototypes
87 *************************************************************************************
88 ************************************************************************************/
89 #if defined(__cplusplus)
90 extern "C" {
91 #endif /* _cplusplus */
92 
93 /*!
94  * @brief Initializes the pwm adapter module for a pwm basic operation.
95  *
96  * @note This API should be called at the beginning of the application using the pwm adapter.
97  *
98  * Example below shows how to use this API to configure the PWM.
99  * @code
100  *   HAL_PWM_HANDLE_DEFINE(pwmHandle);
101  *   HAL_PwmInit((hal_pwm_handle_t)pwmHandle, BOARD_PWM_INSTANCE, BOARD_PWM_SOURCE_CLOCK);
102  * @endcode
103  *
104  * @param  halPwmHandle Hal pwm adapter handle, the handle buffer with size #HAL_PWM_HANDLE_SIZE should be
105  * allocated at upper level
106  * The handle should be 4 byte aligned, because unaligned access doesn't be supported on some devices.
107  * You can define the handle in the following two ways:
108  * #HAL_PWM_HANDLE_DEFINE(halPwmHandle);
109  * or
110  * uint32_t halPwmHandle[((HAL_PWM_HANDLE_SIZE + sizeof(uint32_t) - 1U) / sizeof(uint32_t))];
111  * @param  instance The instance index of the hardware PWM. For example, if FTM is used as the PWM hardware,
112  * 0 should be set to "instance" to use FTM0; 2 should be set to "instance" to use FTM2
113  * detail information please refer to the SOC corresponding RM.
114  * Invalid instance value will cause initialization failure.
115  * @param  srcClock_Hz Frequency of source clock of the pwm module
116  * @retval kStatus_HAL_PwmSuccess pwm initialization succeed
117  *
118  */
119 hal_pwm_status_t HAL_PwmInit(hal_pwm_handle_t halPwmHandle, uint8_t instance, uint32_t srcClock_Hz);
120 
121 /*!
122  * @brief DeInitilizate the pwm adapter module.
123  *
124  * @note This API should be called when not using the pwm adapter driver anymore.
125  *
126  * @param halPwmHandle     Hal pwm adapter handle
127  */
128 void HAL_PwmDeinit(hal_pwm_handle_t halPwmHandle);
129 
130 /*!
131  * @brief setup pwm.
132  *
133  * @note This API should be called when setup the pwm.
134  *
135  * @param halPwmHandle         Hal pwm adapter handle
136  * @param channel             Channel of pwm
137  * @param setupConfig         A pointer to the HAL pwm setup configuration structure
138  * @retval kStatus_HAL_PwmSuccess pwm setup succeed
139  */
140 hal_pwm_status_t HAL_PwmSetupPwm(hal_pwm_handle_t halPwmHandle, uint8_t channel, hal_pwm_setup_config_t *setupConfig);
141 
142 /*!
143  * @brief Update duty cycle of pwm.
144  *
145  * @note This API should be called when need update duty cycle.
146  *
147  * @param halPwmHandle         Hal pwm adapter handle
148  * @param channel             Channel of pwm
149  * @param mode                 PWM mode select
150  * @param dutyCyclePercent     PWM duty cycle percent
151  * @retval kStatus_HAL_PwmSuccess pwm Update duty cycle succeed
152  */
153 hal_pwm_status_t HAL_PwmUpdateDutycycle(hal_pwm_handle_t halPwmHandle,
154                                         uint8_t channel,
155                                         hal_pwm_mode_t mode,
156                                         uint8_t dutyCyclePercent);
157 
158 #if defined(__cplusplus)
159 }
160 #endif
161 /*! @}*/
162 #endif /* _PWM_H_ */
163