Library for the MAX44000 Ambient Light Sensor / Proximity Detector
Dependents: LED_Demo LED_Demo2 LED_Demo
Fork of BMP180 by
MAX44000 Device Driver
MAX44000.cpp@4:a9f09252653a, 2016-09-21 (annotated)
- Committer:
- switches
- Date:
- Wed Sep 21 12:00:34 2016 +0000
- Revision:
- 4:a9f09252653a
- Parent:
- 3:1d508b290354
Set TRIM bit in init function to use factory calibration data.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kgills |
0:b2219e6e444b | 1 | /******************************************************************************* |
switches | 1:1a770989adcb | 2 | * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. |
kgills |
0:b2219e6e444b | 3 | * |
kgills |
0:b2219e6e444b | 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
kgills |
0:b2219e6e444b | 5 | * copy of this software and associated documentation files (the "Software"), |
kgills |
0:b2219e6e444b | 6 | * to deal in the Software without restriction, including without limitation |
kgills |
0:b2219e6e444b | 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
kgills |
0:b2219e6e444b | 8 | * and/or sell copies of the Software, and to permit persons to whom the |
kgills |
0:b2219e6e444b | 9 | * Software is furnished to do so, subject to the following conditions: |
kgills |
0:b2219e6e444b | 10 | * |
kgills |
0:b2219e6e444b | 11 | * The above copyright notice and this permission notice shall be included |
kgills |
0:b2219e6e444b | 12 | * in all copies or substantial portions of the Software. |
kgills |
0:b2219e6e444b | 13 | * |
kgills |
0:b2219e6e444b | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
kgills |
0:b2219e6e444b | 15 | * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
kgills |
0:b2219e6e444b | 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
kgills |
0:b2219e6e444b | 17 | * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES |
kgills |
0:b2219e6e444b | 18 | * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
kgills |
0:b2219e6e444b | 19 | * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
kgills |
0:b2219e6e444b | 20 | * OTHER DEALINGS IN THE SOFTWARE. |
kgills |
0:b2219e6e444b | 21 | * |
kgills |
0:b2219e6e444b | 22 | * Except as contained in this notice, the name of Maxim Integrated |
kgills |
0:b2219e6e444b | 23 | * Products, Inc. shall not be used except as stated in the Maxim Integrated |
kgills |
0:b2219e6e444b | 24 | * Products, Inc. Branding Policy. |
kgills |
0:b2219e6e444b | 25 | * |
kgills |
0:b2219e6e444b | 26 | * The mere transfer of this software does not imply any licenses |
kgills |
0:b2219e6e444b | 27 | * of trade secrets, proprietary technology, copyrights, patents, |
kgills |
0:b2219e6e444b | 28 | * trademarks, maskwork rights, or any other form of intellectual |
kgills |
0:b2219e6e444b | 29 | * property whatsoever. Maxim Integrated Products, Inc. retains all |
kgills |
0:b2219e6e444b | 30 | * ownership rights. |
kgills |
0:b2219e6e444b | 31 | ******************************************************************************* |
kgills |
0:b2219e6e444b | 32 | */ |
kgills |
0:b2219e6e444b | 33 | |
switches | 1:1a770989adcb | 34 | #include "MAX44000.h" |
kgills |
0:b2219e6e444b | 35 | |
kgills |
0:b2219e6e444b | 36 | /***** Definitions *****/ |
switches | 1:1a770989adcb | 37 | #define I2C_ADDR (0x94) // 1001_010x |
kgills |
0:b2219e6e444b | 38 | |
kgills |
0:b2219e6e444b | 39 | //****************************************************************************** |
switches | 1:1a770989adcb | 40 | MAX44000::MAX44000(PinName sda, PinName scl) |
kgills |
0:b2219e6e444b | 41 | { |
kgills |
0:b2219e6e444b | 42 | i2c_ = new I2C(sda, scl); |
kgills |
0:b2219e6e444b | 43 | i2c_owner = true; |
kgills |
0:b2219e6e444b | 44 | |
kgills |
0:b2219e6e444b | 45 | i2c_->frequency(400000); |
kgills |
0:b2219e6e444b | 46 | } |
kgills |
0:b2219e6e444b | 47 | |
kgills |
0:b2219e6e444b | 48 | //****************************************************************************** |
switches | 1:1a770989adcb | 49 | MAX44000::MAX44000(I2C *i2c) : |
kgills |
0:b2219e6e444b | 50 | i2c_(i2c) |
kgills |
0:b2219e6e444b | 51 | { |
kgills |
0:b2219e6e444b | 52 | i2c_owner = false; |
kgills |
0:b2219e6e444b | 53 | } |
kgills |
0:b2219e6e444b | 54 | |
kgills |
0:b2219e6e444b | 55 | //****************************************************************************** |
switches | 1:1a770989adcb | 56 | MAX44000::~MAX44000() |
kgills |
0:b2219e6e444b | 57 | { |
kgills |
0:b2219e6e444b | 58 | if(i2c_owner) { |
kgills |
0:b2219e6e444b | 59 | delete i2c_; |
kgills |
0:b2219e6e444b | 60 | } |
kgills |
0:b2219e6e444b | 61 | } |
kgills |
0:b2219e6e444b | 62 | |
kgills |
0:b2219e6e444b | 63 | //****************************************************************************** |
switches | 1:1a770989adcb | 64 | int MAX44000::init(MAX44000::modes_t mode, MAX44000::alstim_t alstim, MAX44000::alspga_t alspga, MAX44000::drive_t drive) |
kgills |
0:b2219e6e444b | 65 | { |
switches | 1:1a770989adcb | 66 | char data[2]; |
kgills |
0:b2219e6e444b | 67 | |
switches | 1:1a770989adcb | 68 | data[0] = REG_RX_CONFIG; |
switches | 1:1a770989adcb | 69 | data[1] = 0xF0 | ((alstim & 0x03)<<2) | (alspga & 0x03); |
switches | 1:1a770989adcb | 70 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
kgills |
0:b2219e6e444b | 71 | return -1; |
kgills |
0:b2219e6e444b | 72 | } |
kgills |
0:b2219e6e444b | 73 | |
switches | 1:1a770989adcb | 74 | data[0] = REG_TX_CONFIG; |
switches | 1:1a770989adcb | 75 | data[1] = (drive & 0x0F); |
switches | 1:1a770989adcb | 76 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
kgills |
0:b2219e6e444b | 77 | return -1; |
kgills |
0:b2219e6e444b | 78 | } |
kgills |
0:b2219e6e444b | 79 | |
switches | 1:1a770989adcb | 80 | data[0] = REG_MAIN_CONFIG; |
switches | 4:a9f09252653a | 81 | data[1] = ((mode & 0x07)<<2) | (1<<5); // Set bit 5 to use factory trim data |
kgills |
0:b2219e6e444b | 82 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
kgills |
0:b2219e6e444b | 83 | return -1; |
kgills |
0:b2219e6e444b | 84 | } |
kgills |
0:b2219e6e444b | 85 | |
kgills |
0:b2219e6e444b | 86 | return 0; |
kgills |
0:b2219e6e444b | 87 | } |
kgills |
0:b2219e6e444b | 88 | |
kgills |
0:b2219e6e444b | 89 | //****************************************************************************** |
switches | 1:1a770989adcb | 90 | int MAX44000::writeReg(MAX44000::registers_t reg_addr, char reg_data) |
kgills |
0:b2219e6e444b | 91 | { |
switches | 1:1a770989adcb | 92 | char data[2]; |
kgills |
0:b2219e6e444b | 93 | |
switches | 1:1a770989adcb | 94 | data[0] = reg_addr; |
switches | 1:1a770989adcb | 95 | data[1] = reg_data; |
kgills |
0:b2219e6e444b | 96 | if (i2c_->write(I2C_ADDR, data, 2) != 0) { |
kgills |
0:b2219e6e444b | 97 | return -1; |
kgills |
0:b2219e6e444b | 98 | } |
kgills |
0:b2219e6e444b | 99 | |
kgills |
0:b2219e6e444b | 100 | return 0; |
kgills |
0:b2219e6e444b | 101 | } |
kgills |
0:b2219e6e444b | 102 | |
kgills |
0:b2219e6e444b | 103 | //****************************************************************************** |
switches | 1:1a770989adcb | 104 | int MAX44000::readReg(MAX44000::registers_t reg_addr) |
kgills |
0:b2219e6e444b | 105 | { |
switches | 1:1a770989adcb | 106 | char data; |
kgills |
0:b2219e6e444b | 107 | |
switches | 1:1a770989adcb | 108 | data = reg_addr; |
switches | 1:1a770989adcb | 109 | if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { |
kgills |
0:b2219e6e444b | 110 | return -1; |
kgills |
0:b2219e6e444b | 111 | } |
kgills |
0:b2219e6e444b | 112 | |
switches | 1:1a770989adcb | 113 | if (i2c_->read(I2C_ADDR, &data, 1) != 0) { |
kgills |
0:b2219e6e444b | 114 | return -1; |
kgills |
0:b2219e6e444b | 115 | } |
kgills |
0:b2219e6e444b | 116 | |
switches | 1:1a770989adcb | 117 | return (0x0 + data); |
kgills |
0:b2219e6e444b | 118 | } |
kgills |
0:b2219e6e444b | 119 | |
kgills |
0:b2219e6e444b | 120 | //****************************************************************************** |
switches | 1:1a770989adcb | 121 | int MAX44000::readALS(void) |
kgills |
0:b2219e6e444b | 122 | { |
switches | 1:1a770989adcb | 123 | char data; |
switches | 1:1a770989adcb | 124 | int alsData; |
kgills |
0:b2219e6e444b | 125 | |
switches | 1:1a770989adcb | 126 | data = REG_ALS_DATA_HIGH; |
switches | 1:1a770989adcb | 127 | if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { |
switches | 1:1a770989adcb | 128 | return -1; |
switches | 1:1a770989adcb | 129 | } |
switches | 1:1a770989adcb | 130 | |
switches | 1:1a770989adcb | 131 | if (i2c_->read(I2C_ADDR, &data, 1, true) != 0) { // must use repeated start to protect low byte data |
kgills |
0:b2219e6e444b | 132 | return -1; |
kgills |
0:b2219e6e444b | 133 | } |
kgills |
0:b2219e6e444b | 134 | |
switches | 1:1a770989adcb | 135 | if (data & 0x40) { // if the overflow bit is set |
switches | 1:1a770989adcb | 136 | return -1; |
switches | 1:1a770989adcb | 137 | } |
switches | 2:91f97c274e89 | 138 | |
switches | 1:1a770989adcb | 139 | alsData = (data << 8); |
switches | 1:1a770989adcb | 140 | data = REG_ALS_DATA_LOW; |
switches | 1:1a770989adcb | 141 | if (i2c_->write(I2C_ADDR, &data, 1, true) != 0) { |
kgills |
0:b2219e6e444b | 142 | return -1; |
kgills |
0:b2219e6e444b | 143 | } |
kgills |
0:b2219e6e444b | 144 | |
switches | 1:1a770989adcb | 145 | if (i2c_->read(I2C_ADDR, &data, 1) != 0) { |
switches | 1:1a770989adcb | 146 | return -1; |
switches | 1:1a770989adcb | 147 | } |
kgills |
0:b2219e6e444b | 148 | |
switches | 1:1a770989adcb | 149 | alsData += data; |
switches | 1:1a770989adcb | 150 | return alsData; |
kgills |
0:b2219e6e444b | 151 | } |