Closed bentwire closed 1 year ago
Interesting, that panic from fusb302b/src/lib.rs:233:13
occurs when the driver is in an unexpected state. I have a refactor to a type state based implementation that would prevent this at compile time, in the mean time I'm not really sure how to help as looking over the code everything seems to be implemented correctly.
Is there any difference if you flash the device with the cable detached, then attach it? I've seen odd behaviour like that before.
The board isn't wired to allow that unfortunately. Next revision I guess! I did end up adding a "Vendor" command to the list, my laptop advertises DP using a vendor command and it was causing a crash...
I can try out your new impl if you have it somewhere I can get to it. Is it a branch? I did fork your repo to make some small mods to work with latest RP PICO HAL's and RTIC. So playing with patches won't be any issues.
If I can help in any way to get this library working with other MCUs I would love to help! Esp on the RP2040 board I designed for playing with USB PD. :)
@bentwire Oh sorry, I meant the USB-PD device, as in flashing with the USB-C cable detached, then re-attaching?
Just some local changes I'm afraid, but in a totally broken state so nothing worth pushing. The first step is moving timeout
and events
from the Fusb302b
struct into the State
enum, this should reduce quite a few bugs as there would be no shared data between different driver states, and each one starts with a known value. The next step would be a pretty standard type state implementation for each variant of State
.
Absolutely that sounds awesome, would you like to be added as a maintainer on this repo so you can upstream those changes? It'd definitely be great to make sure this library works across different hardware.
My main focus going forward will be to add source support for the FUSB302B, but there are some more sink features that need to be implemented. I'm also not set on the API, if you have any suggestions for changes to the Driver
trait or anything let me know.
@fmckeogh
I'm still pretty new to Rust, I get the basics but macros and stuff still evade me. If you want to add me as a maintainer that is fine, I would be sending you reviews anyway so I think that's ok?
The biggest change I think that would be helpful is to move the callback in to the library so it isn't necessary. It does not seem very 'rusty' to have a callback like that. I was thinking I could call in to the driver to see what state its in, and to ask it to request a new PDO and to save the list of available PDO's for later query and selection.
Does that make sense?
@bentwire
Sounds good, just ping me and I'll review any PRs.
At the time my rationale was that the callback means we can always respond quickly to power events, we're not reliant on users doing it fast enough to avoid a timeout. The other issue is being able to lookup the PDO index internally, giving a nicer/safer API for users. However, I think you're right and that making poll
return an Option<Event>
, and then adding a method for requesting power seems like a reasonable alternative?
I'd like to avoid saving the list of PDOs in the library for space reasons, I'd rather let users choose to save them if they wish, but keeping the minimum size of the library down.
@fmckeogh
That sounds like a perfect compromise to me. Allow for quick response without a callback, and also allow the user to manage the PDO list (Esp since it won't always just be power it seems...)
I can try and work on that if you wish, should have some time this weekend.
Otherwise I will try and put together a diff with just my changes for adding the vendor message and submit a PR.
@bentwire
I should have some time one evening this week, so I'm happy to work on this if you want to work on that PR:)
Any luck with the panic?
I think so? I modified the code slightly to make sure it was in the right state before calling start sync.
It does not feel quite right to me though:
fn establish_usb_20(&mut self) {
self.state = State::Measuring { cc_pin: CcPin::CC1 };
self.events.enqueue(DriverEvent::StateChanged).ok().unwrap();
//while self.events.dequeue().is_some() {}
self.start_sink();
}
Ok cool, let me get that PR put together.
Oh here is some happy news:
warning: `usb-c-fusb302-pi` (bin "usb-c-fusb302-pi") generated 7 warnings (run `cargo fix --bin "usb-c-fusb302-pi"` to apply 6 suggestions)
Finished release [optimized + debuginfo] target(s) in 0.04s
Running `probe-run --chip RP2040 target/thumbv6m-none-eabi/release/usb-c-fusb302-pi`
(HOST) INFO flashing program (6 pages / 24.00 KiB)
(HOST) INFO success!
────────────────────────────────────────────────────────────────────────────────
INFO INIT!
└─ usb_c_fusb302_pi::app::init::{closure#0} @ src/main.rs:222
INFO INIT COMPLETE!
└─ usb_c_fusb302_pi::app::init::{closure#0} @ src/main.rs:253
DEBUG CRC ok
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:291
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Invalid CRC
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:310
DEBUG Good CRC sent
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:295
DEBUG USB PD comm
└─ fusb302b::{impl#2}::establish_usb_pd @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:409
INFO CALLBACK
└─ usb_c_fusb302_pi::callback @ src/main.rs:312
INFO protocol changed
└─ usb_c_fusb302_pi::callback::{closure#0} @ src/main.rs:345
DEBUG CRC ok
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:291
INFO CALLBACK
└─ usb_c_fusb302_pi::callback @ src/main.rs:312
INFO Capabilities changed: 1
└─ usb_c_fusb302_pi::callback @ src/main.rs:315
DEBUG supply @ 0: 5000mV 3000mA
└─ usb_c_fusb302_pi::callback::{closure#0} @ src/main.rs:323
INFO requesting supply FixedSupply(637636908)@0
└─ usb_c_fusb302_pi::callback::{closure#0} @ src/main.rs:337
DEBUG Good CRC packet
└─ fusb302b::{impl#2}::check_for_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:314
INFO CALLBACK
└─ usb_c_fusb302_pi::callback @ src/main.rs:312
INFO power accepted
└─ usb_c_fusb302_pi::callback::{closure#0} @ src/main.rs:346
DEBUG TX ack
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:280
DEBUG CRC ok
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:291
DEBUG Good CRC sent
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:295
DEBUG CRC ok
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:291
DEBUG Good CRC sent
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:295
INFO CALLBACK
└─ usb_c_fusb302_pi::callback @ src/main.rs:312
INFO power ready
└─ usb_c_fusb302_pi::callback::{closure#0} @ src/main.rs:344
DEBUG VENDOR: 64, 1, [1, 128, 0, 255, 214, 196, 43, 238, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
└─ usb_pd::message::{impl#0}::parse @ /home/mike/devel/rust/embedded/usb-pd-rs/usb-pd/src/message.rs:62
DEBUG Vendor Defined Message! [1, 80, 0, ff, d6, c4, 2b, ee]
└─ usb_pd::sink::{impl#0}::handle_msg @ /home/mike/devel/rust/embedded/usb-pd-rs/usb-pd/src/sink.rs:138
DEBUG CRC ok
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:291
DEBUG Good CRC sent
└─ fusb302b::{impl#2}::check_for_interrupts @ /home/mike/devel/rust/embedded/usb-pd-rs/fusb302b/src/lib.rs:295
@fmckeogh Have you been able to find any of the vendor defined message (VDO I think) formats? I want to fix the vendor message so the length is more correct but I can't find any format anywhere...
According to the number of object field in the header there should be 1 object, but it can't be 32bits long because there are obviously at least 8 valid bytes in the packet...
Right now that's the only thing preventing me from putting together a PR with my changes.
@fmckeogh Finally found something. Created a PR for you to review!
Thanks!
@fmckeogh Is it OK if I close this for now?
@bentwire Sure, I just pushed a commit replacing the callback API, let me know what you think:)
@fmckeogh Looks good! Will port to it tonight!
@fmckeogh Working great!
I'm kinda new to rust on embedded, at least with a runtime like RTIC, so I could be completely messed up... Please be gentle :)
Here is the code: https://github.com/bentwire/usb-c-fusb302-pi-rs
Here is the panic: