From 0b02275192427ce400b1c784f2490e02f317cdb4 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Mon, 5 Apr 2021 16:22:17 +1000 Subject: [PATCH] Cleanup message rx event to inline, re-enable non PPS --- source/Core/Drivers/FUSB302/int_n.cpp | 1 - source/Core/Drivers/FUSB302/policy_engine.cpp | 28 +++++----- source/Core/Drivers/FUSB302/policy_engine.h | 7 ++- .../Drivers/FUSB302/policy_engine_user.cpp | 51 +++++++++---------- 4 files changed, 39 insertions(+), 48 deletions(-) diff --git a/source/Core/Drivers/FUSB302/int_n.cpp b/source/Core/Drivers/FUSB302/int_n.cpp index 7e4b5697..4ba4aa57 100644 --- a/source/Core/Drivers/FUSB302/int_n.cpp +++ b/source/Core/Drivers/FUSB302/int_n.cpp @@ -54,7 +54,6 @@ void InterruptHandler::readPendingMessage() { /* Pass the message to the policy engine. */ PolicyEngine::handleMessage(&tempMessage); - PolicyEngine::notify(PolicyEngine::Notifications::PDB_EVT_PE_MSG_RX); } } diff --git a/source/Core/Drivers/FUSB302/policy_engine.cpp b/source/Core/Drivers/FUSB302/policy_engine.cpp index 8943a986..d1ebaba0 100644 --- a/source/Core/Drivers/FUSB302/policy_engine.cpp +++ b/source/Core/Drivers/FUSB302/policy_engine.cpp @@ -155,14 +155,10 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_discovery() { PolicyEngine::policy_engine_state PolicyEngine::pe_sink_wait_cap() { /* Fetch a message from the protocol layer */ - EventBits_t evt = 0; - if (readMessage()) { - evt = (uint32_t)Notifications::PDB_EVT_PE_MSG_RX_PEND; - } else { - evt = waitForEvent((uint32_t)Notifications::PDB_EVT_PE_MSG_RX | (uint32_t)Notifications::PDB_EVT_PE_I_OVRTEMP | (uint32_t)Notifications::PDB_EVT_PE_RESET, - // Wait for cap timeout - PD_T_TYPEC_SINK_WAIT_CAP); - } + EventBits_t evt = waitForEvent((uint32_t)Notifications::PDB_EVT_PE_MSG_RX | (uint32_t)Notifications::PDB_EVT_PE_I_OVRTEMP | (uint32_t)Notifications::PDB_EVT_PE_RESET, + // Wait for cap timeout + PD_T_TYPEC_SINK_WAIT_CAP); + /* If we timed out waiting for Source_Capabilities, send a hard reset */ if (evt == 0) { return PESinkHardReset; @@ -177,7 +173,7 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_wait_cap() { } /* If we got a message */ - if (evt & ((uint32_t)Notifications::PDB_EVT_PE_MSG_RX | (uint32_t)Notifications::PDB_EVT_PE_MSG_RX_PEND)) { + if (evt & (uint32_t)Notifications::PDB_EVT_PE_MSG_RX) { /* Get the message */ while (readMessage()) { /* If we got a Source_Capabilities message, read it. */ @@ -292,13 +288,8 @@ PolicyEngine::policy_engine_state PolicyEngine::pe_sink_select_cap() { PolicyEngine::policy_engine_state PolicyEngine::pe_sink_transition_sink() { /* Wait for the PS_RDY message */ - EventBits_t evt = 0; - - if (messageWaiting()) { - evt = (uint32_t)Notifications::PDB_EVT_PE_MSG_RX; - } else { - evt = waitForEvent((uint32_t)Notifications::PDB_EVT_PE_MSG_RX | (uint32_t)Notifications::PDB_EVT_PE_RESET, PD_T_PS_TRANSITION); - } /* If we got reset signaling, transition to default */ + EventBits_t evt = waitForEvent((uint32_t)Notifications::PDB_EVT_PE_MSG_RX | (uint32_t)Notifications::PDB_EVT_PE_RESET, PD_T_PS_TRANSITION); + /* If we got reset signaling, transition to default */ if (evt & (uint32_t)Notifications::PDB_EVT_PE_RESET) { return PESinkTransitionDefault; } @@ -618,6 +609,11 @@ EventBits_t PolicyEngine::waitForEvent(uint32_t mask, TickType_t ticksToWait) { bool PolicyEngine::isPD3_0() { return (hdr_template & PD_HDR_SPECREV) == PD_SPECREV_3_0; } +void PolicyEngine::handleMessage(union pd_msg *msg) { + xQueueSend(messagesWaiting, msg, 100); + notify(PolicyEngine::Notifications::PDB_EVT_PE_MSG_RX); +} + void PolicyEngine::PPSTimerCallback() { if (PPSTimerEnabled && state == policy_engine_state::PESinkReady) { // I believe even once per second is totally fine, but leaning on faster since everything seems cool with faster diff --git a/source/Core/Drivers/FUSB302/policy_engine.h b/source/Core/Drivers/FUSB302/policy_engine.h index 1cbe034b..489b7a50 100644 --- a/source/Core/Drivers/FUSB302/policy_engine.h +++ b/source/Core/Drivers/FUSB302/policy_engine.h @@ -59,10 +59,9 @@ public: PDB_EVT_PE_HARD_SENT = EVENT_MASK(4), PDB_EVT_PE_I_OVRTEMP = EVENT_MASK(5), PDB_EVT_PE_PPS_REQUEST = EVENT_MASK(6), - PDB_EVT_PE_MSG_RX_PEND = EVENT_MASK(7), /* Never send this from user area*/ - PDB_EVT_PE_GET_SOURCE_CAP = EVENT_MASK(8), - PDB_EVT_PE_NEW_POWER = EVENT_MASK(9), - PDB_EVT_PE_ALL = (EVENT_MASK(10) - 1), + PDB_EVT_PE_GET_SOURCE_CAP = EVENT_MASK(7), + PDB_EVT_PE_NEW_POWER = EVENT_MASK(8), + PDB_EVT_PE_ALL = (EVENT_MASK(9) - 1), }; // Send a notification static void notify(Notifications notification); diff --git a/source/Core/Drivers/FUSB302/policy_engine_user.cpp b/source/Core/Drivers/FUSB302/policy_engine_user.cpp index 4e052364..46edb42c 100644 --- a/source/Core/Drivers/FUSB302/policy_engine_user.cpp +++ b/source/Core/Drivers/FUSB302/policy_engine_user.cpp @@ -10,7 +10,7 @@ #include "policy_engine.h" /* The current draw when the output is disabled */ -#define DPM_MIN_CURRENT PD_MA2PDI(50) +#define DPM_MIN_CURRENT PD_MA2PDI(100) /* * Find the index of the first PDO from capabilities in the voltage range, * using the desired order. @@ -63,29 +63,29 @@ bool PolicyEngine::pdbs_dpm_evaluate_capability(const union pd_msg *capabilities for (uint8_t i = 0; i < numobj; i++) { /* If we have a fixed PDO, its V equals our desired V, and its I is * at least our desired I */ - // if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED) { - // // This is a fixed PDO entry - // // Evaluate if it can produve sufficient current based on the tipResistance (ohms*10) - // // V=I*R -> V/I => minimum resistance, if our tip resistance is >= this then we can use this supply + if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_FIXED) { + // This is a fixed PDO entry + // Evaluate if it can produve sufficient current based on the tipResistance (ohms*10) + // V=I*R -> V/I => minimum resistance, if our tip resistance is >= this then we can use this supply - // int voltage_mv = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities->obj[i])); // voltage in mV units - // int current_a_x100 = PD_PDO_SRC_FIXED_CURRENT_GET(capabilities->obj[i]); // current in 10mA units - // int min_resistance_ohmsx10 = voltage_mv / current_a_x100; - // if (voltage_mv <= (USB_PD_VMAX * 1000)) { - // if (min_resistance_ohmsx10 <= tipResistance) { - // // This is a valid power source we can select as - // if (voltage_mv > bestIndexVoltage||bestIndex == 0xFF) { - // // Higher voltage and valid, select this instead - // bestIndex = i; - // bestIndexVoltage = voltage_mv; - // bestIndexCurrent = current_a_x100; - // bestIsPPS = false; - // } - // } - // } - // } else + int voltage_mv = PD_PDV2MV(PD_PDO_SRC_FIXED_VOLTAGE_GET(capabilities->obj[i])); // voltage in mV units + int current_a_x100 = PD_PDO_SRC_FIXED_CURRENT_GET(capabilities->obj[i]); // current in 10mA units + int min_resistance_ohmsx10 = voltage_mv / current_a_x100; + if (voltage_mv <= (USB_PD_VMAX * 1000)) { + if (min_resistance_ohmsx10 <= tipResistance) { + // This is a valid power source we can select as + if (voltage_mv > bestIndexVoltage || bestIndex == 0xFF) { + // Higher voltage and valid, select this instead + bestIndex = i; + bestIndexVoltage = voltage_mv; + bestIndexCurrent = current_a_x100; + bestIsPPS = false; + } + } + } + } else - if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED && (capabilities->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS) { + if ((capabilities->obj[i] & PD_PDO_TYPE) == PD_PDO_TYPE_AUGMENTED && (capabilities->obj[i] & PD_APDO_TYPE) == PD_APDO_TYPE_PPS) { // If this is a PPS slot, calculate the max voltage in the PPS range that can we be used and maintain uint16_t max_voltage = PD_PAV2MV(PD_APDO_PPS_MAX_VOLTAGE_GET(capabilities->obj[i])); // uint16_t min_voltage = PD_PAV2MV(PD_APDO_PPS_MIN_VOLTAGE_GET(capabilities->obj[i])); @@ -122,7 +122,6 @@ bool PolicyEngine::pdbs_dpm_evaluate_capability(const union pd_msg *capabilities /* Update requested voltage */ _requested_voltage = bestIndexVoltage; - return true; } else { /* Nothing matched (or no configuration), so get 5 V at low current */ request->hdr = hdr_template | PD_MSGTYPE_REQUEST | PD_NUMOBJ(1); @@ -135,9 +134,9 @@ bool PolicyEngine::pdbs_dpm_evaluate_capability(const union pd_msg *capabilities /* Update requested voltage */ _requested_voltage = 5000; - - return false; } + // Even if we didnt match, we return true as we would still like to handshake on 5V at the minimum + return true; } void PolicyEngine::pdbs_dpm_get_sink_capability(union pd_msg *cap) { @@ -223,8 +222,6 @@ void PolicyEngine::pdbs_dpm_transition_default() { void PolicyEngine::pdbs_dpm_transition_requested() { pdNegotiationComplete = true; } -void PolicyEngine::handleMessage(union pd_msg *msg) { xQueueSend(messagesWaiting, msg, 100); } - bool PolicyEngine::messageWaiting() { return uxQueueMessagesWaiting(messagesWaiting) > 0; } bool PolicyEngine::readMessage() { return xQueueReceive(messagesWaiting, &tempMessage, 0) == pdTRUE; }