Initial release.

Dependencies:   X_NUCLEO_COMMON ST_INTERFACES

Dependents:   X_NUCLEO_53L1A1_mbed X_NUCLEO_53L1A1_mbed VL53L1X_Ranging_With_Standalone_Satellite_MbedOS X_NUCLEO_53L1A1

Committer:
johnAlexander
Date:
Wed Jul 24 10:36:51 2019 +0000
Revision:
7:6d3ab15363a2
Updated for mbed coding style guidelines.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
johnAlexander 7:6d3ab15363a2 1 /* Define to prevent from recursive inclusion --------------------------------*/
johnAlexander 7:6d3ab15363a2 2 #ifndef __DEV_53L1X_I2C_H
johnAlexander 7:6d3ab15363a2 3 #define __DEV_53L1X_I2C_H
johnAlexander 7:6d3ab15363a2 4
johnAlexander 7:6d3ab15363a2 5 /* Includes ------------------------------------------------------------------*/
johnAlexander 7:6d3ab15363a2 6 #include "mbed.h"
johnAlexander 7:6d3ab15363a2 7 #include "pinmap.h"
johnAlexander 7:6d3ab15363a2 8
johnAlexander 7:6d3ab15363a2 9 //Class replacing DevI2C class as it was not implementing a 16bit address registers
johnAlexander 7:6d3ab15363a2 10 class VL53L1X_DevI2C : public I2C
johnAlexander 7:6d3ab15363a2 11 {
johnAlexander 7:6d3ab15363a2 12 public:
johnAlexander 7:6d3ab15363a2 13 /** Create a DevI2C Master interface, connected to the specified pins
johnAlexander 7:6d3ab15363a2 14 *
johnAlexander 7:6d3ab15363a2 15 * @param sda I2C data line pin
johnAlexander 7:6d3ab15363a2 16 * @param scl I2C clock line pin
johnAlexander 7:6d3ab15363a2 17 */
johnAlexander 7:6d3ab15363a2 18 VL53L1X_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
johnAlexander 7:6d3ab15363a2 19
johnAlexander 7:6d3ab15363a2 20 /**
johnAlexander 7:6d3ab15363a2 21 * @brief Writes a buffer towards the I2C peripheral device.
johnAlexander 7:6d3ab15363a2 22 * @param pBuffer pointer to the byte-array data to send
johnAlexander 7:6d3ab15363a2 23 * @param DeviceAddr specifies the peripheral device slave address.
johnAlexander 7:6d3ab15363a2 24 * @param RegisterAddr specifies the internal address register
johnAlexander 7:6d3ab15363a2 25 * where to start writing to (must be correctly masked).
johnAlexander 7:6d3ab15363a2 26 * @param NumByteToWrite number of bytes to be written.
johnAlexander 7:6d3ab15363a2 27 * @retval 0 if ok,
johnAlexander 7:6d3ab15363a2 28 * @retval -1 if an I2C error has occured, or
johnAlexander 7:6d3ab15363a2 29 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
johnAlexander 7:6d3ab15363a2 30 * @note On some devices if NumByteToWrite is greater
johnAlexander 7:6d3ab15363a2 31 * than one, the RegisterAddr must be masked correctly!
johnAlexander 7:6d3ab15363a2 32 */
johnAlexander 7:6d3ab15363a2 33 int v53l1x_i2c_write(uint8_t *pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
johnAlexander 7:6d3ab15363a2 34 uint16_t NumByteToWrite)
johnAlexander 7:6d3ab15363a2 35 {
johnAlexander 7:6d3ab15363a2 36 int ret;
johnAlexander 7:6d3ab15363a2 37 uint8_t tmp[TEMP_BUF_SIZE];
johnAlexander 7:6d3ab15363a2 38
johnAlexander 7:6d3ab15363a2 39 if (NumByteToWrite >= TEMP_BUF_SIZE)
johnAlexander 7:6d3ab15363a2 40 return -2;
johnAlexander 7:6d3ab15363a2 41
johnAlexander 7:6d3ab15363a2 42 /* First, send device address. Then, send data and STOP condition */
johnAlexander 7:6d3ab15363a2 43 tmp[0] = RegisterAddr >> 8;
johnAlexander 7:6d3ab15363a2 44 tmp[1] = RegisterAddr & 0x0FF;
johnAlexander 7:6d3ab15363a2 45 memcpy(tmp + 2, pBuffer, NumByteToWrite);
johnAlexander 7:6d3ab15363a2 46
johnAlexander 7:6d3ab15363a2 47 ret = write(DeviceAddr, (const char *)tmp, NumByteToWrite + 2, false);
johnAlexander 7:6d3ab15363a2 48
johnAlexander 7:6d3ab15363a2 49 if (ret)
johnAlexander 7:6d3ab15363a2 50 return -1;
johnAlexander 7:6d3ab15363a2 51 return 0;
johnAlexander 7:6d3ab15363a2 52 }
johnAlexander 7:6d3ab15363a2 53
johnAlexander 7:6d3ab15363a2 54 /**
johnAlexander 7:6d3ab15363a2 55 * @brief Reads a buffer from the I2C peripheral device.
johnAlexander 7:6d3ab15363a2 56 * @param pBuffer pointer to the byte-array to read data in to
johnAlexander 7:6d3ab15363a2 57 * @param DeviceAddr specifies the peripheral device slave address.
johnAlexander 7:6d3ab15363a2 58 * @param RegisterAddr specifies the internal address register
johnAlexander 7:6d3ab15363a2 59 * where to start reading from (must be correctly masked).
johnAlexander 7:6d3ab15363a2 60 * @param NumByteToRead number of bytes to be read.
johnAlexander 7:6d3ab15363a2 61 * @retval 0 if ok,
johnAlexander 7:6d3ab15363a2 62 * @retval -1 if an I2C error has occured
johnAlexander 7:6d3ab15363a2 63 * @note On some devices if NumByteToWrite is greater
johnAlexander 7:6d3ab15363a2 64 * than one, the RegisterAddr must be masked correctly!
johnAlexander 7:6d3ab15363a2 65 */
johnAlexander 7:6d3ab15363a2 66 int v53l1x_i2c_read(uint8_t *pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr,
johnAlexander 7:6d3ab15363a2 67 uint16_t NumByteToRead)
johnAlexander 7:6d3ab15363a2 68 {
johnAlexander 7:6d3ab15363a2 69 int ret;
johnAlexander 7:6d3ab15363a2 70 uint8_t ExpanderData[2];
johnAlexander 7:6d3ab15363a2 71 ExpanderData[0] = RegisterAddr >> 8;
johnAlexander 7:6d3ab15363a2 72 ExpanderData[1] = RegisterAddr & 0x0FF;
johnAlexander 7:6d3ab15363a2 73 /* Send device address, with no STOP condition */
johnAlexander 7:6d3ab15363a2 74 ret = write(DeviceAddr, (const char *)ExpanderData, 2, true);
johnAlexander 7:6d3ab15363a2 75 if (!ret) {
johnAlexander 7:6d3ab15363a2 76 /* Read data, with STOP condition */
johnAlexander 7:6d3ab15363a2 77 ret = read(DeviceAddr, (char *)pBuffer, NumByteToRead, false);
johnAlexander 7:6d3ab15363a2 78 }
johnAlexander 7:6d3ab15363a2 79
johnAlexander 7:6d3ab15363a2 80 if (ret)
johnAlexander 7:6d3ab15363a2 81 return -1;
johnAlexander 7:6d3ab15363a2 82 return 0;
johnAlexander 7:6d3ab15363a2 83 }
johnAlexander 7:6d3ab15363a2 84
johnAlexander 7:6d3ab15363a2 85 private:
johnAlexander 7:6d3ab15363a2 86 static const unsigned int TEMP_BUF_SIZE = 32;
johnAlexander 7:6d3ab15363a2 87 };
johnAlexander 7:6d3ab15363a2 88
johnAlexander 7:6d3ab15363a2 89 #endif /* __DEV_53L1X_I2C_H */