From 9decdcc89c4f3124686f078230f1388024b6d722 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Mon, 23 May 2022 22:25:00 +1000 Subject: [PATCH] Scratch out resistance awareness of the tip --- source/Core/BSP/Magic/BSP.cpp | 29 +++++++++++++++++++++++------ source/Core/Drivers/USBPD.cpp | 4 +++- source/Core/Drivers/USBPD.h | 1 + 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/source/Core/BSP/Magic/BSP.cpp b/source/Core/BSP/Magic/BSP.cpp index f4bc5772..42d9f75c 100644 --- a/source/Core/BSP/Magic/BSP.cpp +++ b/source/Core/BSP/Magic/BSP.cpp @@ -6,10 +6,12 @@ #include "Pins.h" #include "Setup.h" #include "TipThermoModel.h" +#include "USBPD.h" #include "configuration.h" #include "history.hpp" #include "main.hpp" + // These control the period's of time used for the PWM const uint16_t powerPWM = 255; const uint8_t holdoffTicks = 5; // This is the tick delay @@ -224,7 +226,7 @@ void setStatusLED(const enum StatusLED state) { // Dont have one } -uint8_t lastTipResistance = 75; // default safe +uint8_t lastTipResistance = 80; // default safe uint32_t lastTipReadinguV = 0; uint8_t getTipResitanceX10() { // Return tip resistance in x10 ohms @@ -241,9 +243,10 @@ void startMeasureTipResistance() { // If tip is connected, and the tip is cold and the tip is not being heated // We can use the GPIO to inject a small current into the tip and measure this // The gpio is 5.1k -> diode -> tip -> gnd - // Which is around 0.65mA this will induce: - // 6 ohm tip -> 3.9mV (Real world ~= 3320) - // 8 ohm tip -> 5.2mV (Real world ~= 4500) + // Source is 3.3V-0.5V + // Which is around 0.54mA this will induce: + // 6 ohm tip -> 3.24mV (Real world ~= 3320) + // 8 ohm tip -> 4.32mV (Real world ~= 4500) // Which is definitely measureable // Taking shortcuts here as we know we only really have to pick apart 6 and 8 ohm tips // These are reported as 60 and 75 respectively @@ -263,7 +266,21 @@ void FinishMeasureTipResistance() { return; } // newReading -= lastTipReadinguV; - MSG("Tip Delta %lu, %lu %lu \r\n", newReading - lastTipReadinguV, newReading, lastTipReadinguV); + // MSG("Tip Delta %lu, %lu %lu \r\n", newReading - lastTipReadinguV, newReading, lastTipReadinguV); newReading -= lastTipReadinguV; - lastTipReadinguV = newReading; + // As we are only detecting two resistances; we can split the difference for now + uint8_t newRes = 0; + if (newReading > 5000) { + return; // Change nothing as probably disconnected tip + } else if (newReading < 4000) { + newRes = 60; + } else { + newRes = 80; + } + if (lastTipResistance != newRes) { +#ifdef POW_PD + USBPowerDelivery::triggerRenegotiation(); +#endif + } + lastTipResistance = newRes; } \ No newline at end of file diff --git a/source/Core/Drivers/USBPD.cpp b/source/Core/Drivers/USBPD.cpp index 98e99b7e..dfae270f 100644 --- a/source/Core/Drivers/USBPD.cpp +++ b/source/Core/Drivers/USBPD.cpp @@ -66,7 +66,9 @@ bool USBPowerDelivery::fusbPresent() { } bool USBPowerDelivery::isVBUSConnected() { return fusb.isVBUSConnected(); } - +void USBPowerDelivery::triggerRenegotiation() { + +} bool pdbs_dpm_evaluate_capability(const pd_msg *capabilities, pd_msg *request) { /* Get the number of PDOs */ diff --git a/source/Core/Drivers/USBPD.h b/source/Core/Drivers/USBPD.h index 7e27dbea..fe55fdc0 100644 --- a/source/Core/Drivers/USBPD.h +++ b/source/Core/Drivers/USBPD.h @@ -19,6 +19,7 @@ public: static bool negotiationHasWorked(); // Has PD negotiation worked (are we in a PD contract) static uint8_t getStateNumber(); // Debugging - Get the internal state number static bool isVBUSConnected(); // Is the VBus pin connected on the FUSB302 + static void triggerRenegotiation(); // Trigger a restart of voltage selection private: // static int detectionState;