Skip to content

Commit 675aa37

Browse files
committed
Adding SensorProviderJetson for Nvidia Jetson AGX modules
1 parent 2748c33 commit 675aa37

File tree

5 files changed

+386
-1
lines changed

5 files changed

+386
-1
lines changed

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ project(RECSDaemon)
33

44
SET(MAJOR_VERSION 3)
55
SET(MINOR_VERSION 7)
6-
SET(PATCH_VERSION 0)
6+
SET(PATCH_VERSION 1)
77

88
SET(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
99

@@ -69,6 +69,7 @@ else()
6969
add_subdirectory(plugins/LinuxSensorProviderEth)
7070
add_subdirectory(plugins/LinuxSlotDetectorGPIO)
7171
add_subdirectory(plugins/SensorProviderZynq)
72+
add_subdirectory(plugins/SensorProviderJetson)
7273
endif()
7374

7475
if ( ${BUILD_TESTS} )
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
cmake_minimum_required(VERSION 2.8)
2+
project(SensorProviderJetson)
3+
4+
set(CMAKE_BUILD_TYPE debug)
5+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -Wextra")
6+
7+
set (PROJECT_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
8+
set (PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
9+
10+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
11+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/plugins)
12+
13+
include_directories("${PROJECT_BINARY_DIR}")
14+
include_directories("${PROJECT_INCLUDE_DIR}")
15+
16+
file(GLOB SOURCES src/*.cpp)
17+
add_library(${PROJECT_NAME} SHARED ${SOURCES})
18+
if (${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
19+
target_link_libraries(${PROJECT_NAME} wsock32) # For htonl;
20+
endif ()
21+
22+
INSTALL(PROGRAMS ${CMAKE_BINARY_DIR}/plugins/${CMAKE_SHARED_LIBRARY_PREFIX}${PROJECT_NAME}${CMAKE_SHARED_LIBRARY_SUFFIX} DESTINATION plugins)
Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Copyright (C) 2021 christmann informationstechnik + medien GmbH & Co. KG
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
////////////////////////////////////////////////////////////////////////////////
22+
// Author: Micha vor dem Berge <micha.vordemberge@christmann.info>
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
#include "SensorProviderJetson.h"
26+
27+
#include <cstring>
28+
#include <cstdlib>
29+
#include <iostream>
30+
#include <fstream>
31+
#include <stdio.h>
32+
#include <unistd.h>
33+
#include <sys/types.h>
34+
#include <object_model.h>
35+
#include <SensorBean.h>
36+
#include <fcntl.h>
37+
#include <sys/mman.h>
38+
#include <errno.h>
39+
#include <sys/ioctl.h>
40+
#include <sys/reboot.h>
41+
#include <sys/statvfs.h>
42+
43+
using namespace std;
44+
45+
LoggerPtr SensorProviderJetson::logger;
46+
47+
void * SensorProviderJetson::create(PF_ObjectParams *) {
48+
return new SensorProviderJetson();
49+
}
50+
51+
int32_t SensorProviderJetson::destroy(void * p) {
52+
if (!p)
53+
return -1;
54+
delete static_cast<SensorProviderJetson*>(p);
55+
return 0;
56+
}
57+
58+
SensorProviderJetson::SensorProviderJetson() :
59+
mSensors() {
60+
61+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input");
62+
if (myfile.is_open()) {
63+
myfile.close();
64+
65+
// Power Measurements
66+
SensorBean* sensor = new SensorBean("Power CPU", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
67+
sensor->setData((uint32_t)0);
68+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerCpu);
69+
mSensors["Power CPU"] = sensor;
70+
71+
sensor = new SensorBean("Power GPU", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
72+
sensor->setData((uint32_t)0);
73+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerGpu);
74+
mSensors["Power GPU"] = sensor;
75+
76+
sensor = new SensorBean("Power SoC", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
77+
sensor->setData((uint32_t)0);
78+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSoC);
79+
mSensors["Power SoC"] = sensor;
80+
81+
sensor = new SensorBean("Power CV", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
82+
sensor->setData((uint32_t)0);
83+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerCv);
84+
mSensors["Power CV"] = sensor;
85+
86+
sensor = new SensorBean("Power DDR", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
87+
sensor->setData((uint32_t)0);
88+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSoC);
89+
mSensors["Power DDR"] = sensor;
90+
91+
sensor = new SensorBean("Power SYS5V", TYPE_FLOAT, 8, 1, UNIT_POWER, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
92+
sensor->setData((uint32_t)0);
93+
sensor->setUpdateCallback(&SensorProviderJetson::updatePowerSys5V);
94+
mSensors["Power SYS5V"] = sensor;
95+
96+
// Temperature Measurements
97+
sensor = new SensorBean("Temp SoC", TYPE_FLOAT, 8, 1, UNIT_TEMPERATURE, false, false, 0, 0, 0, 0, "", RENDERING_TEXTUAL);
98+
sensor->setData((uint32_t)0);
99+
sensor->setUpdateCallback(&SensorProviderJetson::updateTempSoC);
100+
mSensors["Temp SoC"] = sensor;
101+
} else {
102+
LOG_ERROR(logger, "This plugin only works on Nvidia Jetson Xavier AGX, not initializing.");
103+
}
104+
}
105+
106+
SensorProviderJetson::~SensorProviderJetson() {
107+
}
108+
109+
map<string, ISensor*> SensorProviderJetson::getSensors(void) {
110+
return mSensors;
111+
}
112+
113+
void SensorProviderJetson::updatePowerCpu(SensorBean* sensor) {
114+
string line;
115+
uint16_t milli_power;
116+
117+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power1_input");
118+
if (myfile.is_open()) {
119+
getline(myfile, line);
120+
std::istringstream stream(line);
121+
stream >> milli_power;
122+
myfile.close();
123+
} else {
124+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power1_input'");
125+
return;
126+
}
127+
128+
//LOG_DEBUG(logger, "Power CPU: " << milli_power / 1000.0 << " W");
129+
sensor->setData(milli_power / 1000.0);
130+
}
131+
132+
void SensorProviderJetson::updatePowerGpu(SensorBean* sensor) {
133+
string line;
134+
uint16_t milli_power;
135+
136+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input");
137+
if (myfile.is_open()) {
138+
getline(myfile, line);
139+
std::istringstream stream(line);
140+
stream >> milli_power;
141+
myfile.close();
142+
} else {
143+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power0_input'");
144+
return;
145+
}
146+
147+
//LOG_DEBUG(logger, "Power GPU: " << milli_power / 1000.0 << " W");
148+
sensor->setData(milli_power / 1000.0);
149+
}
150+
151+
void SensorProviderJetson::updatePowerSoC(SensorBean* sensor) {
152+
string line;
153+
uint16_t milli_power;
154+
155+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power2_input");
156+
if (myfile.is_open()) {
157+
getline(myfile, line);
158+
std::istringstream stream(line);
159+
stream >> milli_power;
160+
myfile.close();
161+
} else {
162+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0040/iio_device/in_power2_input'");
163+
return;
164+
}
165+
166+
//LOG_DEBUG(logger, "Power SoC: " << milli_power / 1000.0 << " W");
167+
sensor->setData(milli_power / 1000.0);
168+
}
169+
170+
void SensorProviderJetson::updatePowerCv(SensorBean* sensor) {
171+
string line;
172+
uint16_t milli_power;
173+
174+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power0_input");
175+
if (myfile.is_open()) {
176+
getline(myfile, line);
177+
std::istringstream stream(line);
178+
stream >> milli_power;
179+
myfile.close();
180+
} else {
181+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power0_input'");
182+
return;
183+
}
184+
185+
//LOG_DEBUG(logger, "Power CV: " << milli_power / 1000.0 << " W");
186+
sensor->setData(milli_power / 1000.0);
187+
}
188+
void SensorProviderJetson::updatePowerDDR(SensorBean* sensor) {
189+
string line;
190+
uint16_t milli_power;
191+
192+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power1_input");
193+
if (myfile.is_open()) {
194+
getline(myfile, line);
195+
std::istringstream stream(line);
196+
stream >> milli_power;
197+
myfile.close();
198+
} else {
199+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power1_input'");
200+
return;
201+
}
202+
203+
//LOG_DEBUG(logger, "Power DDR: " << milli_power / 1000.0 << " W");
204+
sensor->setData(milli_power / 1000.0);
205+
}
206+
207+
void SensorProviderJetson::updatePowerSys5V(SensorBean* sensor) {
208+
string line;
209+
uint16_t milli_power;
210+
211+
ifstream myfile("/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power2_input");
212+
if (myfile.is_open()) {
213+
getline(myfile, line);
214+
std::istringstream stream(line);
215+
stream >> milli_power;
216+
myfile.close();
217+
} else {
218+
LOG_ERROR(logger, "Could not read file '/sys/bus/i2c/drivers/ina3221x/1-0041/iio_device/in_power2_input'");
219+
return;
220+
}
221+
222+
//LOG_DEBUG(logger, "Power SYS5V: " << milli_power / 1000.0 << " W");
223+
sensor->setData(milli_power / 1000.0);
224+
}
225+
226+
void SensorProviderJetson::updateTempSoC(SensorBean* sensor) {
227+
string line;
228+
uint16_t milli_temp;
229+
230+
ifstream myfile("/sys/devices/virtual/thermal/thermal_zone4/temp");
231+
if (myfile.is_open()) {
232+
getline(myfile, line);
233+
std::istringstream stream(line);
234+
stream >> milli_temp;
235+
myfile.close();
236+
} else {
237+
LOG_ERROR(logger, "Could not read file '/sys/devices/virtual/thermal/thermal_zone4/temp'");
238+
return;
239+
}
240+
241+
//LOG_DEBUG(logger, "Temp SoC: " << milli_power / 1000.0 << " C");
242+
sensor->setData(milli_temp / 1000.0);
243+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// Copyright (C) 2021 christmann informationstechnik + medien GmbH & Co. KG
3+
//
4+
// Permission is hereby granted, free of charge, to any person obtaining a copy
5+
// of this software and associated documentation files (the "Software"), to deal
6+
// in the Software without restriction, including without limitation the rights
7+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
// copies of the Software, and to permit persons to whom the Software is
9+
// furnished to do so, subject to the following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included in
12+
// all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
// SOFTWARE.
21+
////////////////////////////////////////////////////////////////////////////////
22+
// Author: Micha vor dem Berge <micha.vordemberge@christmann.info>
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
#ifndef SENSORPROVIDERJETSON_H
26+
#define SENSORPROVIDERJETSON_H
27+
28+
#include <object_model.h>
29+
#include <string>
30+
#include <map>
31+
#include <logger.h>
32+
#include <c_object_model.h>
33+
#include <SensorBean.h>
34+
35+
struct PF_ObjectParams;
36+
37+
class SensorProviderJetson: public ISensorProvider {
38+
public:
39+
// static plugin interface
40+
static void * create(PF_ObjectParams *);
41+
static int32_t destroy(void *);
42+
~SensorProviderJetson();
43+
44+
// ISensorProvider methods
45+
virtual std::map<std::string, ISensor*> getSensors(void);
46+
47+
static LoggerPtr logger;
48+
private:
49+
SensorProviderJetson();
50+
static void updatePowerCpu(SensorBean* sensor);
51+
static void updatePowerGpu(SensorBean* sensor);
52+
static void updatePowerSoC(SensorBean* sensor);
53+
static void updatePowerCv(SensorBean* sensor);
54+
static void updatePowerDDR(SensorBean* sensor);
55+
static void updatePowerSys5V(SensorBean* sensor);
56+
static void updateTempSoC(SensorBean* sensor);
57+
58+
std::map<std::string, ISensor*> mSensors;
59+
};
60+
61+
#endif

0 commit comments

Comments
 (0)