Skip to content

Commit

Permalink
Cache drive select and motor on moving it out of Greaseweazle
Browse files Browse the repository at this point in the history
  • Loading branch information
mrenters committed Feb 1, 2025
1 parent 501f47e commit 0b6a316
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 8 deletions.
10 changes: 2 additions & 8 deletions examples/greaseweazle/greaseweazle.ino
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,6 @@ uint32_t transfered_bytes;
uint32_t captured_pulses;
// WARNING! there are 100K max flux pulses per track!
uint8_t flux_transitions[MAX_FLUX_PULSE_PER_TRACK];
bool motor_state = false; // we can cache whether the motor is spinning
bool flux_status; // result of last flux read or write command


Expand All @@ -217,7 +216,7 @@ void loop() {
if (!cmd_len) {
if ((millis() > timestamp) && ((millis() - timestamp) > 10000)) {
Serial1.println("Timed out waiting for command, resetting motor");
if (floppy) {
if (floppy && floppy->drive_is_selected()) {
Serial1.println("goto track 0");
floppy->goto_track(0);
Serial1.println("stop motor");
Expand All @@ -226,7 +225,6 @@ void loop() {
floppy->select(false);
}
Serial1.println("motor reset");
motor_state = false;
timestamp = millis();
}
return;
Expand Down Expand Up @@ -328,7 +326,6 @@ void loop() {
} else {
reply_buffer[i++] = GW_ACK_BADCMD;
}
motor_state = false;
Serial.write(reply_buffer, 2);
}

Expand Down Expand Up @@ -366,10 +363,7 @@ void loop() {
uint8_t unit = cmd_buffer[2];
uint8_t state = cmd_buffer[3];
Serial1.printf("Turn motor %d %s\n\r", unit, state ? "on" : "off");
if (motor_state != state) { // we're in the opposite state
floppy->spin_motor(state);
motor_state = state;
}
floppy->spin_motor(state);
reply_buffer[i++] = GW_ACK_OK;
Serial.write(reply_buffer, 2);
}
Expand Down
19 changes: 19 additions & 0 deletions src/Adafruit_Floppy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ void Adafruit_FloppyBase::soft_reset(void) {
watchdog_delay_ms = 1000;
bus_type = BUSTYPE_IBMPC;

is_drive_selected = false;
is_motor_spinning = false;

if (led_pin >= 0) {
pinMode(led_pin, OUTPUT);
digitalWrite(led_pin, LOW);
Expand All @@ -168,6 +171,8 @@ void Adafruit_FloppyBase::end(void) {
if (_indexpin >= 0) {
pinMode(_indexpin, INPUT);
}
is_drive_selected = false;
is_motor_spinning = false;
}

/**************************************************************************/
Expand Down Expand Up @@ -251,6 +256,7 @@ void Adafruit_Floppy::end(void) {
/**************************************************************************/
void Adafruit_Floppy::select(bool selected) {
digitalWrite(_selectpin, !selected); // Selected logic level 0!
is_drive_selected = selected;
// Select drive
delayMicroseconds(select_delay_us);
}
Expand Down Expand Up @@ -284,7 +290,11 @@ bool Adafruit_Floppy::side(int head) {
*/
/**************************************************************************/
bool Adafruit_Floppy::spin_motor(bool motor_on) {
if (motor_on == is_motor_spinning)
return true; // Already in the correct state

digitalWrite(_motorpin, !motor_on); // Motor on is logic level 0!
is_motor_spinning = motor_on;
if (!motor_on)
return true; // we're done, easy!

Expand Down Expand Up @@ -1018,6 +1028,13 @@ void Adafruit_Apple2Floppy::soft_reset() {
/**************************************************************************/
void Adafruit_Apple2Floppy::select(bool selected) {
digitalWrite(_selectpin, !selected);
is_drive_selected = selected;
// Selecting the drive also turns the motor on, but we need to look
// for index pulses, so leave that job to spin_motor. Deselecting the
// drive will turn it off though.
if (!selected)
is_motor_spinning = false;

if (debug_serial)
debug_serial->printf("set selectpin %d to %d\n", _selectpin, !selected);
}
Expand All @@ -1033,6 +1050,7 @@ void Adafruit_Apple2Floppy::select(bool selected) {
*/
/**************************************************************************/
bool Adafruit_Apple2Floppy::spin_motor(bool motor_on) {
if (motor_on == is_motor_spinning) return true; // already in correct state
if (motor_on) {
delay(motor_delay_ms); // Main motor turn on

Expand Down Expand Up @@ -1064,6 +1082,7 @@ bool Adafruit_Apple2Floppy::spin_motor(bool motor_on) {
if (debug_serial)
debug_serial->println("Found!");
}
is_motor_spinning = motor_on;
return true;
}

Expand Down
16 changes: 16 additions & 0 deletions src/Adafruit_Floppy.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ class Adafruit_FloppyBase {
*/
/**************************************************************************/
virtual void select(bool selected) = 0;
/**************************************************************************/
/*!
@brief Is the drive selected based on interal caching
@returns True if the drive is selected, false otherwise
*/
/**************************************************************************/
bool drive_is_selected(void) { return is_drive_selected; }

/**************************************************************************/
/*!
@brief Turn on or off the floppy motor, if on we wait till we get an
Expand All @@ -84,6 +92,13 @@ class Adafruit_FloppyBase {
/**************************************************************************/
virtual bool spin_motor(bool motor_on) = 0;
/**************************************************************************/
/*!
@brief Is the drive motor spinning based on interal caching
@returns True if the motor is spinning, false otherwise
*/
/**************************************************************************/
bool motor_is_spinning(void) { return is_motor_spinning; }
/**************************************************************************/
/*!
@brief Seek to the desired track, requires the motor to be spun up!
@param track_num The track to step to
Expand Down Expand Up @@ -193,6 +208,7 @@ class Adafruit_FloppyBase {

protected:
bool read_index();
bool is_drive_selected, is_motor_spinning;

private:
#if defined(__SAMD51__)
Expand Down

0 comments on commit 0b6a316

Please sign in to comment.