|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2021 Jim Mussared |
| 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 | +// This is a translation of the cycle counter implementation in ports/stm32/machine_bitstream.c. |
| 28 | + |
| 29 | +#include "py/mpconfig.h" |
| 30 | +#include "py/mphal.h" |
| 31 | + |
| 32 | +#if MICROPY_PY_MACHINE_BITSTREAM |
| 33 | + |
| 34 | +#define NS_TICKS_OVERHEAD (6) |
| 35 | + |
| 36 | +void IRAM_ATTR machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { |
| 37 | + uint32_t pin_mask, gpio_reg_set, gpio_reg_clear; |
| 38 | + #if !CONFIG_IDF_TARGET_ESP32C3 |
| 39 | + if (pin >= 32) { |
| 40 | + pin_mask = 1 << (pin - 32); |
| 41 | + gpio_reg_set = GPIO_OUT1_W1TS_REG; |
| 42 | + gpio_reg_clear = GPIO_OUT1_W1TC_REG; |
| 43 | + } else |
| 44 | + #endif |
| 45 | + { |
| 46 | + pin_mask = 1 << pin; |
| 47 | + gpio_reg_set = GPIO_OUT_W1TS_REG; |
| 48 | + gpio_reg_clear = GPIO_OUT_W1TC_REG; |
| 49 | + } |
| 50 | + |
| 51 | + // Convert ns to cpu ticks [high_time_0, period_0, high_time_1, period_1]. |
| 52 | + uint32_t fcpu_mhz = ets_get_cpu_frequency(); |
| 53 | + for (size_t i = 0; i < 4; ++i) { |
| 54 | + timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; |
| 55 | + if (timing_ns[i] > NS_TICKS_OVERHEAD) { |
| 56 | + timing_ns[i] -= NS_TICKS_OVERHEAD; |
| 57 | + } |
| 58 | + if (i % 2 == 1) { |
| 59 | + // Convert low_time to period (i.e. add high_time). |
| 60 | + timing_ns[i] += timing_ns[i - 1]; |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + uint32_t irq_state = mp_hal_quiet_timing_enter(); |
| 65 | + |
| 66 | + for (size_t i = 0; i < len; ++i) { |
| 67 | + uint8_t b = buf[i]; |
| 68 | + for (size_t j = 0; j < 8; ++j) { |
| 69 | + GPIO_REG_WRITE(gpio_reg_set, pin_mask); |
| 70 | + uint32_t start_ticks = mp_hal_ticks_cpu(); |
| 71 | + uint32_t *t = &timing_ns[b >> 6 & 2]; |
| 72 | + uint32_t end_ticks = start_ticks + t[0]; |
| 73 | + while (mp_hal_ticks_cpu() - start_ticks < t[0]) { |
| 74 | + ; |
| 75 | + } |
| 76 | + GPIO_REG_WRITE(gpio_reg_clear, pin_mask); |
| 77 | + b <<= 1; |
| 78 | + end_ticks += t[1]; |
| 79 | + while (mp_hal_ticks_cpu() - start_ticks < t[1]) { |
| 80 | + ; |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + mp_hal_quiet_timing_exit(irq_state); |
| 86 | +} |
| 87 | + |
| 88 | +#endif // MICROPY_PY_MACHINE_BITSTREAM |
0 commit comments