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