Damien George | 632d8ef | 2016-03-02 13:37:27 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the Micro Python project, http://micropython.org/ |
| 3 | * |
| 4 | * The MIT License (MIT) |
| 5 | * |
| 6 | * Copyright (c) 2016 Damien P. George |
| 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 | |
| 27 | #include <stdio.h> |
| 28 | #include <stdint.h> |
| 29 | |
| 30 | #include "esppwm.h" |
| 31 | |
| 32 | #include "py/nlr.h" |
| 33 | #include "py/runtime.h" |
| 34 | #include "modpyb.h" |
| 35 | |
| 36 | typedef struct _pyb_pwm_obj_t { |
| 37 | mp_obj_base_t base; |
| 38 | pyb_pin_obj_t *pin; |
| 39 | uint8_t active; |
| 40 | uint8_t channel; |
| 41 | } pyb_pwm_obj_t; |
| 42 | |
| 43 | STATIC bool pwm_inited = false; |
| 44 | |
| 45 | /******************************************************************************/ |
| 46 | // MicroPython bindings for PWM |
| 47 | |
| 48 | STATIC void pyb_pwm_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 49 | pyb_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 50 | mp_printf(print, "PWM(%u", self->pin->pin_id); |
| 51 | if (self->active) { |
| 52 | mp_printf(print, ", freq=%u, duty=%u", |
| 53 | pwm_get_freq(self->channel), pwm_get_duty(self->channel)); |
| 54 | } |
| 55 | mp_printf(print, ")"); |
| 56 | } |
| 57 | |
| 58 | STATIC void pyb_pwm_init_helper(pyb_pwm_obj_t *self, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 59 | enum { ARG_freq, ARG_duty }; |
| 60 | static const mp_arg_t allowed_args[] = { |
| 61 | { MP_QSTR_freq, MP_ARG_INT, {.u_int = -1} }, |
| 62 | { MP_QSTR_duty, MP_ARG_INT, {.u_int = -1} }, |
| 63 | }; |
| 64 | mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 65 | mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 66 | |
| 67 | int channel = pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func); |
| 68 | if (channel == -1) { |
| 69 | nlr_raise(mp_obj_new_exception_msg_varg(&mp_type_ValueError, |
| 70 | "PWM not supported on pin %d", self->pin->phys_port)); |
| 71 | } |
| 72 | |
| 73 | self->channel = channel; |
| 74 | self->active = 1; |
| 75 | if (args[ARG_freq].u_int != -1) { |
| 76 | pwm_set_freq(args[ARG_freq].u_int, self->channel); |
| 77 | } |
| 78 | if (args[ARG_duty].u_int != -1) { |
| 79 | pwm_set_duty(args[ARG_duty].u_int, self->channel); |
| 80 | } |
| 81 | |
| 82 | pwm_start(); |
| 83 | } |
| 84 | |
| 85 | STATIC mp_obj_t pyb_pwm_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *args) { |
| 86 | mp_arg_check_num(n_args, n_kw, 1, MP_OBJ_FUN_ARGS_MAX, true); |
| 87 | pyb_pin_obj_t *pin = mp_obj_get_pin_obj(args[0]); |
| 88 | |
| 89 | // create PWM object from the given pin |
| 90 | pyb_pwm_obj_t *self = m_new_obj(pyb_pwm_obj_t); |
| 91 | self->base.type = &pyb_pwm_type; |
| 92 | self->pin = pin; |
| 93 | self->active = 0; |
| 94 | self->channel = -1; |
| 95 | |
| 96 | // start the PWM subsystem if it's not already running |
| 97 | if (!pwm_inited) { |
| 98 | pwm_init(); |
| 99 | pwm_inited = true; |
| 100 | } |
| 101 | |
| 102 | // start the PWM running for this channel |
| 103 | mp_map_t kw_args; |
| 104 | mp_map_init_fixed_table(&kw_args, n_kw, args + n_args); |
| 105 | pyb_pwm_init_helper(self, n_args - 1, args + 1, &kw_args); |
| 106 | |
| 107 | return MP_OBJ_FROM_PTR(self); |
| 108 | } |
| 109 | |
| 110 | STATIC mp_obj_t pyb_pwm_init(size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { |
| 111 | pyb_pwm_init_helper(args[0], n_args - 1, args + 1, kw_args); |
| 112 | return mp_const_none; |
| 113 | } |
| 114 | MP_DEFINE_CONST_FUN_OBJ_KW(pyb_pwm_init_obj, 1, pyb_pwm_init); |
| 115 | |
| 116 | STATIC mp_obj_t pyb_pwm_deinit(mp_obj_t self_in) { |
| 117 | pyb_pwm_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 118 | pwm_delete(self->channel); |
| 119 | self->active = 0; |
| 120 | pwm_start(); |
| 121 | return mp_const_none; |
| 122 | } |
| 123 | STATIC MP_DEFINE_CONST_FUN_OBJ_1(pyb_pwm_deinit_obj, pyb_pwm_deinit); |
| 124 | |
| 125 | STATIC mp_obj_t pyb_pwm_freq(size_t n_args, const mp_obj_t *args) { |
| 126 | //pyb_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 127 | if (n_args == 1) { |
| 128 | // get |
| 129 | return MP_OBJ_NEW_SMALL_INT(pwm_get_freq(0)); |
| 130 | } else { |
| 131 | // set |
| 132 | pwm_set_freq(mp_obj_get_int(args[1]), 0); |
| 133 | pwm_start(); |
| 134 | return mp_const_none; |
| 135 | } |
| 136 | } |
| 137 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pwm_freq_obj, 1, 2, pyb_pwm_freq); |
| 138 | |
| 139 | STATIC mp_obj_t pyb_pwm_duty(size_t n_args, const mp_obj_t *args) { |
| 140 | pyb_pwm_obj_t *self = MP_OBJ_TO_PTR(args[0]); |
| 141 | if (!self->active) { |
| 142 | pwm_add(self->pin->phys_port, self->pin->periph, self->pin->func); |
| 143 | self->active = 1; |
| 144 | } |
| 145 | if (n_args == 1) { |
| 146 | // get |
| 147 | return MP_OBJ_NEW_SMALL_INT(pwm_get_duty(self->channel)); |
| 148 | } else { |
| 149 | // set |
| 150 | pwm_set_duty(mp_obj_get_int(args[1]), self->channel); |
| 151 | pwm_start(); |
| 152 | return mp_const_none; |
| 153 | } |
| 154 | } |
| 155 | STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(pyb_pwm_duty_obj, 1, 2, pyb_pwm_duty); |
| 156 | |
| 157 | STATIC const mp_rom_map_elem_t pyb_pwm_locals_dict_table[] = { |
| 158 | { MP_ROM_QSTR(MP_QSTR_init), MP_ROM_PTR(&pyb_pwm_init_obj) }, |
| 159 | { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&pyb_pwm_deinit_obj) }, |
| 160 | { MP_ROM_QSTR(MP_QSTR_freq), MP_ROM_PTR(&pyb_pwm_freq_obj) }, |
| 161 | { MP_ROM_QSTR(MP_QSTR_duty), MP_ROM_PTR(&pyb_pwm_duty_obj) }, |
| 162 | }; |
| 163 | |
| 164 | STATIC MP_DEFINE_CONST_DICT(pyb_pwm_locals_dict, pyb_pwm_locals_dict_table); |
| 165 | |
| 166 | const mp_obj_type_t pyb_pwm_type = { |
| 167 | { &mp_type_type }, |
| 168 | .name = MP_QSTR_PWM, |
| 169 | .print = pyb_pwm_print, |
| 170 | .make_new = pyb_pwm_make_new, |
| 171 | .locals_dict = (mp_obj_dict_t*)&pyb_pwm_locals_dict, |
| 172 | }; |