forked from RIOT-OS/RIOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathltc4150_saul.c
67 lines (57 loc) · 1.48 KB
/
ltc4150_saul.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
* Copyright 2019 Otto-von-Guericke-Universität Magdeburg
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup drivers_ltc4150
* @{
*
* @file
* @brief SAUL adaption for LTC4150 devices
*
* @author Marian Buschsieweke <marian.buschsieweke@ovgu.de>
*
* @}
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "phydat.h"
#include "saul.h"
#include "ltc4150.h"
static int read_charge(const void *_dev, phydat_t *res)
{
ltc4150_dev_t *dev = (ltc4150_dev_t *)_dev;
int32_t temp[3];
if (ltc4150_charge(dev, (uint32_t *)&temp[1], (uint32_t *)&temp[2]) == 0) {
res->scale = -3;
res->unit = UNIT_COULOMB;
temp[0] = temp[2] - temp[1];
int dim = (dev->params.polarity != GPIO_UNDEF) ? 3 : 1;
phydat_fit(res, temp, (unsigned)dim);
return dim;
}
return -ECANCELED;
}
static int read_current(const void *dev, phydat_t *res)
{
if (ltc4150_avg_current((ltc4150_dev_t *)dev, res->val) == 0) {
res->unit = UNIT_A;
res->scale = -4;
return 1;
}
return -ECANCELED;
}
const saul_driver_t ltc4150_saul_charge_driver = {
.read = read_charge,
.write = saul_notsup,
.type = SAUL_SENSE_CHARGE
};
const saul_driver_t ltc4150_saul_current_driver = {
.read = read_current,
.write = saul_notsup,
.type = SAUL_SENSE_CURRENT
};