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.
Fork of X_NUCLEO_COMMON by
Diff: DevI2C/DevI2C.h
- Revision:
- 19:12be3dfc15fd
- Parent:
- 8:527e203b709b
- Child:
- 20:7ae143567941
--- a/DevI2C/DevI2C.h Wed Jun 08 11:57:03 2016 +0200 +++ b/DevI2C/DevI2C.h Mon Oct 10 06:55:02 2016 +0000 @@ -2,14 +2,14 @@ ****************************************************************************** * @file DevI2C.h * @author AST / EST - * @version V0.0.1 - * @date 21-January-2015 + * @version V1.1.0 + * @date 21-January-2016 * @brief Header file for a special I2C class DevI2C which provides some * helper function for on-board communication ****************************************************************************** * @attention * - * <h2><center>© COPYRIGHT(c) 2014 STMicroelectronics</center></h2> + * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: @@ -42,6 +42,7 @@ /* Includes ------------------------------------------------------------------*/ #include "mbed.h" +#include "pinmap.h" /* Classes -------------------------------------------------------------------*/ /** Helper class DevI2C providing functions for multi-register I2C communication @@ -49,75 +50,93 @@ */ class DevI2C : public I2C { - public: - /** Create a DevI2C Master interface, connected to the specified pins - * - * @param sda I2C data line pin - * @param scl I2C clock line pin - */ - DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} +public: + /** Create a DevI2C Master interface, connected to the specified pins + * + * @param sda I2C data line pin + * @param scl I2C clock line pin + */ + DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {} - /** - * @brief Writes a buffer towards the I2C peripheral device. - * @param pBuffer pointer to the byte-array data to send - * @param DeviceAddr specifies the peripheral device slave address. - * @param RegisterAddr specifies the internal address register - * where to start writing to (must be correctly masked). - * @param NumByteToWrite number of bytes to be written. - * @retval 0 if ok, - * @retval -1 if an I2C error has occured, or - * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) - * @note On some devices if NumByteToWrite is greater - * than one, the RegisterAddr must be masked correctly! - */ - int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, - uint16_t NumByteToWrite) - { - int ret; - uint8_t tmp[TEMP_BUF_SIZE]; - - if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; - - /* First, send device address. Then, send data and STOP condition */ - tmp[0] = RegisterAddr; - memcpy(tmp+1, pBuffer, NumByteToWrite); - - ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); + /** Create a DevI2C Master interface, connected to the specified pins and set their pin modes + * + * @param sda I2C data line pin + * @param sda I2C data pin mode + * @param scl I2C clock line pin + * @param scl I2C clock pin mode + * + * @note this is a workaround to provide a constructor which currently + * is somehow missing in the I2C base class and it's underlying + * implementations. In some circumstances (e.g. while debugging) + * where long latencies between the initialization of the i2c + * interface in the I2C constructor and the setting of the pin + * modes in the beyond constructor might occur, the i2c + * communication might be compromised. + */ + DevI2C(PinName sda, int mode_sda, PinName scl, int mode_scl) : I2C(sda, scl) { + pin_mode(sda, (PinMode)mode_sda); + pin_mode(scl, (PinMode)mode_scl); + } - if(ret) return -1; - return 0; - } + /** + * @brief Writes a buffer towards the I2C peripheral device. + * @param pBuffer pointer to the byte-array data to send + * @param DeviceAddr specifies the peripheral device slave address. + * @param RegisterAddr specifies the internal address register + * where to start writing to (must be correctly masked). + * @param NumByteToWrite number of bytes to be written. + * @retval 0 if ok, + * @retval -1 if an I2C error has occured, or + * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high) + * @note On some devices if NumByteToWrite is greater + * than one, the RegisterAddr must be masked correctly! + */ + int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, + uint16_t NumByteToWrite) { + int ret; + uint8_t tmp[TEMP_BUF_SIZE]; + + if(NumByteToWrite >= TEMP_BUF_SIZE) return -2; + + /* First, send device address. Then, send data and STOP condition */ + tmp[0] = RegisterAddr; + memcpy(tmp+1, pBuffer, NumByteToWrite); + + ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false); + + if(ret) return -1; + return 0; + } - /** - * @brief Reads a buffer from the I2C peripheral device. - * @param pBuffer pointer to the byte-array to read data in to - * @param DaviceAddr specifies the peripheral device slave address. - * @param RegisterAddr specifies the internal address register - * where to start reading from (must be correctly masked). - * @param NumByteToRead number of bytes to be read. - * @retval 0 if ok, - * @retval -1 if an I2C error has occured - * @note On some devices if NumByteToWrite is greater - * than one, the RegisterAddr must be masked correctly! - */ - int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, - uint16_t NumByteToRead) - { - int ret; - - /* Send device address, with no STOP condition */ - ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); - if(!ret) { - /* Read data, with STOP condition */ - ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); - } - - if(ret) return -1; - return 0; - } + /** + * @brief Reads a buffer from the I2C peripheral device. + * @param pBuffer pointer to the byte-array to read data in to + * @param DaviceAddr specifies the peripheral device slave address. + * @param RegisterAddr specifies the internal address register + * where to start reading from (must be correctly masked). + * @param NumByteToRead number of bytes to be read. + * @retval 0 if ok, + * @retval -1 if an I2C error has occured + * @note On some devices if NumByteToWrite is greater + * than one, the RegisterAddr must be masked correctly! + */ + int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, + uint16_t NumByteToRead) { + int ret; - private: - static const unsigned int TEMP_BUF_SIZE = 32; + /* Send device address, with no STOP condition */ + ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true); + if(!ret) { + /* Read data, with STOP condition */ + ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false); + } + + if(ret) return -1; + return 0; + } + +private: + static const unsigned int TEMP_BUF_SIZE = 32; }; #endif /* __DEV_I2C_H */