Skip to content

Commit

Permalink
Handle Sense pin in an open-collector fashion (#52)
Browse files Browse the repository at this point in the history
* Handle Sense pin in an open-collector fashion

* Fix description
  • Loading branch information
SukkoPera authored Aug 21, 2024
1 parent 70a3989 commit f399d65
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
21 changes: 15 additions & 6 deletions config.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,6 @@
#define TWI_PIN_SCL 5

// port definitions, change for different wiring
#define SENSE_PORT PORTD
#define SENSE_DDR DDRD
#define SENSE_PIN 5
#define SENSE_ON() SENSE_PORT &= ~_BV(SENSE_PIN)
#define SENSE_OFF() SENSE_PORT |= _BV(SENSE_PIN)

#define TAPE_READ_PORT PORTD
#define TAPE_READ_DDR DDRD
#define TAPE_READ_PIN 3
Expand All @@ -63,6 +57,21 @@
#define TAPE_WRITE_PINS PINB
#define TAPE_WRITE_PIN 0

/* The Sense pin on the original Datassette is a physical switch that only grounds the line when pressed, it's left
* floating when no keys are pressed. This means that the pin should be driven in an open-collector fashion, otherwise
* it can create problems, in particular on the Plus/4 where the Sense line is shared with data pin 2 of the User Port.
*
* This could be compensated in hardware (put a diode in series, cathode to Arduino side) or in software, by taking
* advantage of the way ATmega pins are configured, which is what we do here:
* - To set sense "high" we set the pin to input mode, pin will go Hi-Z and basically float
* - To set it low, we switch to output mode which will implicitly be low (PORTD defaults to 0 and we never touch it)
*/
#define SENSE_PORT PORTD
#define SENSE_DDR DDRD
#define SENSE_PIN 5
#define SENSE_ON() SENSE_DDR |= _BV(SENSE_PIN)
#define SENSE_OFF() SENSE_DDR &= ~_BV(SENSE_PIN)

#define MOTOR_PORT PORTD
#define MOTOR_DDR DDRD
#define MOTOR_PIN 4
Expand Down
3 changes: 1 addition & 2 deletions tapuino.c
Original file line number Diff line number Diff line change
Expand Up @@ -648,8 +648,7 @@ int tapuino_hardware_setup(void)
TWI_PORT |= _BV(TWI_PIN_SDA);
TWI_PORT |= _BV(TWI_PIN_SCL);

// sense is output to C64
SENSE_DDR |= _BV(SENSE_PIN);
// sense is output to C64 but it works in an open-collector fashion, so no need to set DDR, just make sure it is off
SENSE_OFF();

// read is output to C64
Expand Down

0 comments on commit f399d65

Please sign in to comment.