Skip to content

Commit

Permalink
Update sensors configs to allow a scale and if not specified, scale a…
Browse files Browse the repository at this point in the history
…ppropriately for 1.8V vs 3.3v on BB64
  • Loading branch information
dkulp committed Jan 11, 2025
1 parent 38ebee6 commit 2382bf1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 12 deletions.
21 changes: 15 additions & 6 deletions scripts/functions
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
30 changes: 27 additions & 3 deletions src/sensors/IIOSensorSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down Expand Up @@ -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;
}
}
}
Expand All @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/sensors/IIOSensorSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ class IIOSensorSource : public SensorSource {

uint16_t* readBuffer = nullptr;
size_t readBufferSize = 0;
float vScale = 0;
};
16 changes: 16 additions & 0 deletions src/sensors/Sensors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/util/BBBPruUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand Down

0 comments on commit 2382bf1

Please sign in to comment.