Skip to content

Commit b73d8b0

Browse files
robert-hhdpgeorge
authored andcommitted
rp2/machine_bitstream: Implement the machine.bitstream driver.
Timing error is ~20ns at 125MHz, and ~10ns at 250MHz.
1 parent 7d7d29d commit b73d8b0

File tree

5 files changed

+97
-1
lines changed

5 files changed

+97
-1
lines changed

ports/rp2/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ set(MICROPY_SOURCE_DRIVERS
8484
set(MICROPY_SOURCE_PORT
8585
fatfs_port.c
8686
machine_adc.c
87+
machine_bitstream.c
8788
machine_i2c.c
8889
machine_i2s.c
8990
machine_pin.c

ports/rp2/machine_bitstream.c

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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 "hardware/structs/systick.h"
32+
33+
#if MICROPY_PY_MACHINE_BITSTREAM
34+
35+
#define MP_HAL_BITSTREAM_NS_OVERHEAD (9)
36+
37+
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) {
38+
uint32_t fcpu_mhz = mp_hal_get_cpu_freq() / 1000000;
39+
// Convert ns to clock ticks [high_time_0, period_0, high_time_1, period_1].
40+
for (size_t i = 0; i < 4; ++i) {
41+
timing_ns[i] = fcpu_mhz * timing_ns[i] / 1000;
42+
if (timing_ns[i] > (2 * MP_HAL_BITSTREAM_NS_OVERHEAD)) {
43+
timing_ns[i] -= MP_HAL_BITSTREAM_NS_OVERHEAD;
44+
}
45+
if (i % 2 == 1) {
46+
// Convert low_time to period (i.e. add high_time).
47+
timing_ns[i] += timing_ns[i - 1] - MP_HAL_BITSTREAM_NS_OVERHEAD;
48+
}
49+
}
50+
mp_hal_pin_output(pin);
51+
// Enable the systick counter, source CPU clock.
52+
systick_hw->csr = 5;
53+
54+
uint32_t irq_state = mp_hal_quiet_timing_enter();
55+
56+
for (size_t i = 0; i < len; ++i) {
57+
uint8_t b = buf[i];
58+
for (size_t j = 0; j < 8; ++j) {
59+
uint32_t *t = &timing_ns[b >> 6 & 2];
60+
uint32_t start_ticks = systick_hw->cvr = SYSTICK_MAX;
61+
mp_hal_pin_high(pin);
62+
while ((start_ticks - systick_hw->cvr) < t[0]) {
63+
}
64+
b <<= 1;
65+
mp_hal_pin_low(pin);
66+
while ((start_ticks - systick_hw->cvr) < t[1]) {
67+
}
68+
}
69+
}
70+
71+
mp_hal_quiet_timing_exit(irq_state);
72+
}
73+
74+
#endif // MICROPY_PY_MACHINE_BITSTREAM

ports/rp2/modmachine.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "py/runtime.h"
2828
#include "py/mphal.h"
2929
#include "shared/runtime/pyexec.h"
30+
#include "extmod/machine_bitstream.h"
3031
#include "extmod/machine_i2c.h"
3132
#include "extmod/machine_mem.h"
3233
#include "extmod/machine_pulse.h"
@@ -86,7 +87,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(machine_bootloader_obj, machine_bootloader);
8687

8788
STATIC mp_obj_t machine_freq(size_t n_args, const mp_obj_t *args) {
8889
if (n_args == 0) {
89-
return MP_OBJ_NEW_SMALL_INT(clock_get_hz(clk_sys));
90+
return MP_OBJ_NEW_SMALL_INT(mp_hal_get_cpu_freq());
9091
} else {
9192
mp_int_t freq = mp_obj_get_int(args[0]);
9293
if (!set_sys_clock_khz(freq / 1000, false)) {
@@ -154,6 +155,9 @@ STATIC const mp_rom_map_elem_t machine_module_globals_table[] = {
154155
{ MP_ROM_QSTR(MP_QSTR_disable_irq), MP_ROM_PTR(&machine_disable_irq_obj) },
155156
{ MP_ROM_QSTR(MP_QSTR_enable_irq), MP_ROM_PTR(&machine_enable_irq_obj) },
156157

158+
#if MICROPY_PY_MACHINE_BITSTREAM
159+
{ MP_ROM_QSTR(MP_QSTR_bitstream), MP_ROM_PTR(&machine_bitstream_obj) },
160+
#endif
157161
{ MP_ROM_QSTR(MP_QSTR_time_pulse_us), MP_ROM_PTR(&machine_time_pulse_us_obj) },
158162

159163
{ MP_ROM_QSTR(MP_QSTR_mem8), MP_ROM_PTR(&machine_mem8_obj) },

ports/rp2/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
#define MICROPY_PY_URANDOM_SEED_INIT_FUNC (rosc_random_u32())
8787
#define MICROPY_PY_MACHINE (1)
8888
#define MICROPY_PY_MACHINE_PIN_MAKE_NEW mp_pin_make_new
89+
#define MICROPY_PY_MACHINE_BITSTREAM (1)
8990
#define MICROPY_PY_MACHINE_PULSE (1)
9091
#define MICROPY_PY_MACHINE_PWM (1)
9192
#define MICROPY_PY_MACHINE_PWM_DUTY_U16_NS (1)

ports/rp2/mphalport.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#include "py/mpconfig.h"
2929
#include "py/ringbuf.h"
3030
#include "pico/time.h"
31+
#include "hardware/clocks.h"
32+
#include "hardware/structs/systick.h"
33+
34+
#define SYSTICK_MAX (0xffffff)
3135

3236
extern int mp_interrupt_char;
3337
extern ringbuf_t stdin_ringbuf;
@@ -59,6 +63,10 @@ static inline mp_uint_t mp_hal_ticks_cpu(void) {
5963
return time_us_32();
6064
}
6165

66+
static inline mp_uint_t mp_hal_get_cpu_freq(void) {
67+
return clock_get_hz(clk_sys);
68+
}
69+
6270
// C-level pin HAL
6371

6472
#include "py/obj.h"
@@ -110,4 +118,12 @@ static inline void mp_hal_pin_od_high(mp_hal_pin_obj_t pin) {
110118
gpio_set_dir(pin, GPIO_IN);
111119
}
112120

121+
static inline void mp_hal_pin_low(mp_hal_pin_obj_t pin) {
122+
gpio_clr_mask(1 << pin);
123+
}
124+
125+
static inline void mp_hal_pin_high(mp_hal_pin_obj_t pin) {
126+
gpio_set_mask(1 << pin);
127+
}
128+
113129
#endif // MICROPY_INCLUDED_RP2_MPHALPORT_H

0 commit comments

Comments
 (0)