summaryrefslogtreecommitdiff
path: root/fw/fe310/eos/dev/apds9151.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/fe310/eos/dev/apds9151.c')
-rw-r--r--fw/fe310/eos/dev/apds9151.c119
1 files changed, 119 insertions, 0 deletions
diff --git a/fw/fe310/eos/dev/apds9151.c b/fw/fe310/eos/dev/apds9151.c
new file mode 100644
index 0000000..9e0237e
--- /dev/null
+++ b/fw/fe310/eos/dev/apds9151.c
@@ -0,0 +1,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;
+}