mbed.org local branch of microbit-dal. The real version lives in git at https://github.com/lancaster-university/microbit-dal

Dependencies:   BLE_API nRF51822 mbed-dev-bin

Dependents:   microbit Microbit IoTChallenge1 microbit ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MicroBitI2C.h Source File

MicroBitI2C.h

00001 /*
00002 The MIT License (MIT)
00003 
00004 Copyright (c) 2016 British Broadcasting Corporation.
00005 This software is provided by Lancaster University by arrangement with the BBC.
00006 
00007 Permission is hereby granted, free of charge, to any person obtaining a
00008 copy of this software and associated documentation files (the "Software"),
00009 to deal in the Software without restriction, including without limitation
00010 the rights to use, copy, modify, merge, publish, distribute, sublicense,
00011 and/or sell copies of the Software, and to permit persons to whom the
00012 Software is furnished to do so, subject to the following conditions:
00013 
00014 The above copyright notice and this permission notice shall be included in
00015 all copies or substantial portions of the Software.
00016 
00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
00023 DEALINGS IN THE SOFTWARE.
00024 */
00025 
00026 #ifndef MICROBIT_I2C_H
00027 #define MICROBIT_I2C_H
00028 
00029 #include "mbed.h"
00030 #include "MicroBitConfig.h"
00031 
00032 #define MICROBIT_I2C_MAX_RETRIES 9
00033 
00034 /**
00035   * Class definition for MicroBitI2C.
00036   *
00037   * Presents a wrapped mbed call to capture failed I2C operations caused by a known silicon bug in the nrf51822.
00038   * Attempts to automatically reset and restart the I2C hardware if this case is detected.
00039   *
00040   * For reference see PAN56 in:
00041   *
00042   * https://www.nordicsemi.com/eng/nordic/Products/nRF51822/PAN-nRF51822/24634
00043   *
00044   * v2.0 through to v2.4
00045   */
00046 class MicroBitI2C : public I2C
00047 {
00048     uint8_t retries;
00049 
00050     public:
00051 
00052     /**
00053       * Constructor.
00054       *
00055       * Create an instance of MicroBitI2C for I2C communication.
00056       *
00057       * @param sda the Pin to be used for SDA
00058       *
00059       * @param scl the Pin to be used for SCL
00060       *
00061       * @code
00062       * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
00063       * @endcode
00064       *
00065       * @note This class presents a wrapped mbed call to capture failed I2C operations caused by a known silicon bug in the nrf51822.
00066       * Attempts to automatically reset and restart the I2C hardware if this case is detected.
00067       * \par
00068       * For reference see PAN56 in:
00069       * \par
00070       * https://www.nordicsemi.com/eng/nordic/Products/nRF51822/PAN-nRF51822/24634
00071       * \par
00072       * v2.0 through to v2.4
00073       */
00074     MicroBitI2C(PinName sda, PinName scl);
00075 
00076     /**
00077       * Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
00078       *
00079       * @param address 8-bit I2C slave address [ addr | 1 ]
00080       *
00081       * @param data A pointer to a byte buffer used for storing retrieved data.
00082       *
00083       * @param length Number of bytes to read.
00084       *
00085       * @param repeated if true, stop is not sent at the end. Defaults to false.
00086       *
00087       * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved read failure is detected.
00088       */
00089     int read(int address, char *data, int length, bool repeated = false);
00090 
00091     /**
00092       * Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
00093       *
00094       * @param address 8-bit I2C slave address [ addr | 0 ]
00095       *
00096       * @param data A pointer to a byte buffer containing the data to write.
00097       *
00098       * @param length Number of bytes to write
00099       *
00100       * @param repeated if true, stop is not sent at the end. Defaults to false.
00101       *
00102       * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected.
00103       */
00104     int write(int address, const char *data, int length, bool repeated = false);
00105 };
00106 
00107 #endif