isl29020.c
2.87 KB
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
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* 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_isl29020
* @{
*
* @file
* @brief Device driver implementation for the ISL29020 light sensor
*
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
* @author Peter Kietzmann <peter.kietzmann@haw-hamburg.de>
*
* @}
*/
#include "isl29020.h"
#include "isl29020-internal.h"
#include "periph/i2c.h"
#define ENABLE_DEBUG (0)
#include "debug.h"
int isl29020_init(isl29020_t *dev, i2c_t i2c, uint8_t address,
isl29020_range_t range, isl29020_mode_t mode)
{
int res;
uint8_t tmp;
/* initialize device descriptor */
dev->i2c = i2c;
dev->address = address;
dev->lux_fac = (float)((1 << (10 + (2 * range))) - 1) / 0xffff;
/* acquire exclusive access to the bus */
i2c_acquire(dev->i2c);
/* initialize the I2C bus */
i2c_init_master(i2c, I2C_SPEED_NORMAL);
/* configure and enable the sensor */
tmp = ISL29020_CMD_EN | ISL29020_CMD_MODE | ISL29020_RES_INT_16 | range | (mode << 5);
res = i2c_write_reg(dev->i2c, address, ISL29020_REG_CMD, tmp);
/* release the bus for other threads */
i2c_release(dev->i2c);
if (res < 1) {
return -1;
}
return 0;
}
int isl29020_read(const isl29020_t *dev)
{
uint8_t low, high;
uint16_t res;
int ret;
i2c_acquire(dev->i2c);
/* read lighting value */
ret = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_LDATA, &low);
ret += i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_HDATA, &high);
i2c_release(dev->i2c);
if (ret < 2) {
return -1;
}
res = (high << 8) | low;
DEBUG("ISL29020: Raw value: %i - high: %i, low: %i\n", res, high, low);
/* calculate and return the actual lux value */
return (int)(dev->lux_fac * res);
}
int isl29020_enable(const isl29020_t *dev)
{
int res;
uint8_t tmp;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
return -1;
}
tmp |= ISL29020_CMD_EN;
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp);
if (res < 1) {
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
return 0;
}
int isl29020_disable(const isl29020_t *dev)
{
int res;
uint8_t tmp;
i2c_acquire(dev->i2c);
res = i2c_read_reg(dev->i2c, dev->address, ISL29020_REG_CMD, &tmp);
if (res < 1) {
i2c_release(dev->i2c);
return -1;
}
tmp &= ~(ISL29020_CMD_EN);
res = i2c_write_reg(dev->i2c, dev->address, ISL29020_REG_CMD, tmp);
if (res < 1) {
i2c_release(dev->i2c);
return -1;
}
i2c_release(dev->i2c);
return 0;
}