1 /*
2  * Copyright (C) 2017-2020 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       9. Oct 2020
10  * @model      pwm
11  ******************************************************************************/
12 
13 #ifndef _DRV_PWM_H_
14 #define _DRV_PWM_H_
15 
16 #include <drv/common.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 typedef enum {
23     PWM_POLARITY_HIGH = 0U,                          ///< High level
24     PWM_POLARITY_LOW                                 ///< Low level
25 } csi_pwm_polarity_t;
26 
27 typedef enum {
28     PWM_CAPTURE_POLARITY_POSEDGE = 0U,               ///< Posedge Edge
29     PWM_CAPTURE_POLARITY_NEGEDGE,                    ///< Negedge Edge
30     PWM_CAPTURE_POLARITY_BOTHEDGE                    ///< Both Edge
31 } csi_pwm_capture_polarity_t;
32 
33 typedef enum {
34     PWM_EVENT_CAPTURE_POSEDGE = 0U,                  ///< Capture Posedge Event
35     PWM_EVENT_CAPTURE_NEGEDGE,                       ///< Capture Negedge Event
36     PWM_EVENT_CAPTURE_BOTHEDGE,                      ///< Capture Bothedge Event
37     PWM_EVENT_ERROR,                                 ///< Error
38 } csi_pwm_event_t;
39 
40 typedef struct csi_pwm csi_pwm_t;
41 
42 struct csi_pwm {
43     csi_dev_t      dev;
44     void (*callback)(csi_pwm_t *pwm, csi_pwm_event_t event, uint32_t ch, uint32_t time_us, void *arg);
45     void           *arg;
46     void           *priv;
47 };
48 
49 /**
50   \brief       Initialize PWM interface. Initializes the resources needed for the PWM interface
51   \param[in]   pwm    Handle to operate
52   \param[in]   idx    PWM idx
53   \return      Error code \ref csi_error_t
54 */
55 csi_error_t csi_pwm_init(csi_pwm_t *pwm, uint32_t idx);
56 
57 /**
58   \brief       De-initialize PWM interface. Stops operation and releases the software resources used by the interface
59   \param[in]   pwm    Handle to operate
60   \return      None
61 */
62 void csi_pwm_uninit(csi_pwm_t *pwm);
63 
64 /**
65   \brief       Config PWM out mode
66   \param[in]   pwm               Handle to operate
67   \param[in]   channel           Channel num
68   \param[in]   period_us         The PWM period in us
69   \param[in]   pulse_width_us    The PMW pulse width in us
70   \param[in]   polarity          The PWM polarity \ref csi_pwm_polarity_t
71   \return      Error code \ref csi_error_t
72 */
73 csi_error_t csi_pwm_out_config(csi_pwm_t *pwm,
74                                uint32_t channel,
75                                uint32_t period_us,
76                                uint32_t pulse_width_us,
77                                csi_pwm_polarity_t polarity);
78 
79 /**
80   \brief       Start generate PWM signal
81   \param[in]   pwm        Handle to operate
82   \param[in]   channel    Channel num
83   \return      Error code \ref csi_error_t
84 */
85 csi_error_t csi_pwm_out_start(csi_pwm_t *pwm, uint32_t channel);
86 
87 /**
88   \brief       Stop generate PWM signal
89   \param[in]   pwm        Handle to operate
90   \param[in]   channel    Channel num
91   \return      None
92 */
93 void csi_pwm_out_stop(csi_pwm_t *pwm, uint32_t channel);
94 
95 /**
96   \brief       Config PWM capture mode
97   \param[in]   pwm         Handle to operate
98   \param[in]   channel     Channel num
99   \param[in]   polarity    PWM capture polarity \ref csi_pwm_capture_polarity_t
100   \param[in]   count       PWM capture polarity count
101   \return      Error code \ref csi_error_t
102 */
103 csi_error_t csi_pwm_capture_config(csi_pwm_t *pwm,
104                                    uint32_t channel,
105                                    csi_pwm_capture_polarity_t polarity,
106                                    uint32_t count);
107 
108 /**
109   \brief       Start PWM capture
110   \param[in]   pwm        Handle to operate
111   \param[in]   channel    Channel num
112   \return      Error code \ref csi_error_t
113 */
114 csi_error_t csi_pwm_capture_start(csi_pwm_t *pwm, uint32_t channel);
115 
116 /**
117   \brief       Stop PWM capture
118   \param[in]   pwm        Handle to operate
119   \param[in]   channel    Channel num
120   \return      None
121 */
122 void csi_pwm_capture_stop(csi_pwm_t *pwm, uint32_t channel);
123 
124 /**
125   \brief       Attach PWM callback
126   \param[in]   pwm         Handle to operate
127   \param[in]   callback    Callback func
128   \param[in]   arg         Callback's param
129   \return      Error code \ref csi_error_t
130 */
131 csi_error_t csi_pwm_attach_callback(csi_pwm_t *pwm, void *callback, void *arg);
132 
133 /**
134   \brief       Detach PWM callback
135   \param[in]   pwm    Handle to operate
136   \return      None
137 */
138 void csi_pwm_detach_callback(csi_pwm_t *pwm);
139 
140 /**
141   \brief       Enable PWM power manage
142   \param[in]   pwm    Handle to operate
143   \return      Error code \ref csi_error_t
144 */
145 csi_error_t csi_pwm_enable_pm(csi_pwm_t *pwm);
146 
147 /**
148   \brief       Disable PWM power manage
149   \param[in]   pwm    Handle to operate
150   \return      None
151 */
152 void csi_pwm_disable_pm(csi_pwm_t *pwm);
153 
154 #ifdef __cplusplus
155 }
156 #endif
157 
158 #endif /* _DRV_PWM_H_ */
159