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
ToF_I2C.h
00001 /* Define to prevent from recursive inclusion --------------------------------*/ 00002 #ifndef __DEV_ToF_I2C_H 00003 #define __DEV_ToF_I2C_H 00004 00005 /* Includes ------------------------------------------------------------------*/ 00006 00007 #include "mbed.h" 00008 00009 #include "pinmap.h" 00010 00011 //Class replacing DevI2C class as it was not implementing a 16bit address registers 00012 class ToF_DevI2C : public I2C 00013 { 00014 public: 00015 /** Create a DevI2C Master interface, connected to the specified pins 00016 * 00017 * @param sda I2C data line pin 00018 * @param scl I2C clock line pin 00019 */ 00020 ToF_DevI2C(PinName sda, PinName scl) : I2C(sda, scl) { printf ("hello\n");} 00021 00022 /** 00023 * @brief Writes a buffer towards the I2C peripheral device. 00024 * @param pBuffer pointer to the byte-array data to send 00025 * @param DeviceAddr specifies the peripheral device slave address. 00026 * @param RegisterAddr specifies the internal address register 00027 * where to start writing to (must be correctly masked). 00028 * @param NumByteToWrite number of bytes to be written. 00029 * @retval 0 if ok, 00030 * @retval -1 if an I2C error has occured, or 00031 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) 00032 * @note On some devices if NumByteToWrite is greater 00033 * than one, the RegisterAddr must be masked correctly! 00034 */ 00035 int ToF_i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, 00036 uint16_t NumByteToWrite) { 00037 int ret; 00038 uint8_t tmp[TEMP_BUF_SIZE]; 00039 00040 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; 00041 00042 // First, send device address. Then, send data and STOP condition 00043 tmp[0] = RegisterAddr >> 8; 00044 tmp[1] = RegisterAddr & 0x0FF; 00045 memcpy(tmp+2, pBuffer, NumByteToWrite); 00046 00047 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+2, false); 00048 00049 if(ret) return -1; 00050 return 0; 00051 } 00052 00053 /** 00054 * @brief Reads a buffer from the I2C peripheral device. 00055 * @param pBuffer pointer to the byte-array to read data in to 00056 * @param DeviceAddr specifies the peripheral device slave address. 00057 * @param RegisterAddr specifies the internal address register 00058 * where to start reading from (must be correctly masked). 00059 * @param NumByteToRead number of bytes to be read. 00060 * @retval 0 if ok, 00061 * @retval -1 if an I2C error has occured 00062 * @note On some devices if NumByteToWrite is greater 00063 * than one, the RegisterAddr must be masked correctly! 00064 */ 00065 int ToF_i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint16_t RegisterAddr, 00066 uint16_t NumByteToRead) { 00067 int ret; 00068 uint8_t ExpanderData[2]; 00069 ExpanderData[0] = RegisterAddr >> 8; 00070 ExpanderData[1] = RegisterAddr & 0x0FF; 00071 /* Send device address, with no STOP condition */ 00072 ret = write(DeviceAddr, (const char*)ExpanderData, 2, true); 00073 if(!ret) { 00074 /* Read data, with STOP condition */ 00075 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); 00076 } 00077 00078 if(ret) return -1; 00079 return 0; 00080 } 00081 00082 private: 00083 static const unsigned int TEMP_BUF_SIZE = 256; 00084 }; 00085 00086 #endif /* __DEV_ToF_I2C_H */
Generated on Thu Jul 14 2022 10:14:48 by
1.7.2