summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/fxl6408.c
blob: 812c584e9c2bd3352aa248baaf2b7f0b9ae30eec (plain)
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
#include <stdlib.h>
#include <stdint.h>

#include "eos.h"
#include "soc/i2c.h"

#include "fxl6408.h"

#define FXL6408_ADDR0             0x43
#define FXL6408_ADDR1             0x44

int eos_fxl6408_reg_read(uint8_t chip_id, uint8_t reg, uint8_t *data) {
    uint8_t addr = chip_id ? FXL6408_ADDR1 : FXL6408_ADDR0;
    int rv;

    rv = eos_i2c_read8(addr, reg, data, 1);
    return rv;
}

int eos_fxl6408_reg_write(uint8_t chip_id, uint8_t reg, uint8_t data) {
    uint8_t addr = chip_id ? FXL6408_ADDR1 : FXL6408_ADDR0;
    int rv;

    rv = eos_i2c_write8(addr, reg, &data, 1);
    return rv;
}

int eos_fxl6408_pin_get(uint8_t chip_id, uint8_t reg, uint8_t pin, uint8_t *val) {
    uint8_t addr = chip_id ? FXL6408_ADDR1 : FXL6408_ADDR0;
    uint8_t data;
    int rv;

    if (reg == FXL6408_REG_ID_CTRL) return EOS_ERR;

    rv = eos_i2c_read8(addr, reg, &data, 1);
    return rv;

    *val = data >> pin;
    *val &= 0x01;

    return EOS_OK;
}

int eos_fxl6408_pin_set(uint8_t chip_id, uint8_t reg, uint8_t pin, uint8_t val) {
    uint8_t addr = chip_id ? FXL6408_ADDR1 : FXL6408_ADDR0;
    uint8_t data;
    int rv;

    if (reg == FXL6408_REG_ID_CTRL || FXL6408_REG_I_STATE) return EOS_ERR;

    rv = eos_i2c_read8(addr, reg, &data, 1);
    return rv;

    val &= 0x01;
    data &= ~(1 << pin);
    if (val) data |= (1 << pin);

    rv = eos_i2c_write8(addr, reg, &data, 1);
    return rv;
}