From 2382bf16173a891bf24fd8b04a11fc4399ae85d7 Mon Sep 17 00:00:00 2001 From: Daniel Kulp Date: Fri, 10 Jan 2025 16:40:11 -0500 Subject: [PATCH] Update sensors configs to allow a scale and if not specified, scale appropriately for 1.8V vs 3.3v on BB64 --- scripts/functions | 21 +++++++++++++++------ src/sensors/IIOSensorSource.cpp | 30 +++++++++++++++++++++++++++--- src/sensors/IIOSensorSource.h | 1 + src/sensors/Sensors.cpp | 16 ++++++++++++++++ src/util/BBBPruUtils.cpp | 6 +++--- 5 files changed, 62 insertions(+), 12 deletions(-) diff --git a/scripts/functions b/scripts/functions index 99c22cdce..c9bac9c0a 100755 --- a/scripts/functions +++ b/scripts/functions @@ -505,6 +505,8 @@ populateCCACHE() { PLAT=Pi elif [ "${FPPPLATFORM}" = "BeagleBone Black" ]; then PLAT=BBB + elif [ "${FPPPLATFORM}" = "BeagleBone 64" ]; then + PLAT=BB64 else echo "populateCache stopping, Unknown platform - ${FPPPLATFORM}" MAJVER=0 @@ -584,13 +586,20 @@ function ComputeMakeParallelism() { # will be very slow as we constantly swap in/out CPUS=1 elif [[ ${MEMORY} -lt 512000 ]]; then - SWAPTOTAL=$(grep SwapTotal /proc/meminfo | awk '{print $2}') - # Limited memory, if we have some swap, we'll go ahead with -j 2 - # otherwise we'll need to stick with -j 1 or we run out of memory - if [[ ${SWAPTOTAL} -gt 49000 ]]; then - CPUS=2 - else + ARMV=$(uname -m) + if [ "$ARMV" = "aarch64" ]; then + # 64bit compiles take more memory so if we are less than 512M, + # we need to stick with a single core or it will swap CPUS=1 + else + SWAPTOTAL=$(grep SwapTotal /proc/meminfo | awk '{print $2}') + # Limited memory, if we have some swap, we'll go ahead with -j 2 + # otherwise we'll need to stick with -j 1 or we run out of memory + if [[ ${SWAPTOTAL} -gt 49000 ]]; then + CPUS=2 + else + CPUS=1 + fi fi fi fi diff --git a/src/sensors/IIOSensorSource.cpp b/src/sensors/IIOSensorSource.cpp index bab9e0780..0590e52c2 100644 --- a/src/sensors/IIOSensorSource.cpp +++ b/src/sensors/IIOSensorSource.cpp @@ -31,6 +31,22 @@ IIOSensorSource::IIOSensorSource(Json::Value& config) : } else { usingBuffers = FileExists("/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/buffer/enable") && FileExists("/dev/iio:device" + std::to_string(iioDevNumber)); } + + if (config.isMember("vScale")) { + vScale = config["vScale"].asFloat(); + } else { +#ifdef PLATFORM_BB64 + std::string vs = GetFileContents("/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/in_voltage_scale"); + vScale = std::atof(vs.c_str()); + // original reference is 1.8V which for 12bit would be 0.439453125. + // If the cape doesn't specify a reference scale, we'll adjust and assume + // the params are based on 1.8 ref, but we're reading to 3.3V + vScale = vScale / 0.439453125; +#else + vScale = 1.0f; +#endif + } + // usingBuffers = false; std::string base = "/sys/bus/iio/devices/iio:device" + std::to_string(iioDevNumber) + "/in_voltage"; int max = -1; @@ -48,7 +64,7 @@ IIOSensorSource::IIOSensorSource(Json::Value& config) : values[x] = 0; } } else { - WarningHolder::AddWarning("Could not enable IIOSensorSorce"); + WarningHolder::AddWarning("Could not enable IIOSensorSource"); } } IIOSensorSource::~IIOSensorSource() { @@ -147,13 +163,20 @@ void IIOSensorSource::update(bool forceInstant, bool fromSelect) { for (int x = 0; x < channelMapping.size(); x++) { sums[x] -= mins[x]; sums[x] -= maxes[x]; + float f = sums[x]; + f /= (float)(samples - 2); + f *= vScale; + int idx = channelMapping[x]; - values[idx] = (sums[x] / (samples - 2)); + values[idx] = f; } } else { for (int x = 0; x < channelMapping.size(); x++) { + float f = sums[x]; + f /= (float)(samples); + f *= vScale; int idx = channelMapping[x]; - values[idx] = sums[x] / samples; + values[idx] = f; } } } @@ -169,6 +192,7 @@ void IIOSensorSource::update(bool forceInstant, bool fromSelect) { s += std::atoi(buf); } s /= 3.0; + s *= vScale; values[x] = std::round(s); } } diff --git a/src/sensors/IIOSensorSource.h b/src/sensors/IIOSensorSource.h index 5d54852d4..0456951f6 100644 --- a/src/sensors/IIOSensorSource.h +++ b/src/sensors/IIOSensorSource.h @@ -41,4 +41,5 @@ class IIOSensorSource : public SensorSource { uint16_t* readBuffer = nullptr; size_t readBufferSize = 0; + float vScale = 0; }; diff --git a/src/sensors/Sensors.cpp b/src/sensors/Sensors.cpp index 20ee047ee..973ee798a 100644 --- a/src/sensors/Sensors.cpp +++ b/src/sensors/Sensors.cpp @@ -286,6 +286,20 @@ class AINSensor : public Sensor { if (s.isMember("min")) { min = s["min"].asDouble(); } + if (s.isMember("vScale")) { + vScale = s["vScale"].asFloat(); + } else { +#ifdef PLATFORM_BB64 + std::string vs = GetFileContents("/sys/bus/iio/devices/iio:device0/in_voltage_scale"); + vScale = std::atof(vs.c_str()); + // original reference is 1.8V which for 12bit would be 0.439453125. + // If the cape doesn't specify a reference scale, we'll adjust and assume + // the params are based on 1.8 ref, but we're reading to 3.3V + vScale = vScale / 0.439453125; +#else + vScale = 1.0f; +#endif + } } virtual ~AINSensor() { close(file); @@ -309,6 +323,7 @@ class AINSensor : public Sensor { int i = read(file, buffer, 20); buffer[i] = 0; double d = atof(buffer); + d *= vScale; d /= 4096; // 12 bit a2d d *= (max - min + 1); @@ -324,6 +339,7 @@ class AINSensor : public Sensor { std::string driver; double min = 0.0; double max = 100.0; + double vScale = 1.0; volatile int file; volatile int errcount; diff --git a/src/util/BBBPruUtils.cpp b/src/util/BBBPruUtils.cpp index 3f47f1ae9..9f5545eaa 100644 --- a/src/util/BBBPruUtils.cpp +++ b/src/util/BBBPruUtils.cpp @@ -112,7 +112,7 @@ constexpr size_t DDR_SIZE = 0x00400000; constexpr std::string FIRMWARE_PREFIX = "am62x"; -constexpr bool FAKE_PRU = false; +static bool FAKE_PRU = !FileExists("/sys/class/remoteproc/remoteproc0/state"); #endif static void initPrus() { @@ -140,7 +140,7 @@ static void initPrus() { if (!FileExists("/sys/class/remoteproc/remoteproc0/state")) { system("modprobe pru_rproc"); } - if (ddr_mem_loc == nullptr && ddr_addr) { + if (ddr_mem_loc == nullptr && !FAKE_PRU) { ddr_phy_mem_loc = ddr_addr; ddr_filelen = ddr_sizeb; ddr_mem_loc = (uint8_t*)mmap(0, @@ -149,7 +149,7 @@ static void initPrus() { MAP_SHARED, mem_fd, ddr_addr); - } else if (ddr_addr == 0) { + } else if (ddr_addr == 0 || FAKE_PRU) { // just malloc some memory so we don't crash ddr_phy_mem_loc = ddr_addr; ddr_filelen = ddr_sizeb;