Skip to content

Commit 69fdf9b

Browse files
committed
drivers/ina226: Add bus driver support
Sensor was only supporting hal i2c. Now it can be used with bus drivers. Signed-off-by: Jerzy Kasenberg <[email protected]>
1 parent 352fb7c commit 69fdf9b

File tree

2 files changed

+79
-2
lines changed

2 files changed

+79
-2
lines changed

hw/drivers/sensors/ina226/include/ina226/ina226.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,11 @@ STATS_SECT_START(ina226_stat_section)
131131
STATS_SECT_END
132132

133133
struct ina226_dev {
134+
#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
135+
struct bus_i2c_node i2c_node;
136+
#else
134137
struct os_dev dev;
138+
#endif
135139
struct sensor sensor;
136140
/* Hardware wiring config, (pin, shunt, i2c) */
137141
struct ina226_hw_cfg hw_cfg;
@@ -258,6 +262,12 @@ int ina226_start_continuous_mode(struct ina226_dev *ina226, enum ina226_oper_mod
258262
*/
259263
int ina226_stop_continuous_mode(struct ina226_dev *ina226);
260264

265+
#if MYNEWT_VAL(BUS_DRIVER_PRESENT)
266+
int ina226_create_sensor_dev(struct ina226_dev *ina226, const char *name,
267+
const struct bus_i2c_node_cfg *i2c_cfg,
268+
const struct ina226_hw_cfg *hw_cfg);
269+
#endif
270+
261271
#if MYNEWT_VAL(INA226_CLI)
262272
/**
263273
* Initialize the INA226 shell extensions.

hw/drivers/sensors/ina226/src/ina226.c

Lines changed: 69 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@
2929
#include <ina226/ina226.h>
3030
#include <hal/hal_gpio.h>
3131

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+
3240
/* Define stat names for querying */
3341
STATS_NAME_START(ina226_stat_section)
3442
STATS_NAME(ina226_stat_section, read_count)
@@ -133,6 +141,41 @@ enable_interrupt(struct ina226_dev *ina226)
133141
#define ina226_init_interrupt(i) 0
134142
#endif
135143

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), &reg, 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
136179
int
137180
ina226_write_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t reg_val)
138181
{
@@ -203,6 +246,7 @@ ina226_read_reg(struct ina226_dev *ina226, uint8_t reg, uint16_t *reg_val)
203246

204247
return rc;
205248
}
249+
#endif
206250

207251
int
208252
ina226_reset(struct ina226_dev *ina226)
@@ -445,7 +489,7 @@ ina226_open(struct os_dev *dev, uint32_t wait, void *arg)
445489
}
446490
if (mfg != INA226_MANUFACTURER_ID) {
447491
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);
449493
rc = SYS_ENODEV;
450494
goto exit;
451495
}
@@ -495,7 +539,7 @@ ina226_init(struct os_dev *dev, void *arg)
495539
STATS_NAME_INIT_PARMS(ina226_stat_section));
496540
SYSINIT_PANIC_ASSERT(rc == SYS_EOK);
497541
/* 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));
499543
SYSINIT_PANIC_ASSERT(rc == SYS_EOK);
500544

501545
rc = ina226_init_interrupt(ina226);
@@ -579,3 +623,26 @@ ina226_sensor_get_config(struct sensor *sensor, sensor_type_t typ,
579623
}
580624
return rc;
581625
}
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

Comments
 (0)