|
29 | 29 | #include <ina226/ina226.h> |
30 | 30 | #include <hal/hal_gpio.h> |
31 | 31 |
|
| 32 | +#if MYNEWT_VAL(BUS_DRIVER_PRESENT) |
| 33 | +#define OS_DEV(dev_) (&dev_->i2c_node.bnode.odev) |
| 34 | +#define I2C_ADDR(dev_) (dev_->i2c_node.addr) |
| 35 | +#else |
| 36 | +#define OS_DEV(dev_) (&dev_->dev) |
| 37 | +#define I2C_ADDR(dev_) (dev_->hw_cfg.itf.si_addr) |
| 38 | +#endif |
| 39 | + |
32 | 40 | /* Define stat names for querying */ |
33 | 41 | STATS_NAME_START(ina226_stat_section) |
34 | 42 | STATS_NAME(ina226_stat_section, read_count) |
@@ -133,6 +141,41 @@ enable_interrupt(struct ina226_dev *ina226) |
133 | 141 | #define ina226_init_interrupt(i) 0 |
134 | 142 | #endif |
135 | 143 |
|
| 144 | +#if MYNEWT_VAL(BUS_DRIVER_PRESENT) |
| 145 | +int |
| 146 | +ina226_write_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t reg_val) |
| 147 | +{ |
| 148 | + int rc; |
| 149 | + uint8_t payload[3] = { reg, reg_val >> 8, (uint8_t)reg_val }; |
| 150 | + |
| 151 | + STATS_INC(ina226->stats, write_count); |
| 152 | + rc = bus_node_simple_write(OS_DEV(ina226), payload, sizeof(payload)); |
| 153 | + if (rc) { |
| 154 | + STATS_INC(ina226->stats, write_errors); |
| 155 | + INA226_LOG_ERROR("INA226 write I2C failed\n"); |
| 156 | + } |
| 157 | + |
| 158 | + return rc; |
| 159 | +} |
| 160 | + |
| 161 | +int |
| 162 | +ina226_read_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t *reg_val) |
| 163 | +{ |
| 164 | + int rc; |
| 165 | + uint8_t buf[2]; |
| 166 | + |
| 167 | + STATS_INC(ina226->stats, read_count); |
| 168 | + rc = bus_node_simple_write_read_transact(OS_DEV(ina226), ®, 1, buf, 2); |
| 169 | + if (rc) { |
| 170 | + STATS_INC(ina226->stats, read_errors); |
| 171 | + INA226_LOG_ERROR("INA226 read I2C failed\n"); |
| 172 | + } else { |
| 173 | + *reg_val = (buf[0] << 8) | buf[1]; |
| 174 | + } |
| 175 | + |
| 176 | + return rc; |
| 177 | +} |
| 178 | +#else |
136 | 179 | int |
137 | 180 | ina226_write_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t reg_val) |
138 | 181 | { |
@@ -203,6 +246,7 @@ ina226_read_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t *reg_val) |
203 | 246 |
|
204 | 247 | return rc; |
205 | 248 | } |
| 249 | +#endif |
206 | 250 |
|
207 | 251 | int |
208 | 252 | ina226_reset(struct ina226_dev *ina226) |
@@ -445,7 +489,7 @@ ina226_open(struct os_dev *dev, uint32_t wait, void *arg) |
445 | 489 | } |
446 | 490 | if (mfg != INA226_MANUFACTURER_ID) { |
447 | 491 | INA226_LOG_ERROR("INA226 read ID failed, no INA226 at 0x%X, found 0x%X 0x%X\n", |
448 | | - ina226->hw_cfg.itf.si_addr, mfg, die); |
| 492 | + I2C_ADDR(ina226), mfg, die); |
449 | 493 | rc = SYS_ENODEV; |
450 | 494 | goto exit; |
451 | 495 | } |
@@ -495,7 +539,7 @@ ina226_init(struct os_dev *dev, void *arg) |
495 | 539 | STATS_NAME_INIT_PARMS(ina226_stat_section)); |
496 | 540 | SYSINIT_PANIC_ASSERT(rc == SYS_EOK); |
497 | 541 | /* Register the entry with the stats registry */ |
498 | | - rc = stats_register(ina226->dev.od_name, STATS_HDR(ina226->stats)); |
| 542 | + rc = stats_register(OS_DEV(ina226)->od_name, STATS_HDR(ina226->stats)); |
499 | 543 | SYSINIT_PANIC_ASSERT(rc == SYS_EOK); |
500 | 544 |
|
501 | 545 | rc = ina226_init_interrupt(ina226); |
@@ -579,3 +623,26 @@ ina226_sensor_get_config(struct sensor *sensor, sensor_type_t typ, |
579 | 623 | } |
580 | 624 | return rc; |
581 | 625 | } |
| 626 | + |
| 627 | +#if MYNEWT_VAL(BUS_DRIVER_PRESENT) |
| 628 | + |
| 629 | +static void |
| 630 | +init_node_cb(struct bus_node *bnode, void *arg) |
| 631 | +{ |
| 632 | + ina226_init(&bnode->odev, arg); |
| 633 | +} |
| 634 | + |
| 635 | +int |
| 636 | +ina226_create_sensor_dev(struct ina226_dev *ina226, const char *name, |
| 637 | + const struct bus_i2c_node_cfg *i2c_cfg, |
| 638 | + const struct ina226_hw_cfg *hw_cfg) |
| 639 | +{ |
| 640 | + int rc; |
| 641 | + static struct sensor_itf itf; |
| 642 | + |
| 643 | + rc = sensor_create_i2c_device(&ina226->i2c_node, name, i2c_cfg, |
| 644 | + init_node_cb, (void *)hw_cfg, &itf); |
| 645 | + |
| 646 | + return rc; |
| 647 | +} |
| 648 | +#endif |
0 commit comments