No Changes

Dependencies:   BLE_API mbed-dev-bin nRF51822

Dependents:   microbit

Fork of microbit-dal by Lancaster University

Committer:
Asimov
Date:
Fri Jan 13 21:14:06 2017 +0000
Revision:
75:ae9a17da71af
Parent:
4:9fbeeb89de59
No Changes

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jonathan Austin 1:8aa5cdb4ab67 1 /*
Jonathan Austin 1:8aa5cdb4ab67 2 The MIT License (MIT)
Jonathan Austin 1:8aa5cdb4ab67 3
Jonathan Austin 1:8aa5cdb4ab67 4 Copyright (c) 2016 British Broadcasting Corporation.
Jonathan Austin 1:8aa5cdb4ab67 5 This software is provided by Lancaster University by arrangement with the BBC.
Jonathan Austin 1:8aa5cdb4ab67 6
Jonathan Austin 1:8aa5cdb4ab67 7 Permission is hereby granted, free of charge, to any person obtaining a
Jonathan Austin 1:8aa5cdb4ab67 8 copy of this software and associated documentation files (the "Software"),
Jonathan Austin 1:8aa5cdb4ab67 9 to deal in the Software without restriction, including without limitation
Jonathan Austin 1:8aa5cdb4ab67 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
Jonathan Austin 1:8aa5cdb4ab67 11 and/or sell copies of the Software, and to permit persons to whom the
Jonathan Austin 1:8aa5cdb4ab67 12 Software is furnished to do so, subject to the following conditions:
Jonathan Austin 1:8aa5cdb4ab67 13
Jonathan Austin 1:8aa5cdb4ab67 14 The above copyright notice and this permission notice shall be included in
Jonathan Austin 1:8aa5cdb4ab67 15 all copies or substantial portions of the Software.
Jonathan Austin 1:8aa5cdb4ab67 16
Jonathan Austin 1:8aa5cdb4ab67 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Jonathan Austin 1:8aa5cdb4ab67 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Jonathan Austin 1:8aa5cdb4ab67 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
Jonathan Austin 1:8aa5cdb4ab67 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
Jonathan Austin 1:8aa5cdb4ab67 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
Jonathan Austin 1:8aa5cdb4ab67 23 DEALINGS IN THE SOFTWARE.
Jonathan Austin 1:8aa5cdb4ab67 24 */
Jonathan Austin 1:8aa5cdb4ab67 25
Jonathan Austin 1:8aa5cdb4ab67 26 #include "MicroBitConfig.h"
Jonathan Austin 1:8aa5cdb4ab67 27 #include "MicroBitI2C.h"
Jonathan Austin 1:8aa5cdb4ab67 28 #include "ErrorNo.h"
Jonathan Austin 1:8aa5cdb4ab67 29 #include "twi_master.h"
Jonathan Austin 1:8aa5cdb4ab67 30 #include "nrf_delay.h"
Jonathan Austin 1:8aa5cdb4ab67 31
Jonathan Austin 1:8aa5cdb4ab67 32 /**
Jonathan Austin 1:8aa5cdb4ab67 33 * Constructor.
Jonathan Austin 1:8aa5cdb4ab67 34 *
Jonathan Austin 1:8aa5cdb4ab67 35 * Create an instance of MicroBitI2C for I2C communication.
Jonathan Austin 1:8aa5cdb4ab67 36 *
Jonathan Austin 1:8aa5cdb4ab67 37 * @param sda the Pin to be used for SDA
Jonathan Austin 1:8aa5cdb4ab67 38 *
Jonathan Austin 1:8aa5cdb4ab67 39 * @param scl the Pin to be used for SCL
Jonathan Austin 1:8aa5cdb4ab67 40 *
Jonathan Austin 1:8aa5cdb4ab67 41 * @code
Jonathan Austin 1:8aa5cdb4ab67 42 * MicroBitI2C i2c(I2C_SDA0, I2C_SCL0);
Jonathan Austin 1:8aa5cdb4ab67 43 * @endcode
Jonathan Austin 1:8aa5cdb4ab67 44 *
Jonathan Austin 1:8aa5cdb4ab67 45 * @note This class presents a wrapped mbed call to capture failed I2C operations caused by a known silicon bug in the nrf51822.
Jonathan Austin 1:8aa5cdb4ab67 46 * Attempts to automatically reset and restart the I2C hardware if this case is detected.
LancasterUniversity 4:9fbeeb89de59 47 * \par
Jonathan Austin 1:8aa5cdb4ab67 48 * For reference see PAN56 in:
LancasterUniversity 4:9fbeeb89de59 49 * \par
Jonathan Austin 1:8aa5cdb4ab67 50 * https://www.nordicsemi.com/eng/nordic/Products/nRF51822/PAN-nRF51822/24634
LancasterUniversity 4:9fbeeb89de59 51 * \par
Jonathan Austin 1:8aa5cdb4ab67 52 * v2.0 through to v2.4
Jonathan Austin 1:8aa5cdb4ab67 53 */
Jonathan Austin 1:8aa5cdb4ab67 54 MicroBitI2C::MicroBitI2C(PinName sda, PinName scl) : I2C(sda,scl)
Jonathan Austin 1:8aa5cdb4ab67 55 {
Jonathan Austin 1:8aa5cdb4ab67 56 this->retries = 0;
Jonathan Austin 1:8aa5cdb4ab67 57 }
Jonathan Austin 1:8aa5cdb4ab67 58
Jonathan Austin 1:8aa5cdb4ab67 59 /**
Jonathan Austin 1:8aa5cdb4ab67 60 * Performs a complete read transaction. The bottom bit of the address is forced to 1 to indicate a read.
Jonathan Austin 1:8aa5cdb4ab67 61 *
Jonathan Austin 1:8aa5cdb4ab67 62 * @param address 8-bit I2C slave address [ addr | 1 ]
Jonathan Austin 1:8aa5cdb4ab67 63 *
Jonathan Austin 1:8aa5cdb4ab67 64 * @param data A pointer to a byte buffer used for storing retrieved data.
Jonathan Austin 1:8aa5cdb4ab67 65 *
Jonathan Austin 1:8aa5cdb4ab67 66 * @param length Number of bytes to read.
Jonathan Austin 1:8aa5cdb4ab67 67 *
Jonathan Austin 1:8aa5cdb4ab67 68 * @param repeated if true, stop is not sent at the end. Defaults to false.
Jonathan Austin 1:8aa5cdb4ab67 69 *
Jonathan Austin 1:8aa5cdb4ab67 70 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved read failure is detected.
Jonathan Austin 1:8aa5cdb4ab67 71 */
Jonathan Austin 1:8aa5cdb4ab67 72 int MicroBitI2C::read(int address, char *data, int length, bool repeated)
Jonathan Austin 1:8aa5cdb4ab67 73 {
Jonathan Austin 1:8aa5cdb4ab67 74 int result = I2C::read(address,data,length,repeated);
Jonathan Austin 1:8aa5cdb4ab67 75
Jonathan Austin 1:8aa5cdb4ab67 76 //0 indicates a success, presume failure
Jonathan Austin 1:8aa5cdb4ab67 77 while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
Jonathan Austin 1:8aa5cdb4ab67 78 {
Jonathan Austin 1:8aa5cdb4ab67 79 _i2c.i2c->EVENTS_ERROR = 0;
Jonathan Austin 1:8aa5cdb4ab67 80 _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
Jonathan Austin 1:8aa5cdb4ab67 81 _i2c.i2c->POWER = 0;
Jonathan Austin 1:8aa5cdb4ab67 82 nrf_delay_us(5);
Jonathan Austin 1:8aa5cdb4ab67 83 _i2c.i2c->POWER = 1;
Jonathan Austin 1:8aa5cdb4ab67 84 _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
Jonathan Austin 1:8aa5cdb4ab67 85 twi_master_init_and_clear();
Jonathan Austin 1:8aa5cdb4ab67 86 result = I2C::read(address,data,length,repeated);
Jonathan Austin 1:8aa5cdb4ab67 87 retries++;
Jonathan Austin 1:8aa5cdb4ab67 88 }
Jonathan Austin 1:8aa5cdb4ab67 89
Jonathan Austin 1:8aa5cdb4ab67 90 if(result != 0)
Jonathan Austin 1:8aa5cdb4ab67 91 return MICROBIT_I2C_ERROR;
Jonathan Austin 1:8aa5cdb4ab67 92
Jonathan Austin 1:8aa5cdb4ab67 93 retries = 0;
Jonathan Austin 1:8aa5cdb4ab67 94 return MICROBIT_OK;
Jonathan Austin 1:8aa5cdb4ab67 95 }
Jonathan Austin 1:8aa5cdb4ab67 96
Jonathan Austin 1:8aa5cdb4ab67 97 /**
Jonathan Austin 1:8aa5cdb4ab67 98 * Performs a complete write transaction. The bottom bit of the address is forced to 0 to indicate a write.
Jonathan Austin 1:8aa5cdb4ab67 99 *
Jonathan Austin 1:8aa5cdb4ab67 100 * @param address 8-bit I2C slave address [ addr | 0 ]
Jonathan Austin 1:8aa5cdb4ab67 101 *
Jonathan Austin 1:8aa5cdb4ab67 102 * @param data A pointer to a byte buffer containing the data to write.
Jonathan Austin 1:8aa5cdb4ab67 103 *
Jonathan Austin 1:8aa5cdb4ab67 104 * @param length Number of bytes to write
Jonathan Austin 1:8aa5cdb4ab67 105 *
Jonathan Austin 1:8aa5cdb4ab67 106 * @param repeated if true, stop is not sent at the end. Defaults to false.
Jonathan Austin 1:8aa5cdb4ab67 107 *
Jonathan Austin 1:8aa5cdb4ab67 108 * @return MICROBIT_OK on success, MICROBIT_I2C_ERROR if an unresolved write failure is detected.
Jonathan Austin 1:8aa5cdb4ab67 109 */
Jonathan Austin 1:8aa5cdb4ab67 110 int MicroBitI2C::write(int address, const char *data, int length, bool repeated)
Jonathan Austin 1:8aa5cdb4ab67 111 {
Jonathan Austin 1:8aa5cdb4ab67 112 int result = I2C::write(address,data,length,repeated);
Jonathan Austin 1:8aa5cdb4ab67 113
Jonathan Austin 1:8aa5cdb4ab67 114 //0 indicates a success, presume failure
Jonathan Austin 1:8aa5cdb4ab67 115 while(result != 0 && retries < MICROBIT_I2C_MAX_RETRIES)
Jonathan Austin 1:8aa5cdb4ab67 116 {
Jonathan Austin 1:8aa5cdb4ab67 117 _i2c.i2c->EVENTS_ERROR = 0;
Jonathan Austin 1:8aa5cdb4ab67 118 _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Disabled << TWI_ENABLE_ENABLE_Pos;
Jonathan Austin 1:8aa5cdb4ab67 119 _i2c.i2c->POWER = 0;
Jonathan Austin 1:8aa5cdb4ab67 120 nrf_delay_us(5);
Jonathan Austin 1:8aa5cdb4ab67 121 _i2c.i2c->POWER = 1;
Jonathan Austin 1:8aa5cdb4ab67 122 _i2c.i2c->ENABLE = TWI_ENABLE_ENABLE_Enabled << TWI_ENABLE_ENABLE_Pos;
Jonathan Austin 1:8aa5cdb4ab67 123
Jonathan Austin 1:8aa5cdb4ab67 124 twi_master_init_and_clear();
Jonathan Austin 1:8aa5cdb4ab67 125 result = I2C::write(address,data,length,repeated);
Jonathan Austin 1:8aa5cdb4ab67 126 retries++;
Jonathan Austin 1:8aa5cdb4ab67 127 }
Jonathan Austin 1:8aa5cdb4ab67 128
Jonathan Austin 1:8aa5cdb4ab67 129 if(result != 0)
Jonathan Austin 1:8aa5cdb4ab67 130 return MICROBIT_I2C_ERROR;
Jonathan Austin 1:8aa5cdb4ab67 131
Jonathan Austin 1:8aa5cdb4ab67 132 retries = 0;
Jonathan Austin 1:8aa5cdb4ab67 133 return MICROBIT_OK;
LancasterUniversity 4:9fbeeb89de59 134 }