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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 | #include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#define NRF_LOG_MODULE_NAME "FaBo_210_3Aix"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#define TWI_INSTANCE_ID 0
#define VCNL4010_SLAVE_ADDRESS 0x13 ///< I2C Slave Address
#define VCNL4010_DEVICE_ID 0x21 ///< Product,Revision ID Value
//Register Addresses
#define VCNL4010_REG_CMD 0x80
#define VCNL4010_REG_ID 0x81
#define VCNL4010_REG_PROX_RATE 0x82
#define VCNL4010_REG_LED_CRNT 0x83
#define VCNL4010_REG_AMBI_PARM 0x84
#define VCNL4010_REG_AMBI_DATA_H 0x85
#define VCNL4010_REG_AMBI_DATA_L 0x86
#define VCNL4010_REG_PROX_DATA_H 0x87
#define VCNL4010_REG_PROX_DATA_L 0x88
#define VCNL4010_REG_INT_CTRL 0x89
#define VCNL4010_REG_INT_LOW_H 0x8A
#define VCNL4010_REG_INT_LOW_L 0x8B
#define VCNL4010_REG_INT_HIGH_H 0x8C
#define VCNL4010_REG_INT_HIGH_L 0x8D
#define VCNL4010_REG_INT_STAT 0x8E
#define VCNL4010_REG_PROX_ADJ 0x8F
//Commands
#define VCNL4010_CMD_SELFTIMED_EN 0x01
#define VCNL4010_CMD_PROX_EN 0x02
#define VCNL4010_CMD_ALS_EN 0x04
#define VCNL4010_CMD_PROX_OD 0x08
#define VCNL4010_CMD_ALS_OD 0x10
#define VCNL4010_CMD_PROX_DRDY 0x20
#define VCNL4010_CMD_ALS_DRDY 0x40
//Proximity Measurement Rate
#define VCNL4010_PROX_RATE_1 0x00
#define VCNL4010_PROX_RATE_3 0x01
#define VCNL4010_PROX_RATE_7 0x02
#define VCNL4010_PROX_RATE_16 0x03
#define VCNL4010_PROX_RATE_31 0x04
#define VCNL4010_PROX_RATE_62 0x05
#define VCNL4010_PROX_RATE_125 0x06
#define VCNL4010_PROX_RATE_250 0x07
//Ambient Light Parameter
#define VCNL4010_AMBI_CONT_CONV_MODE 0x80
#define VCNL4010_AMBI_RATE_1 0x00
#define VCNL4010_AMBI_RATE_2 0x10
#define VCNL4010_AMBI_RATE_3 0x20
#define VCNL4010_AMBI_RATE_4 0x30
#define VCNL4010_AMBI_RATE_5 0x40
#define VCNL4010_AMBI_RATE_6 0x50
#define VCNL4010_AMBI_RATE_8 0x60
#define VCNL4010_AMBI_RATE_10 0x70
#define VCNL4010_AMBI_AUTO_OFFSET 0x08
#define VCNL4010_AMBI_AVE_NUM_1 0x00
#define VCNL4010_AMBI_AVE_NUM_2 0x01
#define VCNL4010_AMBI_AVE_NUM_4 0x02
#define VCNL4010_AMBI_AVE_NUM_8 0x03
#define VCNL4010_AMBI_AVE_NUM_16 0x04
#define VCNL4010_AMBI_AVE_NUM_32 0x05
#define VCNL4010_AMBI_AVE_NUM_64 0x06
#define VCNL4010_AMBI_AVE_NUM_128 0x07
bool begin(void);
bool searchDevice(void);
void configuration(void);
void setCommand(uint8_t config);
void setProxRate(uint8_t config);
void setLedCurrent(uint8_t config);
void setAmbiParm(uint8_t config);
bool checkProxReady(void);
bool checkAmbiReady(void);
uint16_t readProx(void);
uint16_t readAmbi(void);
void dumpReg(void);
void writeI2c(uint8_t address, uint8_t data);
void readI2c(uint8_t address,uint8_t * data);
void print_byte(uint8_t val);
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
bool begin() {
if ( searchDevice() ) {
configuration();
return true;
} else {
return false;
}
}
bool searchDevice() {
uint8_t data;
readI2c(VCNL4010_REG_ID,&data);
if ( data == VCNL4010_DEVICE_ID ) {
return true;
} else {
return false;
}
}
void configuration() {
setCommand(
VCNL4010_CMD_SELFTIMED_EN |
VCNL4010_CMD_PROX_EN |
VCNL4010_CMD_ALS_EN );
setProxRate(VCNL4010_PROX_RATE_250);
setLedCurrent(20);
setAmbiParm(
VCNL4010_AMBI_RATE_10 |
VCNL4010_AMBI_AUTO_OFFSET |
VCNL4010_AMBI_AVE_NUM_128 );
}
void setCommand(uint8_t config) {
writeI2c(VCNL4010_REG_CMD, config);
}
void setProxRate(uint8_t config) {
writeI2c(VCNL4010_REG_PROX_RATE, config);
}
void setLedCurrent(uint8_t config) {
writeI2c(VCNL4010_REG_LED_CRNT, config);
}
void setAmbiParm(uint8_t config) {
writeI2c(VCNL4010_REG_AMBI_PARM, config);
}
bool checkProxReady() {
uint8_t data;
NRF_LOG_FLUSH();
readI2c(VCNL4010_REG_CMD,&data);
if ( data & VCNL4010_CMD_PROX_DRDY ) {
return true;
}
return false;
}
bool checkAmbiReady() {
uint8_t data;
readI2c(VCNL4010_REG_CMD,&data);
if ( data & VCNL4010_CMD_ALS_DRDY ) {
return true;
}
return false;
}
uint16_t readProx() {
uint8_t data_h,data_l;
readI2c(VCNL4010_REG_PROX_DATA_H,&data_h);
readI2c(VCNL4010_REG_PROX_DATA_L,&data_l);
uint16_t value;
value = data_h;
value <<= 8;
value |= data_l;
return value;
}
uint16_t readAmbi() {
uint8_t ambi_h,ambi_l;
uint16_t value;
readI2c(VCNL4010_REG_AMBI_DATA_H,&ambi_h);
readI2c(VCNL4010_REG_AMBI_DATA_L,&ambi_l);
value = ambi_h;
value <<= 8;
value |= ambi_l;
return value;
}
void dumpReg() {
uint8_t data;
int i;
for (i=0x80;i<=0x90;i++){
readI2c(i,&data);
NRF_LOG_INFO("0x%x:%b\n\r",i,data);
NRF_LOG_FLUSH();
print_byte(data);
}
}
void print_byte(uint8_t val) {
int i;
NRF_LOG_INFO("B");
NRF_LOG_FLUSH();
for(i=7; i>=0; i--){
NRF_LOG_INFO("%b",val >> i & 1);
NRF_LOG_FLUSH();
}
}
void writeI2c(uint8_t address, uint8_t data) {
uint32_t err_code;
uint8_t reg[2] = {address,data};
err_code = nrf_drv_twi_tx(&m_twi, VCNL4010_SLAVE_ADDRESS, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
nrf_delay_ms(40);
}
void readI2c(uint8_t address,uint8_t * data) {
nrf_delay_ms(10);
uint32_t err_code;
uint8_t reg = address;
err_code = nrf_drv_twi_tx(&m_twi, VCNL4010_SLAVE_ADDRESS, ®, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
nrf_delay_ms(100);
err_code = nrf_drv_twi_rx(&m_twi, VCNL4010_SLAVE_ADDRESS, data, sizeof(data));
APP_ERROR_CHECK(err_code);
nrf_delay_ms(40);
}
void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
{
switch (p_event->type)
{
case NRF_DRV_TWI_EVT_DONE:
if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
{
}
break;
default:
break;
}
}
void twi_init (void)
{
ret_code_t err_code;
const nrf_drv_twi_config_t twi_lm75b_config = {
.scl = ARDUINO_SCL_PIN,
.sda = ARDUINO_SDA_PIN,
.frequency = NRF_TWI_FREQ_100K,
.interrupt_priority = APP_IRQ_PRIORITY_HIGH,
.clear_bus_init = false
};
err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL);
APP_ERROR_CHECK(err_code);
nrf_drv_twi_enable(&m_twi);
}
int main(void)
{
APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
NRF_LOG_INFO("FaBo_Proximity\r\n");
NRF_LOG_FLUSH();
twi_init();
begin();
NRF_LOG_INFO("Init...\r\n");
NRF_LOG_FLUSH();
while(1){
if(checkProxReady()){
NRF_LOG_INFO("Prox:%d\r\n",readProx());
NRF_LOG_FLUSH();
}
nrf_delay_ms(10);
if(checkAmbiReady()){
NRF_LOG_INFO("Ambi:%d\r\n",readAmbi());
NRF_LOG_FLUSH();
}
nrf_delay_ms(1000);
}
}
|