1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "aos_hal_pwm.h"
6 #include "board_mgr.h"
7 #include "py/builtin.h"
8 #include "py/mperrno.h"
9 #include "py/obj.h"
10 #include "py/runtime.h"
11 #include "ulog/ulog.h"
12
13 #define LOG_TAG "DRIVER_PWM"
14
15 extern const mp_obj_type_t driver_pwm_type;
16
17 // this is the actual C-structure for our new object
18 typedef struct {
19 // base represents some basic information, like type
20 mp_obj_base_t Base;
21 // a member created by us
22 char *ModuleName;
23 item_handle_t pwm_handle;
24 } mp_pwm_obj_t;
25
pwm_obj_print(const mp_print_t * print,mp_obj_t self_in,mp_print_kind_t kind)26 void pwm_obj_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind)
27 {
28 mp_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
29 mp_printf(print, "ModuleName(%s)", self->ModuleName);
30 }
31
pwm_obj_make_new(const mp_obj_type_t * type,size_t n_args,size_t n_kw,const mp_obj_t * args)32 STATIC mp_obj_t pwm_obj_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args)
33 {
34 mp_pwm_obj_t *driver_obj = m_new_obj(mp_pwm_obj_t);
35 if (!driver_obj) {
36 mp_raise_OSError(MP_ENOMEM);
37 }
38
39 driver_obj->Base.type = &driver_pwm_type;
40 driver_obj->ModuleName = "pwm";
41 driver_obj->pwm_handle.handle = NULL;
42
43 return MP_OBJ_FROM_PTR(driver_obj);
44 }
45
obj_open(size_t n_args,const mp_obj_t * args)46 STATIC mp_obj_t obj_open(size_t n_args, const mp_obj_t *args)
47 {
48 int ret = -1;
49 pwm_dev_t *pwm_device = NULL;
50
51 if (n_args < 2) {
52 LOGE(LOG_TAG, "args num is illegal :n_args = %d;\n", n_args);
53 mp_raise_OSError(MP_EINVAL);
54 }
55
56 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
57 mp_pwm_obj_t *driver_obj = (mp_pwm_obj_t *)self;
58 if (driver_obj == NULL) {
59 LOGE(LOG_TAG, "driver_obj is NULL\n");
60 mp_raise_OSError(MP_EINVAL);
61 }
62
63 char *id = (char *)mp_obj_str_get_str(args[1]);
64 if (id == NULL) {
65 LOGE(LOG_TAG, "illegal id: %s;\n", id);
66 mp_raise_OSError(MP_EINVAL);
67 }
68
69 ret = py_board_mgr_init();
70 if (ret != 0) {
71 LOGE(LOG_TAG, "py_board_mgr_init failed\n");
72 return MP_ROM_INT(ret);
73 ;
74 }
75
76 ret = py_board_attach_item(MODULE_PWM, id, &(driver_obj->pwm_handle));
77 if (ret != 0) {
78 LOGE(LOG_TAG, "py_board_attach_item failed ret = %d;\n", ret);
79 goto out;
80 }
81
82 pwm_device = py_board_get_node_by_handle(MODULE_PWM, &(driver_obj->pwm_handle));
83 if (NULL == pwm_device) {
84 LOGE(LOG_TAG, "py_board_get_node_by_handle failed;\n");
85 goto out;
86 }
87 ret = aos_hal_pwm_init(pwm_device);
88 return MP_ROM_INT(ret);
89
90 out:
91 if (0 != ret) {
92 LOGE(LOG_TAG, "%s: aos_hal_pwm_init failed ret = %d;\n", __func__, ret);
93 py_board_disattach_item(MODULE_PWM, &(driver_obj->pwm_handle));
94 }
95
96 return MP_ROM_INT(ret);
97 }
98 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pwm_obj_open, 2, obj_open);
99
obj_close(size_t n_args,const mp_obj_t * args)100 STATIC mp_obj_t obj_close(size_t n_args, const mp_obj_t *args)
101 {
102 int ret = -1;
103 pwm_dev_t *pwm_device = NULL;
104 if (n_args < 1) {
105 LOGE(LOG_TAG, "args num is illegal :n_args = %d;\n", n_args);
106 return mp_const_none;
107 }
108 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
109 mp_pwm_obj_t *driver_obj = (mp_pwm_obj_t *)self;
110 if (driver_obj == NULL) {
111 LOGE(LOG_TAG, "driver_obj is NULL\n");
112 return MP_ROM_INT(-MP_EINVAL);
113 }
114
115 pwm_device = py_board_get_node_by_handle(MODULE_PWM, &(driver_obj->pwm_handle));
116 if (NULL == pwm_device) {
117 LOGE(LOG_TAG, "py_board_get_node_by_handle failed;\n");
118 return mp_const_none;
119 }
120
121 ret = aos_hal_pwm_stop(pwm_device);
122 ret |= aos_hal_pwm_finalize(pwm_device);
123 py_board_disattach_item(MODULE_PWM, &(driver_obj->pwm_handle));
124
125 return MP_ROM_INT(ret);
126 }
127 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pwm_obj_close, 1, obj_close);
128
obj_setOption(size_t n_args,const mp_obj_t * args)129 STATIC mp_obj_t obj_setOption(size_t n_args, const mp_obj_t *args)
130 {
131 int ret = -1;
132 pwm_dev_t *pwm_device = NULL;
133 if (n_args < 2) {
134 LOGE(LOG_TAG, "args num is illegal :n_args = %d;\n", n_args);
135 return MP_ROM_INT(-MP_EINVAL);
136 }
137 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
138 mp_pwm_obj_t *driver_obj = (mp_pwm_obj_t *)self;
139 if (driver_obj == NULL) {
140 LOGE(LOG_TAG, "driver_obj is NULL\n");
141 return MP_ROM_INT(-MP_EINVAL);
142 }
143
144 pwm_device = py_board_get_node_by_handle(MODULE_PWM, &(driver_obj->pwm_handle));
145 if (NULL == pwm_device) {
146 LOGE(LOG_TAG, "py_board_get_node_by_handle failed;\n");
147 goto out;
148 }
149
150 if (!mp_obj_is_dict_or_ordereddict(args[1])) {
151 LOGE(LOG_TAG, "%s param1 type error,param type must be dict \r\n",
152 __func__);
153 goto out;
154 }
155
156 mp_obj_t index = mp_obj_new_str_via_qstr("duty", 4);
157 pwm_device->config.duty_cycle = (float)mp_obj_get_int(mp_obj_dict_get(args[1], index)) / 100.0;
158
159 index = mp_obj_new_str_via_qstr("freq", 4);
160 pwm_device->config.freq = mp_obj_get_int(mp_obj_dict_get(args[1], index));
161
162 ret = aos_hal_pwm_stop(pwm_device);
163 if (ret != 0) {
164 LOGE(LOG_TAG, "aos_hal_pwm_stop failed\n");
165 goto out;
166 }
167
168
169 pwm_config_t para;
170 para.duty_cycle = pwm_device->config.duty_cycle;
171 para.freq = pwm_device->config.freq;
172
173 ret = aos_hal_pwm_para_chg(pwm_device, para);
174 if (ret != 0) {
175 LOGE(LOG_TAG, "amp hal pwm init failed\n");
176 goto out;
177 }
178
179 ret = aos_hal_pwm_start(pwm_device);
180 if (ret != 0) {
181 LOGE(LOG_TAG, "aos_hal_pwm_start failed\n");
182 }
183 out:
184 return MP_ROM_INT(ret);
185 }
186 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pwm_obj_setOption, 2, obj_setOption);
187
188
obj_getOption(size_t n_args,const mp_obj_t * args)189 STATIC mp_obj_t obj_getOption(size_t n_args, const mp_obj_t *args)
190 {
191 int ret = -1;
192 pwm_dev_t *pwm_device = NULL;
193 if (n_args < 1) {
194 LOGE(LOG_TAG, "args num is illegal :n_args = %d;\n", n_args);
195 return MP_ROM_INT(-MP_EINVAL);
196 }
197 mp_obj_base_t *self = (mp_obj_base_t *)MP_OBJ_TO_PTR(args[0]);
198 mp_pwm_obj_t *driver_obj = (mp_pwm_obj_t *)self;
199 if (driver_obj == NULL) {
200 LOGE(LOG_TAG, "driver_obj is NULL\n");
201 return MP_ROM_INT(-MP_EINVAL);
202 }
203
204 pwm_device = py_board_get_node_by_handle(MODULE_PWM, &(driver_obj->pwm_handle));
205 if (NULL == pwm_device) {
206 LOGE(LOG_TAG, "py_board_get_node_by_handle failed;\n");
207 return MP_ROM_INT(-MP_EINVAL);
208 }
209 mp_obj_t dict = MP_OBJ_NULL;
210 dict = mp_obj_new_dict(2);
211 mp_obj_dict_store(dict, mp_obj_new_str("freq", 4),
212 mp_obj_new_int((int)(pwm_device->config.freq)));
213 mp_obj_dict_store(dict, mp_obj_new_str("duty", 4),
214 mp_obj_new_int((int)(pwm_device->config.duty_cycle * 100)));
215
216 return dict;
217 }
218 STATIC MP_DEFINE_CONST_FUN_OBJ_VAR(pwm_obj_getOption, 1, obj_getOption);
219
220 STATIC const mp_rom_map_elem_t pwm_locals_dict_table[] = {
221 { MP_OBJ_NEW_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_PWM) },
222 { MP_ROM_QSTR(MP_QSTR_open), MP_ROM_PTR(&pwm_obj_open) },
223 { MP_ROM_QSTR(MP_QSTR_close), MP_ROM_PTR(&pwm_obj_close) },
224 { MP_ROM_QSTR(MP_QSTR_getOption), MP_ROM_PTR(&pwm_obj_getOption) },
225 { MP_ROM_QSTR(MP_QSTR_setOption), MP_ROM_PTR(&pwm_obj_setOption) },
226
227 };
228
229 STATIC MP_DEFINE_CONST_DICT(pwm_locals_dict, pwm_locals_dict_table);
230
231 const mp_obj_type_t driver_pwm_type = {
232 .base = { &mp_type_type },
233 .name = MP_QSTR_PWM,
234 .print = pwm_obj_print,
235 .make_new = pwm_obj_make_new,
236 .locals_dict = (mp_obj_dict_t *)&pwm_locals_dict,
237 };
238