瀏覽代碼

【添加】machine.PWM 模块

Signed-off-by: chenyong <1521761801@qq.com>
chenyong 6 年之前
父節點
當前提交
a392336168
共有 5 個文件被更改,包括 287 次插入1 次删除
  1. 3 0
      port/genhdr/qstrdefs.generated.h
  2. 241 0
      port/machine_pwm.c
  3. 35 0
      port/machine_pwm.h
  4. 4 1
      port/modmachine.c
  5. 4 0
      port/mpconfigport.h

+ 3 - 0
port/genhdr/qstrdefs.generated.h

@@ -697,5 +697,8 @@ QDEF(MP_QSTR_ffimod, (const byte*)"\xca\x06" "ffimod")
 QDEF(MP_QSTR_ffifunc, (const byte*)"\x92\x07" "ffifunc")
 QDEF(MP_QSTR_fficallback, (const byte*)"\xc5\x0b" "fficallback")
 QDEF(MP_QSTR_ffivar, (const byte*)"\x49\x06" "ffivar")
+QDEF(MP_QSTR_channel, (const byte*)"\x26\x07" "channel")
+QDEF(MP_QSTR_duty, (const byte*)"\x19\x04" "duty")
+QDEF(MP_QSTR_PWM, (const byte*)"\x4f\x03" "PWM")
 
 // This file was automatically generated by makeqstrdata.py

+ 241 - 0
port/machine_pwm.c

@@ -0,0 +1,241 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "py/nlr.h"
+#include "py/runtime.h"
+#include "modmachine.h"
+#include "mphalport.h"
+
+#ifdef MICROPYTHON_USING_MACHINE_PWM
+
+#include <rtthread.h>
+#include <drivers/rt_drv_pwm.h>
+
+#define MP_PWM_PULSE_MAX               255
+#define MP_PWM_PERIOD_GET(freq)        (1000000000 / (freq))
+#define MP_PWM_PULSE_GET(period, duty) ((period) / MP_PWM_PULSE_MAX * (duty))
+
+extern const mp_obj_type_t machine_pwm_type;
+
+typedef struct _machine_pwm_obj_t {
+    mp_obj_base_t base;
+    struct rt_device_pwm *pwm_device;
+    uint8_t is_init;
+    uint8_t id;
+    uint8_t channel;
+    uint8_t duty;
+    uint32_t freq;
+} machine_pwm_obj_t;
+
+STATIC void machine_pwm_init_helper(machine_pwm_obj_t *self,
+                                 size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+    rt_err_t result = RT_EOK;
+    uint32_t period = 0, pulse = 0;
+    char pwm_dev_name[RT_NAME_MAX];
+    struct rt_device_pwm *pwm_device = RT_NULL;
+    enum { ARG_channel, ARG_freq, ARG_duty };
+    static const mp_arg_t allowed_args[] = {
+        { MP_QSTR_channel,  MP_ARG_INT, {.u_int = 0} },
+        { MP_QSTR_freq, MP_ARG_INT, {.u_int = 1} },
+        { MP_QSTR_duty, MP_ARG_INT, {.u_int = 0} },
+    };
+    mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+    mp_arg_parse_all(n_args, pos_args, kw_args,
+                     MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+    int tval = args[ARG_channel].u_int;
+    if ((tval < 0) || (tval > 4)) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+                                                "Bad channel %d", tval));
+    }
+    self->channel = tval;
+
+    tval = args[ARG_freq].u_int;
+    if ((tval < 1) || (tval > 156250)) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+                                                "Bad frequency %d", tval));
+    }
+    self->freq = tval;
+
+    tval = args[ARG_duty].u_int;
+    if ((tval < 0) || (tval > 255)) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+                                                "Bad duty %d", tval));
+    }
+    self->duty = tval;
+
+    snprintf(pwm_dev_name, sizeof(pwm_dev_name), "pwm%d", self->id);
+    pwm_device = (struct rt_device_pwm *) rt_device_find(pwm_dev_name);
+    if (pwm_device == RT_NULL || pwm_device->parent.type != RT_Device_Class_Miscellaneous) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, 
+                                                "PWM(%s) don't exist", pwm_dev_name));
+    }
+    self->pwm_device = pwm_device;
+
+    // get period number by frequency
+    period = MP_PWM_PERIOD_GET(self->freq);
+    // get pulse number by duty
+    pulse = MP_PWM_PULSE_GET(period, self->duty);
+
+    result = rt_pwm_set(pwm_device, self->channel, period, pulse);
+    if (result != RT_EOK) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, 
+                                                "PWM(%s) set information error", pwm_dev_name));
+    }
+
+    result = rt_pwm_enable(pwm_device, self->channel);
+    if (result != RT_EOK) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, 
+                                                "PWM(%s) enable error", pwm_dev_name));
+    }
+    self->is_init = RT_TRUE;
+}
+
+STATIC mp_obj_t machine_pwm_make_new(const mp_obj_type_t *type,
+                                  size_t n_args, size_t n_kw, const mp_obj_t *args) {
+    mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true);
+
+    // create PWM object from the given pin
+    machine_pwm_obj_t *self = m_new_obj(machine_pwm_obj_t);
+    self->base.type = &machine_pwm_type;
+    self->is_init = RT_FALSE;
+    self->id = mp_obj_get_int(args[0]);
+    self->channel = 0;
+    self->freq = 1;
+    self->duty = 0;
+
+    mp_map_t kw_args;
+    mp_map_init_fixed_table(&kw_args, n_kw, args + n_args);
+    machine_pwm_init_helper(self, n_args - 1, args + 1, &kw_args);
+
+    return MP_OBJ_FROM_PTR(self);
+}
+
+STATIC mp_obj_t machine_pwm_init(size_t n_args,
+                              const mp_obj_t *args, mp_map_t *kw_args) {
+    machine_pwm_init_helper(args[0], n_args - 1, args + 1, kw_args);
+    return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(machine_pwm_init_obj, 1, machine_pwm_init);
+
+STATIC mp_obj_t machine_pwm_deinit(mp_obj_t self_in) {
+    machine_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in);
+    if (self->is_init == RT_TRUE) {
+        rt_pwm_disable(self->pwm_device, self->channel);
+        self->is_init = RT_FALSE;
+    }
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(machine_pwm_deinit_obj, machine_pwm_deinit);
+
+STATIC mp_obj_t machine_pwm_freq(size_t n_args, const mp_obj_t *args) {
+    machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+    uint32_t period = 0, pulse = 0;
+
+    if (self->is_init == RT_FALSE) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, 
+                                                "PWM device don't initialized"));
+    }
+
+    if (n_args == 1) {
+        // get
+        return MP_OBJ_NEW_SMALL_INT(self->freq);
+    }
+
+    // set
+    int tval = mp_obj_get_int(args[1]);
+    if ((tval < 1) || (tval > 156250)) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+                                                "Bad frequency %d", tval));
+    }
+    
+    // get period number by frequency
+    period = MP_PWM_PERIOD_GET(tval);
+    // get pulse number by duty
+    pulse = MP_PWM_PULSE_GET(period, self->duty);
+    
+    rt_pwm_set(self->pwm_device, self->channel, period, pulse);
+    self->freq = tval;
+    return mp_const_none;
+}
+
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_freq_obj, 1, 2, machine_pwm_freq);
+
+STATIC mp_obj_t machine_pwm_duty(size_t n_args, const mp_obj_t *args) {
+    machine_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+    uint32_t period = 0, pulse = 0;
+
+    if (self->is_init == RT_FALSE) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_OSError, 
+                                                "PWM device don't initialized"));
+    }
+
+    if (n_args == 1) {
+        // get
+        return MP_OBJ_NEW_SMALL_INT(self->duty);
+    }
+
+    // set
+    int tval = mp_obj_get_int(args[1]);
+    if ((tval < 0) || (tval > 255)) {
+        nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError,
+                                                "Bad duty %d", tval));
+    }
+    
+    // get period number by frequency
+    period = MP_PWM_PERIOD_GET(self->freq);
+    // get pulse number by duty
+    pulse = MP_PWM_PULSE_GET(period, tval);
+    
+    rt_pwm_set(self->pwm_device, self->channel, period, pulse);
+    self->duty = tval;
+    return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(machine_pwm_duty_obj,
+        1, 2, machine_pwm_duty);
+
+STATIC const mp_rom_map_elem_t machine_pwm_locals_dict_table[] = {
+    { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&machine_pwm_init_obj) },
+    { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&machine_pwm_deinit_obj) },
+    { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&machine_pwm_freq_obj) },
+    { MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&machine_pwm_duty_obj) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(machine_pwm_locals_dict,
+                            machine_pwm_locals_dict_table);
+
+const mp_obj_type_t machine_pwm_type = {
+    { &mp_type_type },
+    .name = MP_QSTR_PWM,
+    .make_new = machine_pwm_make_new,
+    .locals_dict = (mp_obj_dict_t *) &machine_pwm_locals_dict,
+};
+
+#endif // MICROPYTHON_USING_MACHINE_PWM

+ 35 - 0
port/machine_pwm.h

@@ -0,0 +1,35 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 ChenYong (chenyong@rt-thread.com)
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_MACHINE_PWM_H
+#define MICROPY_INCLUDED_MACHINE_PWM_H
+
+#include "py/obj.h"
+#include <rtthread.h>
+
+extern const mp_obj_type_t machine_pwm_type;
+
+#endif // MICROPY_INCLUDED_MACHINE_PWM_H

+ 4 - 1
port/modmachine.c

@@ -38,6 +38,7 @@
 #include "extmod/machine_spi.h"
 #include "modmachine.h"
 #include "machine_uart.h"
+#include "machine_pwm.h"
 
 #include <rthw.h>
 
@@ -204,7 +205,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
 #if MICROPY_PY_MACHINE_UART
     { MP_ROM_QSTR(MP_QSTR_UART),                MP_ROM_PTR(&machine_uart_type ) },
 #endif
-
+#if MICROPY_PY_MACHINE_PWM
+    { MP_ROM_QSTR(MP_QSTR_PWM),                 MP_ROM_PTR(&machine_pwm_type) },
+#endif
 };
 
 STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);

+ 4 - 0
port/mpconfigport.h

@@ -143,6 +143,10 @@
 #define MICROPY_PY_MACHINE_UART      (1)
 #endif
 
+#ifdef MICROPYTHON_USING_MACHINE_PWM
+#define MICROPY_PY_MACHINE_PWM       (1)
+#endif
+
 /*****************************************************************************/
 /* System Module                                                             */