machine_pwm.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <string.h>
  29. #include "py/nlr.h"
  30. #include "py/runtime.h"
  31. #include "modmachine.h"
  32. #include "mphalport.h"
  33. #ifdef MICROPYTHON_USING_MACHINE_PWM
  34. #include <rtthread.h>
  35. #include <drivers/rt_drv_pwm.h>
  36. #define MP_PWM_PULSE_MAX 255
  37. #define MP_PWM_PERIOD_GET(freq) (1000000000 / (freq))
  38. #define MP_PWM_PULSE_GET(period, duty) ((period) / MP_PWM_PULSE_MAX * (duty))
  39. extern const mp_obj_type_t machine_pwm_type;
  40. typedef struct _machine_pwm_obj_t {
  41. mp_obj_base_t base;
  42. struct rt_device_pwm *pwm_device;
  43. char dev_name[RT_NAME_MAX];
  44. uint8_t is_init;
  45. int8_t id;
  46. uint8_t channel;
  47. uint8_t duty;
  48. uint32_t freq;
  49. } machine_pwm_obj_t;
  50. STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  51. machine_pwm_obj_t *self = self_in;
  52. mp_printf(print, "PWM(%p; ", self);
  53. if (self->id >= 0) {
  54. mp_printf(print, "pwm_id=%d, ", self->id);
  55. } else {
  56. mp_printf(print, "pwm_name=%s, ", self->dev_name);
  57. }
  58. mp_printf(print, "channel=%d, ", self->channel);
  59. mp_printf(print, "freq=%d, ", self->freq);
  60. mp_printf(print, "duty=%d)", self->duty);
  61. }
  62. STATIC void error_check(bool status, const char *msg) {
  63. if (!status) {
  64. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  65. }
  66. }
  67. STATIC void machine_pwm_init_helper(machine_pwm_obj_t *self,
  68. size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  69. rt_err_t result = RT_EOK;
  70. uint32_t period = 0, pulse = 0;
  71. char pwm_dev_name[RT_NAME_MAX];
  72. struct rt_device_pwm *pwm_device = RT_NULL;
  73. enum { ARG_channel, ARG_freq, ARG_duty };
  74. static const mp_arg_t allowed_args[] = {
  75. { MP_QSTR_channel, MP_ARG_INT, {.u_int = 0} },
  76. { MP_QSTR_freq, MP_ARG_INT, {.u_int = 1} },
  77. { MP_QSTR_duty, MP_ARG_INT, {.u_int = 0} },
  78. };
  79. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  80. mp_arg_parse_all(n_args, pos_args, kw_args,
  81. MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  82. int tval = args[ARG_channel].u_int;
  83. if ((tval < 0) || (tval > 4)) {
  84. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  85. "Bad channel %d", tval));
  86. }
  87. self->channel = tval;
  88. tval = args[ARG_freq].u_int;
  89. if ((tval < 1) || (tval > 156250)) {
  90. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  91. "Bad frequency %d", tval));
  92. }
  93. self->freq = tval;
  94. tval = args[ARG_duty].u_int;
  95. if ((tval < 0) || (tval > 255)) {
  96. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  97. "Bad duty %d", tval));
  98. }
  99. self->duty = tval;
  100. if (self->id >= 0) {
  101. rt_snprintf(pwm_dev_name, sizeof(pwm_dev_name), "pwm%d", self->id);
  102. } else {
  103. rt_strncpy(pwm_dev_name, self->dev_name, RT_NAME_MAX);
  104. }
  105. pwm_device = (struct rt_device_pwm *) rt_device_find(pwm_dev_name);
  106. if (pwm_device == RT_NULL || pwm_device->parent.type != RT_Device_Class_Miscellaneous) {
  107. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  108. "PWM(%s) don't exist", pwm_dev_name));
  109. }
  110. self->pwm_device = pwm_device;
  111. // get period number by frequency
  112. period = MP_PWM_PERIOD_GET(self->freq);
  113. // get pulse number by duty
  114. pulse = MP_PWM_PULSE_GET(period, self->duty);
  115. result = rt_pwm_set(pwm_device, self->channel, period, pulse);
  116. error_check(result == RT_EOK, "PWM set information error");
  117. result = rt_pwm_enable(pwm_device, self->channel);
  118. error_check(result == RT_EOK, "PWM enable error");
  119. self->is_init = RT_TRUE;
  120. }
  121. STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type,
  122. size_t n_args, size_t n_kw, const mp_obj_t *args) {
  123. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  124. // create PWM object from the given pin
  125. machine_pwm_obj_t *self = m_new_obj(machine_pwm_obj_t);
  126. self->base.type = &machine_pwm_type;
  127. self->is_init = RT_FALSE;
  128. // check input PWM device name or ID
  129. if (mp_obj_is_small_int(args[0])) {
  130. self->id = mp_obj_get_int(args[0]);
  131. } else if (mp_obj_is_qstr(args[0])) {
  132. self->id = -1;
  133. rt_strncpy(self->dev_name, mp_obj_str_get_str(args[0]), RT_NAME_MAX);
  134. } else {
  135. error_check(0, "Input PWM device name or ID error.");
  136. }
  137. self->channel = 0;
  138. self->freq = 1;
  139. self->duty = 0;
  140. mp_map_t kw_args;
  141. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  142. machine_pwm_init_helper(self, n_args - 1, args + 1, &kw_args);
  143. return MP_OBJ_FROM_PTR(self);
  144. }
  145. STATIC mp_obj_t machine_pwm_init(size_t n_args,
  146. const mp_obj_t *args, mp_map_t *kw_args) {
  147. machine_pwm_init_helper(args[0], n_args - 1, args + 1, kw_args);
  148. return mp_const_none;
  149. }
  150. MP_DEFINE_CONST_FUN_OBJ_KW(machine_pwm_init_obj, 1, machine_pwm_init);
  151. STATIC mp_obj_t machine_pwm_deinit(mp_obj_t self_in) {
  152. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
  153. rt_err_t result = RT_EOK;
  154. if (self->is_init == RT_TRUE) {
  155. result = rt_pwm_disable(self->pwm_device, self->channel);
  156. error_check(result == RT_EOK, "PWM disable error");
  157. self->is_init = RT_FALSE;
  158. }
  159. return mp_const_none;
  160. }
  161. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pwm_deinit_obj, machine_pwm_deinit);
  162. STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *args) {
  163. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  164. uint32_t period = 0, pulse = 0;
  165. rt_err_t result = RT_EOK;
  166. error_check(self->is_init == RT_TRUE, "PWM device uninitialized");
  167. if (n_args == 1) {
  168. // get
  169. return MP_OBJ_NEW_SMALL_INT(self->freq);
  170. }
  171. // set
  172. int tval = mp_obj_get_int(args[1]);
  173. if ((tval < 1) || (tval > 156250)) {
  174. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  175. "Bad frequency %d", tval));
  176. }
  177. // get period number by frequency
  178. period = MP_PWM_PERIOD_GET(tval);
  179. // get pulse number by duty
  180. pulse = MP_PWM_PULSE_GET(period, self->duty);
  181. result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
  182. error_check(result == RT_EOK, "PWM set information error");
  183. self->freq = tval;
  184. return mp_const_none;
  185. }
  186. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_freq_obj, 1, 2, machine_pwm_freq);
  187. STATIC mp_obj_t machine_pwm_duty(size_t n_args, const mp_obj_t *args) {
  188. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  189. uint32_t period = 0, pulse = 0;
  190. rt_err_t result = RT_EOK;
  191. error_check(self->is_init == RT_TRUE, "PWM device uninitialized");
  192. if (n_args == 1) {
  193. // get
  194. return MP_OBJ_NEW_SMALL_INT(self->duty);
  195. }
  196. // set
  197. int tval = mp_obj_get_int(args[1]);
  198. if ((tval < 0) || (tval > 255)) {
  199. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  200. "Bad duty %d", tval));
  201. }
  202. // get period number by frequency
  203. period = MP_PWM_PERIOD_GET(self->freq);
  204. // get pulse number by duty
  205. pulse = MP_PWM_PULSE_GET(period, tval);
  206. result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
  207. error_check(result == RT_EOK, "PWM set information error");
  208. self->duty = tval;
  209. return mp_const_none;
  210. }
  211. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_duty_obj,
  212. 1, 2, machine_pwm_duty);
  213. STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = {
  214. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pwm_init_obj) },
  215. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_pwm_deinit_obj) },
  216. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_pwm_freq_obj) },
  217. { MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&machine_pwm_duty_obj) },
  218. };
  219. STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict,
  220. machine_pwm_locals_dict_table);
  221. const mp_obj_type_t machine_pwm_type = {
  222. { &mp_type_type },
  223. .name = MP_QSTR_PWM,
  224. .print = machine_pwm_print,
  225. .make_new = machine_pwm_make_new,
  226. .locals_dict = (mp_obj_dict_t *) &machine_pwm_locals_dict,
  227. };
  228. #endif // MICROPYTHON_USING_MACHINE_PWM