machine_pwm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. uint8_t is_init;
  44. uint8_t id;
  45. uint8_t channel;
  46. uint8_t duty;
  47. uint32_t freq;
  48. } machine_pwm_obj_t;
  49. STATIC void machine_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
  50. machine_pwm_obj_t *self = self_in;
  51. mp_printf(print, "PWM(%p; ", self);
  52. mp_printf(print, "id=%d, ", self->id);
  53. mp_printf(print, "channel=%d, ", self->channel);
  54. mp_printf(print, "freq=%d, ", self->freq);
  55. mp_printf(print, "duty=%d)", self->duty);
  56. }
  57. STATIC void error_check(bool status, const char *msg) {
  58. if (!status) {
  59. nlr_raise(mp_obj_new_exception_msg(&mp_type_ValueError, msg));
  60. }
  61. }
  62. STATIC void machine_pwm_init_helper(machine_pwm_obj_t *self,
  63. size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
  64. rt_err_t result = RT_EOK;
  65. uint32_t period = 0, pulse = 0;
  66. char pwm_dev_name[RT_NAME_MAX];
  67. struct rt_device_pwm *pwm_device = RT_NULL;
  68. enum { ARG_channel, ARG_freq, ARG_duty };
  69. static const mp_arg_t allowed_args[] = {
  70. { MP_QSTR_channel, MP_ARG_INT, {.u_int = 0} },
  71. { MP_QSTR_freq, MP_ARG_INT, {.u_int = 1} },
  72. { MP_QSTR_duty, MP_ARG_INT, {.u_int = 0} },
  73. };
  74. mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
  75. mp_arg_parse_all(n_args, pos_args, kw_args,
  76. MP_ARRAY_SIZE(allowed_args), allowed_args, args);
  77. int tval = args[ARG_channel].u_int;
  78. if ((tval < 0) || (tval > 4)) {
  79. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  80. "Bad channel %d", tval));
  81. }
  82. self->channel = tval;
  83. tval = args[ARG_freq].u_int;
  84. if ((tval < 1) || (tval > 156250)) {
  85. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  86. "Bad frequency %d", tval));
  87. }
  88. self->freq = tval;
  89. tval = args[ARG_duty].u_int;
  90. if ((tval < 0) || (tval > 255)) {
  91. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  92. "Bad duty %d", tval));
  93. }
  94. self->duty = tval;
  95. snprintf(pwm_dev_name, sizeof(pwm_dev_name), "pwm%d", self->id);
  96. pwm_device = (struct rt_device_pwm *) rt_device_find(pwm_dev_name);
  97. if (pwm_device == RT_NULL || pwm_device->parent.type != RT_Device_Class_Miscellaneous) {
  98. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  99. "PWM(%s) don't exist", pwm_dev_name));
  100. }
  101. self->pwm_device = pwm_device;
  102. // get period number by frequency
  103. period = MP_PWM_PERIOD_GET(self->freq);
  104. // get pulse number by duty
  105. pulse = MP_PWM_PULSE_GET(period, self->duty);
  106. result = rt_pwm_set(pwm_device, self->channel, period, pulse);
  107. error_check(result == RT_EOK, "PWM set information error");
  108. result = rt_pwm_enable(pwm_device, self->channel);
  109. error_check(result == RT_EOK, "PWM enable error");
  110. self->is_init = RT_TRUE;
  111. }
  112. STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type,
  113. size_t n_args, size_t n_kw, const mp_obj_t *args) {
  114. mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
  115. // create PWM object from the given pin
  116. machine_pwm_obj_t *self = m_new_obj(machine_pwm_obj_t);
  117. self->base.type = &machine_pwm_type;
  118. self->is_init = RT_FALSE;
  119. self->id = mp_obj_get_int(args[0]);
  120. self->channel = 0;
  121. self->freq = 1;
  122. self->duty = 0;
  123. mp_map_t kw_args;
  124. mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
  125. machine_pwm_init_helper(self, n_args - 1, args + 1, &kw_args);
  126. return MP_OBJ_FROM_PTR(self);
  127. }
  128. STATIC mp_obj_t machine_pwm_init(size_t n_args,
  129. const mp_obj_t *args, mp_map_t *kw_args) {
  130. machine_pwm_init_helper(args[0], n_args - 1, args + 1, kw_args);
  131. return mp_const_none;
  132. }
  133. MP_DEFINE_CONST_FUN_OBJ_KW(machine_pwm_init_obj, 1, machine_pwm_init);
  134. STATIC mp_obj_t machine_pwm_deinit(mp_obj_t self_in) {
  135. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
  136. rt_err_t result = RT_EOK;
  137. if (self->is_init == RT_TRUE) {
  138. result = rt_pwm_disable(self->pwm_device, self->channel);
  139. error_check(result == RT_EOK, "PWM disable error");
  140. self->is_init = RT_FALSE;
  141. }
  142. return mp_const_none;
  143. }
  144. STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pwm_deinit_obj, machine_pwm_deinit);
  145. STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *args) {
  146. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  147. uint32_t period = 0, pulse = 0;
  148. rt_err_t result = RT_EOK;
  149. error_check(self->is_init == RT_TRUE, "PWM device uninitialized");
  150. if (n_args == 1) {
  151. // get
  152. return MP_OBJ_NEW_SMALL_INT(self->freq);
  153. }
  154. // set
  155. int tval = mp_obj_get_int(args[1]);
  156. if ((tval < 1) || (tval > 156250)) {
  157. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  158. "Bad frequency %d", tval));
  159. }
  160. // get period number by frequency
  161. period = MP_PWM_PERIOD_GET(tval);
  162. // get pulse number by duty
  163. pulse = MP_PWM_PULSE_GET(period, self->duty);
  164. result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
  165. error_check(result == RT_EOK, "PWM set information error");
  166. self->freq = tval;
  167. return mp_const_none;
  168. }
  169. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_freq_obj, 1, 2, machine_pwm_freq);
  170. STATIC mp_obj_t machine_pwm_duty(size_t n_args, const mp_obj_t *args) {
  171. machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
  172. uint32_t period = 0, pulse = 0;
  173. rt_err_t result = RT_EOK;
  174. error_check(self->is_init == RT_TRUE, "PWM device uninitialized");
  175. if (n_args == 1) {
  176. // get
  177. return MP_OBJ_NEW_SMALL_INT(self->duty);
  178. }
  179. // set
  180. int tval = mp_obj_get_int(args[1]);
  181. if ((tval < 0) || (tval > 255)) {
  182. nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
  183. "Bad duty %d", tval));
  184. }
  185. // get period number by frequency
  186. period = MP_PWM_PERIOD_GET(self->freq);
  187. // get pulse number by duty
  188. pulse = MP_PWM_PULSE_GET(period, tval);
  189. result = rt_pwm_set(self->pwm_device, self->channel, period, pulse);
  190. error_check(result == RT_EOK, "PWM set information error");
  191. self->duty = tval;
  192. return mp_const_none;
  193. }
  194. STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_duty_obj,
  195. 1, 2, machine_pwm_duty);
  196. STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = {
  197. { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pwm_init_obj) },
  198. { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_pwm_deinit_obj) },
  199. { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_pwm_freq_obj) },
  200. { MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&machine_pwm_duty_obj) },
  201. };
  202. STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict,
  203. machine_pwm_locals_dict_table);
  204. const mp_obj_type_t machine_pwm_type = {
  205. { &mp_type_type },
  206. .name = MP_QSTR_PWM,
  207. .print = machine_pwm_print,
  208. .make_new = machine_pwm_make_new,
  209. .locals_dict = (mp_obj_dict_t *) &machine_pwm_locals_dict,
  210. };
  211. #endif // MICROPYTHON_USING_MACHINE_PWM