|
| 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 "clock_config.h" |
| 32 | + |
| 33 | +#if MICROPY_PY_MACHINE_BITSTREAM |
| 34 | + |
| 35 | +#if __CORTEX_M == 0 |
| 36 | + |
| 37 | +// No cycle counter on M0, do manual cycle counting instead. |
| 38 | + |
| 39 | +// STM32F091 @ 48MHz |
| 40 | +#define NS_CYCLES_PER_ITER_HIGH (3) |
| 41 | +#define NS_CYCLES_PER_ITER_LOW (3) |
| 42 | +#define NS_OVERHEAD_CYCLES_HIGH (12) |
| 43 | +#define NS_OVERHEAD_CYCLES_LOW (15) |
| 44 | + |
| 45 | +uint32_t mp_hal_delay_ns_calc(uint32_t ns, bool high) { |
| 46 | + uint32_t ncycles = (get_cpu_freq() / 1000000 * ns + 500) / 1000; // + 500 for proper rounding |
| 47 | + uint32_t overhead = MIN(ncycles, high ? NS_OVERHEAD_CYCLES_HIGH : NS_OVERHEAD_CYCLES_LOW); |
| 48 | + return MAX(1, MP_ROUND_DIVIDE(ncycles - overhead, high ? NS_CYCLES_PER_ITER_HIGH : NS_CYCLES_PER_ITER_LOW)); |
| 49 | +} |
| 50 | + |
| 51 | +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { |
| 52 | + volatile const uint32_t mask = 1 << (pin % 32); |
| 53 | + volatile uint32_t *outclr = &PORT->Group[pin / 32].OUTCLR.reg; |
| 54 | + volatile uint32_t *outset = &PORT->Group[pin / 32].OUTSET.reg; |
| 55 | + |
| 56 | + // Convert ns to loop iterations [high_time_0, low_time_0, high_time_1, low_time_1]. |
| 57 | + for (size_t i = 0; i < 4; ++i) { |
| 58 | + timing_ns[i] = mp_hal_delay_ns_calc(timing_ns[i], i % 2 == 0); |
| 59 | + } |
| 60 | + |
| 61 | + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); |
| 62 | + |
| 63 | + // Measured timing for SAMD21 at 48MHz (cycle=20.83ns) |
| 64 | + // timing_ns = (1,1,1,1) |
| 65 | + // high: 310 |
| 66 | + // low: 375 |
| 67 | + // high0: 375 |
| 68 | + // low0: 400 |
| 69 | + // timing_ns = (500, 500, 500, 500) |
| 70 | + // high: 500 |
| 71 | + // low: 500 |
| 72 | + // high0: 565 |
| 73 | + // low0: 540 |
| 74 | + // timing_ns = (1000, 1000, 1000, 1000) |
| 75 | + // high: 1000 |
| 76 | + // low: 1000 |
| 77 | + // high0: 1065 |
| 78 | + // low0: 1040 |
| 79 | + |
| 80 | + // --> high is 12 + n*3 cycles |
| 81 | + // low is 15 + n*3 cycles |
| 82 | + |
| 83 | + // NeoPixel timing (400, 850, 800, 450) (+/-150ns) gives timing_ns=(2, 9, 8, 3) which in cycles is |
| 84 | + // (12 + 6, 15 + 27, 15 + 24, 12 + 9) = (18, 42, 39, 21) |
| 85 | + // --> (375, 875, 812, 437) nanoseconds. |
| 86 | + // Measured output on logic analyser is (375, 875, 815, 435) (+/-5ns at 200MHz) |
| 87 | + |
| 88 | + // Note: the first high/low cycle is longer by 2-3 cycles (40-60ns). |
| 89 | + // This is slightly outside spec, but doesn't seem to cause a problem. |
| 90 | + |
| 91 | + __asm volatile ( |
| 92 | + // Force consistent register assignment. |
| 93 | + // r6 = len |
| 94 | + "ldr r6, %0\n" |
| 95 | + // r4 = buf |
| 96 | + "ldr r4, %1\n" |
| 97 | + // r5 = timing_ms |
| 98 | + "ldr r5, %2\n" |
| 99 | + |
| 100 | + // Must align for consistent timing. |
| 101 | + ".align 4\n" |
| 102 | + |
| 103 | + // Don't increment/decrement before first iteration. |
| 104 | + "b .outer2\n" |
| 105 | + ".outer:\n" |
| 106 | + // ++buf, --len |
| 107 | + " add r4, #1\n" |
| 108 | + " sub r6, #1\n" |
| 109 | + |
| 110 | + // len iterations |
| 111 | + ".outer2:\n" |
| 112 | + " cmp r6, #0\n" |
| 113 | + " beq .done\n" |
| 114 | + |
| 115 | + // r0 = *buf |
| 116 | + " ldrb r0, [r4, #0]\n" |
| 117 | + |
| 118 | + // 8 bits in byte |
| 119 | + " mov r7, #8\n" |
| 120 | + " .inner:\n" |
| 121 | + // *outset = mask |
| 122 | + " ldr r2, %3\n" |
| 123 | + " ldr r1, %5\n" |
| 124 | + " str r1, [r2, #0]\n" |
| 125 | + |
| 126 | + // r3 = (r0 >> 4) & 8 (r0 is 8 if high bit is 1 else 0) |
| 127 | + " mov r8, r6\n" |
| 128 | + " lsr r3, r0, #4\n" |
| 129 | + " mov r6, #8\n" |
| 130 | + " and r3, r6\n" |
| 131 | + " mov r6, r8\n" |
| 132 | + |
| 133 | + // r2 = timing_ns[r2] |
| 134 | + " ldr r2, [r5, r3]\n" |
| 135 | + " .loop1:\n sub r2, #1\n bne .loop1\n" |
| 136 | + |
| 137 | + // *outclr = mask |
| 138 | + " ldr r2, %4\n" |
| 139 | + " str r1, [r2, #0]\n" |
| 140 | + |
| 141 | + // r2 = timing_ns[r3 + 4] |
| 142 | + " add r3, #4\n" |
| 143 | + " ldr r2, [r5, r3]\n" |
| 144 | + " .loop2:\n sub r2, #1\n bne .loop2\n" |
| 145 | + |
| 146 | + // b >>= 1 |
| 147 | + " lsl r0, r0, #1\n" |
| 148 | + |
| 149 | + " sub r7, #1\n" |
| 150 | + // end of inner loop |
| 151 | + " beq .outer\n" |
| 152 | + // continue inner loop |
| 153 | + " b .inner\n" |
| 154 | + |
| 155 | + ".done:\n" |
| 156 | + : |
| 157 | + : "m" (len), "m" (buf), "m" (timing_ns), "m" (outset), "m" (outclr), "m" (mask) |
| 158 | + : "r0", "r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8" |
| 159 | + ); |
| 160 | + |
| 161 | + MICROPY_END_ATOMIC_SECTION(atomic_state); |
| 162 | +} |
| 163 | + |
| 164 | +#else // > CORTEX_M0 |
| 165 | + |
| 166 | +#define NS_TICKS_OVERHEAD (70) |
| 167 | + |
| 168 | +void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) { |
| 169 | + uint32_t fcpu_mhz = get_cpu_freq() / 1000000; |
| 170 | + uint32_t ticks_overhead = fcpu_mhz * NS_TICKS_OVERHEAD / 1000; |
| 171 | + // Convert ns to us ticks [high_time_0, period_0, high_time_1, period_1]. |
| 172 | + for (size_t i = 0; i < 4; ++i) { |
| 173 | + timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000; |
| 174 | + if (timing_ns[i] > ticks_overhead) { |
| 175 | + timing_ns[i] -= ticks_overhead; |
| 176 | + } |
| 177 | + if (i % 2 == 1) { |
| 178 | + // Convert low_time to period (i.e. add high_time). |
| 179 | + timing_ns[i] += timing_ns[i - 1] - ticks_overhead; |
| 180 | + } |
| 181 | + } |
| 182 | + |
| 183 | + mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); |
| 184 | + DWT->CYCCNT = 0; |
| 185 | + |
| 186 | + for (size_t i = 0; i < len; ++i) { |
| 187 | + uint8_t b = buf[i]; |
| 188 | + for (size_t j = 0; j < 8; ++j) { |
| 189 | + uint32_t start_ticks = mp_hal_ticks_cpu(); |
| 190 | + uint32_t *t = &timing_ns[b >> 6 & 2]; |
| 191 | + mp_hal_pin_high(pin); |
| 192 | + while ((mp_hal_ticks_cpu() - start_ticks) < t[0]) { |
| 193 | + } |
| 194 | + b <<= 1; |
| 195 | + mp_hal_pin_low(pin); |
| 196 | + while ((mp_hal_ticks_cpu() - start_ticks) < t[1]) { |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + MICROPY_END_ATOMIC_SECTION(atomic_state); |
| 201 | + |
| 202 | +} |
| 203 | + |
| 204 | +#endif // > CORTEX_M0 |
| 205 | + |
| 206 | +#endif // MICROPY_PY_MACHINE_BITSTREAM |
0 commit comments