From 271c9e7e493e3027f48107aa2a6a973acb40b095 Mon Sep 17 00:00:00 2001 From: Sergey Vlasov Date: Sun, 2 Jul 2023 18:31:04 +0300 Subject: [PATCH] usbpd_stm32g4: Configure UCPD before disabling the dead battery mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The UCPD peripheral initially comes up in the “dead battery” mode, in which it supplies the Rd pull-downs on the CC1 and CC2 lines without any explicit configuration if the DBCC pins are wired appropriately. The dead battery mode must be disabled to use the full functionality of UCPD; however, disabling the dead battery mode must be done only after the UCPD peripheral has been configured in the desired mode, so that the transition between modes happens without going through a state with no pull-down resistance on the CC lines. Fix the code to respect the required initialization order. --- platforms/chibios/drivers/usbpd_stm32g4.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/platforms/chibios/drivers/usbpd_stm32g4.c b/platforms/chibios/drivers/usbpd_stm32g4.c index 0096f22f077c..21b8f6db95a3 100644 --- a/platforms/chibios/drivers/usbpd_stm32g4.c +++ b/platforms/chibios/drivers/usbpd_stm32g4.c @@ -22,8 +22,6 @@ // Initialises the USBPD subsystem __attribute__((weak)) void usbpd_init(void) { - // Disable dead-battery signals - PWR->CR3 |= PWR_CR3_UCPD_DBDIS; // Enable the clock for the UCPD1 peripheral RCC->APB1ENR2 |= RCC_APB1ENR2_UCPD1EN; @@ -46,6 +44,11 @@ __attribute__((weak)) void usbpd_init(void) { CR |= UCPD_CR_ANAMODE | UCPD_CR_CCENABLE_Msk; // Apply the changes UCPD1->CR = CR; + + // Disable dead-battery signals only after UCPD1 is configured to ensure + // that the transition does not go through any intermediate state without + // any pull-down resistance. + PWR->CR3 |= PWR_CR3_UCPD_DBDIS; } // Gets the current state of the USBPD allowance