Skip to content

Commit fa15ae4

Browse files
committed
rp2/machine_bitstream: Implement bitstream for RISC-V using mcycle.
Signed-off-by: Damien George <[email protected]>
1 parent ea2eed1 commit fa15ae4

File tree

1 file changed

+44
-2
lines changed

1 file changed

+44
-2
lines changed

ports/rp2/machine_bitstream.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,25 @@
3434

3535
#define MP_HAL_BITSTREAM_NS_OVERHEAD (9)
3636

37+
#if PICO_RISCV
38+
39+
__attribute__((naked)) void mcycle_init(void) {
40+
__asm volatile (
41+
"li a0, 4\n"
42+
"csrw mcountinhibit, a0\n"
43+
"ret\n"
44+
);
45+
}
46+
47+
__attribute__((naked)) uint32_t mcycle_get(void) {
48+
__asm volatile (
49+
"csrr a0, mcycle\n"
50+
"ret\n"
51+
);
52+
}
53+
54+
#endif
55+
3756
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) {
3857
uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000;
3958
// Convert ns to clock ticks [high_time_0, period_0, high_time_1, period_1].
@@ -49,14 +68,16 @@ void __time_critical_func(machine_bitstream_high_low)(mp_hal_pin_obj_t pin, uint
4968
}
5069
mp_hal_pin_output(pin);
5170

71+
uint32_t irq_state = mp_hal_quiet_timing_enter();
72+
73+
#if PICO_ARM
74+
5275
// Set systick reset value.
5376
systick_hw->rvr = 0x00FFFFFF;
5477

5578
// Enable the systick counter, source CPU clock.
5679
systick_hw->csr = 5;
5780

58-
uint32_t irq_state = mp_hal_quiet_timing_enter();
59-
6081
for (size_t i = 0; i < len; ++i) {
6182
uint8_t b = buf[i];
6283
for (size_t j = 0; j < 8; ++j) {
@@ -72,6 +93,27 @@ void __time_critical_func(machine_bitstream_high_low)(mp_hal_pin_obj_t pin, uint
7293
}
7394
}
7495

96+
#elif PICO_RISCV
97+
98+
mcycle_init();
99+
100+
for (size_t i = 0; i < len; ++i) {
101+
uint8_t b = buf[i];
102+
for (size_t j = 0; j < 8; ++j) {
103+
uint32_t *t = &timing_ns[b >> 6 & 2];
104+
uint32_t start_ticks = mcycle_get();
105+
mp_hal_pin_high(pin);
106+
while ((mcycle_get() - start_ticks) < t[0]) {
107+
}
108+
b <<= 1;
109+
mp_hal_pin_low(pin);
110+
while ((mcycle_get() - start_ticks) < t[1]) {
111+
}
112+
}
113+
}
114+
115+
#endif
116+
75117
mp_hal_quiet_timing_exit(irq_state);
76118
}
77119

0 commit comments

Comments
 (0)