Skip to content

Update floppyio to use newer adafruit_floppy, implement pio-based flux capture on rp2040 #9135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/adafruit_floppy
Submodule adafruit_floppy updated 45 files
+57 −1 .github/workflows/githubci.yml
+4 −1 .gitignore
+6 −0 .gitmodules
+2,458 −0 Doxyfile
+0 −14 Makefile
+15 −11 README.md
+1 −0 doxygen-awesome-css
+124 −0 examples/01_floppy_capture_track_test/01_floppy_capture_track_test.ino
+140 −0 examples/02_mfm_test/02_mfm_test.ino
+75 −66 examples/03_fat_test/03_fat_test.ino
+0 −0 examples/04_msd_test/.feather_m4_express_tinyusb.generate
+0 −0 examples/04_msd_test/.feather_rp2040_tinyusb.generate
+220 −0 examples/04_msd_test/04_msd_test.ino
+32 −0 examples/04_msd_test/display_common.h
+160 −0 examples/04_msd_test/display_floppsy.h
+4 −0 examples/04_msd_test/display_none.h
+9 −0 examples/04_msd_test/display_state.h
+139 −0 examples/05_mfm_write_test/05_mfm_write_test.ino
+156 −0 examples/99_floppy_write_test/99_floppy_write_test.ino
+76 −0 examples/apple2_test/apple2_test.ino
+0 −112 examples/floppy_capture_track_test/floppy_capture_track_test.ino
+0 −0 examples/greaseweazle/.feather_m4_express_tinyusb.generate
+0 −0 examples/greaseweazle/.feather_rp2040_tinyusb.generate
+411 −251 examples/greaseweazle/greaseweazle.ino
+0 −138 examples/mfm_test/mfm_test.ino
+0 −163 examples/msd_test/msd_test.ino
+0 −1 flux.txt
+0 −3 flux.txt.license
+5 −0 host_src/.gitignore
+16 −0 host_src/Makefile
+29 −0 host_src/check_flux.py
+1 −0 host_src/greaseweazle
+91 −0 host_src/main.c
+13 −0 host_src/make_flux.py
+1 −1 library.properties
+0 −3 mfm.license
+837 −98 src/Adafruit_Floppy.cpp
+295 −59 src/Adafruit_Floppy.h
+269 −39 src/Adafruit_MFM_Floppy.cpp
+435 −0 src/arch_rp2.cpp
+26 −0 src/arch_rp2.h
+443 −0 src/arch_samd51.cpp
+101 −0 src/greasepack.h
+389 −167 src/mfm_impl.h
+0 −49 standalone/main.c
10 changes: 10 additions & 0 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,7 @@ msgstr ""
msgid "All event channels in use"
msgstr ""

#: ports/raspberrypi/common-hal/floppyio/__init__.c
#: ports/raspberrypi/common-hal/picodvi/Framebuffer.c
#: ports/raspberrypi/common-hal/rp2pio/StateMachine.c
#: ports/raspberrypi/common-hal/usb_host/Port.c
Expand Down Expand Up @@ -4029,6 +4030,15 @@ msgstr ""
msgid "timeout must be < 655.35 secs"
msgstr ""

#: ports/raspberrypi/common-hal/floppyio/__init__.c
msgid "timeout waiting for flux"
msgstr ""

#: ports/raspberrypi/common-hal/floppyio/__init__.c
#: shared-module/floppyio/__init__.c
msgid "timeout waiting for index pulse"
msgstr ""

#: shared-module/sdcardio/SDCard.c
msgid "timeout waiting for v1 card"
msgstr ""
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ CHIP_VARIANT = RP2040
CHIP_FAMILY = rp2

EXTERNAL_FLASH_DEVICES = "GD25Q64C,W25Q64JVxQ,W25Q128JV"

CIRCUITPY_USB_HOST = 0
CIRCUITPY_PICODVI = 0
8 changes: 4 additions & 4 deletions ports/raspberrypi/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -175,21 +175,21 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
sideset_pin = bit_clock;

if (left_justified) {
program_len = sizeof(i2s_program_left_justified) / sizeof(i2s_program_left_justified[0]);
program_len = MP_ARRAY_SIZE(i2s_program_left_justified);
program = i2s_program_left_justified;
} else {
program_len = sizeof(i2s_program) / sizeof(i2s_program[0]);
program_len = MP_ARRAY_SIZE(i2s_program);
program = i2s_program;
}

} else if (bit_clock->number == word_select->number + 1) {
sideset_pin = word_select;

if (left_justified) {
program_len = sizeof(i2s_program_left_justified_swap) / sizeof(i2s_program_left_justified_swap[0]);
program_len = MP_ARRAY_SIZE(i2s_program_left_justified_swap);
program = i2s_program_left_justified_swap;
} else {
program_len = sizeof(i2s_program_swap) / sizeof(i2s_program_swap[0]);
program_len = MP_ARRAY_SIZE(i2s_program_swap);
program = i2s_program_swap;
}

Expand Down
195 changes: 195 additions & 0 deletions ports/raspberrypi/common-hal/floppyio/__init__.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2024 Jeff Epler for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include "bindings/rp2pio/StateMachine.h"
#include "py/runtime.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/floppyio/__init__.h"
#include "common-hal/floppyio/__init__.h"
#include "shared-bindings/time/__init__.h"
#include "supervisor/shared/tick.h"

static const uint16_t fluxread_program[] = {
// ; Count flux pulses and watch for index pin
// ; flux input is the 'jmp pin'. index is "pin zero".
// ; Counts are in units 3 / F_pio, so e.g., at 30MHz 1 count = 0.1us
// ; Count down while waiting for the counter to go HIGH
// ; The only counting is down, so C code will just have to negate the
// count!
// ; Each 'wait one' loop takes 3 instruction-times
// wait_one:
0x0041, // jmp x--, wait_one_next ; acts as a non-conditional decrement
// of x
// wait_one_next:
0x00c3, // jmp pin wait_zero
0x0000, // jmp wait_one
// ; Each 'wait zero' loop takes 3 instruction-times, needing one
// instruction delay
// ; (it has to match the 'wait one' timing exactly)
// wait_zero:
0x0044, // jmp x--, wait_zero_next ; acts as a non-conditional decrement
// of x
// wait_zero_next:
0x01c3, // jmp pin wait_zero [1]
// ; Top bit is index status, bottom 15 bits are inverse of counts
// ; Combined FIFO gives 16 entries (8 32-bit entries) so with the
// ; smallest plausible pulse of 2us there are 250 CPU cycles available
// @125MHz
0x4001, // in pins, 1
0x402f, // in x, 15
// ; Three cycles for the end of loop, so we need to decrement x to make
// everything
// ; come out right. This has constant timing whether we actually jump back
// vs wrapping.
0x0040, // jmp x--, wait_one
};

typedef struct {
PIO pio;
uint8_t sm;
bool word_available;
uint16_t half;
} floppy_reader;

static bool data_available(floppy_reader *reader) {
return reader->word_available || !pio_sm_is_rx_fifo_empty(reader->pio, reader->sm);
}

static uint16_t read_fifo(floppy_reader *reader) {
if (reader->word_available) {
reader->word_available = false;
return reader->half;
}
uint32_t value = pio_sm_get_blocking(reader->pio, reader->sm);
reader->half = value >> 16;
reader->word_available = true;
return value & 0xffff;
}


int common_hal_floppyio_flux_readinto(void *buf, size_t len, digitalio_digitalinout_obj_t *data, digitalio_digitalinout_obj_t *index, mp_int_t index_wait_ms) {
#define READ_INDEX() (!!(*index_port & index_mask))
uint32_t index_mask;
volatile uint32_t *index_port = common_hal_digitalio_digitalinout_get_reg(index, DIGITALINOUT_REG_READ, &index_mask);

memset(buf, 0, len);

uint32_t pins_we_use = 1 << data->pin->number;

rp2pio_statemachine_obj_t state_machine;
bool ok = rp2pio_statemachine_construct(&state_machine,
fluxread_program, MP_ARRAY_SIZE(fluxread_program),
FLOPPYIO_SAMPLERATE * 3, // 3 PIO cycles per sample count
NULL, 0, // init program
NULL, 0, // out
index->pin, 1, // in
1, 0, // in pull up/down
NULL, 0, // set
NULL, 0, // sideset
0, 0, // initial pin state
data->pin, // jump pin
pins_we_use, false, true,
true, 32, false, // TX setting we don't use
true, // Wait for txstall. If we don't, then we'll deinit too quickly.
true, 32, true, // move 32 bits at a time
false, // claim pins
false, // Not user-interruptible.
false, // No sideset enable
0, -1, // wrap
PIO_ANY_OFFSET // offset
);
if (!ok) {
mp_raise_RuntimeError(MP_ERROR_TEXT("All state machines in use"));
}

floppy_reader reader = { .pio = state_machine.pio, .sm = state_machine.state_machine, };

uint8_t *ptr = buf, *end = ptr + len;

uint64_t index_deadline_us = time_us_64() + index_wait_ms * 1000;

common_hal_mcu_disable_interrupts();

// check if flux is arriving
uint64_t flux_deadline_us = time_us_64() + 20;
while (pio_sm_is_rx_fifo_empty(reader.pio, reader.sm)) {
if (time_us_64() > flux_deadline_us) {
common_hal_mcu_enable_interrupts();
common_hal_rp2pio_statemachine_deinit(&state_machine);
mp_raise_RuntimeError(MP_ERROR_TEXT("timeout waiting for flux"));
}
}

// wait for index pulse low
while (READ_INDEX()) {
if (time_us_64() > index_deadline_us) {
common_hal_mcu_enable_interrupts();
common_hal_rp2pio_statemachine_deinit(&state_machine);
mp_raise_RuntimeError(MP_ERROR_TEXT("timeout waiting for index pulse"));
}
}

pio_sm_clear_fifos(reader.pio, reader.sm);

// if another index doesn't show up ...
index_deadline_us = time_us_64() + index_wait_ms * 1000;

int last = read_fifo(&reader);
bool last_index = READ_INDEX();
while (ptr != end) {

/* Handle index */
bool now_index = READ_INDEX();

if (!now_index && last_index) {
break;
}
last_index = now_index;

if (!data_available(&reader)) {
// no flux is arriving? is ANY flux arriving or has a full revoulution gone by?
if (time_us_64() > index_deadline_us) {
break;
}
continue;
}

int timestamp = read_fifo(&reader);
int delta = last - timestamp;
if (delta < 0) {
delta += 65536;
}
delta /= 2;

last = timestamp;
*ptr++ = delta > 255 ? 255 : delta;
}

common_hal_mcu_enable_interrupts();
common_hal_rp2pio_statemachine_deinit(&state_machine);

return ptr - (uint8_t *)buf;
}
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/neopixel_write/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void common_hal_neopixel_write(const digitalio_digitalinout_obj_t *digitalinout,
// change the pins then though.
uint32_t pins_we_use = 1 << digitalinout->pin->number;
bool ok = rp2pio_statemachine_construct(&state_machine,
neopixel_program, sizeof(neopixel_program) / sizeof(neopixel_program[0]),
neopixel_program, MP_ARRAY_SIZE(neopixel_program),
12800000, // 12.8MHz, to get appropriate sub-bit times in PIO program.
NULL, 0, // init program
NULL, 1, // out
Expand Down
3 changes: 3 additions & 0 deletions ports/unix/variants/coverage/mpconfigvariant.mk
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ SRC_BITMAP := \
shared-bindings/displayio/Bitmap.c \
shared-bindings/displayio/ColorConverter.c \
shared-bindings/displayio/Palette.c \
shared-bindings/floppyio/__init__.c \
shared-bindings/jpegio/__init__.c \
shared-bindings/jpegio/JpegDecoder.c \
shared-bindings/locale/__init__.c \
Expand Down Expand Up @@ -69,6 +70,7 @@ SRC_BITMAP := \
shared-module/displayio/Bitmap.c \
shared-module/displayio/ColorConverter.c \
shared-module/displayio/Palette.c \
shared-module/floppyio/__init__.c \
shared-module/jpegio/__init__.c \
shared-module/jpegio/JpegDecoder.c \
shared-module/os/getenv.c \
Expand All @@ -94,6 +96,7 @@ CFLAGS += \
-DCIRCUITPY_BITMAPTOOLS=1 \
-DCIRCUITPY_CODEOP=1 \
-DCIRCUITPY_DISPLAYIO_UNIX=1 \
-DCIRCUITPY_FLOPPYIO=1 \
-DCIRCUITPY_FUTURE=1 \
-DCIRCUITPY_GIFIO=1 \
-DCIRCUITPY_JPEGIO=1 \
Expand Down
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ SRC_COMMON_HAL_ALL = \
dotclockframebuffer/DotClockFramebuffer.c \
dotclockframebuffer/__init__.c \
dualbank/__init__.c \
floppyio/__init__.c \
frequencyio/FrequencyIn.c \
frequencyio/__init__.c \
imagecapture/ParallelImageCapture.c \
Expand Down
Loading