|
| 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 | +#include "hardware/structs/systick.h" |
| 32 | + |
| 33 | +#if MICROPY_PY_MACHINE_BITSTREAM |
| 34 | + |
| 35 | +#define MP_HAL_BITSTREAM_NS_OVERHEAD (9) |
| 36 | + |
| 37 | +void __time_critical_func(machine_bitstream_high_low)(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { |
| 38 | + uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000; |
| 39 | + // Convert ns to clock ticks [high_time_0, period_0, high_time_1, period_1]. |
| 40 | + for (size_t i = 0; i < 4; ++i) { |
| 41 | + timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; |
| 42 | + if (timing_ns[i] > (2 * MP_HAL_BITSTREAM_NS_OVERHEAD)) { |
| 43 | + timing_ns[i] -= MP_HAL_BITSTREAM_NS_OVERHEAD; |
| 44 | + } |
| 45 | + if (i % 2 == 1) { |
| 46 | + // Convert low_time to period (i.e. add high_time). |
| 47 | + timing_ns[i] += timing_ns[i - 1] - MP_HAL_BITSTREAM_NS_OVERHEAD; |
| 48 | + } |
| 49 | + } |
| 50 | + mp_hal_pin_output(pin); |
| 51 | + // Enable the systick counter, source CPU clock. |
| 52 | + systick_hw->csr = 5; |
| 53 | + |
| 54 | + uint32_t irq_state = mp_hal_quiet_timing_enter(); |
| 55 | + |
| 56 | + for (size_t i = 0; i < len; ++i) { |
| 57 | + uint8_t b = buf[i]; |
| 58 | + for (size_t j = 0; j < 8; ++j) { |
| 59 | + uint32_t *t = &timing_ns[b >> 6 & 2]; |
| 60 | + uint32_t start_ticks = systick_hw->cvr = SYSTICK_MAX; |
| 61 | + mp_hal_pin_high(pin); |
| 62 | + while ((start_ticks - systick_hw->cvr) < t[0]) { |
| 63 | + } |
| 64 | + b <<= 1; |
| 65 | + mp_hal_pin_low(pin); |
| 66 | + while ((start_ticks - systick_hw->cvr) < t[1]) { |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + mp_hal_quiet_timing_exit(irq_state); |
| 72 | +} |
| 73 | + |
| 74 | +#endif // MICROPY_PY_MACHINE_BITSTREAM |
0 commit comments