1 /*
2  * Copyright (C) 2017-2019 Alibaba Group Holding Limited
3  */
4 
5 /******************************************************************************
6  * @file     drv_pwm.h
7  * @brief    header file for pwm driver
8  * @version  V1.0
9  * @date     19. Feb 2019
10  * @model    pwm
11  ******************************************************************************/
12 
13 #ifndef _CSI_PWM_H_
14 #define _CSI_PWM_H_
15 
16 
17 #include <stdint.h>
18 #include <drv_common.h>
19 
20 #ifdef __cplusplus
21 extern "C" {
22 #endif
23 
24 /// definition for pwm handle.
25 typedef void *pwm_handle_t;
26 
27 /****** PWM specific error codes *****/
28 typedef enum {
29     EDRV_PWM_MODE  = (DRV_ERROR_SPECIFIC + 1),     ///< Specified Mode not supported
30 } csi_pwm_error_e;
31 
32 /*----- PWM Input Capture Mode -----*/
33 typedef enum {
34     PWM_INPUT_MODE_EDGE_TIME  = 0,                 ///< Input Edge Time Mode
35     PWM_INPUT_MODE_EDGE_COUNT = 1                  ///< Input Edge Count Mode
36 } pwm_input_mode_e;
37 
38 typedef enum {
39     PWM_INPUT_EVENT_MODE_POSEDGE = 0,              ///< Posedge Edge
40     PWM_INPUT_EVENT_MODE_NEGEDGE = 1,              ///< Negedge Edge
41     PWM_INPUT_EVENT_MODE_BOTHEDGE = 2              ///< Both Edge
42 } pwm_input_event_mode_e;
43 
44 typedef struct {
45     pwm_input_mode_e input_mode;                   ///< Input Mode
46     pwm_input_event_mode_e event_mode;             ///< Input Event Mode
47     uint32_t count;                                ///< Capture Mode Count
48 } pwm_input_config_t;
49 
50 typedef enum {
51     PWM_CAPTURE_EVENT_COUNT = 0,                   ///< Capture Count Event
52     PWM_CAPTURE_EVENT_TIME  = 1,                   ///< Capture Time Event
53     PWM_TIMER_EVENT_TIMEOUT = 2                    ///< Timer Timeout Event
54 } pwm_event_e;
55 
56 typedef void (*pwm_event_cb_t)(int32_t ch, pwm_event_e event, uint32_t val);
57 
58 /**
59   \brief       Initialize PWM Interface. 1. Initializes the resources needed for the PWM interface 2.registers event callback function
60   \param[in]   idx pwm idx
61   \return      handle pwm handle to operate.
62 */
63 pwm_handle_t csi_pwm_initialize(uint32_t idx);
64 
65 /**
66   \brief       De-initialize PWM Interface. stops operation and releases the software resources used by the interface
67   \param[in]   handle pwm handle to operate.
68 */
69 void csi_pwm_uninitialize(pwm_handle_t handle);
70 /**
71   \brief       control pwm power.
72   \param[in]   handle  pwm handle to operate.
73   \param[in]   state   power state.\ref csi_power_stat_e.
74   \return      error code
75 */
76 int32_t csi_pwm_power_control(pwm_handle_t handle, csi_power_stat_e state);
77 
78 /**
79   \brief       config pwm mode.
80   \param[in]   handle           pwm handle to operate.
81   \param[in]   channel          channel num.
82   \param[in]   period_us        the PWM period in us
83   \param[in]   pulse_width_us   the PMW pulse width in us
84   \return      error code
85 */
86 int32_t csi_pwm_config(pwm_handle_t handle,
87                        uint8_t channel,
88                        uint32_t period_us,
89                        uint32_t pulse_width_us);
90 
91 /**
92   \brief       start generate pwm signal.
93   \param[in]   handle   pwm handle to operate.
94   \param[in]   channel  channel num.
95 */
96 void csi_pwm_start(pwm_handle_t handle, uint8_t channel);
97 
98 /**
99   \brief       stop generate pwm signal.
100   \param[in]   handle   pwm handle to operate.
101   \param[in]   channel  channel num.
102 */
103 void csi_pwm_stop(pwm_handle_t handle, uint8_t channel);
104 
105 /**
106   \brief       config pwm clock division.
107   \param[in]   handle   pwm handle to operate.
108   \param[in]   channel  channel num.
109   \param[in]   div      clock div.
110 */
111 void drv_pwm_config_clockdiv(pwm_handle_t handle, uint8_t channel, uint32_t div);
112 
113 /**
114   \brief       get pwm clock division.
115   \param[in]   handle   pwm handle to operate.
116   \param[in]   channel  channel num.
117   \return      clock div.
118 */
119 uint32_t drv_pwm_get_clockdiv(pwm_handle_t handle, uint8_t channel);
120 
121 /**
122   \brief       config pwm clock division.
123   \param[in]   handle   pwm handle to operate.
124   \param[in]   channel  channel num.
125   \param[in]   cb_event event callback.
126 */
127 void drv_pwm_config_cb(pwm_handle_t handle, uint8_t channel, pwm_event_cb_t cb_event);
128 
129 /**
130   \brief       set pwm timeout.
131   \param[in]   handle pwm handle to operate.
132   \param[in]   channel  channel num.
133   \param[in]   timeout the timeout value in microseconds(us).
134 */
135 void drv_pwm_timer_set_timeout(pwm_handle_t handle, uint8_t channel, uint32_t timeout);
136 
137 /**
138   \brief       start pwm timer.
139   \param[in]   handle pwm handle to operate.
140   \param[in]   channel  chnnel num.
141 */
142 void drv_pwm_timer_start(pwm_handle_t handle, uint8_t channel);
143 
144 /**
145   \brief       stop pWM timer.
146   \param[in]   handle pwm handle to operate.
147   \param[in]   channel  chnnel num.
148 */
149 void drv_pwm_timer_stop(pwm_handle_t handle, uint8_t channel);
150 
151 /**
152   \brief       get pwm timer current value
153   \param[in]   handle pwm handle to operate.
154   \param[in]   channel   channel num.
155   \param[out]  value     timer current value
156 */
157 void drv_pwm_timer_get_current_value(pwm_handle_t handle, uint8_t channel, uint32_t *value);
158 
159 /**
160   \brief       get pwm timer reload value
161   \param[in]   handle pwm handle to operate.
162   \param[in]   channel   channel num.
163   \param[out]  value    timer reload value
164 */
165 void drv_pwm_timer_get_load_value(pwm_handle_t handle, uint8_t channel, uint32_t *value);
166 
167 /**
168   \brief       config pwm capture mode.
169   \param[in]   handle   pwm handle to operate.
170   \param[in]   channel  channel num.
171   \param[in]   config   capture config.
172 */
173 void drv_pwm_capture_config(pwm_handle_t handle, uint8_t channel, pwm_input_config_t *config);
174 
175 /**
176   \brief       start pwm capture.
177   \param[in]   handle   pwm handle to operate.
178   \param[in]   channel  channel num.
179 */
180 void drv_pwm_capture_start(pwm_handle_t handle, uint8_t channel);
181 
182 /**
183   \brief       stop pwm capture.
184   \param[in]   handle   pwm handle to operate.
185   \param[in]   channel  channel num.
186 */
187 void drv_pwm_capture_stop(pwm_handle_t handle, uint8_t channel);
188 
189 #ifdef __cplusplus
190 }
191 #endif
192 
193 #endif /* _CSI_PWM_H_ */
194