Add metric feature

This commit is contained in:
Loic Guegan 2023-08-10 17:38:01 +02:00
parent 1e651c5327
commit 451a80d636
2 changed files with 31 additions and 2 deletions

View file

@ -8,12 +8,12 @@ all: ina260.c
make -C $(LML) M=$(PWD) modules
run: ina260.c
-echo 0x40 > /sys/bus/i2c/devices/i2c-2/delete_device
-echo 0x41 > /sys/bus/i2c/devices/i2c-2/delete_device
-rmmod ina260
make clean
make
insmod ina260.ko
echo ina260 0x40 > /sys/bus/i2c/devices/i2c-2/new_device
echo ina260 0x41 > /sys/bus/i2c/devices/i2c-2/new_device
read: read.c
gcc $^ -o read

View file

@ -16,6 +16,7 @@
#define INA260_REG_MANUFACTURER 0xFE
#define INA260_REG_DIE 0xFF
#define INA260_LSB_POWER 10 // 10mW
#define INA260_IS_ATTR(_name) (strcmp(attr->attr.name, #_name) == 0)
// ina260 average modes list
@ -150,6 +151,22 @@ static ssize_t attr_store(struct kobject *_kobj,
return count;
}
static ssize_t attr_metric_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
{
int rvalue;
struct client_data *cdata=container_of(_kobj,struct client_data,kobj);
if(INA260_IS_ATTR(power)){
if(ina260_read_register(cdata, INA260_REG_POWER,&rvalue))
return -1;
return sprintf(buf, "%d.%d\n", INA260_LSB_POWER*rvalue/1000,(INA260_LSB_POWER*rvalue) % 1000);
}
return -1;
}
static ssize_t attr_field_show(struct kobject *_kobj,
struct kobj_attribute *attr,
char *buf)
@ -339,6 +356,17 @@ static const struct attribute_group registers_group = {
.attrs = registers_attrs,
.name = "registers"
};
// ----- Metrics -----
static struct kobj_attribute metric_power_attribute =
__ATTR(power, 0664, attr_metric_show, NULL);
static struct attribute *metrics_attrs[] = {
&metric_power_attribute.attr,
NULL,
};
static const struct attribute_group metrics_group = {
.attrs = metrics_attrs,
.name = "metrics"
};
// ----- Fields -----
static struct kobj_attribute reset_field_attribute =
__ATTR(reset, 0664, attr_field_show, attr_field_store);
@ -446,6 +474,7 @@ static int ina260_probe_new(struct i2c_client *client){
i2c_set_clientdata(client,p);
// Setup sysfs groups:
if(sysfs_create_group(&p->kobj,&registers_group)||
sysfs_create_group(&p->kobj,&metrics_group)||
sysfs_create_group(&p->kobj,&fields_group)){
kobject_put(&p->kobj);
kfree(p);