Skip to content

Commit a415752

Browse files
robert-hhdpgeorge
authored andcommitted
samd/machine_bitstream: Add the machine.bitstream() function.
The SAMD21 implementation is an adaption of @jimmo's code for STM32Lxx. The only changes are the addresses and names of the port registers and the timing parameters. SAMD21: The precision is about +/-25ns at 48MHz clock frequency. The first two cycles are about 40-60 ns longer than set. But still good enough to drive a neopixel device. SAMD51: The precision is about +/-30ns at 120MHz clock frequency. Good enough to drive a neopixel device.
1 parent fd7b57d commit a415752

File tree

6 files changed

+214
-4
lines changed

6 files changed

+214
-4
lines changed

ports/samd/Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,11 @@ LIBSTDCPP_FILE_NAME = "$(shell $(CXX) $(CXXFLAGS) -print-file-name=libstdc++.a)"
8787
LDFLAGS += -L"$(shell dirname $(LIBSTDCPP_FILE_NAME))"
8888
endif
8989

90-
SRC_C = \
90+
SRC_C += \
9191
clock_config.c \
9292
help.c \
9393
machine_adc.c \
94+
machine_bitstream.c \
9495
machine_dac.c \
9596
machine_i2c.c \
9697
machine_led.c \

ports/samd/machine_bitstream.c

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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

ports/samd/modmachine.c

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "py/runtime.h"
28+
#include "extmod/machine_bitstream.h"
2829
#include "extmod/machine_mem.h"
2930
#include "extmod/machine_pulse.h"
3031
#include "extmod/machine_i2c.h"
@@ -167,6 +168,7 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
167168
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
168169
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
169170
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
171+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
170172
};
171173
STATIC MP_DEFINE_CONST_DICT(machine_module_globals, machine_module_globals_table);
172174

ports/samd/mpconfigport.h

+1
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
#define MICROPY_PY_MACHINE_SPI (1)
112112
#define MICROPY_PY_MACHINE_SOFTSPI (1)
113113
#define MICROPY_PY_OS_DUPTERM (3)
114+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
114115
#define MICROPY_PY_MACHINE_PULSE (1)
115116
#define MICROPY_PY_MACHINE_PWM (1)
116117
#define MICROPY_PY_MACHINE_PWM_INIT (0)

ports/samd/mphalport.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,11 @@ static inline mp_uint_t mp_hal_ticks_us(void) {
7171
#endif
7272
}
7373

74-
#if defined (MCU_SAMD21)
74+
#if defined(MCU_SAMD21)
7575

7676
#define mp_hal_ticks_cpu mp_hal_ticks_us
7777

78-
#elif defined (MCU_SAMD51)
78+
#elif defined(MCU_SAMD51)
7979
static inline void mp_hal_ticks_cpu_enable(void) {
8080
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
8181
DWT->CYCCNT = 0;

ports/samd/samd_soc.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void samd_init(void) {
111111
SysTick_Config(get_cpu_freq() / 1000);
112112
init_us_counter();
113113
usb_init();
114-
#if defined (MCU_SAMD51)
114+
#if defined(MCU_SAMD51)
115115
mp_hal_ticks_cpu_enable();
116116
#endif
117117
}

0 commit comments

Comments
 (0)