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:
Wolfgang Betz
Date:
Fri May 22 14:33:42 2015 +0200
Revision:
4:33ee0cf483de
Parent:
3:bc5fc631e9c5
Child:
5:efc56a90dc5a
Correct typo

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
Wolfgang Betz 0:5f1934431e71 5 * @version V0.0.1
Wolfgang Betz 0:5f1934431e71 6 * @date 21-January-2015
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 *
Wolfgang Betz 0:5f1934431e71 12 * <h2><center>&copy; COPYRIGHT(c) 2014 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"
Wolfgang Betz 0:5f1934431e71 45
Wolfgang Betz 0:5f1934431e71 46 /* Classes -------------------------------------------------------------------*/
Wolfgang Betz 0:5f1934431e71 47 /** Helper class DevI2C providing functions for multi-register I2C communication
Wolfgang Betz 0:5f1934431e71 48 * common for a series of I2C devices
Wolfgang Betz 0:5f1934431e71 49 */
Wolfgang Betz 0:5f1934431e71 50 class DevI2C : public I2C
Wolfgang Betz 0:5f1934431e71 51 {
Wolfgang Betz 0:5f1934431e71 52 public:
Wolfgang Betz 0:5f1934431e71 53 /** Create a DevI2C Master interface, connected to the specified pins
Wolfgang Betz 0:5f1934431e71 54 *
Wolfgang Betz 0:5f1934431e71 55 * @param sda I2C data line pin
Wolfgang Betz 0:5f1934431e71 56 * @param scl I2C clock line pin
Wolfgang Betz 0:5f1934431e71 57 */
Wolfgang Betz 1:ca975408dd4e 58 DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
Wolfgang Betz 0:5f1934431e71 59
Wolfgang Betz 0:5f1934431e71 60 /**
Wolfgang Betz 0:5f1934431e71 61 * @brief Writes a buffer from the I2C peripheral device.
Wolfgang Betz 0:5f1934431e71 62 * @param pBuffer pointer to data to be written.
Wolfgang Betz 0:5f1934431e71 63 * @param DeviceAddr specifies the peripheral device slave address.
Wolfgang Betz 0:5f1934431e71 64 * @param RegisterAddr specifies the internal address register
Wolfgang Betz 0:5f1934431e71 65 * where to start writing to (must be correctly masked).
Wolfgang Betz 0:5f1934431e71 66 * @param NumByteToWrite number of bytes to be written.
Wolfgang Betz 3:bc5fc631e9c5 67 * @retval 0 if ok,
Wolfgang Betz 3:bc5fc631e9c5 68 * -1 if an I2C error has occured, or
Wolfgang Betz 3:bc5fc631e9c5 69 * -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
Wolfgang Betz 0:5f1934431e71 70 * @note On some devices if NumByteToWrite is greater
Wolfgang Betz 0:5f1934431e71 71 * than one, the RegisterAddr must be masked correctly!
Wolfgang Betz 0:5f1934431e71 72 */
Wolfgang Betz 0:5f1934431e71 73 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
Wolfgang Betz 0:5f1934431e71 74 uint16_t NumByteToWrite)
Wolfgang Betz 0:5f1934431e71 75 {
Wolfgang Betz 0:5f1934431e71 76 int ret;
Wolfgang Betz 3:bc5fc631e9c5 77 uint8_t tmp[TEMP_BUF_SIZE];
Wolfgang Betz 0:5f1934431e71 78
Wolfgang Betz 3:bc5fc631e9c5 79 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
Wolfgang Betz 3:bc5fc631e9c5 80
Wolfgang Betz 0:5f1934431e71 81 /* First, send device address. Then, send data and STOP condition */
Wolfgang Betz 0:5f1934431e71 82 tmp[0] = RegisterAddr;
Wolfgang Betz 0:5f1934431e71 83 memcpy(tmp+1, pBuffer, NumByteToWrite);
Wolfgang Betz 0:5f1934431e71 84
Wolfgang Betz 0:5f1934431e71 85 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, 0);
Wolfgang Betz 0:5f1934431e71 86
Wolfgang Betz 2:c1cac955d4bd 87 if(ret) return -1;
Wolfgang Betz 0:5f1934431e71 88 return 0;
Wolfgang Betz 0:5f1934431e71 89 }
Wolfgang Betz 0:5f1934431e71 90
Wolfgang Betz 0:5f1934431e71 91 /**
Wolfgang Betz 0:5f1934431e71 92 * @brief Reads a buffer from the I2C peripheral device.
Wolfgang Betz 0:5f1934431e71 93 * @param pBuffer pointer to data to be read.
Wolfgang Betz 0:5f1934431e71 94 * @param DaviceAddr specifies the peripheral device slave address.
Wolfgang Betz 0:5f1934431e71 95 * @param RegisterAddr specifies the internal address register
Wolfgang Betz 0:5f1934431e71 96 * where to start reading from (must be correctly masked).
Wolfgang Betz 0:5f1934431e71 97 * @param NumByteToRead number of bytes to be read.
Wolfgang Betz 0:5f1934431e71 98 * @retval 0 if ok, -1 if an I2C error has occured
Wolfgang Betz 0:5f1934431e71 99 * @note On some devices if NumByteToWrite is greater
Wolfgang Betz 0:5f1934431e71 100 * than one, the RegisterAddr must be masked correctly!
Wolfgang Betz 0:5f1934431e71 101 */
Wolfgang Betz 0:5f1934431e71 102 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
Wolfgang Betz 0:5f1934431e71 103 uint16_t NumByteToRead)
Wolfgang Betz 0:5f1934431e71 104 {
Wolfgang Betz 0:5f1934431e71 105 int ret;
Wolfgang Betz 0:5f1934431e71 106
Wolfgang Betz 0:5f1934431e71 107 /* Send device address, with no STOP condition */
Wolfgang Betz 0:5f1934431e71 108 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, 1);
Wolfgang Betz 0:5f1934431e71 109 if(!ret) {
Wolfgang Betz 0:5f1934431e71 110 /* Read data, with STOP condition */
Wolfgang Betz 0:5f1934431e71 111 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, 0);
Wolfgang Betz 0:5f1934431e71 112 }
Wolfgang Betz 0:5f1934431e71 113
Wolfgang Betz 2:c1cac955d4bd 114 if(ret) return -1;
Wolfgang Betz 0:5f1934431e71 115 return 0;
Wolfgang Betz 0:5f1934431e71 116 }
Wolfgang Betz 3:bc5fc631e9c5 117
Wolfgang Betz 3:bc5fc631e9c5 118 private:
Wolfgang Betz 3:bc5fc631e9c5 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 */