From 61b7c098b9a21311bbcec3b43c279ec9aada311b Mon Sep 17 00:00:00 2001 From: robert-hh Date: Thu, 19 Aug 2021 22:00:38 +0200 Subject: [PATCH] mimxrt/machine_bitstream: Add bitstream function to machine module. Following the code example for ESP32 of Jim Mussard. As a side effect: - mp_hal_ticks_cpu() was implemented, - mp_hal_get_cpu_freq() and mp_hal_ticks_cpu_init() were added and used. - mp_hal_pin_high() and mp_hal_pin_low() were changed for symmetry --- ports/mimxrt/Makefile | 1 + ports/mimxrt/machine_bitstream.c | 73 ++++++++++++++++++++++++++++++++ ports/mimxrt/modmachine.c | 6 ++- ports/mimxrt/mpconfigport.h | 1 + ports/mimxrt/mphalport.h | 15 +++++-- 5 files changed, 92 insertions(+), 4 deletions(-) create mode 100644 ports/mimxrt/machine_bitstream.c diff --git a/ports/mimxrt/Makefile b/ports/mimxrt/Makefile index 58fbe2df33..8b1bb2bfa7 100644 --- a/ports/mimxrt/Makefile +++ b/ports/mimxrt/Makefile @@ -152,6 +152,7 @@ SRC_C += \ fatfs_port.c \ led.c \ machine_adc.c \ + machine_bitstream.c \ machine_i2c.c \ machine_led.c \ machine_pin.c \ diff --git a/ports/mimxrt/machine_bitstream.c b/ports/mimxrt/machine_bitstream.c new file mode 100644 index 0000000000..c6ff469ea6 --- /dev/null +++ b/ports/mimxrt/machine_bitstream.c @@ -0,0 +1,73 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2021 Jim Mussared + * + * 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. + */ + +// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c. + +#include "py/mpconfig.h" +#include "py/mphal.h" + +#if MICROPY_PY_MACHINE_BITSTREAM + +#define NS_TICKS_OVERHEAD (6) + +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) __attribute__((section(".ram_functions"))); +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { + uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000; + // Convert ns to us ticks [high_time_0, period_0, high_time_1, period_1]. + for (size_t i = 0; i < 4; ++i) { + timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; + if (timing_ns[i] > NS_TICKS_OVERHEAD) { + timing_ns[i] -= NS_TICKS_OVERHEAD; + } + if (i % 2 == 1) { + // Convert low_time to period (i.e. add high_time). + timing_ns[i] += timing_ns[i - 1]; + } + } + // Enable the CPU cycle counter, which is not always enabled. + mp_hal_ticks_cpu_init(); + + uint32_t irq_state = mp_hal_quiet_timing_enter(); + + for (size_t i = 0; i < len; ++i) { + uint8_t b = buf[i]; + for (size_t j = 0; j < 8; ++j) { + uint32_t start_ticks = mp_hal_ticks_cpu(); + mp_hal_pin_high(pin); + uint32_t *t = &timing_ns[b >> 6 & 2]; + while ((mp_hal_ticks_cpu() - start_ticks) < t[0]) { + } + mp_hal_pin_low(pin); + b <<= 1; + while ((mp_hal_ticks_cpu() - start_ticks) < t[1]) { + } + } + } + + mp_hal_quiet_timing_exit(irq_state); +} + +#endif // MICROPY_PY_MACHINE_BITSTREAM diff --git a/ports/mimxrt/modmachine.c b/ports/mimxrt/modmachine.c index a98d09bd0e..2e8753df12 100644 --- a/ports/mimxrt/modmachine.c +++ b/ports/mimxrt/modmachine.c @@ -26,6 +26,7 @@ */ #include "py/runtime.h" +#include "extmod/machine_bitstream.h" #include "extmod/machine_mem.h" #include "extmod/machine_i2c.h" #include "extmod/machine_pulse.h" @@ -45,7 +46,7 @@ STATIC mp_obj_t machine_reset(void) { MP_DEFINE_CONST_FUN_OBJ_0(machine_reset_obj, machine_reset); STATIC mp_obj_t machine_freq(void) { - return MP_OBJ_NEW_SMALL_INT(CLOCK_GetFreq(kCLOCK_CpuClk)); + return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq()); } MP_DEFINE_CONST_FUN_OBJ_0(machine_freq_obj, machine_freq); @@ -97,6 +98,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) }, { MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) }, + #if MICROPY_PY_MACHINE_BITSTREAM + { MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) }, + #endif { MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) }, }; STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table); diff --git a/ports/mimxrt/mpconfigport.h b/ports/mimxrt/mpconfigport.h index 9775b29efa..0fe6cd1291 100644 --- a/ports/mimxrt/mpconfigport.h +++ b/ports/mimxrt/mpconfigport.h @@ -124,6 +124,7 @@ uint32_t trng_random_u32(void); #define MICROPY_PY_USELECT (1) #define MICROPY_PY_MACHINE (1) #define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new +#define MICROPY_PY_MACHINE_BITSTREAM (1) #define MICROPY_PY_MACHINE_PULSE (1) #define MICROPY_PY_MACHINE_I2C (1) #define MICROPY_PY_MACHINE_SOFTI2C (1) diff --git a/ports/mimxrt/mphalport.h b/ports/mimxrt/mphalport.h index 94f43001d0..3f0ae51bb6 100644 --- a/ports/mimxrt/mphalport.h +++ b/ports/mimxrt/mphalport.h @@ -30,6 +30,7 @@ #include #include "ticks.h" #include "pin.h" +#include "fsl_clock.h" #define MP_HAL_PIN_FMT "%q" @@ -39,8 +40,8 @@ #define mp_hal_pin_input(p) machine_pin_set_mode(p, PIN_MODE_IN); #define mp_hal_pin_output(p) machine_pin_set_mode(p, PIN_MODE_OUT); #define mp_hal_pin_open_drain(p) machine_pin_set_mode(p, PIN_MODE_OPEN_DRAIN); -#define mp_hal_pin_high(p) (GPIO_PinWrite(p->gpio, p->pin, 1U)) -#define mp_hal_pin_low(p) (GPIO_PinWrite(p->gpio, p->pin, 0U)) +#define mp_hal_pin_high(p) (p->gpio->DR_SET = 1 << p->pin) +#define mp_hal_pin_low(p) (p->gpio->DR_CLEAR = 1 << p->pin) #define mp_hal_pin_write(p, value) (GPIO_PinWrite(p->gpio, p->pin, value)) #define mp_hal_pin_toggle(p) (GPIO_PortToggle(p->gpio, (1 << p->pin))) #define mp_hal_pin_read(p) (GPIO_PinReadPadStatus(p->gpio, p->pin)) @@ -72,8 +73,16 @@ static inline void mp_hal_delay_us(mp_uint_t us) { #define mp_hal_delay_us_fast(us) mp_hal_delay_us(us) +static inline void mp_hal_ticks_cpu_init(void) { + DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk; +} + static inline mp_uint_t mp_hal_ticks_cpu(void) { - return 0; + return DWT->CYCCNT; +} + +static inline mp_uint_t mp_hal_get_cpu_freq(void) { + return CLOCK_GetCpuClkFreq(); }