Skip to content

Commit 72e67f0

Browse files
robert-hhdpgeorge
authored andcommitted
samd/machine_bitstream: Tune ticks for SAMD51 and set pin to output.
Changes in this commit: - Change ticks overhead value for SAMD51. The value was too large and thus timing was quite bad. At 120Mhz it's now within +/- 30ns. - Set the pin to output mode. That way, all Pin identifiers are accepted as argument, not only Pin objects. Tested with ItsyBitsy M4 and M0 boards. Signed-off-by: robert-hh <[email protected]>
1 parent 2e74f0b commit 72e67f0

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

ports/samd/machine_bitstream.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
// No cycle counter on M0, do manual cycle counting instead.
3838

39-
// STM32F091 @ 48MHz
39+
// SAMD21 @ 48MHz
4040
#define NS_CYCLES_PER_ITER_HIGH (3)
4141
#define NS_CYCLES_PER_ITER_LOW (3)
4242
#define NS_OVERHEAD_CYCLES_HIGH (12)
@@ -52,6 +52,7 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const
5252
volatile const uint32_t mask = 1 << (pin % 32);
5353
volatile uint32_t *outclr = &PORT->Group[pin / 32].OUTCLR.reg;
5454
volatile uint32_t *outset = &PORT->Group[pin / 32].OUTSET.reg;
55+
mp_hal_pin_output(pin);
5556

5657
// Convert ns to loop iterations [high_time_0, low_time_0, high_time_1, low_time_1].
5758
for (size_t i = 0; i < 4; ++i) {
@@ -163,11 +164,12 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const
163164

164165
#else // > CORTEX_M0
165166

166-
#define NS_TICKS_OVERHEAD (70)
167+
#define NS_TICKS_OVERHEAD (40)
167168

168169
void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const uint8_t *buf, size_t len) {
169170
uint32_t fcpu_mhz = get_cpu_freq() / 1000000;
170171
uint32_t ticks_overhead = fcpu_mhz * NS_TICKS_OVERHEAD / 1000;
172+
mp_hal_pin_output(pin);
171173
// Convert ns to us ticks [high_time_0, period_0, high_time_1, period_1].
172174
for (size_t i = 0; i < 4; ++i) {
173175
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
@@ -191,8 +193,8 @@ void machine_bitstream_high_low(mp_hal_pin_obj_t pin, uint32_t *timing_ns, const
191193
mp_hal_pin_high(pin);
192194
while ((mp_hal_ticks_cpu() - start_ticks) < t[0]) {
193195
}
194-
b <<= 1;
195196
mp_hal_pin_low(pin);
197+
b <<= 1;
196198
while ((mp_hal_ticks_cpu() - start_ticks) < t[1]) {
197199
}
198200
}

0 commit comments

Comments
 (0)