Skip to content

Commit e64cda5

Browse files
jimmodpgeorge
authored andcommitted
stm32: Add implementation of machine.bitstream.
Hand-written version for M0, and cycle-counter version for everything else. Signed-off-by: Jim Mussared <[email protected]>
1 parent 870000f commit e64cda5

File tree

6 files changed

+215
-0
lines changed

6 files changed

+215
-0
lines changed

ports/stm32/Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,7 @@ SRC_C += \
318318
gccollect.c \
319319
help.c \
320320
machine_adc.c \
321+
machine_bitstream.c \
321322
machine_i2c.c \
322323
machine_i2s.c \
323324
machine_spi.c \

ports/stm32/boards/NUCLEO_L073RZ/mpconfigboard.h

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#define MICROPY_PY_UHEAPQ (0)
2020
#define MICROPY_PY_UTIMEQ (0)
2121

22+
#define MICROPY_PY_MACHINE_BITSTREAM (0)
23+
2224
#define MICROPY_HW_ENABLE_INTERNAL_FLASH_STORAGE (0)
2325
#define MICROPY_HW_ENABLE_RTC (1)
2426
#define MICROPY_HW_ENABLE_ADC (0)

ports/stm32/machine_bitstream.c

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
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+
#include "py/runtime.h"
28+
#include "py/mphal.h"
29+
#include "extmod/machine_bitstream.h"
30+
31+
#if MICROPY_PY_MACHINE_BITSTREAM
32+
33+
#if __CORTEX_M == 0
34+
35+
// No cycle counter on M0, do manual cycle counting instead.
36+
37+
// STM32F091 @ 48MHz
38+
#define NS_CYCLES_PER_ITER_HIGH (6)
39+
#define NS_CYCLES_PER_ITER_LOW (6)
40+
#define NS_OVERHEAD_CYCLES_HIGH (12)
41+
#define NS_OVERHEAD_CYCLES_LOW (18)
42+
43+
uint32_t mp_hal_delay_ns_calc(uint32_t ns, bool high) {
44+
uint32_t ncycles = SystemCoreClock / 1000000 * ns / 1000;
45+
uint32_t overhead = MIN(ncycles, high ? NS_OVERHEAD_CYCLES_HIGH : NS_OVERHEAD_CYCLES_LOW);
46+
return MAX(1, MP_ROUND_DIVIDE(ncycles - overhead, high ? NS_CYCLES_PER_ITER_HIGH : NS_CYCLES_PER_ITER_LOW));
47+
}
48+
49+
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
50+
const uint32_t high_mask = pin->pin_mask;
51+
const uint32_t low_mask = pin->pin_mask << 16;
52+
volatile uint32_t *bsrr = &pin->gpio->BSRR;
53+
54+
// Convert ns to loop iterations [high_time_0, low_time_0, high_time_1, low_time_1].
55+
for (size_t i = 0; i < 4; ++i) {
56+
timing_ns[i] = mp_hal_delay_ns_calc(timing_ns[i], i % 2 == 0);
57+
}
58+
59+
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
60+
61+
// Measured timing for F091 at 48MHz (cycle=20.83ns)
62+
// timing_ns = (1,1,1,1)
63+
// high: 370
64+
// low: 500
65+
// low8: 660
66+
// timing_ns = (2,2,2,2)
67+
// high: 490
68+
// low: 620
69+
// low8: 805
70+
71+
// --> high is 12 + n*6 cycles
72+
// low is 18 + n*6 cycles
73+
74+
// NeoPixel timing (400, 850, 800, 450) (+/-150ns) gives timing_ns=(1, 4, 4, 1) which in cycles is
75+
// (12 + 6, 18 + 24, 12 + 24, 18 + 6) = (18, 42, 36, 24)
76+
// --> (375, 875, 750, 500) nanoseconds.
77+
// Measured output on logic analyser is (370, 870, 750, 490) (+/-10ns at 100MHz)
78+
79+
// Note: final low of LSB is longer by 8 cycles (160ns) (due to start of outer loop and fetching next byte).
80+
// This is slightly outside spec, but doesn't seem to cause a problem.
81+
82+
__asm volatile (
83+
// Force consistent register assignment.
84+
// r6 = len
85+
"ldr r6, %0\n"
86+
// r4 = buf
87+
"ldr r4, %1\n"
88+
// r5 = timing_ms
89+
"ldr r5, %2\n"
90+
91+
// Must align for consistent timing.
92+
".align 4\n"
93+
94+
// Don't increment/decrement before first iteration.
95+
"b .outer2\n"
96+
".outer:\n"
97+
// ++buf, --len
98+
" add r4, #1\n"
99+
" sub r6, #1\n"
100+
101+
// len iterations
102+
".outer2:\n"
103+
" cmp r6, #0\n"
104+
" beq .done\n"
105+
106+
// r0 = *buf
107+
" ldrb r0, [r4, #0]\n"
108+
109+
// 8 bits in byte
110+
" mov r7, #8\n"
111+
" .inner:\n"
112+
// *bsrr = high_mask
113+
" ldr r1, %3\n"
114+
" ldr r2, %4\n"
115+
" str r2, [r1, #0]\n"
116+
117+
// r3 = (r0 >> 4) & 8 (r0 is 8 if high bit is 1 else 0)
118+
" mov r8, r6\n"
119+
" lsr r3, r0, #4\n"
120+
" mov r6, #8\n"
121+
" and r3, r6\n"
122+
" mov r6, r8\n"
123+
124+
// r2 = timing_ns[r2]
125+
" ldr r2, [r5, r3]\n"
126+
" .loop1:\n sub r2, #1\n bne .loop1\n"
127+
128+
// *bsrr = low_mask
129+
" ldr r2, %5\n"
130+
" str r2, [r1, #0]\n"
131+
132+
// r2 = timing_ns[r3 + 4]
133+
" add r3, #4\n"
134+
" ldr r2, [r5, r3]\n"
135+
" .loop2:\n sub r2, #1\n bne .loop2\n"
136+
137+
// b >>= 1
138+
" lsl r0, r0, #1\n"
139+
140+
" sub r7, #1\n"
141+
// end of inner loop
142+
" beq .outer\n"
143+
// continue inner loop
144+
" b .inner\n"
145+
146+
".done:\n"
147+
:
148+
: "m" (len), "m" (buf), "m" (timing_ns), "m" (bsrr), "m" (high_mask), "m" (low_mask)
149+
: "r0", "r1", "r2", "r3", "r7", "r8"
150+
);
151+
152+
MICROPY_END_ATOMIC_SECTION(atomic_state);
153+
}
154+
155+
#else // > CORTEX_M0
156+
157+
// Use cycle counter for timing.
158+
159+
// Measured on PYBV11 at 168MHz & 128MHz and PYBD_SF6 at 128MHz & 144MHz.
160+
#define NS_CYCLES_OVERHEAD (6)
161+
162+
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
163+
const uint32_t high_mask = pin->pin_mask;
164+
const uint32_t low_mask = pin->pin_mask << 16;
165+
volatile uint32_t *bsrr = &pin->gpio->BSRR;
166+
167+
// Convert ns to cycles [high_time_0, low_time_0, high_time_1, low_time_1].
168+
for (size_t i = 0; i < 4; ++i) {
169+
timing_ns[i] = SystemCoreClock / 1000000 * timing_ns[i] / 1000;
170+
if (timing_ns[i] > NS_CYCLES_OVERHEAD) {
171+
timing_ns[i] -= NS_CYCLES_OVERHEAD;
172+
}
173+
if (i % 2 == 1) {
174+
timing_ns[i] += timing_ns[i - 1];
175+
}
176+
}
177+
178+
mp_hal_ticks_cpu_enable();
179+
180+
mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION();
181+
182+
for (size_t i = 0; i < len; ++i) {
183+
uint8_t b = buf[i];
184+
for (size_t j = 0; j < 8; ++j) {
185+
DWT->CYCCNT = 0;
186+
*bsrr = high_mask;
187+
uint32_t *t = &timing_ns[b >> 6 & 2];
188+
while (DWT->CYCCNT < t[0]) {
189+
;
190+
}
191+
*bsrr = low_mask;
192+
b <<= 1;
193+
while (DWT->CYCCNT < t[1]) {
194+
;
195+
}
196+
}
197+
}
198+
199+
MICROPY_END_ATOMIC_SECTION(atomic_state);
200+
}
201+
202+
#endif // > CORTEX_M0
203+
204+
#endif // MICROPY_PY_MACHINE_BITSTREAM

ports/stm32/modmachine.c

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "py/objstr.h"
3434
#include "py/mperrno.h"
3535
#include "py/mphal.h"
36+
#include "extmod/machine_bitstream.h"
3637
#include "extmod/machine_mem.h"
3738
#include "extmod/machine_signal.h"
3839
#include "extmod/machine_pulse.h"
@@ -406,6 +407,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
406407
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
407408
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
408409

410+
#if MICROPY_PY_MACHINE_BITSTREAM
411+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
412+
#endif
409413
#if MICROPY_PY_MACHINE_PULSE
410414
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
411415
#endif

ports/stm32/mpconfigport.h

+3
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,9 @@
193193
#define MICROPY_PY_LWIP_SOCK_RAW (MICROPY_PY_LWIP)
194194
#ifndef MICROPY_PY_MACHINE
195195
#define MICROPY_PY_MACHINE (1)
196+
#ifndef MICROPY_PY_MACHINE_BITSTREAM
197+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
198+
#endif
196199
#define MICROPY_PY_MACHINE_PULSE (1)
197200
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
198201
#define MICROPY_PY_MACHINE_I2C (1)

py/misc.h

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ typedef unsigned int uint;
5555

5656
// Round-up integer division
5757
#define MP_CEIL_DIVIDE(a, b) (((a) + (b) - 1) / (b))
58+
#define MP_ROUND_DIVIDE(a, b) (((a) + (b) / 2) / (b))
5859

5960
/** memory allocation ******************************************/
6061

0 commit comments

Comments
 (0)