Merge pull request #53 from mrenters/cache_state
Cache drive select and motor on moving it out of Greaseweazle
This commit is contained in:
commit
7c489bf56b
3 changed files with 100 additions and 43 deletions
|
|
@ -208,7 +208,6 @@ uint32_t transfered_bytes;
|
||||||
uint32_t captured_pulses;
|
uint32_t captured_pulses;
|
||||||
// WARNING! there are 100K max flux pulses per track!
|
// WARNING! there are 100K max flux pulses per track!
|
||||||
uint8_t flux_transitions[MAX_FLUX_PULSE_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
|
bool flux_status; // result of last flux read or write command
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -217,16 +216,18 @@ void loop() {
|
||||||
if (!cmd_len) {
|
if (!cmd_len) {
|
||||||
if ((millis() > timestamp) && ((millis() - timestamp) > 10000)) {
|
if ((millis() > timestamp) && ((millis() - timestamp) > 10000)) {
|
||||||
Serial1.println("Timed out waiting for command, resetting motor");
|
Serial1.println("Timed out waiting for command, resetting motor");
|
||||||
if (floppy) {
|
if (floppy && floppy->motor_is_spinning()) {
|
||||||
|
floppy->select(true); // Need to select before we can move
|
||||||
Serial1.println("goto track 0");
|
Serial1.println("goto track 0");
|
||||||
floppy->goto_track(0);
|
floppy->goto_track(0);
|
||||||
Serial1.println("stop motor");
|
Serial1.println("stop motor");
|
||||||
floppy->spin_motor(false);
|
floppy->spin_motor(false);
|
||||||
|
}
|
||||||
|
if (floppy && floppy->drive_is_selected()) {
|
||||||
Serial1.println("deselect");
|
Serial1.println("deselect");
|
||||||
floppy->select(false);
|
floppy->select(false);
|
||||||
}
|
}
|
||||||
Serial1.println("motor reset");
|
Serial1.println("motor reset");
|
||||||
motor_state = false;
|
|
||||||
timestamp = millis();
|
timestamp = millis();
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
|
|
@ -328,7 +329,6 @@ void loop() {
|
||||||
} else {
|
} else {
|
||||||
reply_buffer[i++] = GW_ACK_BADCMD;
|
reply_buffer[i++] = GW_ACK_BADCMD;
|
||||||
}
|
}
|
||||||
motor_state = false;
|
|
||||||
Serial.write(reply_buffer, 2);
|
Serial.write(reply_buffer, 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -366,10 +366,7 @@ void loop() {
|
||||||
uint8_t unit = cmd_buffer[2];
|
uint8_t unit = cmd_buffer[2];
|
||||||
uint8_t state = cmd_buffer[3];
|
uint8_t state = cmd_buffer[3];
|
||||||
Serial1.printf("Turn motor %d %s\n\r", unit, state ? "on" : "off");
|
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);
|
floppy->spin_motor(state);
|
||||||
motor_state = state;
|
|
||||||
}
|
|
||||||
reply_buffer[i++] = GW_ACK_OK;
|
reply_buffer[i++] = GW_ACK_OK;
|
||||||
Serial.write(reply_buffer, 2);
|
Serial.write(reply_buffer, 2);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -145,6 +145,10 @@ void Adafruit_FloppyBase::soft_reset(void) {
|
||||||
watchdog_delay_ms = 1000;
|
watchdog_delay_ms = 1000;
|
||||||
bus_type = BUSTYPE_IBMPC;
|
bus_type = BUSTYPE_IBMPC;
|
||||||
|
|
||||||
|
is_drive_selected = false;
|
||||||
|
is_motor_spinning = false;
|
||||||
|
is_index_seen = false;
|
||||||
|
|
||||||
if (led_pin >= 0) {
|
if (led_pin >= 0) {
|
||||||
pinMode(led_pin, OUTPUT);
|
pinMode(led_pin, OUTPUT);
|
||||||
digitalWrite(led_pin, LOW);
|
digitalWrite(led_pin, LOW);
|
||||||
|
|
@ -168,6 +172,9 @@ void Adafruit_FloppyBase::end(void) {
|
||||||
if (_indexpin >= 0) {
|
if (_indexpin >= 0) {
|
||||||
pinMode(_indexpin, INPUT);
|
pinMode(_indexpin, INPUT);
|
||||||
}
|
}
|
||||||
|
is_drive_selected = false;
|
||||||
|
is_motor_spinning = false;
|
||||||
|
is_index_seen = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
|
@ -251,6 +258,7 @@ void Adafruit_Floppy::end(void) {
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
void Adafruit_Floppy::select(bool selected) {
|
void Adafruit_Floppy::select(bool selected) {
|
||||||
digitalWrite(_selectpin, !selected); // Selected logic level 0!
|
digitalWrite(_selectpin, !selected); // Selected logic level 0!
|
||||||
|
is_drive_selected = selected;
|
||||||
// Select drive
|
// Select drive
|
||||||
delayMicroseconds(select_delay_us);
|
delayMicroseconds(select_delay_us);
|
||||||
}
|
}
|
||||||
|
|
@ -284,9 +292,18 @@ bool Adafruit_Floppy::side(int head) {
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
bool Adafruit_Floppy::spin_motor(bool motor_on) {
|
bool Adafruit_Floppy::spin_motor(bool motor_on) {
|
||||||
|
if (motor_on != is_motor_spinning) {
|
||||||
digitalWrite(_motorpin, !motor_on); // Motor on is logic level 0!
|
digitalWrite(_motorpin, !motor_on); // Motor on is logic level 0!
|
||||||
if (!motor_on)
|
is_motor_spinning = motor_on;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!motor_on) {
|
||||||
|
is_index_seen = false;
|
||||||
return true; // we're done, easy!
|
return true; // we're done, easy!
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_index_seen)
|
||||||
|
return true; // motor on and already have seen index pulses
|
||||||
|
|
||||||
delay(motor_delay_ms); // Main motor turn on
|
delay(motor_delay_ms); // Main motor turn on
|
||||||
|
|
||||||
|
|
@ -304,14 +321,16 @@ bool Adafruit_Floppy::spin_motor(bool motor_on) {
|
||||||
yield();
|
yield();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_index_seen = !timedout;
|
||||||
|
|
||||||
|
if (debug_serial) {
|
||||||
if (timedout) {
|
if (timedout) {
|
||||||
if (debug_serial)
|
|
||||||
debug_serial->println("Didn't find an index pulse!");
|
debug_serial->println("Didn't find an index pulse!");
|
||||||
return false;
|
} else {
|
||||||
}
|
|
||||||
if (debug_serial)
|
|
||||||
debug_serial->println("Found!");
|
debug_serial->println("Found!");
|
||||||
return true;
|
}
|
||||||
|
}
|
||||||
|
return is_index_seen;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
|
|
@ -1017,7 +1036,17 @@ void Adafruit_Apple2Floppy::soft_reset() {
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
void Adafruit_Apple2Floppy::select(bool selected) {
|
void Adafruit_Apple2Floppy::select(bool selected) {
|
||||||
|
if (selected == is_drive_selected)
|
||||||
|
return; // Already in correct state
|
||||||
|
|
||||||
digitalWrite(_selectpin, !selected);
|
digitalWrite(_selectpin, !selected);
|
||||||
|
is_drive_selected = selected;
|
||||||
|
is_motor_spinning = selected;
|
||||||
|
|
||||||
|
// Selecting the drive also turns the motor on, but we need to look
|
||||||
|
// for index pulses, so leave that job to spin_motor.
|
||||||
|
is_index_seen = false;
|
||||||
|
|
||||||
if (debug_serial)
|
if (debug_serial)
|
||||||
debug_serial->printf("set selectpin %d to %d\n", _selectpin, !selected);
|
debug_serial->printf("set selectpin %d to %d\n", _selectpin, !selected);
|
||||||
}
|
}
|
||||||
|
|
@ -1033,7 +1062,12 @@ void Adafruit_Apple2Floppy::select(bool selected) {
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
bool Adafruit_Apple2Floppy::spin_motor(bool motor_on) {
|
bool Adafruit_Apple2Floppy::spin_motor(bool motor_on) {
|
||||||
if (motor_on) {
|
if (!motor_on)
|
||||||
|
return true; // Nothing to do
|
||||||
|
|
||||||
|
if (motor_on && is_index_seen)
|
||||||
|
return true; // motor on and already have index pulses
|
||||||
|
|
||||||
delay(motor_delay_ms); // Main motor turn on
|
delay(motor_delay_ms); // Main motor turn on
|
||||||
|
|
||||||
uint32_t index_stamp = millis();
|
uint32_t index_stamp = millis();
|
||||||
|
|
@ -1056,15 +1090,16 @@ bool Adafruit_Apple2Floppy::spin_motor(bool motor_on) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
is_index_seen = !timedout;
|
||||||
|
|
||||||
|
if (debug_serial) {
|
||||||
if (timedout) {
|
if (timedout) {
|
||||||
if (debug_serial)
|
|
||||||
debug_serial->println("Didn't find an index pulse!");
|
debug_serial->println("Didn't find an index pulse!");
|
||||||
return false;
|
} else {
|
||||||
}
|
|
||||||
if (debug_serial)
|
|
||||||
debug_serial->println("Found!");
|
debug_serial->println("Found!");
|
||||||
}
|
}
|
||||||
return true;
|
}
|
||||||
|
return is_index_seen;
|
||||||
}
|
}
|
||||||
|
|
||||||
// stepping FORWARD through phases steps OUT towards SMALLER track numbers
|
// stepping FORWARD through phases steps OUT towards SMALLER track numbers
|
||||||
|
|
|
||||||
|
|
@ -75,6 +75,14 @@ public:
|
||||||
*/
|
*/
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
virtual void select(bool selected) = 0;
|
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
|
@brief Turn on or off the floppy motor, if on we wait till we get an
|
||||||
|
|
@ -86,6 +94,20 @@ public:
|
||||||
/**************************************************************************/
|
/**************************************************************************/
|
||||||
virtual bool spin_motor(bool motor_on) = 0;
|
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 Are index pulses being seen?
|
||||||
|
@returns True if we're seeing index pulses, false otherwise
|
||||||
|
*/
|
||||||
|
/**************************************************************************/
|
||||||
|
bool index_pulses_seen(void) { return is_index_seen; }
|
||||||
|
/**************************************************************************/
|
||||||
/*!
|
/*!
|
||||||
@brief Seek to the desired track, requires the motor to be spun up!
|
@brief Seek to the desired track, requires the motor to be spun up!
|
||||||
@param track_num The track to step to
|
@param track_num The track to step to
|
||||||
|
|
@ -195,6 +217,9 @@ public:
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
bool read_index();
|
bool read_index();
|
||||||
|
bool is_drive_selected; ///< cached drive select state
|
||||||
|
bool is_motor_spinning; ///< cached motor spinning state
|
||||||
|
bool is_index_seen; ///< cached index pulses seen state
|
||||||
|
|
||||||
private:
|
private:
|
||||||
#if defined(__SAMD51__)
|
#if defined(__SAMD51__)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue