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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
#include <stdlib.h>
#include <stdint.h>
#include "eos.h"
#include "soc/i2c.h"
#include "apds9151.h"
#define APDS9151_ADDR 0x52
static int reg_read8(uint8_t reg, uint8_t *data) {
int rv;
rv = eos_i2c_read8(APDS9151_ADDR, reg, data, 1);
return rv;
}
static int reg_write8(uint8_t reg, uint8_t data) {
int rv;
rv = eos_i2c_write8(APDS9151_ADDR, reg, &data, 1);
return rv;
}
static int reg_read16(uint8_t reg, uint16_t *data) {
int rv;
uint8_t b[2];
rv = eos_i2c_read8(APDS9151_ADDR, reg, b, 2);
if (rv) return rv;
*data = \
((uint16_t)b[0]) | \
((uint16_t)b[1] << 8);
return EOS_OK;
}
static int reg_write16(uint8_t reg, uint16_t data) {
int rv;
uint8_t b[2];
b[0] = data;
b[1] = data >> 8;
rv = eos_i2c_write8(APDS9151_ADDR, reg, b, 2);
if (rv) return rv;
return EOS_OK;
}
static int reg_read32(uint8_t reg, uint32_t *data) {
int rv;
uint8_t b[3];
rv = eos_i2c_read8(APDS9151_ADDR, reg, b, 3);
if (rv) return rv;
*data = \
((uint32_t)b[0]) | \
((uint32_t)b[1] << 8) | \
((uint32_t)b[2] << 16);
return EOS_OK;
}
static int reg_write32(uint8_t reg, uint32_t data) {
int rv;
uint8_t b[3];
b[0] = data;
b[1] = data >> 8;
b[2] = data >> 16;
rv = eos_i2c_write8(APDS9151_ADDR, reg, b, 3);
if (rv) return rv;
return EOS_OK;
}
int eos_apds9151_reg_read(uint8_t reg, void *data) {
int rv = EOS_OK;
switch (reg & (~APDS9151_REG_MASK)) {
case APDS9151_REG_SIZE_8: {
rv = reg_read8(reg & APDS9151_REG_MASK, (uint8_t *)data);
break;
}
case APDS9151_REG_SIZE_16: {
rv = reg_read16(reg & APDS9151_REG_MASK, (uint16_t *)data);
break;
}
case APDS9151_REG_SIZE_32: {
rv = reg_read32(reg & APDS9151_REG_MASK, (uint32_t *)data);
break;
}
default: return EOS_ERR;
}
return rv;
}
int eos_apds9151_reg_write(uint8_t reg, uint32_t data) {
int rv = EOS_OK;
switch (reg & (~APDS9151_REG_MASK)) {
case APDS9151_REG_SIZE_8: {
rv = reg_write8(reg & APDS9151_REG_MASK, (uint8_t)data);
break;
}
case APDS9151_REG_SIZE_16: {
rv = reg_write16(reg & APDS9151_REG_MASK, (uint16_t)data);
break;
}
case APDS9151_REG_SIZE_32: {
rv = reg_write32(reg & APDS9151_REG_MASK, (uint32_t)data);
break;
}
default: return EOS_ERR;
}
return rv;
}
|