Library for supporting the Nucleo Sensor Shield.

Dependents:   Nucleo_Sensors_Demo m2x-temp_ethernet_demo m2x-MEMS_ACKme_Wifi_demo m2x_MEMS_Ublox_Cellular_demo ... more

Fork of Nucleo_Sensor_Shield by Daniel Griffin

Warning: Deprecated!

Supported drivers and applications can be found at this link.

Committer:
dangriffin
Date:
Tue Dec 16 21:14:32 2014 +0000
Revision:
2:57888ec40e75
Parent:
0:0433918efb54
Additional checking of return code in pressure sensor driver.

Who changed what in which revision?

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