Skip to content

Commit d816a4f

Browse files
committed
Add floppyio
Initially enabled for samd51, this enables reading raw flux data as well as DOS/MFM formatted media. This is only the low-level code for reading & decoding flux pulses from a floppy drive. high level details will live in a Python library. adafruit-circuitpython-floppy will take care of details like stepping from track to track, etc.
1 parent db5f99c commit d816a4f

9 files changed

Lines changed: 311 additions & 0 deletions

File tree

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,6 @@
199199
url = https://github.com/raspberrypi/rpi-firmware.git
200200
branch = master
201201
shallow = true
202+
[submodule "lib/adafruit_floppy"]
203+
path = lib/adafruit_floppy
204+
url = https://github.com/adafruit/Adafruit_Floppy

lib/adafruit_floppy

Submodule adafruit_floppy added at d5d3412
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
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+
#pragma once
28+
29+
// empirical-ish from Arduino @ 120MHz
30+
#define FLOPPYIO_SAMPLERATE (14666667)

ports/atmel-samd/mpconfigport.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
8787
CIRCUITPY_ALARM ?= 1
8888
CIRCUITPY_PS2IO ?= 1
8989
CIRCUITPY_SAMD ?= 1
90+
CIRCUITPY_FLOPPYIO ?= 1
9091
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
9192
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FRAMEBUFFERIO)
9293
CIRCUITPY_WATCHDOG ?= 1
@@ -107,6 +108,7 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
107108
CIRCUITPY_ALARM ?= 1
108109
CIRCUITPY_PS2IO ?= 1
109110
CIRCUITPY_SAMD ?= 1
111+
CIRCUITPY_FLOPPYIO ?= 1
110112
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
111113
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FRAMEBUFFERIO)
112114

py/circuitpy_defns.mk

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,9 @@ endif
164164
ifeq ($(CIRCUITPY_VECTORIO),1)
165165
SRC_PATTERNS += vectorio/%
166166
endif
167+
ifeq ($(CIRCUITPY_FLOPPYIO),1)
168+
SRC_PATTERNS += floppyio/%
169+
endif
167170
ifeq ($(CIRCUITPY_FRAMEBUFFERIO),1)
168171
SRC_PATTERNS += framebufferio/%
169172
endif
@@ -526,6 +529,7 @@ SRC_SHARED_MODULE_ALL = \
526529
displayio/TileGrid.c \
527530
displayio/area.c \
528531
displayio/__init__.c \
532+
floppyio/__init__.c \
529533
fontio/BuiltinFont.c \
530534
fontio/__init__.c \
531535
framebufferio/FramebufferDisplay.c \

py/circuitpy_mpconfig.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ CFLAGS += -DCIRCUITPY_ESPIDF=$(CIRCUITPY_ESPIDF)
219219
CIRCUITPY__EVE ?= 0
220220
CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE)
221221

222+
CIRCUITPY_FLOPPYIO ?= 0
223+
CFLAGS += -DCIRCUITPY_FLOPPYIO=$(CIRCUITPY_FLOPPYIO)
224+
222225
CIRCUITPY_FREQUENCYIO ?= $(CIRCUITPY_FULL_BUILD)
223226
CFLAGS += -DCIRCUITPY_FREQUENCYIO=$(CIRCUITPY_FREQUENCYIO)
224227

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
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+
#include "shared-bindings/floppyio/__init__.h"
28+
#include "shared-bindings/digitalio/DigitalInOut.h"
29+
#include "common-hal/floppyio/__init__.h"
30+
31+
#include <stdint.h>
32+
33+
#include "py/binary.h"
34+
#include "py/enum.h"
35+
#include "py/obj.h"
36+
#include "py/runtime.h"
37+
38+
//| def flux_readinto(buffer: WriteableBuffer, data: DigitalInOut, index: DigitalInOut) -> int:
39+
//| """Read flux transition information into the buffer.
40+
//|
41+
//| The function returns when the buffer has filled, or when the index input
42+
//| indicates that one full revolution of data has been recorded. Due to
43+
//| technical limitations, this process may not be interruptible by
44+
//| KeyboardInterrupt.
45+
//|
46+
//| :param buffer: Read data into this buffer. Each element represents the time between successive zero-to-one transitions.
47+
//| :param data: Pin on which the flux data appears
48+
//| :param index: Pin on which the index pulse appears
49+
//| :return: The actual number of bytes of read
50+
//| """
51+
//| ...
52+
//|
53+
STATIC mp_obj_t floppyio_flux_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
54+
enum { ARG_buffer, ARG_data, ARG_index };
55+
static const mp_arg_t allowed_args[] = {
56+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
57+
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
58+
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
59+
};
60+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
61+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
62+
63+
mp_buffer_info_t bufinfo;
64+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
65+
digitalio_digitalinout_obj_t *data = assert_digitalinout(args[ARG_data].u_obj);
66+
digitalio_digitalinout_obj_t *index = assert_digitalinout(args[ARG_index].u_obj);
67+
68+
return MP_OBJ_NEW_SMALL_INT(common_hal_floppyio_flux_readinto(bufinfo.buf, bufinfo.len, data, index));
69+
}
70+
MP_DEFINE_CONST_FUN_OBJ_KW(floppyio_flux_readinto_obj, 0, floppyio_flux_readinto);
71+
72+
//| def mfm_readinto(buffer: WriteableBuffer, data: DigitalInOut, index: DigitalInOut) -> int:
73+
//| """Read mfm blocks into the buffer.
74+
//|
75+
//| The track is assumed to consist of 512-byte sectors.
76+
//|
77+
//| The function returns when all sectors have been successfully read, or
78+
//| a number of index pulses have occurred. Due to technical limitations, this
79+
//| process may not be interruptible by KeyboardInterrupt.
80+
//|
81+
//| :param buffer: Read data into this buffer. Must be a multiple of 512.
82+
//| :param data: Pin on which the mfm data appears
83+
//| :param index: Pin on which the index pulse appears
84+
//| :return: The actual number of sectors read
85+
//| """
86+
//| ...
87+
//|
88+
STATIC mp_obj_t floppyio_mfm_readinto(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
89+
enum { ARG_buffer, ARG_data, ARG_index };
90+
static const mp_arg_t allowed_args[] = {
91+
{ MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
92+
{ MP_QSTR_data, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
93+
{ MP_QSTR_index, MP_ARG_REQUIRED | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
94+
};
95+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
96+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
97+
98+
mp_buffer_info_t bufinfo;
99+
mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
100+
digitalio_digitalinout_obj_t *data = assert_digitalinout(args[ARG_data].u_obj);
101+
digitalio_digitalinout_obj_t *index = assert_digitalinout(args[ARG_index].u_obj);
102+
103+
if (bufinfo.len % 512 != 0) {
104+
mp_raise_ValueError(translate("Buffer must be a multiple of 512 bytes"));
105+
}
106+
size_t n_sectors = bufinfo.len / 512;
107+
return MP_OBJ_NEW_SMALL_INT(common_hal_floppyio_mfm_readinto(bufinfo.buf, n_sectors, data, index));
108+
}
109+
MP_DEFINE_CONST_FUN_OBJ_KW(floppyio_mfm_readinto_obj, 0, floppyio_mfm_readinto);
110+
111+
//| samplerate: int
112+
//| """The approximate sample rate in Hz used by flux_readinto."""
113+
//|
114+
115+
STATIC const mp_rom_map_elem_t floppyio_module_globals_table[] = {
116+
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_floppyio) },
117+
{ MP_ROM_QSTR(MP_QSTR_flux_readinto), MP_ROM_PTR(&floppyio_flux_readinto_obj) },
118+
{ MP_ROM_QSTR(MP_QSTR_mfm_readinto), MP_ROM_PTR(&floppyio_mfm_readinto_obj) },
119+
{ MP_ROM_QSTR(MP_QSTR_samplerate), MP_ROM_INT(FLOPPYIO_SAMPLERATE) },
120+
};
121+
STATIC MP_DEFINE_CONST_DICT(floppyio_module_globals, floppyio_module_globals_table);
122+
123+
const mp_obj_module_t floppyio_module = {
124+
.base = {&mp_type_module },
125+
.globals = (mp_obj_dict_t *)&floppyio_module_globals,
126+
};
127+
128+
MP_REGISTER_MODULE(MP_QSTR_floppyio, floppyio_module, CIRCUITPY_FLOPPYIO);
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
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+
#pragma once
28+
29+
#include "common-hal/digitalio/DigitalInOut.h"
30+
31+
int common_hal_floppyio_flux_readinto(void *buf, size_t len, digitalio_digitalinout_obj_t *data, digitalio_digitalinout_obj_t *index);
32+
int common_hal_floppyio_mfm_readinto(void *buf, size_t n_sector, digitalio_digitalinout_obj_t *data, digitalio_digitalinout_obj_t *index);

shared-module/floppyio/__init__.c

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2022 Jeff Epler for Adafruit Industries
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+
#include "hal_gpio.h"
28+
29+
#include "py/runtime.h"
30+
31+
#include "shared-bindings/time/__init__.h"
32+
#include "shared-bindings/floppyio/__init__.h"
33+
#include "common-hal/floppyio/__init__.h"
34+
#include "shared-bindings/digitalio/DigitalInOut.h"
35+
36+
#define T2_5 (FLOPPYIO_SAMPLERATE * 5 / 2 / 1000000)
37+
#define T3_5 (FLOPPYIO_SAMPLERATE * 7 / 2 / 1000000)
38+
39+
#define MFM_IO_MMIO (1)
40+
#include "lib/adafruit_floppy/mfm_impl.h"
41+
42+
__attribute__((optimize("O3")))
43+
int common_hal_floppyio_flux_readinto(void *buf, size_t len, digitalio_digitalinout_obj_t *data, digitalio_digitalinout_obj_t *index) {
44+
uint32_t index_mask;
45+
volatile uint32_t *index_port = common_hal_digitalio_digitalinout_get_reg(index, DIGITALINOUT_REG_READ, &index_mask);
46+
47+
uint32_t data_mask;
48+
volatile uint32_t *data_port = common_hal_digitalio_digitalinout_get_reg(data, DIGITALINOUT_REG_READ, &data_mask);
49+
50+
#undef READ_INDEX
51+
#undef READ_DATA
52+
#define READ_INDEX() (!!(*index_port & index_mask))
53+
#define READ_DATA() (!!(*data_port & data_mask))
54+
55+
memset(buf, 0, len);
56+
57+
uint8_t *pulses = buf, *pulses_ptr = pulses, *pulses_end = pulses + len;
58+
59+
common_hal_mcu_disable_interrupts();
60+
61+
// wait for index pulse low
62+
while (READ_INDEX()) { /* NOTHING */
63+
}
64+
65+
66+
// if data line is low, wait till it rises
67+
while (!READ_DATA()) { /* NOTHING */
68+
}
69+
70+
// and then till it drops down
71+
while (READ_DATA()) { /* NOTHING */
72+
}
73+
74+
uint32_t index_state = 0;
75+
while (pulses_ptr < pulses_end) {
76+
index_state = (index_state << 1) | READ_INDEX();
77+
if ((index_state & 3) == 2) { // a zero-to-one transition
78+
break;
79+
}
80+
81+
unsigned pulse_count = 3;
82+
while (!READ_DATA()) {
83+
pulse_count++;
84+
}
85+
86+
while (READ_DATA()) {
87+
pulse_count++;
88+
}
89+
90+
*pulses_ptr++ = MIN(255, pulse_count);
91+
}
92+
common_hal_mcu_enable_interrupts();
93+
94+
return pulses_ptr - pulses;
95+
}
96+
97+
int common_hal_floppyio_mfm_readinto(void *buf, size_t n_sectors, digitalio_digitalinout_obj_t *data, digitalio_digitalinout_obj_t *index) {
98+
mfm_io_t io;
99+
io.index_port = common_hal_digitalio_digitalinout_get_reg(index, DIGITALINOUT_REG_READ, &io.index_mask);
100+
io.data_port = common_hal_digitalio_digitalinout_get_reg(data, DIGITALINOUT_REG_READ, &io.data_mask);
101+
102+
common_hal_mcu_disable_interrupts();
103+
uint8_t validity[n_sectors];
104+
int result = read_track(io, n_sectors, buf, validity);
105+
common_hal_mcu_enable_interrupts();
106+
107+
return result;
108+
}

0 commit comments

Comments
 (0)