News for hackers. Stuff that matters.
Re:Programming ARM μC in Rust at four different levels of ab... by classics2 on Thursday February 22, 2018 @03:40AM (#16436163)
> A GPIO pin starts out as an input; in that state, you can’t call the function set_high. It is a compile time error!

Which is throughly incorrect. Of course you can set the state of an output register that’s set to input, that state will just not appear on the pin.

Re:Programming ARM μC in Rust at four different levels of ab... by jabot on Thursday February 22, 2018 @03:49AM (#16436213)
The state of the pin is encoded in the type of the variable that is used to access the pin.

This kind of encoding prevents you from making the programming error of writing to an input pin - which is technically possible, just useless.

Re:Programming ARM μC in Rust at four different levels of ab... by emilfihlman on Thursday February 22, 2018 @04:15AM (#16436320)
Not even remotely useless, it's often a feature.
Re:Programming ARM μC in Rust at four different levels of ab... by jabot on Thursday February 22, 2018 @04:45AM (#16436443)
Well, _if_ you need that feature, you can wrap the trait into another trait that allows a write to the pin, but discards it.

However, by default, nonsensical operations should not be possible.

Re:Programming ARM μC in Rust at four different levels of ab... by leoedin on Thursday February 22, 2018 @05:54AM (#16436709)
AVR microcontrollers use the same register to define the pin value when it's set as an output, and define the state of the internal pullups when it's set as an input.

I haven't seen that in ARM microcontrollers, but as peripherals vary across manufacturers I don't think you can assume that it's not done this way.

Re:Programming ARM μC in Rust at four different levels of ab... by Doxin on Friday February 23, 2018 @06:05AM (#16445578)
Wouldn't you then write two sets of functions? set_high, set_low, enable_pullup, disable_pullup. Now internally set_high and enable_pullup might do the exact same thing, but externally they signal intent and prevent you from trying to set a pullup on an output for example.
Re:Programming ARM μC in Rust at four different levels of ab... by jsight on Thursday February 22, 2018 @05:04AM (#16436521)
Oh? When would you use this? I'm sure there must be some reason, but I can't think of one.
Re:Programming ARM μC in Rust at four different levels of ab... by bleke on Thursday February 22, 2018 @05:49AM (#16436688)
To prevent glitches it rarely problem but sometimes happens: imagine that default pin state must be high (on reset pin are floating) and moment you set pin mode to output it goes low, because data register is 0
Re:Programming ARM μC in Rust at four different levels of ab... by dbcurtis on Thursday February 22, 2018 @02:26PM (#16440584)
Consider a I/O pin set to open-drain drive. It either actively pulls the output pin to the ground rail, or the I/O floats. Externally, you have a pull-up resistor.

If there are multiple chips connected in open-drain, you write the I/O pin to 0 or 1 to either ground or float the pin. You read the I/O pin to see if anyone else has grounded it if you haven't. This was called "wired-OR" in the old days. (It's really AND, but was commonly used in DeMorgan equivalent form for communication buses before tri-state drivers became a thing. I'll get my cane and hobble back to my rocking chair now..)

Re:Programming ARM μC in Rust at four different levels of ab... by posterboy on Thursday February 22, 2018 @08:03PM (#16443252)
I think in the old days, before CMOS, this was called open-collector. I had learned about it in first semester digital logic not too long ago playing with the "high speed" 7400 series. If I remember correctly, the latch needs to support the open collector capability, but the reason escapes me.
Re:Programming ARM μC in Rust at four different levels of ab... by dbcurtis on Friday February 23, 2018 @03:06PM (#16449041)
Open collector if you have a bipolar technology. Modern microcontrollers are CMOS, so the pull-down transistor has a drain, not a collector. TTL is bipolar, so the transistor has a collector.
Re:Programming ARM μC in Rust at four different levels of ab... by InitialLastName on Thursday February 22, 2018 @11:19AM (#16438639)
As mentioned above, in some uC's the output register defines the pull direction when the pin is an input.

In other cases, you want to avoid glitching when the pin switches to an output on startup.

Re:Programming ARM μC in Rust at four different levels of ab... by arjo129 on Thursday February 22, 2018 @08:31AM (#16437383)
It can be used to implement capacitive touch.
Re:Programming ARM μC in Rust at four different levels of ab... by KMag on Thursday February 22, 2018 @04:02AM (#16436270)
> Which is throughly incorrect. Of course you can set the state of an output register that’s set to input, that state will just not appear on the pin.

The context is Rust, where the statement is clearly correct. One can't call a function if the Rust compiler refuses to generate the binary. The author probably should have used a period between the first and second independent clauses and a semicolon between the second and third in order to show the close relations ship between being unable to call the function and it being a compile error.

Re:Programming ARM μC in Rust at four different levels of ab... by foldr on Thursday February 22, 2018 @06:22AM (#16436825)
The OP means that this is an incorrect model of the microcontroller.
Re:Programming ARM μC in Rust at four different levels of ab... by KMag on Thursday February 22, 2018 @06:31PM (#16442616)
My point was that the OP's objection would be valid if the statement being objected to were about architectural limitations of the hardware.

However, the statement being objected to was under the section about the 3rd level of abstraction: "The Embedded HAL to the Rescue", which is detailing an extra semantic restriction imposed by Rust. The final two sentences of section 2 even allude to the prevented actions being legal but almost certainly being a logic error. To object that the hardware allows nonsensical actions without trapping is to miss the point of section 3 of the article. At the third level of abstraction: Rust prevents something legal but nonsensical.

Now, if this particular board doesn't clear output state when switching a GPIO pin from input to output, then it's a valid criticism that perhaps the developer wants to set the output state correctly before driving the pin and the prevented action is in fact semantically meaningful and perhaps the intent of the developer. However, that wasn't the OP's objection. The OP's objection was simply that Rust was preventing something that the underlying platform allows.

Re:Programming ARM μC in Rust at four different levels of ab... by foldr on Friday February 23, 2018 @06:08AM (#16445589)
No, I just mean that you misunderstood what the OP was calling "incorrect". They weren't saying that it's incorrect that a compile time error is triggered in the situation described.
Re:Programming ARM μC in Rust at four different levels of ab... by KMag on Friday February 23, 2018 @07:46PM (#16450907)
I understand that the OP was calling the model of the microcontroller incorrect.

To recap: The article said "Rust doesn't allow you to do X" The OP said "Wrong!!!! The mocrocontoller totally allows you to do X" I pointed out that the article never said the microcontroller doesn't allow X.

Re:Programming ARM μC in Rust at four different levels of ab... by bleke on Thursday February 22, 2018 @03:58AM (#16436255)
As for input it can be always read to check real pin state, for example in open collector where 'high' value is determined by pull-up resistor, because microcontroller just make pin floating
Re:Programming ARM μC in Rust at four different levels of ab... by mcguire on Thursday February 22, 2018 @02:41PM (#16440733)
... which is probably not what you wanted.

One would suspect that other functions in the HAL that control the feature you are looking for directly.