From 6e062bfa59c79d238187b86b0e7b5c3519d45928 Mon Sep 17 00:00:00 2001 From: "Ben V. Brown" Date: Mon, 20 Jun 2022 20:23:11 +1000 Subject: [PATCH] tip measurement --- source/Core/BSP/Magic/BSP.cpp | 69 ++++++++++++++++++++++++------- source/Core/Drivers/crc16.cpp | 19 +++++++++ source/Core/Drivers/crc16.hpp | 6 +++ source/Core/Threads/GUIThread.cpp | 2 +- source/Core/Threads/PIDThread.cpp | 5 --- 5 files changed, 81 insertions(+), 20 deletions(-) create mode 100644 source/Core/Drivers/crc16.cpp create mode 100644 source/Core/Drivers/crc16.hpp diff --git a/source/Core/BSP/Magic/BSP.cpp b/source/Core/BSP/Magic/BSP.cpp index 66a7492c..514e15f2 100644 --- a/source/Core/BSP/Magic/BSP.cpp +++ b/source/Core/BSP/Magic/BSP.cpp @@ -8,6 +8,7 @@ #include "TipThermoModel.h" #include "USBPD.h" #include "configuration.h" +#include "crc16.hpp" #include "history.hpp" #include "main.hpp" @@ -233,9 +234,7 @@ uint8_t getTipResitanceX10() { return lastTipResistance; } void startMeasureTipResistance() { - if (isTipDisconnected()) { - return; - } + if (lastTipReadinguV) { return; } @@ -257,9 +256,6 @@ void startMeasureTipResistance() { void FinishMeasureTipResistance() { gpio_write(TIP_RESISTANCE_SENSE, 0); - if (isTipDisconnected()) { - return; - } // read the tip uV with the current source on uint32_t newReading = (TipThermoModel::convertTipRawADCTouV(getTipRawTemp(0))); if (newReading < lastTipReadinguV) { @@ -279,11 +275,44 @@ void FinishMeasureTipResistance() { } lastTipResistance = newRes; } +volatile bool tipMeasurementOccuring = false; +volatile uint8_t tipSenseResistancex10Ohms = 0; + +void performTipMeasurementStep(bool start) { + static TickType_t lastMeas = 0; + // Inter state that performs the steps to measure the resistor on the tip + // Return 1 if a measurement is ongoing + + // We want to perform our startup measurements of the tip resistance until we detect one fitted + + // Step 1; if not setup, we turn on pullup and then wait + if (tipMeasurementOccuring == false && (start || tipSenseResistancex10Ohms == 0 || lastMeas == 0)) { + // Block starting if tip removed + if (isTipDisconnected()) { + return; + } + tipMeasurementOccuring = true; + tipSenseResistancex10Ohms = 0; + lastMeas = xTaskGetTickCount(); + startMeasureTipResistance(); + return; + } + + // Wait 100ms for settle time + if ((xTaskGetTickCount() - lastMeas) < (TICKS_100MS)) { + return; + } + + lastMeas = xTaskGetTickCount(); + // We are sensing the resistance + FinishMeasureTipResistance(); + + tipMeasurementOccuring = false; +} uint8_t preStartChecks() { - // startMeasureTipResistance(); - // TODO - return 0; + performTipMeasurementStep(false); + return tipMeasurementOccuring ? 1 : 0; } // Return hardware unique ID if possible @@ -299,20 +328,32 @@ uint64_t getDeviceID() { return __builtin_bswap64(tmp); } +uint16_t gethash() { + uint32_t userData = 0; + EF_Ctrl_Read_Sw_Usage(0, &userData); + userData &= 0xFFFF; // We only want the lower two bytes + // TODO FOR TESTING OVERRIDE + userData = 0xDEAD; + const uint16_t crcInitialVector = 0xCAFE; + uint8_t crcPayload[] = {userData & 0xFF, (userData >> 8) & 0xFF, 0, 0, 0, 0, 0, 0, 0, 0}; + EF_Ctrl_Read_Chip_ID(crcPayload + 2); + uint32_t result = crcInitialVector; + for (int i = 0; i < 10; i++) { + result = crc16(result, crcPayload[i]); + } + return result & 0xFFFF; +} uint32_t getDeviceValidation() { uint32_t userData = 0; EF_Ctrl_Read_Sw_Usage(0, &userData); // 4 byte user data burned in at factory - + // TODO TESTING + // userData |= gethash() << 16; return userData; } uint8_t getDeviceValidationStatus() { - uint32_t userData = 0; - EF_Ctrl_Read_Sw_Usage(0, &userData); - userData &= 0xFFFF; // We only want the lower two bytes - userData = 0xDEAD; // TODO TESTING KEY return 1; // Device is OK } \ No newline at end of file diff --git a/source/Core/Drivers/crc16.cpp b/source/Core/Drivers/crc16.cpp new file mode 100644 index 00000000..9966c9f0 --- /dev/null +++ b/source/Core/Drivers/crc16.cpp @@ -0,0 +1,19 @@ + +#include "crc16.hpp" +#define POLYNOM 0x8005 +unsigned int crc16(unsigned int crcValue, unsigned char newByte) { + unsigned char i; + + for (i = 0; i < 8; i++) { + + if (((crcValue & 0x8000) >> 8) ^ (newByte & 0x80)) { + crcValue = (crcValue << 1) ^ POLYNOM; + } else { + crcValue = (crcValue << 1); + } + + newByte <<= 1; + } + + return crcValue; +} \ No newline at end of file diff --git a/source/Core/Drivers/crc16.hpp b/source/Core/Drivers/crc16.hpp new file mode 100644 index 00000000..b598c6fb --- /dev/null +++ b/source/Core/Drivers/crc16.hpp @@ -0,0 +1,6 @@ +#ifndef DRIVERS_CRC16_H_ +#define DRIVERS_CRC16_H_ + +unsigned int crc16(unsigned int crcValue, unsigned char newByte); + +#endif \ No newline at end of file diff --git a/source/Core/Threads/GUIThread.cpp b/source/Core/Threads/GUIThread.cpp index 96aafd46..8f2b0140 100644 --- a/source/Core/Threads/GUIThread.cpp +++ b/source/Core/Threads/GUIThread.cpp @@ -969,7 +969,7 @@ void startGUITask(void const *argument) { } #endif #endif - BootLogo::handleShowingLogo((uint8_t *)FLASH_LOGOADDR); + // BootLogo::handleShowingLogo((uint8_t *)FLASH_LOGOADDR); showWarnings(); if (getSettingValue(SettingsOptions::AutoStartMode)) { diff --git a/source/Core/Threads/PIDThread.cpp b/source/Core/Threads/PIDThread.cpp index 067f961b..a1b8351d 100644 --- a/source/Core/Threads/PIDThread.cpp +++ b/source/Core/Threads/PIDThread.cpp @@ -77,11 +77,6 @@ void startPIDTask(void const *argument __unused) { detectThermalRunaway(currentTipTempInC, 0); } setOutputx10WattsViaFilters(x10WattsOut); - // If the output is off, take tip measurement reading - // if (x10WattsOut == 0 && PIDTempTarget == 0) { - // startMeasureTipResistance(); - // measuringTipResistance = true; - // } } else { // ADC interrupt timeout setTipPWM(0, false);