From f9512983ff0c63a719d7708496e1a827a2ca8a64 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Tue, 4 Aug 2020 10:59:05 -0400 Subject: [PATCH 01/11] Add PulseOut --- ports/esp32s2/common-hal/pulseio/PulseOut.c | 54 ++++++++++++++------- ports/esp32s2/common-hal/pulseio/PulseOut.h | 6 +-- ports/esp32s2/mpconfigport.h | 13 ++--- ports/esp32s2/peripherals/rmt.c | 8 +++ ports/esp32s2/peripherals/rmt.h | 1 + ports/esp32s2/supervisor/port.c | 3 ++ shared-bindings/pulseio/PulseOut.c | 30 +++++++++--- shared-bindings/pulseio/PulseOut.h | 6 +++ 8 files changed, 87 insertions(+), 34 deletions(-) diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.c b/ports/esp32s2/common-hal/pulseio/PulseOut.c index c448c578df..dc93d7e964 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseOut.c +++ b/ports/esp32s2/common-hal/pulseio/PulseOut.c @@ -29,32 +29,52 @@ #include "shared-bindings/pulseio/PWMOut.h" #include "py/runtime.h" -// STATIC void turn_on(pulseio_pulseout_obj_t *pulseout) { -// } - -// STATIC void turn_off(pulseio_pulseout_obj_t *pulseout) { -// } - -// STATIC void start_timer(void) { -// } - -// STATIC void pulseout_event_handler(void) { -// } - -void pulseout_reset() { -} +// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier) { - mp_raise_NotImplementedError(translate("PulseOut not supported on this chip")); + const mcu_pin_obj_t* pin, + uint32_t frequency) { + + rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); + + // Configure Channel + rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel); + config.tx_config.carrier_en = true; + config.tx_config.carrier_duty_percent = 50; + config.tx_config.carrier_freq_hz = frequency; + config.clk_div = 80; + + rmt_config(&config); + rmt_driver_install(channel, 0, 0); + + self->channel = channel; } bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self) { - return false; + return (self->channel == RMT_CHANNEL_MAX); } void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self) { + esp32s2_peripherals_free_rmt(self->channel); + self->channel = RMT_CHANNEL_MAX; + } void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pulses, uint16_t length) { + rmt_item32_t items[length]; + + // Circuitpython allows 16 bit pulse values, while ESP32 only allows 15 bits + // Thus, we use entire items for one pulse, rather than switching inside each item + for (size_t i = 0; i < length; i++) { + // Setting the RMT duration to 0 has undefined behavior, so avoid that pre-emptively. + if (pulses[i] == 0) { + pulses[i] = 1; + } + uint32_t level = (i % 2) ? 0 : 1; + const rmt_item32_t item = {{{ (pulses[i] & 0x8000 ? 0x7FFF : 1), level, (pulses[i] & 0x7FFF), level}}}; + items[i] = item; + } + + rmt_write_items(self->channel, items, length, true); + rmt_wait_tx_done(self->channel, pdMS_TO_TICKS(100)); } diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.h b/ports/esp32s2/common-hal/pulseio/PulseOut.h index f465d00792..5139059926 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseOut.h +++ b/ports/esp32s2/common-hal/pulseio/PulseOut.h @@ -29,14 +29,14 @@ #include "common-hal/microcontroller/Pin.h" #include "common-hal/pulseio/PWMOut.h" +#include "driver/rmt.h" +#include "rmt.h" #include "py/obj.h" typedef struct { mp_obj_base_t base; - pulseio_pwmout_obj_t *pwmout; + rmt_channel_t channel; } pulseio_pulseout_obj_t; -void pulseout_reset(void); - #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEOUT_H diff --git a/ports/esp32s2/mpconfigport.h b/ports/esp32s2/mpconfigport.h index d340bb4805..6b070afea6 100644 --- a/ports/esp32s2/mpconfigport.h +++ b/ports/esp32s2/mpconfigport.h @@ -28,19 +28,20 @@ #ifndef ESP32S2_MPCONFIGPORT_H__ #define ESP32S2_MPCONFIGPORT_H__ -#define CIRCUITPY_INTERNAL_NVM_SIZE (0) -#define MICROPY_NLR_THUMB (0) +#define CIRCUITPY_INTERNAL_NVM_SIZE (0) +#define MICROPY_NLR_THUMB (0) -#define MICROPY_PY_UJSON (0) -#define MICROPY_USE_INTERNAL_PRINTF (0) +#define MICROPY_PY_UJSON (0) +#define MICROPY_USE_INTERNAL_PRINTF (0) #include "py/circuitpy_mpconfig.h" +#define CPY_PULSEOUT_USES_DIGITALIO (1) #define MICROPY_PORT_ROOT_POINTERS \ CIRCUITPY_COMMON_ROOT_POINTERS -#define MICROPY_NLR_SETJMP (1) -#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000 +#define MICROPY_NLR_SETJMP (1) +#define CIRCUITPY_DEFAULT_STACK_SIZE (0x6000) #endif // __INCLUDED_ESP32S2_MPCONFIGPORT_H diff --git a/ports/esp32s2/peripherals/rmt.c b/ports/esp32s2/peripherals/rmt.c index f17957c1c4..16605edf5e 100644 --- a/ports/esp32s2/peripherals/rmt.c +++ b/ports/esp32s2/peripherals/rmt.c @@ -29,6 +29,14 @@ bool rmt_reserved_channels[RMT_CHANNEL_MAX]; +void esp32s2_peripherals_rmt_reset(void) { + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { + if (rmt_reserved_channels[i]) { + esp32s2_peripherals_free_rmt(i); + } + } +} + rmt_channel_t esp32s2_peripherals_find_and_reserve_rmt(void) { for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { if (!rmt_reserved_channels[i]) { diff --git a/ports/esp32s2/peripherals/rmt.h b/ports/esp32s2/peripherals/rmt.h index 4741a5bc59..01ed09907a 100644 --- a/ports/esp32s2/peripherals/rmt.h +++ b/ports/esp32s2/peripherals/rmt.h @@ -31,6 +31,7 @@ #include "driver/rmt.h" #include +void esp32s2_peripherals_rmt_reset(void); rmt_channel_t esp32s2_peripherals_find_and_reserve_rmt(void); void esp32s2_peripherals_free_rmt(rmt_channel_t chan); diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index e4767e5146..d287079b79 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -42,6 +42,8 @@ #include "supervisor/memory.h" #include "supervisor/shared/tick.h" +#include "rmt.h" + STATIC esp_timer_handle_t _tick_timer; void tick_timer_cb(void* arg) { @@ -66,6 +68,7 @@ void reset_port(void) { vTaskDelay(4); #if CIRCUITPY_PULSEIO + esp32s2_peripherals_rmt_reset(); pwmout_reset(); #endif #if CIRCUITPY_BUSIO diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 3d35f05445..10524f51b3 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -64,19 +64,33 @@ //| pulse.send(pulses)""" //| ... //| -STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { - mp_arg_check_num(n_args, kw_args, 1, 1, false); - mp_obj_t carrier_obj = args[0]; +STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { - mp_raise_TypeError_varg(translate("Expected a %q"), pulseio_pwmout_type.name); - } - - // create Pulse object from the given pin pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); self->base.type = &pulseio_pulseout_type; + #ifndef CPY_PULSEOUT_USES_DIGITALIO + // Most ports pass a PWMOut + mp_arg_check_num(n_args, kw_args, 1, 1, false); + mp_obj_t carrier_obj = args[0]; + if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { + mp_raise_TypeError_varg(translate("Expected a %q"), pulseio_pwmout_type.name); + } common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); + #else + // ESP32-S2 Special Case + enum { ARG_pin, ARG_frequency}; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 38000} }, + }; + 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); + + const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + + common_hal_pulseio_pulseout_construct(self, pin, args[ARG_frequency].u_int); + #endif return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/pulseio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h index 390910ff62..090f6d15c6 100644 --- a/shared-bindings/pulseio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -33,8 +33,14 @@ extern const mp_obj_type_t pulseio_pulseout_type; +#ifndef CPY_PULSEOUT_USES_DIGITALIO extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, const pulseio_pwmout_obj_t* carrier); +#else +extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const mcu_pin_obj_t* pin, uint32_t frequency); +#endif + extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); extern bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self); extern void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, From 88fcc19e24597140fb3e5f45ab8d87705fb4f5ed Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Mon, 10 Aug 2020 12:14:16 -0400 Subject: [PATCH 02/11] Add PulseIn --- ports/esp32s2/background.c | 7 + ports/esp32s2/common-hal/pulseio/PulseIn.c | 177 +++++++++++++++++++-- ports/esp32s2/common-hal/pulseio/PulseIn.h | 6 + ports/esp32s2/peripherals/rmt.c | 1 + ports/esp32s2/supervisor/port.c | 2 + 5 files changed, 184 insertions(+), 9 deletions(-) diff --git a/ports/esp32s2/background.c b/ports/esp32s2/background.c index 40ce9ecfdf..3160d9974e 100644 --- a/ports/esp32s2/background.c +++ b/ports/esp32s2/background.c @@ -35,10 +35,17 @@ #include "shared-module/displayio/__init__.h" #endif +#if CIRCUITPY_PULSEIO +#include "common-hal/pulseio/PulseIn.h" +#endif + void port_background_task(void) { // Zero delay in case FreeRTOS wants to switch to something else. vTaskDelay(0); + #if CIRCUITPY_PULSEIO + pulsein_background(); + #endif } void port_start_background_task(void) {} diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.c b/ports/esp32s2/common-hal/pulseio/PulseIn.c index 65fc6631d4..5209c51ca4 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseIn.c +++ b/ports/esp32s2/common-hal/pulseio/PulseIn.c @@ -27,49 +27,208 @@ #include "common-hal/pulseio/PulseIn.h" #include "py/runtime.h" -// STATIC void pulsein_handler(uint8_t num) { -// } +STATIC uint8_t refcount = 0; +STATIC pulseio_pulsein_obj_t * handles[RMT_CHANNEL_MAX]; + +// Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset + +STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) { + //mp_printf(&mp_plat_print, "Update internal Buffer\n"); + uint32_t length = 0; + rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 0); + if (items) { + length /= 4; + //mp_printf(&mp_plat_print, "Length%d\n",length); + // TODO: If the size of the recieve is larger than the buffer, reset it? + + for (size_t i=0; i < length; i++) { + //mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), + // items[i].level0,(items[i].duration1 * 3),items[i].level1); + uint16_t pos = (self->start + self->len) % self->maxlen; + self->buffer[pos] = items[i].duration0 * 3; + // Check if second item exists before incrementing + if (items[i].duration1) { + self->buffer[pos+1] = items[i].duration1 * 3; + if (self->len < (self->maxlen - 1)) { + self->len += 2; + } else { + self->start += 2; + } + } else { + if (self->len < self->maxlen) { + self->len++; + } else { + self->start++; + } + } + } + vRingbufferReturnItem(self->buf_handle, (void *) items); + } +} + +// We can't access the RMT interrupt, so we need a global service to prevent +// the ringbuffer from overflowing and crashing the peripheral +void pulsein_background(void) { + //mp_printf(&mp_plat_print, "BG Task!\n"); + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { + if (handles[i]) { + //mp_printf(&mp_plat_print, "Located viable handle:%d\n",i); + update_internal_buffer(handles[i]); + UBaseType_t items_waiting; + vRingbufferGetInfo(handles[i]->buf_handle, NULL, NULL, NULL, NULL, &items_waiting); + //mp_printf(&mp_plat_print, "items waiting:%d\n",items_waiting); + // if (items_waiting > handles[i]->maxlen) { + // mp_printf(&mp_plat_print, "Overage!\n"); + // mp_printf(&mp_plat_print, "Items waiting detected:%d\n", items_waiting); + // update_internal_buffer(handles[i]); + // } + } + } +} void pulsein_reset(void) { + mp_printf(&mp_plat_print, "Pulsein Reset called\n"); + for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { + handles[i] = NULL; + } + supervisor_disable_tick(); + refcount = 0; } void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu_pin_obj_t* pin, uint16_t maxlen, bool idle_state) { - mp_raise_NotImplementedError(translate("PulseIn not supported on this chip")); + self->buffer = (uint16_t *) m_malloc(maxlen * sizeof(uint16_t), false); + if (self->buffer == NULL) { + mp_raise_msg_varg(&mp_type_MemoryError, translate("Failed to allocate RX buffer of %d bytes"), maxlen * sizeof(uint16_t)); + } + self->pin = pin; + self->maxlen = maxlen; + // self->idle_state = idle_state; + self->start = 0; + self->len = 0; + // self->first_edge = true; + self->paused = false; + // self->last_overflow = 0; + // self->last_count = 0; + + rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); + mp_printf(&mp_plat_print, "Selected Channel:%d!\n",channel); + + // Configure Channel + rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel); + config.rx_config.filter_en = true; + config.rx_config.idle_threshold = 30000; + config.clk_div = 240; + + rmt_config(&config); + size_t len = 1000; //TODO: pick a reasonable number for this? + // size_t len = maxlen * 4; + rmt_driver_install(channel, len, 0); + + self->channel = channel; + + // Store handle for background updates + handles[channel] = self; + + rmt_get_ringbuf_handle(channel, &(self->buf_handle)); + rmt_rx_start(channel, true); + + supervisor_enable_tick(); + refcount++; } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { - return false; + return handles[self->channel] ? true : false; } void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { + handles[self->channel] = NULL; + esp32s2_peripherals_free_rmt(self->channel); + reset_pin_number(self->pin); + refcount--; + if (refcount == 0) { + supervisor_disable_tick(); + } } void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { + self->paused = true; + rmt_rx_stop(); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { + // Make sure we're paused. + if ( !self->paused ) { + common_hal_pulseio_pulsein_pause(self); + } + + self->paused = false; + rmt_rx_start(); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { + self->start = 0; + self->len = 0; } uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) { - return false; + //mp_printf(&mp_plat_print, "Call GetItem\n"); + update_internal_buffer(self); + if (index < 0) { + index += self->len; + } + if (index < 0 || index >= self->len) { + mp_raise_IndexError(translate("index out of range")); + } + uint16_t value = self->buffer[(self->start + index) % self->maxlen]; + return value; } uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { - return false; + mp_printf(&mp_plat_print, "Call PopLeft\n"); + update_internal_buffer(self); + + if (self->len == 0) { + mp_raise_IndexError(translate("pop from an empty PulseIn")); + } + + uint16_t value = self->buffer[self->start]; + self->start = (self->start + 1) % self->maxlen; + self->len--; + + return value; + + + // uint32_t length = 0; + // rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 10); + // mp_printf(&mp_plat_print, "Length%d\n",length); + // if (items) { + // length /= 4; + // mp_printf(&mp_plat_print, "Length%d\n",length); + // for (size_t i=0; i < length; i++) { + // mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), + // items[i].level0,(items[i].duration1 * 3),items[i].level1); + // } + // vRingbufferReturnItem(self->buf_handle, (void *) items); + // } + // // while(items) { + // // mp_printf(&mp_plat_print, "Length%d",length); + // // mp_printf(&mp_plat_print, "Item val0:%d, val1:%d, val:%d",items->duration0, + // // items->duration1,items->val); + // // vRingbufferReturnItem(self->buf_handle, (void *) items); + // // items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 1); + // // } + // return items ? items[0].duration0 : false; } uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { - return false; + return self->maxlen; } bool common_hal_pulseio_pulsein_get_paused(pulseio_pulsein_obj_t* self) { - return false; + return self->paused; } uint16_t common_hal_pulseio_pulsein_get_len(pulseio_pulsein_obj_t* self) { - return false; + return self->len; } diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.h b/ports/esp32s2/common-hal/pulseio/PulseIn.h index b317c516c1..1ac47cfe10 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseIn.h +++ b/ports/esp32s2/common-hal/pulseio/PulseIn.h @@ -30,13 +30,18 @@ #include "common-hal/microcontroller/Pin.h" #include "py/obj.h" +#include "driver/rmt.h" +#include "rmt.h" typedef struct { mp_obj_base_t base; const mcu_pin_obj_t* pin; + rmt_channel_t channel; bool idle_state; bool paused; + + RingbufHandle_t buf_handle; volatile bool first_edge; uint16_t* buffer; @@ -49,5 +54,6 @@ typedef struct { } pulseio_pulsein_obj_t; void pulsein_reset(void); +void pulsein_background(void); #endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_PULSEIO_PULSEIN_H diff --git a/ports/esp32s2/peripherals/rmt.c b/ports/esp32s2/peripherals/rmt.c index 16605edf5e..c3a063eecc 100644 --- a/ports/esp32s2/peripherals/rmt.c +++ b/ports/esp32s2/peripherals/rmt.c @@ -30,6 +30,7 @@ bool rmt_reserved_channels[RMT_CHANNEL_MAX]; void esp32s2_peripherals_rmt_reset(void) { + mp_printf(&mp_plat_print, "RMT Reset called\n"); for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { if (rmt_reserved_channels[i]) { esp32s2_peripherals_free_rmt(i); diff --git a/ports/esp32s2/supervisor/port.c b/ports/esp32s2/supervisor/port.c index d287079b79..46590b5513 100644 --- a/ports/esp32s2/supervisor/port.c +++ b/ports/esp32s2/supervisor/port.c @@ -39,6 +39,7 @@ #include "common-hal/busio/SPI.h" #include "common-hal/busio/UART.h" #include "common-hal/pulseio/PWMOut.h" +#include "common-hal/pulseio/PulseIn.h" #include "supervisor/memory.h" #include "supervisor/shared/tick.h" @@ -70,6 +71,7 @@ void reset_port(void) { #if CIRCUITPY_PULSEIO esp32s2_peripherals_rmt_reset(); pwmout_reset(); + pulsein_reset(); #endif #if CIRCUITPY_BUSIO i2c_reset(); From 0fc730bc5aeab1187a0b175aa4d390c0e7da7e1b Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Fri, 14 Aug 2020 16:33:24 -0400 Subject: [PATCH 03/11] Expand PulseOut API, debug cleanup --- .../atmel-samd/common-hal/pulseio/PulseOut.c | 10 +- ports/cxd56/common-hal/pulseio/PulseOut.c | 12 ++- ports/esp32s2/common-hal/pulseio/PulseIn.c | 91 +++++++------------ ports/esp32s2/common-hal/pulseio/PulseIn.h | 3 - ports/esp32s2/common-hal/pulseio/PulseOut.c | 12 ++- ports/esp32s2/mpconfigport.h | 2 - ports/esp32s2/peripherals/rmt.c | 1 - .../mimxrt10xx/common-hal/pulseio/PulseOut.c | 5 +- ports/nrf/common-hal/pulseio/PulseOut.c | 10 +- ports/stm/common-hal/pulseio/PulseOut.c | 10 +- shared-bindings/pulseio/PulseOut.c | 39 ++++---- shared-bindings/pulseio/PulseOut.h | 10 +- 12 files changed, 102 insertions(+), 103 deletions(-) diff --git a/ports/atmel-samd/common-hal/pulseio/PulseOut.c b/ports/atmel-samd/common-hal/pulseio/PulseOut.c index e9b2137cb0..de2f7b8d2a 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseOut.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseOut.c @@ -96,7 +96,15 @@ void pulseout_reset() { } void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier) { + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (!carrier || pin || frequency) { + mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ + Construct and pass a PWM Carrier instead")); + } + if (refcount == 0) { // Find a spare timer. Tc *tc = NULL; diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.c b/ports/cxd56/common-hal/pulseio/PulseOut.c index 21b4c77e55..2851a04658 100644 --- a/ports/cxd56/common-hal/pulseio/PulseOut.c +++ b/ports/cxd56/common-hal/pulseio/PulseOut.c @@ -58,8 +58,16 @@ static bool pulseout_timer_handler(unsigned int *next_interval_us, void *arg) return true; } -void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t *self, - const pulseio_pwmout_obj_t *carrier) { +void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (!carrier || pin || frequency) { + mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ + Construct and pass a PWM Carrier instead")); + } + if (pulse_fd < 0) { pulse_fd = open("/dev/timer0", O_RDONLY); } diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.c b/ports/esp32s2/common-hal/pulseio/PulseIn.c index 5209c51ca4..d741628305 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseIn.c +++ b/ports/esp32s2/common-hal/pulseio/PulseIn.c @@ -25,6 +25,7 @@ */ #include "common-hal/pulseio/PulseIn.h" +#include "shared-bindings/microcontroller/__init__.h" #include "py/runtime.h" STATIC uint8_t refcount = 0; @@ -33,17 +34,11 @@ STATIC pulseio_pulsein_obj_t * handles[RMT_CHANNEL_MAX]; // Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) { - //mp_printf(&mp_plat_print, "Update internal Buffer\n"); uint32_t length = 0; rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 0); if (items) { length /= 4; - //mp_printf(&mp_plat_print, "Length%d\n",length); - // TODO: If the size of the recieve is larger than the buffer, reset it? - for (size_t i=0; i < length; i++) { - //mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), - // items[i].level0,(items[i].duration1 * 3),items[i].level1); uint16_t pos = (self->start + self->len) % self->maxlen; self->buffer[pos] = items[i].duration0 * 3; // Check if second item exists before incrementing @@ -69,25 +64,16 @@ STATIC void update_internal_buffer(pulseio_pulsein_obj_t* self) { // We can't access the RMT interrupt, so we need a global service to prevent // the ringbuffer from overflowing and crashing the peripheral void pulsein_background(void) { - //mp_printf(&mp_plat_print, "BG Task!\n"); for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { if (handles[i]) { - //mp_printf(&mp_plat_print, "Located viable handle:%d\n",i); update_internal_buffer(handles[i]); UBaseType_t items_waiting; vRingbufferGetInfo(handles[i]->buf_handle, NULL, NULL, NULL, NULL, &items_waiting); - //mp_printf(&mp_plat_print, "items waiting:%d\n",items_waiting); - // if (items_waiting > handles[i]->maxlen) { - // mp_printf(&mp_plat_print, "Overage!\n"); - // mp_printf(&mp_plat_print, "Items waiting detected:%d\n", items_waiting); - // update_internal_buffer(handles[i]); - // } } } } void pulsein_reset(void) { - mp_printf(&mp_plat_print, "Pulsein Reset called\n"); for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { handles[i] = NULL; } @@ -103,48 +89,48 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu } self->pin = pin; self->maxlen = maxlen; - // self->idle_state = idle_state; + self->idle_state = idle_state; self->start = 0; self->len = 0; - // self->first_edge = true; self->paused = false; - // self->last_overflow = 0; - // self->last_count = 0; + // Set pull settings + gpio_pullup_dis(pin->number); + gpio_pulldown_dis(pin->number); + if (idle_state) { + gpio_pullup_en(pin->number); + } else { + gpio_pulldown_en(pin->number); + } + + // Find a free RMT Channel and configure it rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); - mp_printf(&mp_plat_print, "Selected Channel:%d!\n",channel); - - // Configure Channel rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel); config.rx_config.filter_en = true; - config.rx_config.idle_threshold = 30000; - config.clk_div = 240; - + config.rx_config.idle_threshold = 30000; // 30*3=90ms idle required to register a sequence + config.clk_div = 240; // All measurements are divided by 3 to accomodate 65ms pulses rmt_config(&config); - size_t len = 1000; //TODO: pick a reasonable number for this? - // size_t len = maxlen * 4; - rmt_driver_install(channel, len, 0); + rmt_driver_install(channel, 1000, 0); //TODO: pick a more specific buffer size? + // Store this object and the buffer handle for background updates self->channel = channel; - - // Store handle for background updates handles[channel] = self; - rmt_get_ringbuf_handle(channel, &(self->buf_handle)); - rmt_rx_start(channel, true); + // start RMT RX, and enable ticks so the core doesn't turn off. + rmt_rx_start(channel, true); supervisor_enable_tick(); refcount++; } bool common_hal_pulseio_pulsein_deinited(pulseio_pulsein_obj_t* self) { - return handles[self->channel] ? true : false; + return handles[self->channel] ? false : true; } void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { handles[self->channel] = NULL; esp32s2_peripherals_free_rmt(self->channel); - reset_pin_number(self->pin); + reset_pin_number(self->pin->number); refcount--; if (refcount == 0) { supervisor_disable_tick(); @@ -153,7 +139,7 @@ void common_hal_pulseio_pulsein_deinit(pulseio_pulsein_obj_t* self) { void common_hal_pulseio_pulsein_pause(pulseio_pulsein_obj_t* self) { self->paused = true; - rmt_rx_stop(); + rmt_rx_stop(self->channel); } void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t trigger_duration) { @@ -162,17 +148,25 @@ void common_hal_pulseio_pulsein_resume(pulseio_pulsein_obj_t* self, uint16_t tri common_hal_pulseio_pulsein_pause(self); } + if (trigger_duration > 0) { + gpio_set_direction(self->pin->number, GPIO_MODE_DEF_OUTPUT); + gpio_set_level(self->pin->number, !self->idle_state); + common_hal_mcu_delay_us((uint32_t)trigger_duration); + gpio_set_level(self->pin->number, self->idle_state); + gpio_set_direction(self->pin->number, GPIO_MODE_INPUT); // should revert to pull direction + } + self->paused = false; - rmt_rx_start(); + rmt_rx_start(self->channel, false); } void common_hal_pulseio_pulsein_clear(pulseio_pulsein_obj_t* self) { + // Buffer only updates in BG tasks or fetches, so no extra protection is needed self->start = 0; self->len = 0; } uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_t index) { - //mp_printf(&mp_plat_print, "Call GetItem\n"); update_internal_buffer(self); if (index < 0) { index += self->len; @@ -185,7 +179,6 @@ uint16_t common_hal_pulseio_pulsein_get_item(pulseio_pulsein_obj_t* self, int16_ } uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { - mp_printf(&mp_plat_print, "Call PopLeft\n"); update_internal_buffer(self); if (self->len == 0) { @@ -197,28 +190,6 @@ uint16_t common_hal_pulseio_pulsein_popleft(pulseio_pulsein_obj_t* self) { self->len--; return value; - - - // uint32_t length = 0; - // rmt_item32_t *items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 10); - // mp_printf(&mp_plat_print, "Length%d\n",length); - // if (items) { - // length /= 4; - // mp_printf(&mp_plat_print, "Length%d\n",length); - // for (size_t i=0; i < length; i++) { - // mp_printf(&mp_plat_print, "Item d0:%d, l0:%d, d1:%d, l1:%d\n",(items[i].duration0 * 3), - // items[i].level0,(items[i].duration1 * 3),items[i].level1); - // } - // vRingbufferReturnItem(self->buf_handle, (void *) items); - // } - // // while(items) { - // // mp_printf(&mp_plat_print, "Length%d",length); - // // mp_printf(&mp_plat_print, "Item val0:%d, val1:%d, val:%d",items->duration0, - // // items->duration1,items->val); - // // vRingbufferReturnItem(self->buf_handle, (void *) items); - // // items = (rmt_item32_t *) xRingbufferReceive(self->buf_handle, &length, 1); - // // } - // return items ? items[0].duration0 : false; } uint16_t common_hal_pulseio_pulsein_get_maxlen(pulseio_pulsein_obj_t* self) { diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.h b/ports/esp32s2/common-hal/pulseio/PulseIn.h index 1ac47cfe10..97d70d4b03 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseIn.h +++ b/ports/esp32s2/common-hal/pulseio/PulseIn.h @@ -42,15 +42,12 @@ typedef struct { bool paused; RingbufHandle_t buf_handle; - volatile bool first_edge; uint16_t* buffer; uint16_t maxlen; volatile uint16_t start; volatile uint16_t len; - volatile uint32_t last_overflow; - volatile uint16_t last_count; } pulseio_pulsein_obj_t; void pulsein_reset(void); diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.c b/ports/esp32s2/common-hal/pulseio/PulseOut.c index dc93d7e964..463443ef0c 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseOut.c +++ b/ports/esp32s2/common-hal/pulseio/PulseOut.c @@ -32,15 +32,21 @@ // Requires rmt.c void esp32s2_peripherals_reset_all(void) to reset void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const mcu_pin_obj_t* pin, - uint32_t frequency) { + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (carrier || !pin || !frequency) { + mp_raise_NotImplementedError(translate("Port does not accept PWM carrier. \ + Pass a pin, frequency and duty cycle instead")); + } rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); // Configure Channel rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel); config.tx_config.carrier_en = true; - config.tx_config.carrier_duty_percent = 50; + config.tx_config.carrier_duty_percent = (duty_cycle * 100) / (1<<16); config.tx_config.carrier_freq_hz = frequency; config.clk_div = 80; diff --git a/ports/esp32s2/mpconfigport.h b/ports/esp32s2/mpconfigport.h index 6b070afea6..07f83434af 100644 --- a/ports/esp32s2/mpconfigport.h +++ b/ports/esp32s2/mpconfigport.h @@ -36,8 +36,6 @@ #include "py/circuitpy_mpconfig.h" -#define CPY_PULSEOUT_USES_DIGITALIO (1) - #define MICROPY_PORT_ROOT_POINTERS \ CIRCUITPY_COMMON_ROOT_POINTERS #define MICROPY_NLR_SETJMP (1) diff --git a/ports/esp32s2/peripherals/rmt.c b/ports/esp32s2/peripherals/rmt.c index c3a063eecc..16605edf5e 100644 --- a/ports/esp32s2/peripherals/rmt.c +++ b/ports/esp32s2/peripherals/rmt.c @@ -30,7 +30,6 @@ bool rmt_reserved_channels[RMT_CHANNEL_MAX]; void esp32s2_peripherals_rmt_reset(void) { - mp_printf(&mp_plat_print, "RMT Reset called\n"); for (size_t i = 0; i < RMT_CHANNEL_MAX; i++) { if (rmt_reserved_channels[i]) { esp32s2_peripherals_free_rmt(i); diff --git a/ports/mimxrt10xx/common-hal/pulseio/PulseOut.c b/ports/mimxrt10xx/common-hal/pulseio/PulseOut.c index ffa885688a..3aefd87662 100644 --- a/ports/mimxrt10xx/common-hal/pulseio/PulseOut.c +++ b/ports/mimxrt10xx/common-hal/pulseio/PulseOut.c @@ -94,7 +94,10 @@ void pulseout_reset() { } void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier) { + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { // if (refcount == 0) { // // Find a spare timer. // Tc *tc = NULL; diff --git a/ports/nrf/common-hal/pulseio/PulseOut.c b/ports/nrf/common-hal/pulseio/PulseOut.c index 270cb45d6d..9f5efcfdc6 100644 --- a/ports/nrf/common-hal/pulseio/PulseOut.c +++ b/ports/nrf/common-hal/pulseio/PulseOut.c @@ -100,7 +100,15 @@ void pulseout_reset() { } void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier) { + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (!carrier || pin || frequency) { + mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ + Construct and pass a PWM Carrier instead")); + } + if (refcount == 0) { timer = nrf_peripherals_allocate_timer_or_throw(); } diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index bcc25d8177..33311fd53d 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -113,7 +113,15 @@ void pulseout_reset() { } void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier) { + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle) { + if (!carrier || pin || frequency) { + mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ + Construct and pass a PWM Carrier instead")); + } + // Add to active PulseOuts refcount++; TIM_TypeDef * tim_instance = stm_peripherals_find_timer(); diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 10524f51b3..2adbc3cf6a 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -69,29 +69,24 @@ STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_ar pulseio_pulseout_obj_t *self = m_new_obj(pulseio_pulseout_obj_t); self->base.type = &pulseio_pulseout_type; - #ifndef CPY_PULSEOUT_USES_DIGITALIO - // Most ports pass a PWMOut - mp_arg_check_num(n_args, kw_args, 1, 1, false); - mp_obj_t carrier_obj = args[0]; - if (!MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { - mp_raise_TypeError_varg(translate("Expected a %q"), pulseio_pwmout_type.name); + mp_obj_t carrier_obj = pos_args[0]; + if (MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { + // PWM Carrier + mp_arg_check_num(n_args, kw_args, 1, 1, false); + common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj), NULL, 0, 0); + } else { + // Pin and Frequency + enum { ARG_pin, ARG_frequency}; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 38000} }, + { MP_QSTR_duty_cycle, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 1<<15} }, + }; + 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); + const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); + common_hal_pulseio_pulseout_construct(self, NULL, pin, args[ARG_frequency].u_int, args[ARG_frequency].u_int); } - common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj)); - #else - // ESP32-S2 Special Case - enum { ARG_pin, ARG_frequency}; - static const mp_arg_t allowed_args[] = { - { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, - { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 38000} }, - }; - 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); - - const mcu_pin_obj_t* pin = validate_obj_is_free_pin(args[ARG_pin].u_obj); - - common_hal_pulseio_pulseout_construct(self, pin, args[ARG_frequency].u_int); - #endif - return MP_OBJ_FROM_PTR(self); } diff --git a/shared-bindings/pulseio/PulseOut.h b/shared-bindings/pulseio/PulseOut.h index 090f6d15c6..9d4778e93d 100644 --- a/shared-bindings/pulseio/PulseOut.h +++ b/shared-bindings/pulseio/PulseOut.h @@ -33,13 +33,11 @@ extern const mp_obj_type_t pulseio_pulseout_type; -#ifndef CPY_PULSEOUT_USES_DIGITALIO extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const pulseio_pwmout_obj_t* carrier); -#else -extern void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, - const mcu_pin_obj_t* pin, uint32_t frequency); -#endif + const pulseio_pwmout_obj_t* carrier, + const mcu_pin_obj_t* pin, + uint32_t frequency, + uint16_t duty_cycle); extern void common_hal_pulseio_pulseout_deinit(pulseio_pulseout_obj_t* self); extern bool common_hal_pulseio_pulseout_deinited(pulseio_pulseout_obj_t* self); From d0d6a951da9ef39f68a0d118db6c22c7e4ec9b2b Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Mon, 17 Aug 2020 10:10:49 -0400 Subject: [PATCH 04/11] Translations --- locale/circuitpython.pot | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index a1b286e2ed..b98e0e2132 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-08-14 09:36-0400\n" +"POT-Creation-Date: 2020-08-17 10:10-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -744,7 +744,7 @@ msgstr "" #: shared-bindings/aesio/aes.c shared-bindings/busio/SPI.c #: shared-bindings/microcontroller/Pin.c -#: shared-bindings/neopixel_write/__init__.c shared-bindings/pulseio/PulseOut.c +#: shared-bindings/neopixel_write/__init__.c #: shared-bindings/terminalio/Terminal.c msgid "Expected a %q" msgstr "" @@ -1328,6 +1328,15 @@ msgstr "" msgid "Polygon needs at least 3 points" msgstr "" +#: ports/atmel-samd/common-hal/pulseio/PulseOut.c +#: ports/cxd56/common-hal/pulseio/PulseOut.c +#: ports/nrf/common-hal/pulseio/PulseOut.c +#: ports/stm/common-hal/pulseio/PulseOut.c +msgid "" +"Port does not accept pins or frequency. " +"Construct and pass a PWM Carrier instead" +msgstr "" + #: shared-bindings/_bleio/Adapter.c msgid "Prefix buffer must be on the heap" msgstr "" @@ -1340,6 +1349,10 @@ msgstr "" msgid "Pull not used when direction is output." msgstr "" +#: ports/stm/ref/pulseout-pre-timeralloc.c +msgid "PulseOut not supported on this chip" +msgstr "" + #: ports/stm/common-hal/os/__init__.c msgid "RNG DeInit Error" msgstr "" From da75445cd58d0fbf72c6b323b1b0bd53197f9cf3 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Tue, 18 Aug 2020 11:42:06 -0400 Subject: [PATCH 05/11] Style changes, reposition runtime errors --- locale/circuitpython.pot | 4 ++-- ports/atmel-samd/common-hal/pulseio/PulseOut.c | 2 +- ports/cxd56/common-hal/pulseio/PulseOut.c | 2 +- ports/esp32s2/common-hal/neopixel_write/__init__.c | 3 +++ ports/esp32s2/common-hal/pulseio/PulseIn.c | 3 +++ ports/esp32s2/common-hal/pulseio/PulseOut.c | 7 ++++++- ports/esp32s2/peripherals/rmt.c | 4 ++-- ports/nrf/common-hal/pulseio/PulseOut.c | 2 +- ports/stm/common-hal/pulseio/PulseOut.c | 2 +- shared-bindings/pulseio/PulseOut.c | 4 ++-- 10 files changed, 22 insertions(+), 11 deletions(-) diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index b98e0e2132..d9d5d6e17d 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2020-08-17 10:10-0400\n" +"POT-Creation-Date: 2020-08-18 11:19-0400\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -1334,7 +1334,7 @@ msgstr "" #: ports/stm/common-hal/pulseio/PulseOut.c msgid "" "Port does not accept pins or frequency. " -"Construct and pass a PWM Carrier instead" +"Construct and pass a PWMOut Carrier instead" msgstr "" #: shared-bindings/_bleio/Adapter.c diff --git a/ports/atmel-samd/common-hal/pulseio/PulseOut.c b/ports/atmel-samd/common-hal/pulseio/PulseOut.c index de2f7b8d2a..b53c8da2ba 100644 --- a/ports/atmel-samd/common-hal/pulseio/PulseOut.c +++ b/ports/atmel-samd/common-hal/pulseio/PulseOut.c @@ -102,7 +102,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, uint16_t duty_cycle) { if (!carrier || pin || frequency) { mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ - Construct and pass a PWM Carrier instead")); + Construct and pass a PWMOut Carrier instead")); } if (refcount == 0) { diff --git a/ports/cxd56/common-hal/pulseio/PulseOut.c b/ports/cxd56/common-hal/pulseio/PulseOut.c index 2851a04658..08081020f3 100644 --- a/ports/cxd56/common-hal/pulseio/PulseOut.c +++ b/ports/cxd56/common-hal/pulseio/PulseOut.c @@ -65,7 +65,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, uint16_t duty_cycle) { if (!carrier || pin || frequency) { mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ - Construct and pass a PWM Carrier instead")); + Construct and pass a PWMOut Carrier instead")); } if (pulse_fd < 0) { diff --git a/ports/esp32s2/common-hal/neopixel_write/__init__.c b/ports/esp32s2/common-hal/neopixel_write/__init__.c index 1fc976ec36..193d754f43 100644 --- a/ports/esp32s2/common-hal/neopixel_write/__init__.c +++ b/ports/esp32s2/common-hal/neopixel_write/__init__.c @@ -93,6 +93,9 @@ void common_hal_neopixel_write (const digitalio_digitalinout_obj_t* digitalinout // Reserve channel uint8_t number = digitalinout->pin->number; rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); + if (channel == RMT_CHANNEL_MAX) { + mp_raise_RuntimeError(translate("All timers in use")); + } // Configure Channel rmt_config_t config = RMT_DEFAULT_CONFIG_TX(number, channel); diff --git a/ports/esp32s2/common-hal/pulseio/PulseIn.c b/ports/esp32s2/common-hal/pulseio/PulseIn.c index d741628305..f7429ec12c 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseIn.c +++ b/ports/esp32s2/common-hal/pulseio/PulseIn.c @@ -105,6 +105,9 @@ void common_hal_pulseio_pulsein_construct(pulseio_pulsein_obj_t* self, const mcu // Find a free RMT Channel and configure it rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); + if (channel == RMT_CHANNEL_MAX) { + mp_raise_RuntimeError(translate("All timers in use")); + } rmt_config_t config = RMT_DEFAULT_CONFIG_RX(pin->number, channel); config.rx_config.filter_en = true; config.rx_config.idle_threshold = 30000; // 30*3=90ms idle required to register a sequence diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.c b/ports/esp32s2/common-hal/pulseio/PulseOut.c index 463443ef0c..a07ec39dc2 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseOut.c +++ b/ports/esp32s2/common-hal/pulseio/PulseOut.c @@ -42,6 +42,9 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, } rmt_channel_t channel = esp32s2_peripherals_find_and_reserve_rmt(); + if (channel == RMT_CHANNEL_MAX) { + mp_raise_RuntimeError(translate("All timers in use")); + } // Configure Channel rmt_config_t config = RMT_DEFAULT_CONFIG_TX(pin->number, channel); @@ -82,5 +85,7 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu } rmt_write_items(self->channel, items, length, true); - rmt_wait_tx_done(self->channel, pdMS_TO_TICKS(100)); + while (rmt_wait_tx_done(self->channel, 0) != ESP_OK) { + RUN_BACKGROUND_TASKS(); + } } diff --git a/ports/esp32s2/peripherals/rmt.c b/ports/esp32s2/peripherals/rmt.c index 16605edf5e..b7629dbd5c 100644 --- a/ports/esp32s2/peripherals/rmt.c +++ b/ports/esp32s2/peripherals/rmt.c @@ -44,8 +44,8 @@ rmt_channel_t esp32s2_peripherals_find_and_reserve_rmt(void) { return i; } } - mp_raise_RuntimeError(translate("All timers in use")); - return false; + // Returning the max indicates a reservation failure. + return RMT_CHANNEL_MAX; } void esp32s2_peripherals_free_rmt(rmt_channel_t chan) { diff --git a/ports/nrf/common-hal/pulseio/PulseOut.c b/ports/nrf/common-hal/pulseio/PulseOut.c index 9f5efcfdc6..bdc5cfbef5 100644 --- a/ports/nrf/common-hal/pulseio/PulseOut.c +++ b/ports/nrf/common-hal/pulseio/PulseOut.c @@ -106,7 +106,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, uint16_t duty_cycle) { if (!carrier || pin || frequency) { mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ - Construct and pass a PWM Carrier instead")); + Construct and pass a PWMOut Carrier instead")); } if (refcount == 0) { diff --git a/ports/stm/common-hal/pulseio/PulseOut.c b/ports/stm/common-hal/pulseio/PulseOut.c index 33311fd53d..eec1049dd2 100644 --- a/ports/stm/common-hal/pulseio/PulseOut.c +++ b/ports/stm/common-hal/pulseio/PulseOut.c @@ -119,7 +119,7 @@ void common_hal_pulseio_pulseout_construct(pulseio_pulseout_obj_t* self, uint16_t duty_cycle) { if (!carrier || pin || frequency) { mp_raise_NotImplementedError(translate("Port does not accept pins or frequency. \ - Construct and pass a PWM Carrier instead")); + Construct and pass a PWMOut Carrier instead")); } // Add to active PulseOuts diff --git a/shared-bindings/pulseio/PulseOut.c b/shared-bindings/pulseio/PulseOut.c index 2adbc3cf6a..3fd722558e 100644 --- a/shared-bindings/pulseio/PulseOut.c +++ b/shared-bindings/pulseio/PulseOut.c @@ -71,11 +71,11 @@ STATIC mp_obj_t pulseio_pulseout_make_new(const mp_obj_type_t *type, size_t n_ar mp_obj_t carrier_obj = pos_args[0]; if (MP_OBJ_IS_TYPE(carrier_obj, &pulseio_pwmout_type)) { - // PWM Carrier + // Use a PWMOut Carrier mp_arg_check_num(n_args, kw_args, 1, 1, false); common_hal_pulseio_pulseout_construct(self, (pulseio_pwmout_obj_t *)MP_OBJ_TO_PTR(carrier_obj), NULL, 0, 0); } else { - // Pin and Frequency + // Use a Pin, frequency, and duty cycle enum { ARG_pin, ARG_frequency}; static const mp_arg_t allowed_args[] = { { MP_QSTR_pin, MP_ARG_REQUIRED | MP_ARG_OBJ }, From dd425ee6c54deee72d0fa7cd42282b8157a54da6 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Tue, 18 Aug 2020 15:54:50 -0400 Subject: [PATCH 06/11] Remove breaking parenthesis --- ports/esp32s2/common-hal/pulseio/PulseOut.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/esp32s2/common-hal/pulseio/PulseOut.c b/ports/esp32s2/common-hal/pulseio/PulseOut.c index a07ec39dc2..d3c163ca8b 100644 --- a/ports/esp32s2/common-hal/pulseio/PulseOut.c +++ b/ports/esp32s2/common-hal/pulseio/PulseOut.c @@ -86,6 +86,6 @@ void common_hal_pulseio_pulseout_send(pulseio_pulseout_obj_t* self, uint16_t* pu rmt_write_items(self->channel, items, length, true); while (rmt_wait_tx_done(self->channel, 0) != ESP_OK) { - RUN_BACKGROUND_TASKS(); + RUN_BACKGROUND_TASKS; } } From 50560272383f56b937c349df28e06cd73f39fd3c Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Tue, 18 Aug 2020 19:17:59 -0500 Subject: [PATCH 07/11] gen_display_resources: report missing character count, if any --- tools/gen_display_resources.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/gen_display_resources.py b/tools/gen_display_resources.py index e4197aabf4..6657b6a272 100644 --- a/tools/gen_display_resources.py +++ b/tools/gen_display_resources.py @@ -58,15 +58,20 @@ filtered_characters = all_characters # Try to pre-load all of the glyphs. Misses will still be slow later. f.load_glyphs(set(ord(c) for c in all_characters)) +missing = 0 # Get each glyph. for c in set(all_characters): if ord(c) not in f._glyphs: + missing += 1 filtered_characters = filtered_characters.replace(c, "") continue g = f.get_glyph(ord(c)) if g["shift"][1] != 0: raise RuntimeError("y shift") +if missing > 0: + print("Font missing", missing, "characters", file=sys.stderr) + x, y, dx, dy = f.get_bounding_box() tile_x, tile_y = x - dx, y - dy total_bits = tile_x * len(all_characters) From 6ae5bedf3b7cc5adfd57202f757c335dce54c790 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 19 Aug 2020 11:27:52 -0500 Subject: [PATCH 08/11] SAM E54: Use correct CFG_TUSB_MCU This doesn't actually change the code that is built, but it is "more correct". --- ports/atmel-samd/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/atmel-samd/Makefile b/ports/atmel-samd/Makefile index 65542354e6..30487ae3ef 100644 --- a/ports/atmel-samd/Makefile +++ b/ports/atmel-samd/Makefile @@ -103,7 +103,7 @@ ifeq ($(CHIP_FAMILY), same54) PERIPHERALS_CHIP_FAMILY=sam_d5x_e5x OPTIMIZATION_FLAGS ?= -O2 # TinyUSB defines -CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD51 -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=1024 +CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAME5X -DCFG_TUD_MIDI_RX_BUFSIZE=128 -DCFG_TUD_CDC_RX_BUFSIZE=256 -DCFG_TUD_MIDI_TX_BUFSIZE=128 -DCFG_TUD_CDC_TX_BUFSIZE=256 -DCFG_TUD_MSC_BUFSIZE=1024 endif # option to override default optimization level, set in boards/$(BOARD)/mpconfigboard.mk From 23aefe9509f7c58acd53d96b5d26a90b7b5b15db Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Wed, 19 Aug 2020 16:47:20 -0500 Subject: [PATCH 09/11] Add usb-endpoint-count checking .. however, the number of endpoints is only set for SAMD (8). Other ports need to set the value. Otherwise, the build will show the message ``` Unable to check whether maximum number of endpoints is respected ``` --- ports/atmel-samd/mpconfigport.mk | 2 ++ supervisor/supervisor.mk | 5 +++++ tools/gen_usb_descriptor.py | 11 +++++++++++ 3 files changed, 18 insertions(+) diff --git a/ports/atmel-samd/mpconfigport.mk b/ports/atmel-samd/mpconfigport.mk index 3fecf867e0..a16daf4b00 100644 --- a/ports/atmel-samd/mpconfigport.mk +++ b/ports/atmel-samd/mpconfigport.mk @@ -91,3 +91,5 @@ endif # samd51 INTERNAL_LIBM = 1 USB_SERIAL_NUMBER_LENGTH = 32 + +USB_NUM_EP = 8 diff --git a/supervisor/supervisor.mk b/supervisor/supervisor.mk index 25b240036d..c876a11cce 100644 --- a/supervisor/supervisor.mk +++ b/supervisor/supervisor.mk @@ -171,6 +171,10 @@ ifndef USB_MIDI_EP_NUM_IN USB_MIDI_EP_NUM_IN = 0 endif +ifndef USB_NUM_EP +USB_NUM_EP = 0 +endif + USB_DESCRIPTOR_ARGS = \ --manufacturer $(USB_MANUFACTURER)\ --product $(USB_PRODUCT)\ @@ -180,6 +184,7 @@ USB_DESCRIPTOR_ARGS = \ --interface_name $(USB_INTERFACE_NAME)\ --devices $(USB_DEVICES)\ --hid_devices $(USB_HID_DEVICES)\ + --max_ep $(USB_NUM_EP) \ --cdc_ep_num_notification $(USB_CDC_EP_NUM_NOTIFICATION)\ --cdc_ep_num_data_out $(USB_CDC_EP_NUM_DATA_OUT)\ --cdc_ep_num_data_in $(USB_CDC_EP_NUM_DATA_IN)\ diff --git a/tools/gen_usb_descriptor.py b/tools/gen_usb_descriptor.py index fb91fd3345..baee8cad7b 100644 --- a/tools/gen_usb_descriptor.py +++ b/tools/gen_usb_descriptor.py @@ -62,6 +62,8 @@ parser.add_argument('--midi_ep_num_out', type=int, default=0, help='endpoint number of MIDI OUT') parser.add_argument('--midi_ep_num_in', type=int, default=0, help='endpoint number of MIDI IN') +parser.add_argument('--max_ep', type=int, default=0, + help='total number of endpoints available') parser.add_argument('--output_c_file', type=argparse.FileType('w', encoding='UTF-8'), required=True) parser.add_argument('--output_h_file', type=argparse.FileType('w', encoding='UTF-8'), required=True) @@ -376,6 +378,15 @@ if 'AUDIO' in args.devices: # interface cross-references. interfaces = util.join_interfaces(interfaces_to_join, renumber_endpoints=args.renumber_endpoints) +if args.max_ep != 0: + for interface in interfaces: + for subdescriptor in interface.subdescriptors: + endpoint_address = getattr(subdescriptor, 'bEndpointAddress', 0) & 0x7f + if endpoint_address > args.max_ep: + raise ValueError("Endpoint address %d of %s may not exceed %d" % (endpoint_address & 0x7f, interface.description, args.max_ep)) +else: + print("Unable to check whether maximum number of endpoints is respected", file=sys.stderr) + # Now adjust the CDC interface cross-references. cdc_union.bMasterInterface = cdc_comm_interface.bInterfaceNumber From ebabc5db37f1c8e86c099f0af386d56823eff431 Mon Sep 17 00:00:00 2001 From: George Waters Date: Wed, 19 Aug 2020 23:25:20 -0400 Subject: [PATCH 10/11] Handle home, delete, & emacs key w/ utf-8 in repl --- lib/mp-readline/readline.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index 464916ca4e..6da71c40f5 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -144,7 +144,7 @@ int readline_process_char(int c) { goto right_arrow_key; } else if (c == CHAR_CTRL_K) { // CTRL-K is kill from cursor to end-of-line, inclusive - vstr_cut_tail_bytes(rl.line, last_line_len - rl.cursor_pos); + vstr_cut_tail_bytes(rl.line, rl.line->len - rl.cursor_pos); // set redraw parameters redraw_from_cursor = true; } else if (c == CHAR_CTRL_N) { @@ -155,6 +155,7 @@ int readline_process_char(int c) { goto up_arrow_key; } else if (c == CHAR_CTRL_U) { // CTRL-U is kill from beginning-of-line up to cursor + cont_chars = count_cont_bytes(rl.line->buf+rl.orig_line_len, rl.line->buf+rl.cursor_pos); vstr_cut_out_bytes(rl.line, rl.orig_line_len, rl.cursor_pos - rl.orig_line_len); // set redraw parameters redraw_step_back = rl.cursor_pos - rl.orig_line_len; @@ -342,6 +343,7 @@ left_arrow_key: if (c == '~') { if (rl.escape_seq_buf[0] == '1' || rl.escape_seq_buf[0] == '7') { home_key: + cont_chars = count_cont_bytes(rl.line->buf+rl.orig_line_len, rl.line->buf+rl.cursor_pos); redraw_step_back = rl.cursor_pos - rl.orig_line_len; } else if (rl.escape_seq_buf[0] == '4' || rl.escape_seq_buf[0] == '8') { end_key: @@ -352,7 +354,12 @@ end_key: delete_key: #endif if (rl.cursor_pos < rl.line->len) { - vstr_cut_out_bytes(rl.line, rl.cursor_pos, 1); + size_t len = 1; + while (UTF8_IS_CONT(rl.line->buf[rl.cursor_pos+len]) && + rl.cursor_pos+len < rl.line->len) { + len++; + } + vstr_cut_out_bytes(rl.line, rl.cursor_pos, len); redraw_from_cursor = true; } } else { From e2891bc7d41a06dcee30daf521cd8693d38402bb Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Thu, 20 Aug 2020 16:21:25 +0200 Subject: [PATCH 11/11] spresense: call usb_background function --- ports/cxd56/supervisor/port.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index a40d31fafb..bbd864f903 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -36,6 +36,8 @@ #include "boards/board.h" #include "supervisor/port.h" +#include "supervisor/background_callback.h" +#include "supervisor/usb.h" #include "supervisor/shared/tick.h" #include "common-hal/microcontroller/Pin.h" @@ -114,6 +116,11 @@ uint32_t port_get_saved_word(void) { return _ebss; } +static background_callback_t callback; +static void usb_background_do(void* unused) { + usb_background(); +} + volatile bool _tick_enabled; void board_timerhook(void) { @@ -121,6 +128,8 @@ void board_timerhook(void) if (_tick_enabled) { supervisor_tick(); } + + background_callback_add(&callback, usb_background_do, NULL); } uint64_t port_get_raw_ticks(uint8_t* subticks) {