Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use hardware TWI for the PAT9125 (optical) filament sensor #2814

Merged
merged 15 commits into from
Feb 2, 2021

Conversation

wavexx
Copy link
Collaborator

@wavexx wavexx commented Aug 25, 2020

The PAT9125 on the MK3 is attached to the hardware SDA/SCL pins, but we're still using a software I2C implementation for some reason.

We add a new configuration option PAT9125_I2C (which is also enabled by default by this PR) to use the hardware TWI module in 400khz fast mode.

We save 170 bytes on the MK3S variant (which still requires probing to detect the current variant) and 268 on the MK3, plus a little bit of latency since despite the code still being synchronous, we can at least avoid having to wait for the stop condition.

PFW-1191

wavexx added 6 commits August 20, 2020 15:34
Remove probing from Marlin_main and move it into pat9125_probe so that
it can support the various variants.
- Only implement a single syncronous read/write function to read a byte,
  since that's all we need currently
- Implement a compact IR_SENSOR probe for PAT9125
- Saves 242 bytes compared to PAT9125_SWI2C
Both would be technically correct.
@vintagepc
Copy link
Contributor

vintagepc commented Sep 9, 2020

Three guesses what prompted this change 🤣
(and the first two don't count)

@wavexx
Copy link
Collaborator Author

wavexx commented Sep 9, 2020 via email

@vintagepc
Copy link
Contributor

vintagepc commented Sep 9, 2020

Did you end up testing it out by wiring up the hardware SPI sim? IIRC the SPIPeripheral class was written to support both bitbanged and "real" I2C connections and the fake PAT shouldn't care which it is wired to.

Edit: for anyone else wondering about the context for this, see
vintagepc/MK404#7 (comment)_

Firmware/twi.c Outdated Show resolved Hide resolved
@wavexx
Copy link
Collaborator Author

wavexx commented Sep 27, 2020

Did you end up testing it out by wiring up the hardware SPI sim? IIRC the SPIPeripheral class was written to support both bitbanged and "real" I2C connections and the fake PAT shouldn't care which it is wired to.

Took a while, but getting back to this.
You mean the I2CPeripheral here?

At least without changes, the sensor doesn't seem to be detected right now. The SCL/SDA irqs do not seem to be generated in this case so the callbacks are never called.

@vintagepc
Copy link
Contributor

Ah, ok. If you wouldn't mind tossing up a MK404 issue with a .hex and I'll take a look. It's possible that something is still unimplemented.

The wiring for the PAT9125 on RAMBo10a boards is not directly connected
to the SCL pin and requires the sw mode.

Detect this requirement by checking the definition for the SWI2C_SCL pin
in the board definition.

Remove SWI2C_SCL/SDA from the other boards to use the HW mode.
Firmware/pat9125.c Outdated Show resolved Hide resolved
- Enter a repeated-start for reading data
- Write in the same session
Copy link
Contributor

@vintagepc vintagepc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't have write access to the repo but I approve of this change regardless given its origins 😉

@DRracer
Copy link
Collaborator

DRracer commented Jan 26, 2021

@wavexx @vintagepc is this ready to get merged into MK3?

@vintagepc
Copy link
Contributor

I think it is, yes :) All was working as expected in MK404

@DRracer
Copy link
Collaborator

DRracer commented Jan 26, 2021

OK, my only complaint is the usage of "digitalWrite()" - can we get rid of this?

@vintagepc
Copy link
Contributor

This is what it's doing, seems like it should be possible to just strip out the unnecessary bits and just do the actions we need to set it high:

void digitalWrite(uint8_t pin, uint8_t val)
{
	uint8_t timer = digitalPinToTimer(pin);
	uint8_t bit = digitalPinToBitMask(pin);
	uint8_t port = digitalPinToPort(pin);
	volatile uint8_t *out;

	if (port == NOT_A_PIN) return;

	// If the pin that support PWM output, we need to turn it off
	// before doing a digital write.
	if (timer != NOT_ON_TIMER) turnOffPWM(timer);

	out = portOutputRegister(port);

	uint8_t oldSREG = SREG;
	cli();

	if (val == LOW) {
		*out &= ~bit;
	} else {
		*out |= bit;
	}

	SREG = oldSREG;
}

@leptun
Copy link
Collaborator

leptun commented Jan 26, 2021

We can use the fastio macros here, can't we? Just fix the SCL and SDA definition in fastio so that it's fastio compatible

@wavexx
Copy link
Collaborator Author

wavexx commented Jan 26, 2021

We can remove it, but is it necessary? digitalWrite is already used elsewhere, so we won't save any space AFAIK. It's also not on a hot code path.

@wavexx
Copy link
Collaborator Author

wavexx commented Jan 26, 2021

Checking now if using FASTIO can lead to some space savings..

@wavexx
Copy link
Collaborator Author

wavexx commented Jan 27, 2021

Using fastio would actually save 12 bytes :), however I need to add some crummy defines just to make it build: including fastio.h is not enough, which is something that would be nice to fix.

#2690 would make sense IMHO

If including #2690 makes sense in this release, I could proceed with something less invasive (another copy of CRITICAL_SECTION.. uugh) to be revisited later @DRracer

Move CRITICAL_SECTION_START/END into fastio.h, where it's needed.
fastio relies on macros for pin definitions, so we cannot use the const
declaration in Sd2PinMap or the arduino's definition.

Declare SDA/SCL_PIN into pins.h based on the current MCU, which is
identical in all our variants.

Remove the conflicting/unused declaration in Sd2PinMap.
@wavexx
Copy link
Collaborator Author

wavexx commented Jan 29, 2021

@DRracer now using fastio!

@DRracer DRracer merged commit 2b81abb into prusa3d:MK3 Feb 2, 2021
@wavexx wavexx deleted the MK3_PAT9125_I2C branch February 3, 2021 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants