5.2.1 - Updated I2C files
Dependents: mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510
drivers/I2C.cpp@0:098463de4c5d, 2017-01-25 (annotated)
- Committer:
- group-onsemi
- Date:
- Wed Jan 25 20:34:15 2017 +0000
- Revision:
- 0:098463de4c5d
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
group-onsemi | 0:098463de4c5d | 1 | /* mbed Microcontroller Library |
group-onsemi | 0:098463de4c5d | 2 | * Copyright (c) 2006-2015 ARM Limited |
group-onsemi | 0:098463de4c5d | 3 | * |
group-onsemi | 0:098463de4c5d | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
group-onsemi | 0:098463de4c5d | 5 | * you may not use this file except in compliance with the License. |
group-onsemi | 0:098463de4c5d | 6 | * You may obtain a copy of the License at |
group-onsemi | 0:098463de4c5d | 7 | * |
group-onsemi | 0:098463de4c5d | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
group-onsemi | 0:098463de4c5d | 9 | * |
group-onsemi | 0:098463de4c5d | 10 | * Unless required by applicable law or agreed to in writing, software |
group-onsemi | 0:098463de4c5d | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
group-onsemi | 0:098463de4c5d | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
group-onsemi | 0:098463de4c5d | 13 | * See the License for the specific language governing permissions and |
group-onsemi | 0:098463de4c5d | 14 | * limitations under the License. |
group-onsemi | 0:098463de4c5d | 15 | */ |
group-onsemi | 0:098463de4c5d | 16 | #include "drivers/I2C.h" |
group-onsemi | 0:098463de4c5d | 17 | |
group-onsemi | 0:098463de4c5d | 18 | #if DEVICE_I2C |
group-onsemi | 0:098463de4c5d | 19 | |
group-onsemi | 0:098463de4c5d | 20 | namespace mbed { |
group-onsemi | 0:098463de4c5d | 21 | |
group-onsemi | 0:098463de4c5d | 22 | I2C *I2C::_owner = NULL; |
group-onsemi | 0:098463de4c5d | 23 | SingletonPtr<PlatformMutex> I2C::_mutex; |
group-onsemi | 0:098463de4c5d | 24 | |
group-onsemi | 0:098463de4c5d | 25 | I2C::I2C(PinName sda, PinName scl) : |
group-onsemi | 0:098463de4c5d | 26 | #if DEVICE_I2C_ASYNCH |
group-onsemi | 0:098463de4c5d | 27 | _irq(this), _usage(DMA_USAGE_NEVER), |
group-onsemi | 0:098463de4c5d | 28 | #endif |
group-onsemi | 0:098463de4c5d | 29 | _i2c(), _hz(100000) { |
group-onsemi | 0:098463de4c5d | 30 | // No lock needed in the constructor |
group-onsemi | 0:098463de4c5d | 31 | |
group-onsemi | 0:098463de4c5d | 32 | // The init function also set the frequency to 100000 |
group-onsemi | 0:098463de4c5d | 33 | i2c_init(&_i2c, sda, scl); |
group-onsemi | 0:098463de4c5d | 34 | |
group-onsemi | 0:098463de4c5d | 35 | // Used to avoid unnecessary frequency updates |
group-onsemi | 0:098463de4c5d | 36 | _owner = this; |
group-onsemi | 0:098463de4c5d | 37 | } |
group-onsemi | 0:098463de4c5d | 38 | |
group-onsemi | 0:098463de4c5d | 39 | void I2C::frequency(int hz) { |
group-onsemi | 0:098463de4c5d | 40 | lock(); |
group-onsemi | 0:098463de4c5d | 41 | _hz = hz; |
group-onsemi | 0:098463de4c5d | 42 | |
group-onsemi | 0:098463de4c5d | 43 | // We want to update the frequency even if we are already the bus owners |
group-onsemi | 0:098463de4c5d | 44 | i2c_frequency(&_i2c, _hz); |
group-onsemi | 0:098463de4c5d | 45 | |
group-onsemi | 0:098463de4c5d | 46 | // Updating the frequency of the bus we become the owners of it |
group-onsemi | 0:098463de4c5d | 47 | _owner = this; |
group-onsemi | 0:098463de4c5d | 48 | unlock(); |
group-onsemi | 0:098463de4c5d | 49 | } |
group-onsemi | 0:098463de4c5d | 50 | |
group-onsemi | 0:098463de4c5d | 51 | void I2C::aquire() { |
group-onsemi | 0:098463de4c5d | 52 | lock(); |
group-onsemi | 0:098463de4c5d | 53 | if (_owner != this) { |
group-onsemi | 0:098463de4c5d | 54 | i2c_frequency(&_i2c, _hz); |
group-onsemi | 0:098463de4c5d | 55 | _owner = this; |
group-onsemi | 0:098463de4c5d | 56 | } |
group-onsemi | 0:098463de4c5d | 57 | unlock(); |
group-onsemi | 0:098463de4c5d | 58 | } |
group-onsemi | 0:098463de4c5d | 59 | |
group-onsemi | 0:098463de4c5d | 60 | // write - Master Transmitter Mode |
group-onsemi | 0:098463de4c5d | 61 | int I2C::write(int address, const char* data, int length, bool repeated) { |
group-onsemi | 0:098463de4c5d | 62 | lock(); |
group-onsemi | 0:098463de4c5d | 63 | aquire(); |
group-onsemi | 0:098463de4c5d | 64 | |
group-onsemi | 0:098463de4c5d | 65 | int stop = (repeated) ? 0 : 1; |
group-onsemi | 0:098463de4c5d | 66 | int written = i2c_write(&_i2c, address, data, length, stop); |
group-onsemi | 0:098463de4c5d | 67 | |
group-onsemi | 0:098463de4c5d | 68 | unlock(); |
group-onsemi | 0:098463de4c5d | 69 | return length != written; |
group-onsemi | 0:098463de4c5d | 70 | } |
group-onsemi | 0:098463de4c5d | 71 | |
group-onsemi | 0:098463de4c5d | 72 | int I2C::write(int data) { |
group-onsemi | 0:098463de4c5d | 73 | lock(); |
group-onsemi | 0:098463de4c5d | 74 | int ret = i2c_byte_write(&_i2c, data); |
group-onsemi | 0:098463de4c5d | 75 | unlock(); |
group-onsemi | 0:098463de4c5d | 76 | return ret; |
group-onsemi | 0:098463de4c5d | 77 | } |
group-onsemi | 0:098463de4c5d | 78 | |
group-onsemi | 0:098463de4c5d | 79 | // read - Master Reciever Mode |
group-onsemi | 0:098463de4c5d | 80 | int I2C::read(int address, char* data, int length, bool repeated) { |
group-onsemi | 0:098463de4c5d | 81 | lock(); |
group-onsemi | 0:098463de4c5d | 82 | aquire(); |
group-onsemi | 0:098463de4c5d | 83 | |
group-onsemi | 0:098463de4c5d | 84 | int stop = (repeated) ? 0 : 1; |
group-onsemi | 0:098463de4c5d | 85 | int read = i2c_read(&_i2c, address, data, length, stop); |
group-onsemi | 0:098463de4c5d | 86 | |
group-onsemi | 0:098463de4c5d | 87 | unlock(); |
group-onsemi | 0:098463de4c5d | 88 | return length != read; |
group-onsemi | 0:098463de4c5d | 89 | } |
group-onsemi | 0:098463de4c5d | 90 | |
group-onsemi | 0:098463de4c5d | 91 | int I2C::read(int ack) { |
group-onsemi | 0:098463de4c5d | 92 | lock(); |
group-onsemi | 0:098463de4c5d | 93 | int ret; |
group-onsemi | 0:098463de4c5d | 94 | if (ack) { |
group-onsemi | 0:098463de4c5d | 95 | ret = i2c_byte_read(&_i2c, 0); |
group-onsemi | 0:098463de4c5d | 96 | } else { |
group-onsemi | 0:098463de4c5d | 97 | ret = i2c_byte_read(&_i2c, 1); |
group-onsemi | 0:098463de4c5d | 98 | } |
group-onsemi | 0:098463de4c5d | 99 | unlock(); |
group-onsemi | 0:098463de4c5d | 100 | return ret; |
group-onsemi | 0:098463de4c5d | 101 | } |
group-onsemi | 0:098463de4c5d | 102 | |
group-onsemi | 0:098463de4c5d | 103 | void I2C::start(void) { |
group-onsemi | 0:098463de4c5d | 104 | lock(); |
group-onsemi | 0:098463de4c5d | 105 | i2c_start(&_i2c); |
group-onsemi | 0:098463de4c5d | 106 | unlock(); |
group-onsemi | 0:098463de4c5d | 107 | } |
group-onsemi | 0:098463de4c5d | 108 | |
group-onsemi | 0:098463de4c5d | 109 | void I2C::stop(void) { |
group-onsemi | 0:098463de4c5d | 110 | lock(); |
group-onsemi | 0:098463de4c5d | 111 | i2c_stop(&_i2c); |
group-onsemi | 0:098463de4c5d | 112 | unlock(); |
group-onsemi | 0:098463de4c5d | 113 | } |
group-onsemi | 0:098463de4c5d | 114 | |
group-onsemi | 0:098463de4c5d | 115 | void I2C::lock() { |
group-onsemi | 0:098463de4c5d | 116 | _mutex->lock(); |
group-onsemi | 0:098463de4c5d | 117 | } |
group-onsemi | 0:098463de4c5d | 118 | |
group-onsemi | 0:098463de4c5d | 119 | void I2C::unlock() { |
group-onsemi | 0:098463de4c5d | 120 | _mutex->unlock(); |
group-onsemi | 0:098463de4c5d | 121 | } |
group-onsemi | 0:098463de4c5d | 122 | |
group-onsemi | 0:098463de4c5d | 123 | #if DEVICE_I2C_ASYNCH |
group-onsemi | 0:098463de4c5d | 124 | |
group-onsemi | 0:098463de4c5d | 125 | int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event, bool repeated) |
group-onsemi | 0:098463de4c5d | 126 | { |
group-onsemi | 0:098463de4c5d | 127 | lock(); |
group-onsemi | 0:098463de4c5d | 128 | if (i2c_active(&_i2c)) { |
group-onsemi | 0:098463de4c5d | 129 | unlock(); |
group-onsemi | 0:098463de4c5d | 130 | return -1; // transaction ongoing |
group-onsemi | 0:098463de4c5d | 131 | } |
group-onsemi | 0:098463de4c5d | 132 | aquire(); |
group-onsemi | 0:098463de4c5d | 133 | |
group-onsemi | 0:098463de4c5d | 134 | _callback = callback; |
group-onsemi | 0:098463de4c5d | 135 | int stop = (repeated) ? 0 : 1; |
group-onsemi | 0:098463de4c5d | 136 | _irq.callback(&I2C::irq_handler_asynch); |
group-onsemi | 0:098463de4c5d | 137 | i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage); |
group-onsemi | 0:098463de4c5d | 138 | unlock(); |
group-onsemi | 0:098463de4c5d | 139 | return 0; |
group-onsemi | 0:098463de4c5d | 140 | } |
group-onsemi | 0:098463de4c5d | 141 | |
group-onsemi | 0:098463de4c5d | 142 | void I2C::abort_transfer(void) |
group-onsemi | 0:098463de4c5d | 143 | { |
group-onsemi | 0:098463de4c5d | 144 | lock(); |
group-onsemi | 0:098463de4c5d | 145 | i2c_abort_asynch(&_i2c); |
group-onsemi | 0:098463de4c5d | 146 | unlock(); |
group-onsemi | 0:098463de4c5d | 147 | } |
group-onsemi | 0:098463de4c5d | 148 | |
group-onsemi | 0:098463de4c5d | 149 | void I2C::irq_handler_asynch(void) |
group-onsemi | 0:098463de4c5d | 150 | { |
group-onsemi | 0:098463de4c5d | 151 | int event = i2c_irq_handler_asynch(&_i2c); |
group-onsemi | 0:098463de4c5d | 152 | if (_callback && event) { |
group-onsemi | 0:098463de4c5d | 153 | _callback.call(event); |
group-onsemi | 0:098463de4c5d | 154 | } |
group-onsemi | 0:098463de4c5d | 155 | |
group-onsemi | 0:098463de4c5d | 156 | } |
group-onsemi | 0:098463de4c5d | 157 | |
group-onsemi | 0:098463de4c5d | 158 | |
group-onsemi | 0:098463de4c5d | 159 | #endif |
group-onsemi | 0:098463de4c5d | 160 | |
group-onsemi | 0:098463de4c5d | 161 | } // namespace mbed |
group-onsemi | 0:098463de4c5d | 162 | |
group-onsemi | 0:098463de4c5d | 163 | #endif |