-
Notifications
You must be signed in to change notification settings - Fork 267
fix USB-PD implementation #936
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
Merged
+15
−1
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -553,7 +553,7 @@ typedef struct | |
| USBPD_CC_e lastCCLine; | ||
| USBPD_SPR_CapabilitiesMessage_t caps; | ||
| uint8_t messageID; | ||
| uint8_t pdoCount; | ||
| volatile uint8_t pdoCount; | ||
| bool gotSourceGoodCRC; | ||
| } USBPD_Instance_t; | ||
|
|
||
|
|
@@ -641,6 +641,11 @@ USBPD_Result_e USBPD_SinkNegotiate( void ) | |
| break; | ||
|
|
||
| case eSTATE_SOURCE_CAP: | ||
| // Wait for the GoodCRC TX (started inside ParsePacket) to finish before | ||
| // sending the Request. Without this, SelectPDO's SendMessage races with | ||
| // the in-progress GoodCRC transmission (~450 µs at 300 kHz BMC), causing | ||
| // strict chargers to reject or ignore the request. | ||
| while ( USBPD->CONTROL & PD_TX_EN ); | ||
| USBPD_SelectPDO( 0, 0 ); // Select the first PDO by default | ||
| s_instance.state = eSTATE_WAIT_ACCEPT; | ||
| break; | ||
|
|
@@ -770,6 +775,12 @@ USBPD_Result_e USBPD_SelectPDO( uint8_t index, uint32_t voltageIn100mV ) | |
| */ | ||
| size_t USBPD_GetCapabilities( USBPD_SPR_CapabilitiesMessage_t **capabilities ) | ||
| { | ||
| // Barrier: caps is written by the IRQ via memcpy; without this, LTO can | ||
| // prove the main-thread call chain never writes caps and fold all reads to | ||
| // zero. The barrier is placed here so every caller gets correct data | ||
| // regardless of which state the negotiation exited through. | ||
| __asm volatile( "" ::: "memory" ); | ||
|
|
||
| if ( s_instance.pdoCount == 0 ) | ||
| { | ||
| return 0; | ||
|
|
@@ -872,6 +883,8 @@ static void ParsePacket( void ) | |
|
|
||
| case eUSBPD_CTRL_MSG_PS_RDY: nextState = eSTATE_PS_RDY; break; | ||
|
|
||
| case eUSBPD_CTRL_MSG_WAIT: nextState = eSTATE_CABLE_DETECT; break; | ||
|
|
||
|
Comment on lines
+886
to
+887
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The ThinkPad power brick sent the WAIT command, and only negotiated successfully with this
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Got it. |
||
| default: break; | ||
| } | ||
| } | ||
|
|
@@ -931,6 +944,7 @@ void USBPD_IRQHandler( void ) | |
| if ( USBPD->STATUS & IF_RX_RESET ) | ||
| { | ||
| USBPD->STATUS |= IF_RX_RESET; | ||
| SwitchRXMode(); | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Without the barrier (and the
volatilefield above), negotiation always failed when compiled with LTOThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it.