|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2019 "Matt Trentini" <matt.trentini@gmail.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 | + |
| 27 | +#include "py/runtime.h" |
| 28 | +#include "modmachine.h" |
| 29 | +#include "mphalport.h" |
| 30 | +#include "driver/rmt.h" |
| 31 | + |
| 32 | +// This exposes the ESP32's RMT module to MicroPython. RMT is provided by the Espressif ESP-IDF: |
| 33 | +// |
| 34 | +// https://docs.espressif.com/projects/esp-idf/en/latest/api-reference/peripherals/rmt.html |
| 35 | +// |
| 36 | +// With some examples provided: |
| 37 | +// |
| 38 | +// https://github.com/espressif/arduino-esp32/tree/master/libraries/ESP32/examples/RMT |
| 39 | +// |
| 40 | +// RMT allows accurate (down to 12.5ns resolution) transmit - and receive - of pulse signals. |
| 41 | +// Originally designed to generate infrared remote control signals, the module is very |
| 42 | +// flexible and quite easy-to-use. |
| 43 | +// |
| 44 | +// This current MicroPython implementation lacks some major features, notably receive pulses |
| 45 | +// and carrier output. |
| 46 | + |
| 47 | +// Forward declaration |
| 48 | +extern const mp_obj_type_t esp32_rmt_type; |
| 49 | + |
| 50 | +typedef struct _esp32_rmt_obj_t { |
| 51 | + mp_obj_base_t base; |
| 52 | + uint8_t channel_id; |
| 53 | + gpio_num_t pin; |
| 54 | + uint8_t clock_div; |
| 55 | + mp_uint_t num_items; |
| 56 | + rmt_item32_t* items; |
| 57 | +} esp32_rmt_obj_t; |
| 58 | + |
| 59 | +// Defined in machine_time.c; simply added the error message |
| 60 | +// Fixme: Should use this updated error hadline more widely in the ESP32 port. |
| 61 | +// At least update the method in machine_time.c. |
| 62 | +STATIC esp_err_t check_esp_err(esp_err_t code) { |
| 63 | + if (code) { |
| 64 | + mp_raise_msg(&mp_type_OSError, esp_err_to_name(code)); |
| 65 | + } |
| 66 | + |
| 67 | + return code; |
| 68 | +} |
| 69 | + |
| 70 | +STATIC mp_obj_t esp32_rmt_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *all_args) { |
| 71 | + static const mp_arg_t allowed_args[] = { |
| 72 | + { MP_QSTR_id, MP_ARG_REQUIRED | MP_ARG_INT, {.u_int = -1} }, |
| 73 | + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 74 | + { MP_QSTR_clock_div, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 8} }, // 100ns resolution |
| 75 | + }; |
| 76 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 77 | + mp_arg_parse_all_kw_array(n_args, n_kw, all_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 78 | + mp_uint_t channel_id = args[0].u_int; |
| 79 | + gpio_num_t pin_id = machine_pin_get_id(args[1].u_obj); |
| 80 | + mp_uint_t clock_div = args[2].u_int; |
| 81 | + |
| 82 | + if (clock_div < 1 || clock_div > 255) { |
| 83 | + mp_raise_ValueError("clock_div must be between 1 and 255"); |
| 84 | + } |
| 85 | + |
| 86 | + esp32_rmt_obj_t *self = m_new_obj_with_finaliser(esp32_rmt_obj_t); |
| 87 | + self->base.type = &esp32_rmt_type; |
| 88 | + self->channel_id = channel_id; |
| 89 | + self->pin = pin_id; |
| 90 | + self->clock_div = clock_div; |
| 91 | + |
| 92 | + rmt_config_t config; |
| 93 | + config.rmt_mode = RMT_MODE_TX; |
| 94 | + config.channel = (rmt_channel_t) self->channel_id; |
| 95 | + config.gpio_num = self->pin; |
| 96 | + config.mem_block_num = 1; |
| 97 | + config.tx_config.loop_en = 0; |
| 98 | + |
| 99 | + config.tx_config.carrier_en = 0; |
| 100 | + config.tx_config.idle_output_en = 1; |
| 101 | + config.tx_config.idle_level = 0; |
| 102 | + config.tx_config.carrier_duty_percent = 0; |
| 103 | + config.tx_config.carrier_freq_hz = 0; |
| 104 | + config.tx_config.carrier_level = 1; |
| 105 | + |
| 106 | + config.clk_div = self->clock_div; |
| 107 | + |
| 108 | + check_esp_err(rmt_config(&config)); |
| 109 | + check_esp_err(rmt_driver_install(config.channel, 0, 0)); |
| 110 | + |
| 111 | + return MP_OBJ_FROM_PTR(self); |
| 112 | +} |
| 113 | + |
| 114 | +STATIC void esp32_rmt_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) { |
| 115 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 116 | + if (self->pin != -1) { |
| 117 | + mp_printf(print, "RMT(channel=%u, pin=%u, source_freq=%u, clock_div=%u)", |
| 118 | + self->channel_id, self->pin, APB_CLK_FREQ, self->clock_div); |
| 119 | + } else { |
| 120 | + mp_printf(print, "RMT()"); |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +STATIC mp_obj_t esp32_rmt_deinit(mp_obj_t self_in) { |
| 125 | + // fixme: check for valid channel. Return exception if error occurs. |
| 126 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 127 | + if (self->pin != -1) { // Check if channel has already been deinitialised. |
| 128 | + rmt_driver_uninstall(self->channel_id); |
| 129 | + self->pin = -1; // -1 to indicate RMT is unused |
| 130 | + m_free(self->items); |
| 131 | + } |
| 132 | + return mp_const_none; |
| 133 | +} |
| 134 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_rmt_deinit_obj, esp32_rmt_deinit); |
| 135 | + |
| 136 | +// Return the source frequency. |
| 137 | +// Currently only the APB clock (80MHz) can be used but it is possible other |
| 138 | +// clock sources will added in the future. |
| 139 | +STATIC mp_obj_t esp32_rmt_source_freq(mp_obj_t self_in) { |
| 140 | + return mp_obj_new_int(APB_CLK_FREQ); |
| 141 | +} |
| 142 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_rmt_source_freq_obj, esp32_rmt_source_freq); |
| 143 | + |
| 144 | +// Return the clock divider. |
| 145 | +STATIC mp_obj_t esp32_rmt_clock_div(mp_obj_t self_in) { |
| 146 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 147 | + return mp_obj_new_int(self->clock_div); |
| 148 | +} |
| 149 | +STATIC MP_DEFINE_CONST_FUN_OBJ_1(esp32_rmt_clock_div_obj, esp32_rmt_clock_div); |
| 150 | + |
| 151 | +// Query whether the channel has finished sending pulses. Takes an optional |
| 152 | +// timeout (in ticks of the 80MHz clock), returning true if the pulse stream has |
| 153 | +// completed or false if they are still transmitting (or timeout is reached). |
| 154 | +STATIC mp_obj_t esp32_rmt_wait_done(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 155 | + static const mp_arg_t allowed_args[] = { |
| 156 | + { MP_QSTR_self, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 157 | + { MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} }, |
| 158 | + }; |
| 159 | + |
| 160 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 161 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 162 | + |
| 163 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(args[0].u_obj); |
| 164 | + |
| 165 | + esp_err_t err = rmt_wait_tx_done(self->channel_id, args[1].u_int); |
| 166 | + return err == ESP_OK ? mp_const_true : mp_const_false; |
| 167 | +} |
| 168 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_rmt_wait_done_obj, 1, esp32_rmt_wait_done); |
| 169 | + |
| 170 | +STATIC mp_obj_t esp32_rmt_loop(mp_obj_t self_in, mp_obj_t loop) { |
| 171 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(self_in); |
| 172 | + check_esp_err(rmt_set_tx_loop_mode(self->channel_id, mp_obj_get_int(loop))); |
| 173 | + return mp_const_none; |
| 174 | +} |
| 175 | +STATIC MP_DEFINE_CONST_FUN_OBJ_2(esp32_rmt_loop_obj, esp32_rmt_loop); |
| 176 | + |
| 177 | +STATIC mp_obj_t esp32_rmt_write_pulses(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 178 | + static const mp_arg_t allowed_args[] = { |
| 179 | + { MP_QSTR_self, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 180 | + { MP_QSTR_pulses, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = mp_const_none} }, |
| 181 | + { MP_QSTR_start, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1} }, |
| 182 | + }; |
| 183 | + |
| 184 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 185 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 186 | + |
| 187 | + esp32_rmt_obj_t *self = MP_OBJ_TO_PTR(args[0].u_obj); |
| 188 | + mp_obj_t pulses = args[1].u_obj; |
| 189 | + mp_uint_t start = args[2].u_int; |
| 190 | + |
| 191 | + if (start < 0 || start > 1) { |
| 192 | + mp_raise_ValueError("start must be 0 or 1"); |
| 193 | + } |
| 194 | + |
| 195 | + size_t pulses_length = 0; |
| 196 | + mp_obj_t* pulses_ptr = NULL; |
| 197 | + mp_obj_get_array(pulses, &pulses_length, &pulses_ptr); |
| 198 | + |
| 199 | + mp_uint_t num_items = (pulses_length / 2) + (pulses_length % 2); |
| 200 | + if (num_items > self->num_items) { |
| 201 | + self->items = (rmt_item32_t*)m_realloc(self->items, num_items * sizeof(rmt_item32_t *)); |
| 202 | + self->num_items = num_items; |
| 203 | + } |
| 204 | + |
| 205 | + for (mp_uint_t item_index = 0; item_index < num_items; item_index++) { |
| 206 | + mp_uint_t pulse_index = item_index * 2; |
| 207 | + self->items[item_index].duration0 = mp_obj_get_int(pulses_ptr[pulse_index++]); |
| 208 | + self->items[item_index].level0 = start++; // Note that start _could_ wrap. |
| 209 | + if (pulse_index < pulses_length) { |
| 210 | + self->items[item_index].duration1 = mp_obj_get_int(pulses_ptr[pulse_index]); |
| 211 | + self->items[item_index].level1 = start++; |
| 212 | + } |
| 213 | + } |
| 214 | + check_esp_err(rmt_write_items(self->channel_id, self->items, num_items, false /* non-blocking */)); |
| 215 | + |
| 216 | + return mp_const_none; |
| 217 | +} |
| 218 | +STATIC MP_DEFINE_CONST_FUN_OBJ_KW(esp32_rmt_write_pulses_obj, 2, esp32_rmt_write_pulses); |
| 219 | + |
| 220 | +STATIC const mp_rom_map_elem_t esp32_rmt_locals_dict_table[] = { |
| 221 | + { MP_ROM_QSTR(MP_QSTR___del__), MP_ROM_PTR(&esp32_rmt_deinit_obj) }, |
| 222 | + { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&esp32_rmt_deinit_obj) }, |
| 223 | + { MP_ROM_QSTR(MP_QSTR_source_freq), MP_ROM_PTR(&esp32_rmt_source_freq_obj) }, |
| 224 | + { MP_ROM_QSTR(MP_QSTR_clock_div), MP_ROM_PTR(&esp32_rmt_clock_div_obj) }, |
| 225 | + { MP_ROM_QSTR(MP_QSTR_wait_done), MP_ROM_PTR(&esp32_rmt_wait_done_obj) }, |
| 226 | + { MP_ROM_QSTR(MP_QSTR_loop), MP_ROM_PTR(&esp32_rmt_loop_obj) }, |
| 227 | + { MP_ROM_QSTR(MP_QSTR_write_pulses), MP_ROM_PTR(&esp32_rmt_write_pulses_obj) }, |
| 228 | +}; |
| 229 | +STATIC MP_DEFINE_CONST_DICT(esp32_rmt_locals_dict, esp32_rmt_locals_dict_table); |
| 230 | + |
| 231 | +const mp_obj_type_t esp32_rmt_type = { |
| 232 | + { &mp_type_type }, |
| 233 | + .name = MP_QSTR_RMT, |
| 234 | + .print = esp32_rmt_print, |
| 235 | + .make_new = esp32_rmt_make_new, |
| 236 | + .locals_dict = (mp_obj_dict_t*)&esp32_rmt_locals_dict, |
| 237 | +}; |
0 commit comments