Local changes to increase sample rate

Fork of X_NUCLEO_COMMON by ST

Committer:
Arkadi
Date:
Sun May 21 12:53:32 2017 +0000
Revision:
22:daf134fdbbaf
Parent:
19:12be3dfc15fd
local libary

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) {}
Wolfgang Betz 0:5f1934431e71 60
wobetz 19:12be3dfc15fd 61 /** Create a DevI2C Master interface, connected to the specified pins and set their pin modes
wobetz 19:12be3dfc15fd 62 *
wobetz 19:12be3dfc15fd 63 * @param sda I2C data line pin
wobetz 19:12be3dfc15fd 64 * @param sda I2C data pin mode
wobetz 19:12be3dfc15fd 65 * @param scl I2C clock line pin
wobetz 19:12be3dfc15fd 66 * @param scl I2C clock pin mode
wobetz 19:12be3dfc15fd 67 *
wobetz 19:12be3dfc15fd 68 * @note this is a workaround to provide a constructor which currently
wobetz 19:12be3dfc15fd 69 * is somehow missing in the I2C base class and it's underlying
wobetz 19:12be3dfc15fd 70 * implementations. In some circumstances (e.g. while debugging)
wobetz 19:12be3dfc15fd 71 * where long latencies between the initialization of the i2c
wobetz 19:12be3dfc15fd 72 * interface in the I2C constructor and the setting of the pin
wobetz 19:12be3dfc15fd 73 * modes in the beyond constructor might occur, the i2c
wobetz 19:12be3dfc15fd 74 * communication might be compromised.
wobetz 19:12be3dfc15fd 75 */
wobetz 19:12be3dfc15fd 76 DevI2C(PinName sda, int mode_sda, PinName scl, int mode_scl) : I2C(sda, scl) {
wobetz 19:12be3dfc15fd 77 pin_mode(sda, (PinMode)mode_sda);
wobetz 19:12be3dfc15fd 78 pin_mode(scl, (PinMode)mode_scl);
Arkadi 22:daf134fdbbaf 79 //frequency(400000);
Arkadi 22:daf134fdbbaf 80
wobetz 19:12be3dfc15fd 81 }
Wolfgang Betz 0:5f1934431e71 82
wobetz 19:12be3dfc15fd 83 /**
wobetz 19:12be3dfc15fd 84 * @brief Writes a buffer towards the I2C peripheral device.
wobetz 19:12be3dfc15fd 85 * @param pBuffer pointer to the byte-array data to send
wobetz 19:12be3dfc15fd 86 * @param DeviceAddr specifies the peripheral device slave address.
wobetz 19:12be3dfc15fd 87 * @param RegisterAddr specifies the internal address register
wobetz 19:12be3dfc15fd 88 * where to start writing to (must be correctly masked).
wobetz 19:12be3dfc15fd 89 * @param NumByteToWrite number of bytes to be written.
wobetz 19:12be3dfc15fd 90 * @retval 0 if ok,
wobetz 19:12be3dfc15fd 91 * @retval -1 if an I2C error has occured, or
wobetz 19:12be3dfc15fd 92 * @retval -2 on temporary buffer overflow (i.e. NumByteToWrite was too high)
wobetz 19:12be3dfc15fd 93 * @note On some devices if NumByteToWrite is greater
wobetz 19:12be3dfc15fd 94 * than one, the RegisterAddr must be masked correctly!
wobetz 19:12be3dfc15fd 95 */
wobetz 19:12be3dfc15fd 96 int i2c_write(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
wobetz 19:12be3dfc15fd 97 uint16_t NumByteToWrite) {
wobetz 19:12be3dfc15fd 98 int ret;
wobetz 19:12be3dfc15fd 99 uint8_t tmp[TEMP_BUF_SIZE];
wobetz 19:12be3dfc15fd 100
wobetz 19:12be3dfc15fd 101 if(NumByteToWrite >= TEMP_BUF_SIZE) return -2;
wobetz 19:12be3dfc15fd 102
wobetz 19:12be3dfc15fd 103 /* First, send device address. Then, send data and STOP condition */
wobetz 19:12be3dfc15fd 104 tmp[0] = RegisterAddr;
wobetz 19:12be3dfc15fd 105 memcpy(tmp+1, pBuffer, NumByteToWrite);
wobetz 19:12be3dfc15fd 106
wobetz 19:12be3dfc15fd 107 ret = write(DeviceAddr, (const char*)tmp, NumByteToWrite+1, false);
wobetz 19:12be3dfc15fd 108
wobetz 19:12be3dfc15fd 109 if(ret) return -1;
wobetz 19:12be3dfc15fd 110 return 0;
wobetz 19:12be3dfc15fd 111 }
Wolfgang Betz 0:5f1934431e71 112
wobetz 19:12be3dfc15fd 113 /**
wobetz 19:12be3dfc15fd 114 * @brief Reads a buffer from the I2C peripheral device.
wobetz 19:12be3dfc15fd 115 * @param pBuffer pointer to the byte-array to read data in to
wobetz 19:12be3dfc15fd 116 * @param DaviceAddr specifies the peripheral device slave address.
wobetz 19:12be3dfc15fd 117 * @param RegisterAddr specifies the internal address register
wobetz 19:12be3dfc15fd 118 * where to start reading from (must be correctly masked).
wobetz 19:12be3dfc15fd 119 * @param NumByteToRead number of bytes to be read.
wobetz 19:12be3dfc15fd 120 * @retval 0 if ok,
wobetz 19:12be3dfc15fd 121 * @retval -1 if an I2C error has occured
wobetz 19:12be3dfc15fd 122 * @note On some devices if NumByteToWrite is greater
wobetz 19:12be3dfc15fd 123 * than one, the RegisterAddr must be masked correctly!
wobetz 19:12be3dfc15fd 124 */
wobetz 19:12be3dfc15fd 125 int i2c_read(uint8_t* pBuffer, uint8_t DeviceAddr, uint8_t RegisterAddr,
wobetz 19:12be3dfc15fd 126 uint16_t NumByteToRead) {
wobetz 19:12be3dfc15fd 127 int ret;
Wolfgang Betz 3:bc5fc631e9c5 128
wobetz 19:12be3dfc15fd 129 /* Send device address, with no STOP condition */
wobetz 19:12be3dfc15fd 130 ret = write(DeviceAddr, (const char*)&RegisterAddr, 1, true);
wobetz 19:12be3dfc15fd 131 if(!ret) {
wobetz 19:12be3dfc15fd 132 /* Read data, with STOP condition */
wobetz 19:12be3dfc15fd 133 ret = read(DeviceAddr, (char*)pBuffer, NumByteToRead, false);
wobetz 19:12be3dfc15fd 134 }
wobetz 19:12be3dfc15fd 135
wobetz 19:12be3dfc15fd 136 if(ret) return -1;
wobetz 19:12be3dfc15fd 137 return 0;
wobetz 19:12be3dfc15fd 138 }
wobetz 19:12be3dfc15fd 139
wobetz 19:12be3dfc15fd 140 private:
wobetz 19:12be3dfc15fd 141 static const unsigned int TEMP_BUF_SIZE = 32;
Wolfgang Betz 0:5f1934431e71 142 };
Wolfgang Betz 0:5f1934431e71 143
Wolfgang Betz 0:5f1934431e71 144 #endif /* __DEV_I2C_H */