Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: X_NUCLEO_COMMON ST_INTERFACES
Dependents: VL53L1CB_noshield_1sensor_polls_auton VL53L1CB_noshield_1sensor_interrupt_auton X_NUCLEO_53L1A2
X_NUCLEO_COMMON/DevI2C/DevI2C.h@0:3ac96e360672, 2020-11-06 (annotated)
- Committer:
- charlesmn
- Date:
- Fri Nov 06 10:06:37 2020 +0000
- Revision:
- 0:3ac96e360672
Library for ST Vl53L1A1 time of flight sensor.
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| charlesmn | 0:3ac96e360672 | 1 | /** | 
| charlesmn | 0:3ac96e360672 | 2 | ****************************************************************************** | 
| charlesmn | 0:3ac96e360672 | 3 | * @file DevI2C.h | 
| charlesmn | 0:3ac96e360672 | 4 | * @author AST / EST | 
| charlesmn | 0:3ac96e360672 | 5 | * @version V1.1.0 | 
| charlesmn | 0:3ac96e360672 | 6 | * @date 21-January-2016 | 
| charlesmn | 0:3ac96e360672 | 7 | * @brief Header file for a special I2C class DevI2C which provides some | 
| charlesmn | 0:3ac96e360672 | 8 | * helper function for on-board communication | 
| charlesmn | 0:3ac96e360672 | 9 | ****************************************************************************** | 
| charlesmn | 0:3ac96e360672 | 10 | * @attention | 
| charlesmn | 0:3ac96e360672 | 11 | * | 
| charlesmn | 0:3ac96e360672 | 12 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> | 
| charlesmn | 0:3ac96e360672 | 13 | * | 
| charlesmn | 0:3ac96e360672 | 14 | * Redistribution and use in source and binary forms, with or without modification, | 
| charlesmn | 0:3ac96e360672 | 15 | * are permitted provided that the following conditions are met: | 
| charlesmn | 0:3ac96e360672 | 16 | * 1. Redistributions of source code must retain the above copyright notice, | 
| charlesmn | 0:3ac96e360672 | 17 | * this list of conditions and the following disclaimer. | 
| charlesmn | 0:3ac96e360672 | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, | 
| charlesmn | 0:3ac96e360672 | 19 | * this list of conditions and the following disclaimer in the documentation | 
| charlesmn | 0:3ac96e360672 | 20 | * and/or other materials provided with the distribution. | 
| charlesmn | 0:3ac96e360672 | 21 | * 3. Neither the name of STMicroelectronics nor the names of its contributors | 
| charlesmn | 0:3ac96e360672 | 22 | * may be used to endorse or promote products derived from this software | 
| charlesmn | 0:3ac96e360672 | 23 | * without specific prior written permission. | 
| charlesmn | 0:3ac96e360672 | 24 | * | 
| charlesmn | 0:3ac96e360672 | 25 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | 
| charlesmn | 0:3ac96e360672 | 26 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 
| charlesmn | 0:3ac96e360672 | 27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | 
| charlesmn | 0:3ac96e360672 | 28 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | 
| charlesmn | 0:3ac96e360672 | 29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | 
| charlesmn | 0:3ac96e360672 | 30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | 
| charlesmn | 0:3ac96e360672 | 31 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | 
| charlesmn | 0:3ac96e360672 | 32 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | 
| charlesmn | 0:3ac96e360672 | 33 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 
| charlesmn | 0:3ac96e360672 | 34 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 
| charlesmn | 0:3ac96e360672 | 35 | * | 
| charlesmn | 0:3ac96e360672 | 36 | ****************************************************************************** | 
| charlesmn | 0:3ac96e360672 | 37 | */ | 
| charlesmn | 0:3ac96e360672 | 38 | |
| charlesmn | 0:3ac96e360672 | 39 | /* Define to prevent from recursive inclusion --------------------------------*/ | 
| charlesmn | 0:3ac96e360672 | 40 | #ifndef __DEV_I2C_H | 
| charlesmn | 0:3ac96e360672 | 41 | #define __DEV_I2C_H | 
| charlesmn | 0:3ac96e360672 | 42 | |
| charlesmn | 0:3ac96e360672 | 43 | /* Includes ------------------------------------------------------------------*/ | 
| charlesmn | 0:3ac96e360672 | 44 | #include "mbed.h" | 
| charlesmn | 0:3ac96e360672 | 45 | #include "pinmap.h" | 
| charlesmn | 0:3ac96e360672 | 46 | |
| charlesmn | 0:3ac96e360672 | 47 | /* Classes -------------------------------------------------------------------*/ | 
| charlesmn | 0:3ac96e360672 | 48 | /** Helper class DevI2C providing functions for multi-register I2C communication | 
| charlesmn | 0:3ac96e360672 | 49 | * common for a series of I2C devices | 
| charlesmn | 0:3ac96e360672 | 50 | */ | 
| charlesmn | 0:3ac96e360672 | 51 | class DevI2C : public I2C | 
| charlesmn | 0:3ac96e360672 | 52 | { | 
| charlesmn | 0:3ac96e360672 | 53 | public: | 
| charlesmn | 0:3ac96e360672 | 54 | /** Create a DevI2C Master interface, connected to the specified pins | 
| charlesmn | 0:3ac96e360672 | 55 | * | 
| charlesmn | 0:3ac96e360672 | 56 | * @param sda I2C data line pin | 
| charlesmn | 0:3ac96e360672 | 57 | * @param scl I2C clock line pin | 
| charlesmn | 0:3ac96e360672 | 58 | */ | 
| charlesmn | 0:3ac96e360672 | 59 | DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} | 
| charlesmn | 0:3ac96e360672 | 60 | |
| charlesmn | 0:3ac96e360672 | 61 | /** | 
| charlesmn | 0:3ac96e360672 | 62 | * @brief Writes a buffer towards the I2C peripheral device. | 
| charlesmn | 0:3ac96e360672 | 63 | * @param pBuffer pointer to the byte-array data to send | 
| charlesmn | 0:3ac96e360672 | 64 | * @param DeviceAddr specifies the peripheral device slave address. | 
| charlesmn | 0:3ac96e360672 | 65 | * @param RegisterAddr specifies the internal address register | 
| charlesmn | 0:3ac96e360672 | 66 | * where to start writing to (must be correctly masked). | 
| charlesmn | 0:3ac96e360672 | 67 | * @param NumByteToWrite number of bytes to be written. | 
| charlesmn | 0:3ac96e360672 | 68 | * @retval 0 if ok, | 
| charlesmn | 0:3ac96e360672 | 69 | * @retval -1 if an I2C error has occured, or | 
| charlesmn | 0:3ac96e360672 | 70 | * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) | 
| charlesmn | 0:3ac96e360672 | 71 | * @note On some devices if NumByteToWrite is greater | 
| charlesmn | 0:3ac96e360672 | 72 | * than one, the RegisterAddr must be masked correctly! | 
| charlesmn | 0:3ac96e360672 | 73 | */ | 
| charlesmn | 0:3ac96e360672 | 74 | int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, | 
| charlesmn | 0:3ac96e360672 | 75 | uint16_t NumByteToWrite) { | 
| charlesmn | 0:3ac96e360672 | 76 | int ret; | 
| charlesmn | 0:3ac96e360672 | 77 | uint8_t tmp[TEMP_BUF_SIZE]; | 
| charlesmn | 0:3ac96e360672 | 78 | |
| charlesmn | 0:3ac96e360672 | 79 | if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; | 
| charlesmn | 0:3ac96e360672 | 80 | |
| charlesmn | 0:3ac96e360672 | 81 | /* First, send device address. Then, send data and STOP condition */ | 
| charlesmn | 0:3ac96e360672 | 82 | tmp[0] = RegisterAddr; | 
| charlesmn | 0:3ac96e360672 | 83 | memcpy(tmp+1, pBuffer, NumByteToWrite); | 
| charlesmn | 0:3ac96e360672 | 84 | |
| charlesmn | 0:3ac96e360672 | 85 | ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); | 
| charlesmn | 0:3ac96e360672 | 86 | |
| charlesmn | 0:3ac96e360672 | 87 | if(ret) return -1; | 
| charlesmn | 0:3ac96e360672 | 88 | return 0; | 
| charlesmn | 0:3ac96e360672 | 89 | } | 
| charlesmn | 0:3ac96e360672 | 90 | |
| charlesmn | 0:3ac96e360672 | 91 | /** | 
| charlesmn | 0:3ac96e360672 | 92 | * @brief Reads a buffer from the I2C peripheral device. | 
| charlesmn | 0:3ac96e360672 | 93 | * @param pBuffer pointer to the byte-array to read data in to | 
| charlesmn | 0:3ac96e360672 | 94 | * @param DeviceAddr specifies the peripheral device slave address. | 
| charlesmn | 0:3ac96e360672 | 95 | * @param RegisterAddr specifies the internal address register | 
| charlesmn | 0:3ac96e360672 | 96 | * where to start reading from (must be correctly masked). | 
| charlesmn | 0:3ac96e360672 | 97 | * @param NumByteToRead number of bytes to be read. | 
| charlesmn | 0:3ac96e360672 | 98 | * @retval 0 if ok, | 
| charlesmn | 0:3ac96e360672 | 99 | * @retval -1 if an I2C error has occured | 
| charlesmn | 0:3ac96e360672 | 100 | * @note On some devices if NumByteToWrite is greater | 
| charlesmn | 0:3ac96e360672 | 101 | * than one, the RegisterAddr must be masked correctly! | 
| charlesmn | 0:3ac96e360672 | 102 | */ | 
| charlesmn | 0:3ac96e360672 | 103 | int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, | 
| charlesmn | 0:3ac96e360672 | 104 | uint16_t NumByteToRead) { | 
| charlesmn | 0:3ac96e360672 | 105 | int ret; | 
| charlesmn | 0:3ac96e360672 | 106 | |
| charlesmn | 0:3ac96e360672 | 107 | /* Send device address, with no STOP condition */ | 
| charlesmn | 0:3ac96e360672 | 108 | ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); | 
| charlesmn | 0:3ac96e360672 | 109 | if(!ret) { | 
| charlesmn | 0:3ac96e360672 | 110 | /* Read data, with STOP condition */ | 
| charlesmn | 0:3ac96e360672 | 111 | ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); | 
| charlesmn | 0:3ac96e360672 | 112 | } | 
| charlesmn | 0:3ac96e360672 | 113 | |
| charlesmn | 0:3ac96e360672 | 114 | if(ret) return -1; | 
| charlesmn | 0:3ac96e360672 | 115 | return 0; | 
| charlesmn | 0:3ac96e360672 | 116 | } | 
| charlesmn | 0:3ac96e360672 | 117 | |
| charlesmn | 0:3ac96e360672 | 118 | private: | 
| charlesmn | 0:3ac96e360672 | 119 | static const unsigned int TEMP_BUF_SIZE = 256; //~~ was 32 | 
| charlesmn | 0:3ac96e360672 | 120 | }; | 
| charlesmn | 0:3ac96e360672 | 121 | |
| charlesmn | 0:3ac96e360672 | 122 | #endif /* __DEV_I2C_H */ |