Comments (10)
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.
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.
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.
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.
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 justmode::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.
@kallemooo, would you be interested in implementing this? Or would you rather have someone else take this upon themselves? ;)
from avr-hal.
I need it for my temperature sensor project, so I will give it a try.
from avr-hal.
That would be awesome! If you need any help, let me know; either here or in the #embedded-rust
channel!
from avr-hal.
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.
I'm not good in terms. But the main thing is that such pin could only do next things:
- Read input when we need to read pin
- If we need to set it to LOW state -> switch mode to output and do -> set low thing
- 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)
- can't find crate for `core` HOT 12
- cargo build - error[E0658] - proc_macro::Literal::byte_character(byte) HOT 4
- Very weird error when compiling a very basic program: __addsf3 multiple defenitions HOT 9
- Ravedude freezes on programmer uploading HOT 5
- Arduino Nano: avrdude error: programmer is not responding HOT 11
- Watchdog intterupt mode HOT 1
- [Solved] Cannot find `pwm` in `embedded_hal` HOT 2
- peripherals type alias HOT 2
- Enhance `avr_hal_generic::renamed_pins! {}` to also generate type aliases for each pin
- tier 3 target and broken HOT 6
- Utilising the `atmega2560`'s "USART in SPI Mode" HOT 10
- Issue with math calculations with floats to serial port output HOT 6
- Incorrect ADC readings after toolchain bump HOT 15
- Weird issue with two u8 ranged loops HOT 14
- error: no matching package named `avr-hal` found HOT 7
- Serial writes aren't working on `rustc 1.82.0-nightly (8e86c9567 2024-08-01)` HOT 12
- Possible miscompilation in LLVM HOT 14
- Can't specify different upload and monitor serial ports. HOT 3
- Ravedude Console Timeout HOT 1
- Document new Ravedude.toml manifest HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from avr-hal.