Generic mbed Extensions used by STM Expansion Board Firmware Packages.

Dependents:   X_NUCLEO_IKS01A1 X_NUCLEO_6180XA1 1-DoorCloser 1-DoorCloser ... more

Fork of X_NUCLEO_COMMON by ST Expansion SW Team

Generic mbed Extensions used by STM Expansion Board Firmware Packages

DbgMCU

Helper class DbgMCU providing a default constructor which enables debugging on STM32 MCUs while using sleep modes.

DevI2C

Helper class DevI2C providing functions for multi-register I2C communication common for a series of I2C devices.

DevSPI

Helper class DevSPI providing functions for SPI communication common for a series of SPI devices.

Committer:
johnAlexander
Date:
Tue Aug 08 14:37:26 2017 +0000
Revision:
22:21096473f63e
Parent:
21:0dbcdb8dab03
Update DevI2C to allow it to continue to inherit from mbed I2C master, following I2C's updates to be un-copyable.

Who changed what in which revision?

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