Skip to content

Commit

Permalink
Merge pull request #5852 from jepler/floppy
Browse files Browse the repository at this point in the history
Add floppyio
  • Loading branch information
dhalbert authored Apr 26, 2022
2 parents c8e8171 + 766bf8f commit 3a8fb4e
Show file tree
Hide file tree
Showing 24 changed files with 444 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,9 @@
url = https://github.com/raspberrypi/rpi-firmware.git
branch = master
shallow = true
[submodule "lib/adafruit_floppy"]
path = lib/adafruit_floppy
url = https://github.com/adafruit/Adafruit_Floppy
[submodule "ports/stm/st_driver/cmsis_device_f4"]
path = ports/stm/st_driver/cmsis_device_f4
url = https://github.com/STMicroelectronics/cmsis_device_f4.git
Expand Down
1 change: 1 addition & 0 deletions lib/adafruit_floppy
Submodule adafruit_floppy added at e36a61
1 change: 1 addition & 0 deletions ports/atmel-samd/boards/kicksat-sprite/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ CIRCUITPY_AUDIOMIXER = 0
CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BLEIO_HCI = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_FLOPPYIO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
CIRCUITPY_GETPASS = 0
CIRCUITPY_KEYPAD = 0
Expand Down
27 changes: 27 additions & 0 deletions ports/atmel-samd/common-hal/digitalio/DigitalInOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,30 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
}
}
}

bool common_hal_digitalio_has_reg_op(digitalinout_reg_op_t op) {
return true;
}

volatile uint32_t *common_hal_digitalio_digitalinout_get_reg(digitalio_digitalinout_obj_t *self, digitalinout_reg_op_t op, uint32_t *mask) {
const uint8_t pin = self->pin->number;
int port = GPIO_PORT(pin);

*mask = 1u << GPIO_PIN(pin);


switch (op) {
case DIGITALINOUT_REG_READ:
return (volatile uint32_t *)&PORT->Group[port].IN.reg;
case DIGITALINOUT_REG_WRITE:
return &PORT->Group[port].OUT.reg;
case DIGITALINOUT_REG_SET:
return &PORT->Group[port].OUTSET.reg;
case DIGITALINOUT_REG_RESET:
return &PORT->Group[port].OUTCLR.reg;
case DIGITALINOUT_REG_TOGGLE:
return &PORT->Group[port].OUTTGL.reg;
default:
return NULL;
}
}
30 changes: 30 additions & 0 deletions ports/atmel-samd/common-hal/floppyio/__init__.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 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.
*/

#pragma once

// empirical-ish from Arduino @ 120MHz
#define FLOPPYIO_SAMPLERATE (14666667)
4 changes: 3 additions & 1 deletion ports/atmel-samd/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ CIRCUITPY_TOUCHIO_USE_NATIVE = 0
CIRCUITPY_ALARM ?= 1
CIRCUITPY_PS2IO ?= 1
CIRCUITPY_SAMD ?= 1
CIRCUITPY_FLOPPYIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FRAMEBUFFERIO)
CIRCUITPY_WATCHDOG ?= 1
Expand All @@ -110,14 +111,15 @@ endif # samd51

ifeq ($(CHIP_FAMILY),same51)

# No native touchio on SAMD51.
# No native touchio on SAME51.
CIRCUITPY_TOUCHIO_USE_NATIVE = 0

# The ?='s allow overriding in mpconfigboard.mk.

CIRCUITPY_ALARM ?= 1
CIRCUITPY_PS2IO ?= 1
CIRCUITPY_SAMD ?= 1
CIRCUITPY_FLOPPYIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_FULL_BUILD)
CIRCUITPY_RGBMATRIX ?= $(CIRCUITPY_FRAMEBUFFERIO)

Expand Down
9 changes: 7 additions & 2 deletions ports/raspberrypi/bindings/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@
//| initial_sideset_pin_state: int = 0,
//| initial_sideset_pin_direction: int = 0x1f,
//| sideset_enable: bool = False,
//| jmp_pin: Optional[microcontroller.Pin] = None,
//| jmp_pin_pull: Optional[digitalio.Pull] = None,
//| exclusive_pin_use: bool = True,
//| auto_pull: bool = False,
//| pull_threshold: int = 32,
Expand Down Expand Up @@ -116,6 +118,7 @@
//| :param int initial_sideset_pin_direction: the initial output direction for sideset pins starting at first_sideset_pin
//| :param bool sideset_enable: True when the top sideset bit is to enable. This should be used with the ".side_set # opt" directive
//| :param ~microcontroller.Pin jmp_pin: the pin which determines the branch taken by JMP PIN instructions
//| :param ~digitalio.Pull jmp_pin_pull: The pull value for the jmp pin, default is no pull.
//| :param bool exclusive_pin_use: When True, do not share any pins with other state machines. Pins are never shared with other peripherals
//| :param bool auto_pull: When True, automatically load data from the tx FIFO into the
//| output shift register (OSR) when an OUT instruction shifts more than pull_threshold bits
Expand Down Expand Up @@ -157,7 +160,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
ARG_first_set_pin, ARG_set_pin_count, ARG_initial_set_pin_state, ARG_initial_set_pin_direction,
ARG_first_sideset_pin, ARG_sideset_pin_count, ARG_initial_sideset_pin_state, ARG_initial_sideset_pin_direction,
ARG_sideset_enable,
ARG_jmp_pin,
ARG_jmp_pin, ARG_jmp_pin_pull,
ARG_exclusive_pin_use,
ARG_auto_pull, ARG_pull_threshold, ARG_out_shift_right,
ARG_wait_for_txstall,
Expand Down Expand Up @@ -193,6 +196,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
{ MP_QSTR_sideset_enable, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },

{ MP_QSTR_jmp_pin, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
{ MP_QSTR_jmp_pin_pull, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },

{ MP_QSTR_exclusive_pin_use, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} },
{ MP_QSTR_auto_pull, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
Expand Down Expand Up @@ -242,6 +246,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
}

const mcu_pin_obj_t *jmp_pin = validate_obj_is_pin_or_none(args[ARG_jmp_pin].u_obj);
digitalio_pull_t jmp_pin_pull = validate_pull(args[ARG_jmp_pin_pull].u_rom_obj, MP_QSTR_jmp_pull);

mp_int_t pull_threshold = args[ARG_pull_threshold].u_int;
mp_int_t push_threshold = args[ARG_push_threshold].u_int;
Expand Down Expand Up @@ -278,7 +283,7 @@ STATIC mp_obj_t rp2pio_statemachine_make_new(const mp_obj_type_t *type, size_t n
first_set_pin, args[ARG_set_pin_count].u_int, args[ARG_initial_set_pin_state].u_int, args[ARG_initial_set_pin_direction].u_int,
first_sideset_pin, args[ARG_sideset_pin_count].u_int, args[ARG_initial_sideset_pin_state].u_int, args[ARG_initial_sideset_pin_direction].u_int,
args[ARG_sideset_enable].u_bool,
jmp_pin,
jmp_pin, jmp_pin_pull,
0,
args[ARG_exclusive_pin_use].u_bool,
args[ARG_auto_pull].u_bool, pull_threshold, args[ARG_out_shift_right].u_bool,
Expand Down
3 changes: 2 additions & 1 deletion ports/raspberrypi/bindings/rp2pio/StateMachine.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

#include "py/obj.h"

#include "shared-bindings/digitalio/Pull.h"
#include "common-hal/microcontroller/Pin.h"
#include "common-hal/rp2pio/StateMachine.h"

Expand All @@ -45,7 +46,7 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, uint32_t initial_set_pin_state, uint32_t initial_set_pin_direction,
const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction,
bool sideset_enable,
const mcu_pin_obj_t *jmp_pin,
const mcu_pin_obj_t *jmp_pin, digitalio_pull_t jmp_pin_pull,
uint32_t wait_gpio_mask,
bool exclusive_pin_use,
bool auto_pull, uint8_t pull_threshold, bool out_shift_right,
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/audiobusio/I2SOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t *self,
NULL, 0, 0, 0x1f, // set pins
bit_clock, 2, 0, 0x1f, // sideset pins
false, // No sideset enable
NULL, // jump pin
NULL, PULL_NONE, // jump pin
0, // wait gpio pins
true, // exclusive pin use
false, 32, false, // shift out left to start with MSB
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/audiobusio/PDMIn.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ void common_hal_audiobusio_pdmin_construct(audiobusio_pdmin_obj_t *self,
NULL, 0, 0, 0x1f, // set pins
clock_pin, 1, 0, 0x1f, // sideset pins
false, // No sideset enable
NULL, // jump pin
NULL, PULL_NONE, // jump pin
0, // wait gpio pins
true, // exclusive pin use
false, 32, false, // out settings
Expand Down
25 changes: 25 additions & 0 deletions ports/raspberrypi/common-hal/digitalio/DigitalInOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,3 +164,28 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(
}
return PULL_NONE;
}

bool common_hal_digitalio_has_reg_op(digitalinout_reg_op_t op) {
return true;
}

volatile uint32_t *common_hal_digitalio_digitalinout_get_reg(digitalio_digitalinout_obj_t *self, digitalinout_reg_op_t op, uint32_t *mask) {
const uint8_t pin = self->pin->number;

*mask = 1u << pin;

switch (op) {
case DIGITALINOUT_REG_READ:
return (volatile uint32_t *)&sio_hw->gpio_in;
case DIGITALINOUT_REG_WRITE:
return &sio_hw->gpio_out;
case DIGITALINOUT_REG_SET:
return &sio_hw->gpio_set;
case DIGITALINOUT_REG_RESET:
return &sio_hw->gpio_clr;
case DIGITALINOUT_REG_TOGGLE:
return &sio_hw->gpio_togl;
default:
return NULL;
}
}
37 changes: 37 additions & 0 deletions ports/raspberrypi/common-hal/floppyio/__init__.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* This file is part of the Micro Python project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2022 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.
*/

#pragma once

// empirical-ish from RP2040 @ 125MHz for floppy_flux_readinto
#define FLOPPYIO_SAMPLERATE (24000000)
// empirical-ish from RP2040 @ 125MHz for floppy_mfm_readinto
// my guess is these are slower because the more complex routine falls out of cache, but it's just
// speculation because the loops are very similar. When looking at raw bins with a modified
// version of adafruit_floppy, it can be seen that there are _two_ peaks for T2 and T3, rather
// than a single one, around 36 (mostly) and 43 (rarer), compared to a single peak around 48.
#define T2_5 (54)
#define T3_5 (75)
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void common_hal_imagecapture_parallelimagecapture_construct(imagecapture_paralle
NULL, 0, 0, 0, // sideset pins
#endif
false, // No sideset enable
NULL, // jump pin
NULL, PULL_NONE, // jump pin
(1 << vertical_sync->number) | (1 << horizontal_reference->number) | (1 << data_clock->number), // wait gpio pins
true, // exclusive pin use
false, 32, false, // out settings
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/paralleldisplay/ParallelBus.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void common_hal_paralleldisplay_parallelbus_construct(paralleldisplay_parallelbu
NULL, 0, 0, 0, // first set pin
write, 1, 0, 1, // first sideset pin
false, // No sideset enable
NULL, // jump pin
NULL, PULL_NONE, // jump pin
0, // wait gpio pins
true, // exclusive pin usage
true, 8, true, // TX, auto pull every 8 bits. shift left to output msb first
Expand Down
2 changes: 1 addition & 1 deletion ports/raspberrypi/common-hal/rotaryio/IncrementalEncoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
NULL, 0, 0, 0x1f, // set pins
NULL, 0, 0, 0x1f, // sideset pins
false, // No sideset enable
NULL, // jump pin
NULL, PULL_NONE, // jump pin
0, // wait gpio pins
true, // exclusive pin use
false, 32, false, // out settings
Expand Down
13 changes: 12 additions & 1 deletion ports/raspberrypi/common-hal/rp2pio/StateMachine.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "bindings/rp2pio/StateMachine.h"

#include "common-hal/microcontroller/__init__.h"
#include "shared-bindings/digitalio/Pull.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/microcontroller/Pin.h"

Expand Down Expand Up @@ -383,7 +384,7 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
const mcu_pin_obj_t *first_set_pin, uint8_t set_pin_count, uint32_t initial_set_pin_state, uint32_t initial_set_pin_direction,
const mcu_pin_obj_t *first_sideset_pin, uint8_t sideset_pin_count, uint32_t initial_sideset_pin_state, uint32_t initial_sideset_pin_direction,
bool sideset_enable,
const mcu_pin_obj_t *jmp_pin,
const mcu_pin_obj_t *jmp_pin, digitalio_pull_t jmp_pull,
uint32_t wait_gpio_mask,
bool exclusive_pin_use,
bool auto_pull, uint8_t pull_threshold, bool out_shift_right,
Expand Down Expand Up @@ -531,6 +532,16 @@ void common_hal_rp2pio_statemachine_construct(rp2pio_statemachine_obj_t *self,
// Deal with pull up/downs
uint32_t pull_up = mask_and_rotate(first_in_pin, in_pin_count, pull_pin_up);
uint32_t pull_down = mask_and_rotate(first_in_pin, in_pin_count, pull_pin_down);

if (jmp_pin) {
uint32_t jmp_mask = mask_and_rotate(jmp_pin, 1, 0x1f);
if (jmp_pull == PULL_UP) {
pull_up |= jmp_mask;
}
if (jmp_pull == PULL_DOWN) {
pull_up |= jmp_mask;
}
}
if (initial_pin_direction & (pull_up | pull_down)) {
mp_raise_ValueError(translate("pull masks conflict with direction masks"));
}
Expand Down
1 change: 1 addition & 0 deletions ports/raspberrypi/mpconfigport.mk
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ CIRCUITPY_ALARM ?= 1

CIRCUITPY_RP2PIO ?= 1
CIRCUITPY_NEOPIXEL_WRITE ?= $(CIRCUITPY_RP2PIO)
CIRCUITPY_FLOPPYIO ?= 1
CIRCUITPY_FRAMEBUFFERIO ?= $(CIRCUITPY_DISPLAYIO)
CIRCUITPY_FULL_BUILD ?= 1
CIRCUITPY_AUDIOMP3 ?= 1
Expand Down
4 changes: 4 additions & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ endif
ifeq ($(CIRCUITPY_VECTORIO),1)
SRC_PATTERNS += vectorio/%
endif
ifeq ($(CIRCUITPY_FLOPPYIO),1)
SRC_PATTERNS += floppyio/%
endif
ifeq ($(CIRCUITPY_FRAMEBUFFERIO),1)
SRC_PATTERNS += framebufferio/%
endif
Expand Down Expand Up @@ -545,6 +548,7 @@ SRC_SHARED_MODULE_ALL = \
displayio/TileGrid.c \
displayio/area.c \
displayio/__init__.c \
floppyio/__init__.c \
fontio/BuiltinFont.c \
fontio/__init__.c \
framebufferio/FramebufferDisplay.c \
Expand Down
3 changes: 3 additions & 0 deletions py/circuitpy_mpconfig.mk
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,9 @@ CFLAGS += -DCIRCUITPY_ESPIDF=$(CIRCUITPY_ESPIDF)
CIRCUITPY__EVE ?= 0
CFLAGS += -DCIRCUITPY__EVE=$(CIRCUITPY__EVE)

CIRCUITPY_FLOPPYIO ?= 0
CFLAGS += -DCIRCUITPY_FLOPPYIO=$(CIRCUITPY_FLOPPYIO)

CIRCUITPY_FREQUENCYIO ?= $(CIRCUITPY_FULL_BUILD)
CFLAGS += -DCIRCUITPY_FREQUENCYIO=$(CIRCUITPY_FREQUENCYIO)

Expand Down
11 changes: 11 additions & 0 deletions shared-bindings/digitalio/DigitalInOut.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ typedef enum {
DIGITALINOUT_INPUT_ONLY
} digitalinout_result_t;

typedef enum {
DIGITALINOUT_REG_READ,
DIGITALINOUT_REG_WRITE,
DIGITALINOUT_REG_SET,
DIGITALINOUT_REG_RESET,
DIGITALINOUT_REG_TOGGLE,
} digitalinout_reg_op_t;

digitalinout_result_t common_hal_digitalio_digitalinout_construct(digitalio_digitalinout_obj_t *self, const mcu_pin_obj_t *pin);
void common_hal_digitalio_digitalinout_deinit(digitalio_digitalinout_obj_t *self);
bool common_hal_digitalio_digitalinout_deinited(digitalio_digitalinout_obj_t *self);
Expand All @@ -56,4 +64,7 @@ digitalio_pull_t common_hal_digitalio_digitalinout_get_pull(digitalio_digitalino
void common_hal_digitalio_digitalinout_never_reset(digitalio_digitalinout_obj_t *self);
digitalio_digitalinout_obj_t *assert_digitalinout(mp_obj_t obj);

volatile uint32_t *common_hal_digitalio_digitalinout_get_reg(digitalio_digitalinout_obj_t *self, digitalinout_reg_op_t op, uint32_t *mask);
bool common_hal_digitalio_has_reg_op(digitalinout_reg_op_t op);

#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DIGITALIO_DIGITALINOUT_H
Loading

0 comments on commit 3a8fb4e

Please sign in to comment.