Git Product home page Git Product logo

Comments (10)

Rahix avatar Rahix commented on July 19, 2024

I think it should be possible to add an additional mode where the pin type does exactly what you describe. Though I'm not sure what the semantics should be when e.g. setting the pin low and then reading. This would 'toggle' the state implicitly and this could be confusing ...

from avr-hal.

jonahbron avatar jonahbron commented on July 19, 2024

What about another pin struct that implements both traits, that can internally switch itself? Something roughly like this (likely full of mistakes)

enum InputOrOutputPin {
    Output(OutputPin),
    Input(InputPin),
}
struct InputOutputPin {
    pin: InputOrOutputPin
}

impl InputOutputPin {
    def get_output(&mut self) -> OutputPin {
        match self.pin {
            Output(output_pin) => {
                output_pin
            },
            Input(input_pin) => {
                self.pin = Output(input_pin.release().into_output())
            },
        }
    }
    def get_input(&mut self) -> InputPin {
        match self.pin {
            Input(input_pin) => {
                input_pin
            },
            Output(output_pin) => {
                self.pin = Input(output.release().into_input())
            },
        }
    }
}

impl OutputPin for InputOutputPin {
    def set_high(&mut self) -> () {
        self.get_output().set_high()
    }
    def set_low(&mut self) -> () {
        self.get_output().set_low()
    }
}
impl InputPin for InputOutputPin {
    def is_high(&mut self) -> () {
        self.get_input().is_high()
    }
    def is_low(&mut self) -> () {
        self.get_input().is_low()
    }
}

from avr-hal.

kallemooo avatar kallemooo commented on July 19, 2024

The proposed InputOutputPin will not work with the one-wire-bus library as it expects the set_high() to release the bus.
The InputOutputPin approach will force the the pin to high state (PushPull).
In open drain mode you shall never force the pin high (using push-pull) as other devices on the bus can set the bus low at the same time, and that will cause a high current rush, and possible break the devices.

Emulating an open drain pin on the AVR in a glitch safe mode needs to switch between input and output
mode as part of the emulation. To do that I see the following approach.

  • Init state: Input floating. (DDRxn = 0, PORTxn = 0)
  • set_low(): Switch to output low mode (DDRxn = 1). This will force the pin low.
  • set_high(): Switch to input floating mode. (DDRxn = 0). The external pull-up will set the pin high.
  • is_high() and is_low(): Read the PINxn register. Do not switch mode.

I would suggest to follow the STM32-hal approach with modes for output Output<OpenDrain> and Output<PushPull>.
But that will break the current API for Output pin, but I do not know if it is a problem.

Note:

Do not use the internal pull-up when emulating open drain mode.
As you cannot switch from input pull-up to output low in one operation.
It always needs two writes. And that can cause glitches on the bus, or high current rush depending on the approach.

from avr-hal.

Rahix avatar Rahix commented on July 19, 2024

Emulating an open drain pin

Is it still 'emulating' at this point? If I understand correctly, this is electrically what an open-drain pin does, no?

I would suggest to follow the STM32-hal approach with modes for output Output and Output.

I would add a mode::OpenDrainIO (or just mode::OpenDrain, not sure what makes more sense) where pins implement both input and output traits (as you described, using DDR for switching the state).

from avr-hal.

kallemooo avatar kallemooo commented on July 19, 2024

Emulating an open drain pin

Is it still 'emulating' at this point? If I understand correctly, this is electrically what an open-drain pin does, no?

It is electrically what an open-drain pin does, but as the HW do not directly support the mode it is sort of a SW emulation.
But on electrical level is not an emulation.

I would suggest to follow the STM32-hal approach with modes for output Output and Output.

I would add a mode::OpenDrainIO (or just mode::OpenDrain, not sure what makes more sense) where pins implement both input and output traits (as you described, using DDR for switching the state).

I looked at the code and I think a new mode mode::OpenDrain is the best approach.

from avr-hal.

Rahix avatar Rahix commented on July 19, 2024

@kallemooo, would you be interested in implementing this? Or would you rather have someone else take this upon themselves? ;)

from avr-hal.

kallemooo avatar kallemooo commented on July 19, 2024

I need it for my temperature sensor project, so I will give it a try.

from avr-hal.

Rahix avatar Rahix commented on July 19, 2024

That would be awesome! If you need any help, let me know; either here or in the #embedded-rust channel!

from avr-hal.

couchand avatar couchand commented on July 19, 2024

A brief note on the terminology: AVRs all have push-pull output stages, so the term "open drain" is strictly incorrect. I think tri-state output would be the usual term in this case.

from avr-hal.

rumatoest avatar rumatoest commented on July 19, 2024

I'm not good in terms. But the main thing is that such pin could only do next things:

  1. Read input when we need to read pin
  2. If we need to set it to LOW state -> switch mode to output and do -> set low thing
  3. If we need to set "HIGHT" state -> just switch to input mode (considering that there is already pull up resistor in circuit)

Just look into DHT drivers from Adafruit - they do similar stuff in C++ code.

from avr-hal.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.