VCNL4100 High Sensitivity Long Distance Proximity and Ambient Light Sensor with I2C Interface

Dependents:   test_VCNL4100 testSensor

Committer:
Rhyme
Date:
Mon May 08 07:16:46 2017 +0000
Revision:
0:fbf1a72181fc
First compilable version / no test yet

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:fbf1a72181fc 1 #include "mbed.h"
Rhyme 0:fbf1a72181fc 2 #include "VCNL4100.h"
Rhyme 0:fbf1a72181fc 3
Rhyme 0:fbf1a72181fc 4 /* I2C protocol
Rhyme 0:fbf1a72181fc 5 *
Rhyme 0:fbf1a72181fc 6 * I2C bus timing for Sendign Word Command Format
Rhyme 0:fbf1a72181fc 7 * <s> <8bit: Slave Address>W <sa> <8bit: command code> <sa>
Rhyme 0:fbf1a72181fc 8 * <8bit: Data Byte Low> <sa> <8bit: Data Byte High> <sa> <p>
Rhyme 0:fbf1a72181fc 9 *
Rhyme 0:fbf1a72181fc 10 * I2C bus timing for Receiving Word Command Format
Rhyme 0:fbf1a72181fc 11 * <s> <8bit: Slave Address>W <sa> <8bit: Command Code> <sa>
Rhyme 0:fbf1a72181fc 12 * <s> <8bit: Slave Address>R <sa> <8bit: Data Byte Low> <ma>
Rhyme 0:fbf1a72181fc 13 * <8bit: Data Byte High> <ma> <p>
Rhyme 0:fbf1a72181fc 14 *
Rhyme 0:fbf1a72181fc 15 * <s> Start by Master
Rhyme 0:fbf1a72181fc 16 * <p> Stop by Master
Rhyme 0:fbf1a72181fc 17 * <ma> ACK by Master
Rhyme 0:fbf1a72181fc 18 * <sa> ACK by Slave (VCNL4100)
Rhyme 0:fbf1a72181fc 19 */
Rhyme 0:fbf1a72181fc 20
Rhyme 0:fbf1a72181fc 21 /* Internal Registers */
Rhyme 0:fbf1a72181fc 22 #define REG_ALS_CONF 0x00
Rhyme 0:fbf1a72181fc 23 #define REG_ALS_THDH_L 0x01
Rhyme 0:fbf1a72181fc 24 #define REG_ALS_THDL_L 0x02
Rhyme 0:fbf1a72181fc 25 #define REG_PS_CONF1 0x03
Rhyme 0:fbf1a72181fc 26 #define REG_PS_CONF3 0x04
Rhyme 0:fbf1a72181fc 27 #define REG_PS_THDL 0x06
Rhyme 0:fbf1a72181fc 28 #define REG_PS_DATA 0x08
Rhyme 0:fbf1a72181fc 29 #define REG_ASL_DATA 0x09
Rhyme 0:fbf1a72181fc 30 #define REG_INT_FLAG 0x0B
Rhyme 0:fbf1a72181fc 31
Rhyme 0:fbf1a72181fc 32 VCNL4100::VCNL4100(PinName sda, PinName scl, int addr) :
Rhyme 0:fbf1a72181fc 33 m_i2c(sda, scl), m_addr(addr<<1)
Rhyme 0:fbf1a72181fc 34 {
Rhyme 0:fbf1a72181fc 35 // activate the peripheral
Rhyme 0:fbf1a72181fc 36 }
Rhyme 0:fbf1a72181fc 37
Rhyme 0:fbf1a72181fc 38
Rhyme 0:fbf1a72181fc 39 VCNL4100::~VCNL4100()
Rhyme 0:fbf1a72181fc 40 {
Rhyme 0:fbf1a72181fc 41 }
Rhyme 0:fbf1a72181fc 42
Rhyme 0:fbf1a72181fc 43 void VCNL4100::readRegs(int addr, uint8_t * data, int len) {
Rhyme 0:fbf1a72181fc 44 char t[1] = {addr};
Rhyme 0:fbf1a72181fc 45 m_i2c.write(m_addr, t, 1, true);
Rhyme 0:fbf1a72181fc 46 m_i2c.read(m_addr, (char *)data, len);
Rhyme 0:fbf1a72181fc 47 }
Rhyme 0:fbf1a72181fc 48
Rhyme 0:fbf1a72181fc 49 void VCNL4100::writeRegs(uint8_t * data, int len) {
Rhyme 0:fbf1a72181fc 50 m_i2c.write(m_addr, (char *)data, len);
Rhyme 0:fbf1a72181fc 51 }
Rhyme 0:fbf1a72181fc 52
Rhyme 0:fbf1a72181fc 53 uint8_t VCNL4100::getAlsConf(void)
Rhyme 0:fbf1a72181fc 54 {
Rhyme 0:fbf1a72181fc 55 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 56 readRegs(REG_ALS_CONF, data, 2) ;
Rhyme 0:fbf1a72181fc 57 return(data[0]) ;
Rhyme 0:fbf1a72181fc 58 }
Rhyme 0:fbf1a72181fc 59
Rhyme 0:fbf1a72181fc 60 void VCNL4100::setAlsConf(uint8_t conf)
Rhyme 0:fbf1a72181fc 61 {
Rhyme 0:fbf1a72181fc 62 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 63 data[0] = REG_ALS_CONF ;
Rhyme 0:fbf1a72181fc 64 data[1] = conf ;
Rhyme 0:fbf1a72181fc 65 data[2] = 0x00 ;
Rhyme 0:fbf1a72181fc 66 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 67 }
Rhyme 0:fbf1a72181fc 68
Rhyme 0:fbf1a72181fc 69 uint16_t VCNL4100::getAlsThdh(void)
Rhyme 0:fbf1a72181fc 70 {
Rhyme 0:fbf1a72181fc 71 uint16_t thdh ;
Rhyme 0:fbf1a72181fc 72 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 73 readRegs(REG_ALS_THDH_L, data, 2) ;
Rhyme 0:fbf1a72181fc 74 thdh = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 75 return( thdh ) ;
Rhyme 0:fbf1a72181fc 76 }
Rhyme 0:fbf1a72181fc 77
Rhyme 0:fbf1a72181fc 78 void VCNL4100::setAlsThdh(uint16_t thdh)
Rhyme 0:fbf1a72181fc 79 {
Rhyme 0:fbf1a72181fc 80 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 81 data[0] = REG_ALS_THDH_L ;
Rhyme 0:fbf1a72181fc 82 data[1] = thdh & 0xFF ;
Rhyme 0:fbf1a72181fc 83 data[2] = (thdh >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 84 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 85 }
Rhyme 0:fbf1a72181fc 86
Rhyme 0:fbf1a72181fc 87 uint16_t VCNL4100::getAlsThdl(void)
Rhyme 0:fbf1a72181fc 88 {
Rhyme 0:fbf1a72181fc 89 uint16_t thdl ;
Rhyme 0:fbf1a72181fc 90 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 91 readRegs(REG_ALS_THDL_L, data, 2) ;
Rhyme 0:fbf1a72181fc 92 thdl = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 93 return( thdl ) ;
Rhyme 0:fbf1a72181fc 94 }
Rhyme 0:fbf1a72181fc 95
Rhyme 0:fbf1a72181fc 96 void VCNL4100::setAlsThdl(uint16_t thdl)
Rhyme 0:fbf1a72181fc 97 {
Rhyme 0:fbf1a72181fc 98 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 99 data[0] = REG_ALS_THDL_L ;
Rhyme 0:fbf1a72181fc 100 data[1] = thdl & 0xFF ;
Rhyme 0:fbf1a72181fc 101 data[2] = (thdl >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 102 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 103 }
Rhyme 0:fbf1a72181fc 104
Rhyme 0:fbf1a72181fc 105 uint16_t VCNL4100::getPsConf12(void)
Rhyme 0:fbf1a72181fc 106 {
Rhyme 0:fbf1a72181fc 107 uint16_t psconf12 ;
Rhyme 0:fbf1a72181fc 108 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 109 readRegs(REG_PS_CONF1, data, 2) ;
Rhyme 0:fbf1a72181fc 110 psconf12 = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 111 return( psconf12 ) ;
Rhyme 0:fbf1a72181fc 112 }
Rhyme 0:fbf1a72181fc 113
Rhyme 0:fbf1a72181fc 114 void VCNL4100::setPsConf12(uint16_t conf12)
Rhyme 0:fbf1a72181fc 115 {
Rhyme 0:fbf1a72181fc 116 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 117 data[0] = REG_PS_CONF1 ;
Rhyme 0:fbf1a72181fc 118 data[1] = conf12 & 0xFF ;
Rhyme 0:fbf1a72181fc 119 data[2] = (conf12 >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 120 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 121 }
Rhyme 0:fbf1a72181fc 122
Rhyme 0:fbf1a72181fc 123 uint8_t VCNL4100::getPsConf1(void)
Rhyme 0:fbf1a72181fc 124 {
Rhyme 0:fbf1a72181fc 125 uint16_t tmp ;
Rhyme 0:fbf1a72181fc 126 uint8_t psconf1 ;
Rhyme 0:fbf1a72181fc 127 tmp = getPsConf12() ;
Rhyme 0:fbf1a72181fc 128 psconf1 = tmp & 0xFF ;
Rhyme 0:fbf1a72181fc 129 return( psconf1 ) ;
Rhyme 0:fbf1a72181fc 130 }
Rhyme 0:fbf1a72181fc 131
Rhyme 0:fbf1a72181fc 132 void VCNL4100::setPsConf1(uint8_t conf1)
Rhyme 0:fbf1a72181fc 133 {
Rhyme 0:fbf1a72181fc 134 uint16_t tmp ;
Rhyme 0:fbf1a72181fc 135 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 136 data[0] = REG_PS_CONF1 ;
Rhyme 0:fbf1a72181fc 137 tmp = getPsConf12() ;
Rhyme 0:fbf1a72181fc 138 data[1] = conf1 ;
Rhyme 0:fbf1a72181fc 139 data[2] = (tmp >> 8) & 0xFF ; // conf2
Rhyme 0:fbf1a72181fc 140 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 141 }
Rhyme 0:fbf1a72181fc 142
Rhyme 0:fbf1a72181fc 143 uint8_t VCNL4100::getPsConf2(void)
Rhyme 0:fbf1a72181fc 144 {
Rhyme 0:fbf1a72181fc 145 uint16_t tmp ;
Rhyme 0:fbf1a72181fc 146 uint8_t psconf2 ;
Rhyme 0:fbf1a72181fc 147 tmp = getPsConf12() ;
Rhyme 0:fbf1a72181fc 148 psconf2 = (tmp >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 149 return( psconf2 ) ;
Rhyme 0:fbf1a72181fc 150 }
Rhyme 0:fbf1a72181fc 151
Rhyme 0:fbf1a72181fc 152 void VCNL4100::setPsConf2(uint8_t conf2)
Rhyme 0:fbf1a72181fc 153 {
Rhyme 0:fbf1a72181fc 154 uint16_t tmp ;
Rhyme 0:fbf1a72181fc 155 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 156 data[0] = REG_PS_CONF1 ;
Rhyme 0:fbf1a72181fc 157 tmp = getPsConf12() ;
Rhyme 0:fbf1a72181fc 158 data[1] = tmp & 0xFF ;
Rhyme 0:fbf1a72181fc 159 data[2] = conf2 ;
Rhyme 0:fbf1a72181fc 160 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 161 }
Rhyme 0:fbf1a72181fc 162
Rhyme 0:fbf1a72181fc 163 uint16_t VCNL4100::getPsConf3Spo(void)
Rhyme 0:fbf1a72181fc 164 {
Rhyme 0:fbf1a72181fc 165 uint16_t conf3spo ;
Rhyme 0:fbf1a72181fc 166 uint8_t data[2] ; /* data[0] = PS_CONF3 ; data[1] = SPO ; */
Rhyme 0:fbf1a72181fc 167 readRegs(REG_PS_CONF3, data, 2) ;
Rhyme 0:fbf1a72181fc 168 conf3spo = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 169 return( conf3spo ) ;
Rhyme 0:fbf1a72181fc 170 }
Rhyme 0:fbf1a72181fc 171
Rhyme 0:fbf1a72181fc 172 void VCNL4100::setPsConf3Spo(uint16_t conf3spo)
Rhyme 0:fbf1a72181fc 173 {
Rhyme 0:fbf1a72181fc 174 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 175 data[0] = REG_PS_CONF3 ;
Rhyme 0:fbf1a72181fc 176 data[1] = conf3spo & 0xFF ; /* PS_CONF3 */
Rhyme 0:fbf1a72181fc 177 data[2] = (conf3spo >> 8) & 0xFF ; /* PS_SPO */
Rhyme 0:fbf1a72181fc 178 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 179 }
Rhyme 0:fbf1a72181fc 180
Rhyme 0:fbf1a72181fc 181 uint8_t VCNL4100::getPsConf3(void)
Rhyme 0:fbf1a72181fc 182 {
Rhyme 0:fbf1a72181fc 183 uint16_t conf3spo ;
Rhyme 0:fbf1a72181fc 184 uint8_t conf3 ;
Rhyme 0:fbf1a72181fc 185 conf3spo = getPsConf3Spo() ;
Rhyme 0:fbf1a72181fc 186 conf3 = conf3spo & 0xFF ;
Rhyme 0:fbf1a72181fc 187 return( conf3 ) ;
Rhyme 0:fbf1a72181fc 188 }
Rhyme 0:fbf1a72181fc 189
Rhyme 0:fbf1a72181fc 190 void VCNL4100::setPsConf3(uint8_t conf3)
Rhyme 0:fbf1a72181fc 191 {
Rhyme 0:fbf1a72181fc 192 uint16_t conf3spo ;
Rhyme 0:fbf1a72181fc 193 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 194 conf3spo = getPsConf3Spo() ;
Rhyme 0:fbf1a72181fc 195 data[0] = REG_PS_CONF3 ;
Rhyme 0:fbf1a72181fc 196 data[1] = conf3 ;
Rhyme 0:fbf1a72181fc 197 data[2] = (conf3spo >> 8) & 0xFF ; /* spo */
Rhyme 0:fbf1a72181fc 198 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 199 }
Rhyme 0:fbf1a72181fc 200
Rhyme 0:fbf1a72181fc 201 uint8_t VCNL4100::getSpo(void)
Rhyme 0:fbf1a72181fc 202 {
Rhyme 0:fbf1a72181fc 203 uint16_t conf3spo ;
Rhyme 0:fbf1a72181fc 204 uint8_t spo ;
Rhyme 0:fbf1a72181fc 205 conf3spo = getPsConf3Spo() ;
Rhyme 0:fbf1a72181fc 206 spo = (conf3spo >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 207 return( spo ) ;
Rhyme 0:fbf1a72181fc 208 }
Rhyme 0:fbf1a72181fc 209
Rhyme 0:fbf1a72181fc 210 void VCNL4100::setSpo(uint8_t spo)
Rhyme 0:fbf1a72181fc 211 {
Rhyme 0:fbf1a72181fc 212 uint16_t conf3spo ;
Rhyme 0:fbf1a72181fc 213 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 214 conf3spo = getPsConf3Spo() ;
Rhyme 0:fbf1a72181fc 215 data[0] = REG_PS_CONF3 ;
Rhyme 0:fbf1a72181fc 216 data[1] = conf3spo & 0xFF ; /* PS_CONF3 */
Rhyme 0:fbf1a72181fc 217 data[2] = spo ; /* PS_SPO */
Rhyme 0:fbf1a72181fc 218 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 219 }
Rhyme 0:fbf1a72181fc 220
Rhyme 0:fbf1a72181fc 221 uint16_t VCNL4100::getPsThd(void)
Rhyme 0:fbf1a72181fc 222 {
Rhyme 0:fbf1a72181fc 223 uint16_t psthd ;
Rhyme 0:fbf1a72181fc 224 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 225 readRegs(REG_PS_THDL, data, 2) ;
Rhyme 0:fbf1a72181fc 226 psthd = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 227 return( psthd ) ;
Rhyme 0:fbf1a72181fc 228 }
Rhyme 0:fbf1a72181fc 229
Rhyme 0:fbf1a72181fc 230 void VCNL4100::setPsThd(uint16_t psthd)
Rhyme 0:fbf1a72181fc 231 {
Rhyme 0:fbf1a72181fc 232 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 233 data[0] = REG_PS_THDL ;
Rhyme 0:fbf1a72181fc 234 data[1] = psthd & 0xFF ;
Rhyme 0:fbf1a72181fc 235 data[2] = (psthd >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 236 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 237 }
Rhyme 0:fbf1a72181fc 238
Rhyme 0:fbf1a72181fc 239 uint8_t VCNL4100::getPsThdl(void)
Rhyme 0:fbf1a72181fc 240 {
Rhyme 0:fbf1a72181fc 241 uint16_t thd16 ;
Rhyme 0:fbf1a72181fc 242 uint8_t thdl ;
Rhyme 0:fbf1a72181fc 243 thd16 = getPsThd() ;
Rhyme 0:fbf1a72181fc 244 thdl = thd16 & 0xFF ;
Rhyme 0:fbf1a72181fc 245 return( thdl ) ;
Rhyme 0:fbf1a72181fc 246 }
Rhyme 0:fbf1a72181fc 247
Rhyme 0:fbf1a72181fc 248 void VCNL4100::setPsThdl(uint8_t thdl)
Rhyme 0:fbf1a72181fc 249 {
Rhyme 0:fbf1a72181fc 250 uint16_t thd16 ;
Rhyme 0:fbf1a72181fc 251 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 252 thd16 = getPsThd() ;
Rhyme 0:fbf1a72181fc 253 data[0] = REG_PS_THDL ;
Rhyme 0:fbf1a72181fc 254 data[1] = thdl ;
Rhyme 0:fbf1a72181fc 255 data[2] = (thd16 >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 256 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 257 }
Rhyme 0:fbf1a72181fc 258
Rhyme 0:fbf1a72181fc 259 uint8_t VCNL4100::getPsThdh(void)
Rhyme 0:fbf1a72181fc 260 {
Rhyme 0:fbf1a72181fc 261 uint16_t thd16 ;
Rhyme 0:fbf1a72181fc 262 uint8_t thdh ;
Rhyme 0:fbf1a72181fc 263 thd16 = getPsThd() ;
Rhyme 0:fbf1a72181fc 264 thdh = (thd16 >> 8) & 0xFF ;
Rhyme 0:fbf1a72181fc 265 return( thdh ) ;
Rhyme 0:fbf1a72181fc 266 }
Rhyme 0:fbf1a72181fc 267
Rhyme 0:fbf1a72181fc 268 void VCNL4100::setPsThdh(uint8_t thdh)
Rhyme 0:fbf1a72181fc 269 {
Rhyme 0:fbf1a72181fc 270 uint16_t thd16 ;
Rhyme 0:fbf1a72181fc 271 uint8_t data[3] ;
Rhyme 0:fbf1a72181fc 272 thd16 = getPsThd() ;
Rhyme 0:fbf1a72181fc 273 data[0] = REG_PS_THDL ;
Rhyme 0:fbf1a72181fc 274 data[1] = thd16 & 0xFF ;
Rhyme 0:fbf1a72181fc 275 data[2] = thdh ;
Rhyme 0:fbf1a72181fc 276 writeRegs(data, 3) ;
Rhyme 0:fbf1a72181fc 277 }
Rhyme 0:fbf1a72181fc 278
Rhyme 0:fbf1a72181fc 279 uint8_t VCNL4100::getPsData(void)
Rhyme 0:fbf1a72181fc 280 {
Rhyme 0:fbf1a72181fc 281 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 282 readRegs(REG_PS_DATA, data, 2) ;
Rhyme 0:fbf1a72181fc 283 return(data[0]) ;
Rhyme 0:fbf1a72181fc 284 }
Rhyme 0:fbf1a72181fc 285
Rhyme 0:fbf1a72181fc 286 uint16_t VCNL4100::getAlsData(void)
Rhyme 0:fbf1a72181fc 287 {
Rhyme 0:fbf1a72181fc 288 uint16_t alsdata ;
Rhyme 0:fbf1a72181fc 289 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 290 readRegs(REG_ASL_DATA, data, 2) ;
Rhyme 0:fbf1a72181fc 291 alsdata = (data[1] << 8) | data[0] ;
Rhyme 0:fbf1a72181fc 292 return( alsdata ) ;
Rhyme 0:fbf1a72181fc 293 }
Rhyme 0:fbf1a72181fc 294
Rhyme 0:fbf1a72181fc 295 uint8_t VCNL4100::getIntFlag(void)
Rhyme 0:fbf1a72181fc 296 {
Rhyme 0:fbf1a72181fc 297 uint8_t data[2] ;
Rhyme 0:fbf1a72181fc 298 readRegs(REG_INT_FLAG, data, 2) ;
Rhyme 0:fbf1a72181fc 299 return( data[1] ) ;
Rhyme 0:fbf1a72181fc 300 }