Line Health / VL6180
Committer:
mwilkens241
Date:
Fri Mar 31 15:49:47 2017 +0000
Revision:
2:40f590e28101
Parent:
0:15e49005d54e
stole some things out of here, not much changed

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mwilkens241 0:15e49005d54e 1 /**
mwilkens241 0:15e49005d54e 2 ******************************************************************************
mwilkens241 0:15e49005d54e 3 * @file DevI2C.h
mwilkens241 0:15e49005d54e 4 * @author AST / EST
mwilkens241 0:15e49005d54e 5 * @version V1.1.0
mwilkens241 0:15e49005d54e 6 * @date 21-January-2016
mwilkens241 0:15e49005d54e 7 * @brief Header file for a special I2C class DevI2C which provides some
mwilkens241 0:15e49005d54e 8 * helper function for on-board communication
mwilkens241 0:15e49005d54e 9 ******************************************************************************
mwilkens241 0:15e49005d54e 10 * @attention
mwilkens241 0:15e49005d54e 11 *
mwilkens241 0:15e49005d54e 12 * <h2><center>&copy; COPYRIGHT(c) 2016 STMicroelectronics</center></h2>
mwilkens241 0:15e49005d54e 13 *
mwilkens241 0:15e49005d54e 14 * Redistribution and use in source and binary forms, with or without modification,
mwilkens241 0:15e49005d54e 15 * are permitted provided that the following conditions are met:
mwilkens241 0:15e49005d54e 16 * 1. Redistributions of source code must retain the above copyright notice,
mwilkens241 0:15e49005d54e 17 * this list of conditions and the following disclaimer.
mwilkens241 0:15e49005d54e 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
mwilkens241 0:15e49005d54e 19 * this list of conditions and the following disclaimer in the documentation
mwilkens241 0:15e49005d54e 20 * and/or other materials provided with the distribution.
mwilkens241 0:15e49005d54e 21 * 3. Neither the name of STMicroelectronics nor the names of its contributors
mwilkens241 0:15e49005d54e 22 * may be used to endorse or promote products derived from this software
mwilkens241 0:15e49005d54e 23 * without specific prior written permission.
mwilkens241 0:15e49005d54e 24 *
mwilkens241 0:15e49005d54e 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
mwilkens241 0:15e49005d54e 26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
mwilkens241 0:15e49005d54e 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
mwilkens241 0:15e49005d54e 28 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
mwilkens241 0:15e49005d54e 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
mwilkens241 0:15e49005d54e 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
mwilkens241 0:15e49005d54e 31 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
mwilkens241 0:15e49005d54e 32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
mwilkens241 0:15e49005d54e 33 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
mwilkens241 0:15e49005d54e 34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
mwilkens241 0:15e49005d54e 35 *
mwilkens241 0:15e49005d54e 36 ******************************************************************************
mwilkens241 0:15e49005d54e 37 */
mwilkens241 0:15e49005d54e 38
mwilkens241 0:15e49005d54e 39 /* Define to prevent from recursive inclusion --------------------------------*/
mwilkens241 0:15e49005d54e 40 #ifndef __DEV_I2C_H
mwilkens241 0:15e49005d54e 41 #define __DEV_I2C_H
mwilkens241 0:15e49005d54e 42
mwilkens241 0:15e49005d54e 43 /* Includes ------------------------------------------------------------------*/
mwilkens241 0:15e49005d54e 44 #include "mbed.h"
mwilkens241 0:15e49005d54e 45 #include "pinmap.h"
mwilkens241 0:15e49005d54e 46
mwilkens241 0:15e49005d54e 47 /* Classes -------------------------------------------------------------------*/
mwilkens241 0:15e49005d54e 48 /** Helper class DevI2C providing functions for multi-register I2C communication
mwilkens241 0:15e49005d54e 49 * common for a series of I2C devices
mwilkens241 0:15e49005d54e 50 */
mwilkens241 0:15e49005d54e 51 class DevI2C : public I2C
mwilkens241 0:15e49005d54e 52 {
mwilkens241 0:15e49005d54e 53 public:
mwilkens241 0:15e49005d54e 54 /** Create a DevI2C Master interface, connected to the specified pins
mwilkens241 0:15e49005d54e 55 *
mwilkens241 0:15e49005d54e 56 * @param sda I2C data line pin
mwilkens241 0:15e49005d54e 57 * @param scl I2C clock line pin
mwilkens241 0:15e49005d54e 58 */
mwilkens241 0:15e49005d54e 59 DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
mwilkens241 0:15e49005d54e 60
mwilkens241 0:15e49005d54e 61 /** Create a DevI2C Master interface, connected to the specified pins and set their pin modes
mwilkens241 0:15e49005d54e 62 *
mwilkens241 0:15e49005d54e 63 * @param sda I2C data line pin
mwilkens241 0:15e49005d54e 64 * @param sda I2C data pin mode
mwilkens241 0:15e49005d54e 65 * @param scl I2C clock line pin
mwilkens241 0:15e49005d54e 66 * @param scl I2C clock pin mode
mwilkens241 0:15e49005d54e 67 *
mwilkens241 0:15e49005d54e 68 * @note this is a workaround to provide a constructor which currently
mwilkens241 0:15e49005d54e 69 * is somehow missing in the I2C base class and it's underlying
mwilkens241 0:15e49005d54e 70 * implementations. In some circumstances (e.g. while debugging)
mwilkens241 0:15e49005d54e 71 * where long latencies between the initialization of the i2c
mwilkens241 0:15e49005d54e 72 * interface in the I2C constructor and the setting of the pin
mwilkens241 0:15e49005d54e 73 * modes in the beyond constructor might occur, the i2c
mwilkens241 0:15e49005d54e 74 * communication might be compromised.
mwilkens241 0:15e49005d54e 75 */
mwilkens241 0:15e49005d54e 76 DevI2C(PinName sda, int mode_sda, PinName scl, int mode_scl) : I2C(sda, scl) {
mwilkens241 0:15e49005d54e 77 pin_mode(sda, (PinMode)mode_sda);
mwilkens241 0:15e49005d54e 78 pin_mode(scl, (PinMode)mode_scl);
mwilkens241 0:15e49005d54e 79 }
mwilkens241 0:15e49005d54e 80
mwilkens241 0:15e49005d54e 81 /**
mwilkens241 0:15e49005d54e 82 * @brief Writes a buffer towards the I2C peripheral device.
mwilkens241 0:15e49005d54e 83 * @param pBuffer pointer to the byte-array data to send
mwilkens241 0:15e49005d54e 84 * @param DeviceAddr specifies the peripheral device slave address.
mwilkens241 0:15e49005d54e 85 * @param RegisterAddr specifies the internal address register
mwilkens241 0:15e49005d54e 86 * where to start writing to (must be correctly masked).
mwilkens241 0:15e49005d54e 87 * @param NumByteToWrite number of bytes to be written.
mwilkens241 0:15e49005d54e 88 * @retval 0 if ok,
mwilkens241 0:15e49005d54e 89 * @retval -1 if an I2C error has occured, or
mwilkens241 0:15e49005d54e 90 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
mwilkens241 0:15e49005d54e 91 * @note On some devices if NumByteToWrite is greater
mwilkens241 0:15e49005d54e 92 * than one, the RegisterAddr must be masked correctly!
mwilkens241 0:15e49005d54e 93 */
mwilkens241 0:15e49005d54e 94 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
mwilkens241 0:15e49005d54e 95 uint16_t NumByteToWrite) {
mwilkens241 0:15e49005d54e 96 int ret;
mwilkens241 0:15e49005d54e 97 uint8_t tmp[TEMP_BUF_SIZE];
mwilkens241 0:15e49005d54e 98
mwilkens241 0:15e49005d54e 99 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
mwilkens241 0:15e49005d54e 100
mwilkens241 0:15e49005d54e 101 /* First, send device address. Then, send data and STOP condition */
mwilkens241 0:15e49005d54e 102 tmp[0] = RegisterAddr;
mwilkens241 0:15e49005d54e 103 memcpy(tmp+1, pBuffer, NumByteToWrite);
mwilkens241 0:15e49005d54e 104
mwilkens241 0:15e49005d54e 105 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
mwilkens241 0:15e49005d54e 106
mwilkens241 0:15e49005d54e 107 if(ret) return -1;
mwilkens241 0:15e49005d54e 108 return 0;
mwilkens241 0:15e49005d54e 109 }
mwilkens241 0:15e49005d54e 110
mwilkens241 0:15e49005d54e 111 /**
mwilkens241 0:15e49005d54e 112 * @brief Reads a buffer from the I2C peripheral device.
mwilkens241 0:15e49005d54e 113 * @param pBuffer pointer to the byte-array to read data in to
mwilkens241 0:15e49005d54e 114 * @param DaviceAddr specifies the peripheral device slave address.
mwilkens241 0:15e49005d54e 115 * @param RegisterAddr specifies the internal address register
mwilkens241 0:15e49005d54e 116 * where to start reading from (must be correctly masked).
mwilkens241 0:15e49005d54e 117 * @param NumByteToRead number of bytes to be read.
mwilkens241 0:15e49005d54e 118 * @retval 0 if ok,
mwilkens241 0:15e49005d54e 119 * @retval -1 if an I2C error has occured
mwilkens241 0:15e49005d54e 120 * @note On some devices if NumByteToWrite is greater
mwilkens241 0:15e49005d54e 121 * than one, the RegisterAddr must be masked correctly!
mwilkens241 0:15e49005d54e 122 */
mwilkens241 0:15e49005d54e 123 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
mwilkens241 0:15e49005d54e 124 uint16_t NumByteToRead) {
mwilkens241 0:15e49005d54e 125 int ret;
mwilkens241 0:15e49005d54e 126
mwilkens241 0:15e49005d54e 127 /* Send device address, with no STOP condition */
mwilkens241 0:15e49005d54e 128 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
mwilkens241 0:15e49005d54e 129 if(!ret) {
mwilkens241 0:15e49005d54e 130 /* Read data, with STOP condition */
mwilkens241 0:15e49005d54e 131 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
mwilkens241 0:15e49005d54e 132 }
mwilkens241 0:15e49005d54e 133
mwilkens241 0:15e49005d54e 134 if(ret) return -1;
mwilkens241 0:15e49005d54e 135 return 0;
mwilkens241 0:15e49005d54e 136 }
mwilkens241 0:15e49005d54e 137
mwilkens241 0:15e49005d54e 138 private:
mwilkens241 0:15e49005d54e 139 static const unsigned int TEMP_BUF_SIZE = 32;
mwilkens241 0:15e49005d54e 140 };
mwilkens241 0:15e49005d54e 141
mwilkens241 0:15e49005d54e 142 #endif /* __DEV_I2C_H */