1 #include <stdint.h>
2 #include "pwmout_api.h"
3 #include "aos/hal/pwm.h"
4
5
6 static pwmout_t PWM[18];
7
8 u32 port2pin[18][2]={
9 {0, PA_12},
10 {1, PA_13},
11 {2, PA_23},
12 {3, PA_24},
13 {4, PA_25},
14 {5, PA_26},
15 {6, PA_28},
16 {7, PA_30},
17 {8, PB_4},
18 {9, PB_5},
19 {10, PB_18},
20 {11, PB_19},
21 {12, PB_20},
22 {13, PB_21},
23 {14, PB_22},
24 {15, PB_23},
25 {16, PB_24},
26 {17, PB_25}, //this channel also can be PB_7
27 };
28
29 extern void pwmout_init(pwmout_t* obj, PinName pin);
30 extern void pwmout_free(pwmout_t* obj);
31 extern void pwmout_write(pwmout_t* obj, float percent);
32 extern float pwmout_read(pwmout_t* obj);
33 extern void pwmout_period_us(pwmout_t* obj, int us);
34 extern void pwmout_start(pwmout_t* obj);
35 extern void pwmout_stop(pwmout_t* obj);
36
37 /**
38 * Initialises a PWM pin
39 *
40 *
41 * @param[in] pwm the PWM device
42 *
43 * @return 0 : on success, EIO : if an error occurred with any step
44 */
hal_pwm_init(pwm_dev_t * pwm)45 int32_t hal_pwm_init(pwm_dev_t *pwm)
46 {
47 int period_us;
48 period_us = 1000000/pwm->config.freq;
49
50 pwm->priv = &(PWM[pwm->port]);
51
52
53 pwmout_init( pwm->priv , port2pin[pwm->port][1]);
54
55 pwmout_period_us(pwm->priv , period_us);
56
57 pwmout_write(pwm->priv , pwm->config.duty_cycle);
58
59 return 0;
60
61 }
62
63 /**
64 * Starts Pulse-Width Modulation signal output on a PWM pin
65 *
66 * @param[in] pwm the PWM device
67 *
68 * @return 0 : on success, EIO : if an error occurred with any step
69 */
hal_pwm_start(pwm_dev_t * pwm)70 int32_t hal_pwm_start(pwm_dev_t *pwm)
71 {
72 pwmout_start(pwm->priv);
73 return 0;
74 }
75
76 /**
77 * Stops output on a PWM pin
78 *
79 * @param[in] pwm the PWM device
80 *
81 * @return 0 : on success, EIO : if an error occurred with any step
82 */
hal_pwm_stop(pwm_dev_t * pwm)83 int32_t hal_pwm_stop(pwm_dev_t *pwm)
84 {
85 pwmout_stop(pwm->priv);
86
87 return 0;
88 }
89
90 /**
91 * change the para of pwm
92 *
93 * @param[in] pwm the PWM device
94 * @param[in] para the para of pwm
95 *
96 * @return 0 : on success, EIO : if an error occurred with any step
97 */
hal_pwm_para_chg(pwm_dev_t * pwm,pwm_config_t para)98 int32_t hal_pwm_para_chg(pwm_dev_t *pwm, pwm_config_t para)
99 {
100 int period_us;
101 period_us = 1000000/para.freq;
102
103 pwmout_period_us(pwm->priv, period_us);
104
105 pwmout_write(pwm->priv, para.duty_cycle);
106 return 0;
107 }
108
109 /**
110 * De-initialises an PWM interface, Turns off an PWM hardware interface
111 *
112 * @param[in] pwm the interface which should be de-initialised
113 *
114 * @return 0 : on success, EIO : if an error occurred with any step
115 */
hal_pwm_finalize(pwm_dev_t * pwm)116 int32_t hal_pwm_finalize(pwm_dev_t *pwm)
117 {
118 pwmout_free(pwm->priv);
119 return 0;
120
121 }
122