40 #define INA226_RAW // remove this 44 #include <px4_platform_common/px4_config.h> 45 #include <px4_platform_common/getopt.h> 52 #include <px4_platform_common/px4_work_queue/ScheduledWorkItem.hpp> 55 #define INA226_BUS_DEFAULT PX4_I2C_BUS_EXPANSION 56 #define INA226_BASEADDR 0x41 59 #define INA226_REG_CONFIGURATION (0x00) 60 #define INA226_REG_SHUNTVOLTAGE (0x01) 61 #define INA226_REG_BUSVOLTAGE (0x02) 62 #define INA226_REG_POWER (0x03) 63 #define INA226_REG_CURRENT (0x04) 64 #define INA226_REG_CALIBRATION (0x05) 65 #define INA226_REG_MASKENABLE (0x06) 66 #define INA226_REG_ALERTLIMIT (0x07) 67 #define INA226_MFG_ID (0xfe) 68 #define INA226_MFG_DIEID (0xff) 70 #define INA226_MFG_ID_TI (0x5449) // TI 71 #define INA226_MFG_DIE (0x2260) // INA2260 74 #define INA226_MODE_SHIFTS (0) 75 #define INA226_MODE_MASK (7 << INA226_MODE_SHIFTS) 76 #define INA226_MODE_SHUTDOWN (0 << INA226_MODE_SHIFTS) 77 #define INA226_MODE_SHUNT_TRIG (1 << INA226_MODE_SHIFTS) 78 #define INA226_MODE_BUS_TRIG (2 << INA226_MODE_SHIFTS) 79 #define INA226_MODE_SHUNT_BUS_TRIG (3 << INA226_MODE_SHIFTS) 80 #define INA226_MODE_ADC_OFF (4 << INA226_MODE_SHIFTS) 81 #define INA226_MODE_SHUNT_CONT (5 << INA226_MODE_SHIFTS) 82 #define INA226_MODE_BUS_CONT (6 << INA226_MODE_SHIFTS) 83 #define INA226_MODE_SHUNT_BUS_CONT (7 << INA226_MODE_SHIFTS) 85 #define INA226_VSHCT_SHIFTS (3) 86 #define INA226_VSHCT_MASK (7 << INA226_VSHCT_SHIFTS) 87 #define INA226_VSHCT_140US (0 << INA226_VSHCT_SHIFTS) 88 #define INA226_VSHCT_204US (1 << INA226_VSHCT_SHIFTS) 89 #define INA226_VSHCT_332US (2 << INA226_VSHCT_SHIFTS) 90 #define INA226_VSHCT_588US (3 << INA226_VSHCT_SHIFTS) 91 #define INA226_VSHCT_1100US (4 << INA226_VSHCT_SHIFTS) 92 #define INA226_VSHCT_2116US (5 << INA226_VSHCT_SHIFTS) 93 #define INA226_VSHCT_4156US (6 << INA226_VSHCT_SHIFTS) 94 #define INA226_VSHCT_8244US (7 << INA226_VSHCT_SHIFTS) 96 #define INA226_VBUSCT_SHIFTS (6) 97 #define INA226_VBUSCT_MASK (7 << INA226_VBUSCT_SHIFTS) 98 #define INA226_VBUSCT_140US (0 << INA226_VBUSCT_SHIFTS) 99 #define INA226_VBUSCT_204US (1 << INA226_VBUSCT_SHIFTS) 100 #define INA226_VBUSCT_332US (2 << INA226_VBUSCT_SHIFTS) 101 #define INA226_VBUSCT_588US (3 << INA226_VBUSCT_SHIFTS) 102 #define INA226_VBUSCT_1100US (4 << INA226_VBUSCT_SHIFTS) 103 #define INA226_VBUSCT_2116US (5 << INA226_VBUSCT_SHIFTS) 104 #define INA226_VBUSCT_4156US (6 << INA226_VBUSCT_SHIFTS) 105 #define INA226_VBUSCT_8244US (7 << INA226_VBUSCT_SHIFTS) 107 #define INA226_AVERAGES_SHIFTS (9) 108 #define INA226_AVERAGES_MASK (7 << INA226_AVERAGES_SHIFTS) 109 #define INA226_AVERAGES_1 (0 << INA226_AVERAGES_SHIFTS) 110 #define INA226_AVERAGES_4 (1 << INA226_AVERAGES_SHIFTS) 111 #define INA226_AVERAGES_16 (2 << INA226_AVERAGES_SHIFTS) 112 #define INA226_AVERAGES_64 (3 << INA226_AVERAGES_SHIFTS) 113 #define INA226_AVERAGES_128 (4 << INA226_AVERAGES_SHIFTS) 114 #define INA226_AVERAGES_256 (5 << INA226_AVERAGES_SHIFTS) 115 #define INA226_AVERAGES_512 (6 << INA226_AVERAGES_SHIFTS) 116 #define INA226_AVERAGES_1024 (7 << INA226_AVERAGES_SHIFTS) 118 #define INA226_CONFIG (INA226_MODE_SHUNT_BUS_CONT | INA226_VSHCT_588US | INA226_VBUSCT_588US | INA226_AVERAGES_64) 120 #define INA226_RST (1 << 15) 124 #define INA226_LEN (1 << 0) 125 #define INA226_APOL (1 << 1) 126 #define INA226_OVF (1 << 2) 127 #define INA226_CVRF (1 << 3) 128 #define INA226_AFF (1 << 4) 130 #define INA226_CNVR (1 << 10) 131 #define INA226_POL (1 << 11) 132 #define INA226_BUL (1 << 12) 133 #define INA226_BOL (1 << 13) 134 #define INA226_SUL (1 << 14) 135 #define INA226_SOL (1 << 15) 137 #define INA226_CONVERSION_INTERVAL (100000-7) 138 #define MAX_CURRENT 164.0f 139 #define DN_MAX 32768.0f 140 #define INA226_CONST 0.00512f 141 #define INA226_SHUNT 0.0005f 142 #define INA226_VSCALE 0.00125f 144 #define swap16(w) __builtin_bswap16((w)) 146 class INA226 :
public device::I2C, px4::ScheduledWorkItem
192 int read(uint8_t address);
193 int write(uint8_t address, uint16_t
data);
225 I2C(
"INA226", nullptr, bus, address, 100000),
226 ScheduledWorkItem(MODULE_NAME,
px4::device_bus_to_wq(get_device_id())),
279 int ret = transfer(&address, 1, &
data.b[0],
sizeof(
data.b));
283 PX4_DEBUG(
"i2c::transfer returned %d", ret);
292 uint8_t
data[3] = {address, ((uint8_t)((value & 0xff00) >> 8)), (uint8_t)(value & 0xff)};
293 return transfer(data,
sizeof(data),
nullptr, 0);
341 PX4_DEBUG(
"probe mfgid %d", value);
352 PX4_DEBUG(
"probe die id %d", value);
369 PX4_DEBUG(
"i2c::transfer returned %d", ret);
402 #if defined(INA226_RAW) 425 PX4_DEBUG(
"error reading from sensor: %d", ret);
458 PX4_DEBUG(
"collection error");
479 PX4_DEBUG(
"measure error ina226");
523 if (g_dev !=
nullptr) {
524 PX4_ERR(
"already started");
546 if (g_dev !=
nullptr) {
547 PX4_ERR(
"already started");
552 g_dev =
new INA226(i2c_bus);
554 if (g_dev ==
nullptr) {
558 if (
OK != g_dev->
init()) {
566 if (g_dev !=
nullptr) {
580 if (g_dev !=
nullptr) {
585 PX4_ERR(
"driver not running");
598 if (g_dev ==
nullptr) {
599 PX4_ERR(
"driver poll restart failed");
603 printf(
"state @ %p\n", g_dev);
615 PX4_INFO(
"usage: ina226 command [options]");
616 PX4_INFO(
"options:");
618 PX4_INFO(
"\t-a --all");
619 PX4_INFO(
"command:");
620 PX4_INFO(
"\tstart|stop|test|info");
628 const char *myoptarg =
nullptr;
629 bool start_all =
false;
633 while ((ch = px4_getopt(argc, argv,
"ab:R:", &myoptind, &myoptarg)) != EOF) {
637 i2c_bus = atoi(myoptarg);
645 PX4_WARN(
"Unknown option!");
650 if (myoptind >= argc) {
657 if (!strcmp(argv[myoptind],
"start")) {
669 if (!strcmp(argv[myoptind],
"stop")) {
676 if (!strcmp(argv[myoptind],
"info") || !strcmp(argv[myoptind],
"status")) {
#define INA226_REG_BUSVOLTAGE
int start()
Attempt to start driver on all available I2C busses.
#define PARAM_INVALID
Handle returned when a parameter cannot be found.
__EXPORT int ina226_main(int argc, char *argv[])
measure the time elapsed performing an event
__EXPORT int param_get(param_t param, void *val)
Copy the value of a parameter.
API for the uORB lightweight object broker.
orb_advert_t _power_monitor_topic
#define INA226_MODE_SHIFTS
#define INA226_BUS_DEFAULT
int read(uint8_t address)
Test whetpower_monitorhe device supported by the driver is present at a specific address.
static void ina2262_usage()
#define INA226_REG_ALERTLIMIT
count the number of times an event occurs
High-resolution timer with callouts and timekeeping.
Global flash based parameter store.
void stop()
Stop the automatic measurement state machine.
#define ORB_ID(_name)
Generates a pointer to the uORB metadata structure for a given topic.
void perf_count(perf_counter_t handle)
Count a performance event.
void perf_free(perf_counter_t handle)
Free a counter.
void init()
Activates/configures the hardware registers.
perf_counter_t _sample_perf
INA226(int bus=INA226_BUS_DEFAULT, int address=INA226_BASEADDR)
void Run() override
Perform a poll cycle; collect from the previous measurement and start a new one.
int stop()
Stop the driver.
void perf_end(perf_counter_t handle)
End a performance event.
#define INA226_REG_CURRENT
void print_info()
Diagnostics - print some basic information about the driver.
void start()
Initialise the automatic measurement state machine and start it.
__BEGIN_DECLS typedef void * orb_advert_t
ORB topic advertiser handle.
static const int i2c_bus_options[]
__EXPORT param_t param_find(const char *name)
Look up a parameter by name.
perf_counter_t _comms_errors
#define INA226_MODE_SHUNT_BUS_TRIG
Local functions in support of the shell command.
int write(uint8_t address, uint16_t data)
void perf_print_counter(perf_counter_t handle)
Print one performance counter to stdout.
int info()
Print a little info about the driver.
#define INA226_REG_CALIBRATION
#define NUM_I2C_BUS_OPTIONS
#define INA226_REG_SHUNTVOLTAGE
void perf_begin(perf_counter_t handle)
Begin a performance event.
#define INA226_REG_MASKENABLE
#define INA226_CONVERSION_INTERVAL
__EXPORT hrt_abstime hrt_absolute_time(void)
Get absolute time in [us] (does not wrap).
uint32_t param_t
Parameter handle.
static int orb_publish_auto(const struct orb_metadata *meta, orb_advert_t *handle, const void *data, int *instance, int priority)
Advertise as the publisher of a topic.
int start_bus(int i2c_bus)
Start the driver on a specific bus.
#define INA226_REG_CONFIGURATION
Performance measuring tools.
Base class for devices connected via I2C.