Fork of X_NUCLEO_COMMON lib adding 16-bit SPI support

Fork of X_NUCLEO_COMMON by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers DevI2C.h Source File

DevI2C.h

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    DevI2C.h
00004  * @author  AST / EST
00005  * @version V0.0.1
00006  * @date    21-January-2015
00007  * @brief   Header file for a special I2C class DevI2C which provides some
00008  *          helper function for on-board communication
00009  ******************************************************************************
00010  * @attention
00011  *
00012  * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00013  *
00014  * Redistribution and use in source and binary forms, with or without modification,
00015  * are permitted provided that the following conditions are met:
00016  *   1. Redistributions of source code must retain the above copyright notice,
00017  *      this list of conditions and the following disclaimer.
00018  *   2. Redistributions in binary form must reproduce the above copyright notice,
00019  *      this list of conditions and the following disclaimer in the documentation
00020  *      and/or other materials provided with the distribution.
00021  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022  *      may be used to endorse or promote products derived from this software
00023  *      without specific prior written permission.
00024  *
00025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035  *
00036  ******************************************************************************
00037  */
00038 
00039 /* Define to prevent from recursive inclusion --------------------------------*/
00040 #ifndef __DEV_I2C_H
00041 #define __DEV_I2C_H
00042 
00043 /* Includes ------------------------------------------------------------------*/
00044 #include "mbed.h"
00045 
00046 /* Classes -------------------------------------------------------------------*/
00047 /** Helper class DevI2C providing functions for multi-register I2C communication
00048  *  common for a series of I2C devices
00049  */
00050 class DevI2C : public I2C
00051 {
00052  public:
00053     /** Create a DevI2C Master interface, connected to the specified pins
00054      *
00055      *  @param sda I2C data line pin
00056      *  @param scl I2C clock line pin
00057      */
00058         DevI2C(PinName sda, PinName scl) : I2C(sda, scl) {}
00059 
00060     /**
00061      * @brief  Writes a buffer towards the I2C peripheral device.
00062      * @param  pBuffer pointer to the byte-array data to send
00063      * @param  DeviceAddr specifies the peripheral device slave address.
00064      * @param  RegisterAddr specifies the internal address register 
00065      *         where to start writing to (must be correctly masked).
00066      * @param  NumByteToWrite number of bytes to be written.
00067      * @retval 0 if ok, 
00068      * @retval -1 if an I2C error has occured, or
00069      * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
00070      * @note   On some devices if NumByteToWrite is greater
00071      *         than one, the RegisterAddr must be masked correctly!
00072      */
00073     int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 
00074               uint16_t NumByteToWrite)
00075     {
00076         int ret;
00077         uint8_t tmp[TEMP_BUF_SIZE];
00078     
00079         if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
00080         
00081         /* First, send device address. Then, send data and STOP condition */
00082         tmp[0] = RegisterAddr;
00083         memcpy(tmp+1, pBuffer, NumByteToWrite);
00084 
00085         ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
00086 
00087         if(ret) return -1;
00088         return 0;
00089     }
00090 
00091     /**
00092      * @brief  Reads a buffer from the I2C peripheral device.
00093      * @param  pBuffer pointer to the byte-array to read data in to
00094      * @param  DaviceAddr specifies the peripheral device slave address.
00095      * @param  RegisterAddr specifies the internal address register 
00096      *         where to start reading from (must be correctly masked).
00097      * @param  NumByteToRead number of bytes to be read.
00098      * @retval 0 if ok, 
00099      * @retval -1 if an I2C error has occured
00100      * @note   On some devices if NumByteToWrite is greater
00101      *         than one, the RegisterAddr must be masked correctly!
00102      */
00103     int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr, 
00104              uint16_t NumByteToRead)
00105     {
00106         int ret;
00107     
00108         /* Send device address, with no STOP condition */
00109         ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
00110         if(!ret) {
00111             /* Read data, with STOP condition  */
00112             ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
00113         }
00114     
00115         if(ret) return -1;
00116         return 0;
00117     }
00118 
00119  private:
00120     static const unsigned int TEMP_BUF_SIZE = 32;
00121 };
00122 
00123 #endif /* __DEV_I2C_H */