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

Add fully type erased pin variant #317

Open
wants to merge 9 commits into
base: master
Choose a base branch
from

Conversation

jg2562
Copy link
Contributor

@jg2562 jg2562 commented Feb 14, 2022

Adds type erased pins which allow for generic pin usage over code.

Solves #315

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 14, 2022

I have not yet gone through all the data sheets to verify that the 0x400 offset mentioned in the issue.

@mlamoore
Copy link
Contributor

mlamoore commented Feb 14, 2022 via email

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 14, 2022

I should be able to check it in about a day with an actual microcontroller and digital analyzer for one of the variants. Thanks for the info! I will double check it for the proposed supported architectures in the other PR as well.

src/gpio.rs Outdated
/// This is useful when you want to collect the
/// pins into an array where you need all the
/// elements to have the same type
pub fn erase(&self) -> ErasedPin<MODE> {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please use 4 spaces for indentation, not tabs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed! Apologies, i thought cargo fmt would handle that.

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 14, 2022

Are there any thoughts about having the pins include the fmt trait? I left it out from the stm32f4 port because of a error formatting the pin mode. We could potentially ditch the mode in the fmt or use their stripped type name function.

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 15, 2022

@mlamoore I didn't realize that those 4 reference manuals that you checked covered all currently created microcontrollers of the stmh7 family, I just thought it just was the supported ones in this crate. I double-checked them as well, and it looks just like you said, so it should work just fine.

Also, I was able to test on hardware today, for the most part I was getting the expected behavior, but a few pins weren't working. I don't know if it relates to this change or something else, I will be checking in on that more tomorrow.

EDIT: I checked it out, it seemed unrelated to this change. However, it might be helpful if someone else could double check.

src/gpio.rs Outdated
let block_ptr = (crate::pac::GPIOA::ptr() as usize + offset)
as *const crate::pac::gpioa::RegisterBlock;

unsafe { &*block_ptr }
Copy link
Member

Choose a reason for hiding this comment

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

Can be simplified to just

unsafe { &*crate::pac::GPIOA::ptr().add(offset) }

I don't think the FIXME applies any more

@richardeoin
Copy link
Member

Looks good! To get this merged, I'd like to see:

  • A new example for this in the examples folder, to help new users discover it and to get some CI coverage
  • (More) successful testing on hardware. I can help with that :)

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 19, 2022

I have a testing program I used, where pretty much all the pins are individually written high then low in a loop and then by connecting them to pin A0 you get a report back on which pin you just probed. It's kinda of cumbersome, but it is helpful for debugging and an example of using erased pins in an array. This example does use the Debug trait I mentioned earlier, though. Otherwise, I could come up with a small example of a few differnet pins from different ports in the same array if you'd like.

Also thanks! I'm definitely having some odd behaviors on a few select pins, but I think that this is not related to this PR. So it'd be very helpful to have a second opinion.

@mlamoore
Copy link
Contributor

mlamoore commented Feb 19, 2022 via email

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 19, 2022

Yeah, leaving out the mode would probably be easiest for now. I was planning on formatting it with the port as the ASCII representation of the port. so A0 would just become 'PA0'.

In terms of the example, I'll have to learn a bit about the logging, but that does sound like an appropriate idea for the example

@jg2562
Copy link
Contributor Author

jg2562 commented Feb 25, 2022

I finally got around to adding the example, let me know what you guys think!

Copy link
Member

@richardeoin richardeoin left a comment

Choose a reason for hiding this comment

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

Thanks, looking good!

  • It might be a bad idea to run the example on a development board, because the development board hardware drives many of the pins. For some pins, it would cause two outputs to drive each other. To avoid this the example could just make all the pins inputs and print the current state? (high or low)
  • Could you run rustfmt to make the formatting consistent with the rest of the project? You can follow the Quick Start section on the rustfmt page https://github.com/rust-lang/rustfmt

examples/pin_probe.rs Show resolved Hide resolved
examples/pin_probe.rs Outdated Show resolved Hide resolved
src/gpio.rs Outdated Show resolved Hide resolved
@jg2562
Copy link
Contributor Author

jg2562 commented Feb 27, 2022

Thanks, looking good!

* It might be a bad idea to run the example on a development board, because the development board hardware drives many of the pins. For some pins, it would cause two outputs to drive each other. To avoid this the example could just make all the pins inputs and print the current state? (high or low)

* Could you run rustfmt to make the formatting consistent with the rest of the project? You can follow the Quick Start section on the rustfmt page https://github.com/rust-lang/rustfmt

Sorry about the formatting, still getting used to running formatting and clippy after changes.

That is a good point, it would not reveal if a single output is pinned out multiple times. The reason I avoided having most pins as inputs is when probing you might touch the probe to ground and I wanted to avoid powering down the entire dev kit at the time. My initial use case for this was testing the documented pinouts of a dev kit that seemed to not actually match the real pinouts. So I was simply moving the A0 probe down the header and verifying that the reports given match the documentation. Interestingly as a sidenote, I did find a few outputs were not connected to the pins they claimed. That all being said, if you think we should change the use case of the script a bit to be more beginner-friendly/useful, I would be happy to update it!

Also let me know if the documentation should be improved, wasn't sure if I captured your guy's documentation standards.

@richardeoin
Copy link
Member

The explaination at the top of the example looks good, thanks.

I understand the use case, but I was a little concerned that on some (more expensive) development boards there are devices directly connected to the H7 like Ethernet PHYs, SD cards, CAN transceivers and so on. Those devices have outputs, if a beginner was to run this example they might drive the output of those devices with an H7 output, which is bad. Experienced developers should know better, but it might save someone a lot of pain if the example mostly used inputs instead.

The example does say "experimenting" at the top though, if you don't have time to change it then we can also just include it like this :)

@jg2562
Copy link
Contributor Author

jg2562 commented Mar 4, 2022

Yeh i could probably redo it, but what would you imagine the output looks like at that point?

@richardeoin
Copy link
Member

I think the output would be a list of pins and their input value (high/low)

@jg2562
Copy link
Contributor Author

jg2562 commented Mar 15, 2022

I just updated the example, but I haven't been able to run it due to my dev kit. It should now just read in all the inputs and print them to the rtt channel.

@richardeoin
Copy link
Member

I've now merged #334, which adds type erased pins as well as many other things(!) That's good, but means looking at this PR again and seeing if any parts of it should still be included.

@jg2562
Copy link
Contributor Author

jg2562 commented Mar 21, 2022

Oh awesome! I was very excited for #334 and figured it would eliminate the need for the majority of this PR. I would say that the only thing that didn't overlap was the example, if you would still like to keep that around. Lastly, I would like to double-check #334's debug formatting to make sure it outputs what we expect as I remember that being very f4 specific.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants