mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Thu Sep 06 13:40:20 2018 +0100
Revision:
187:0387e8f68319
Parent:
184:08ed48f1de7f
Child:
189:f392fc9709a3
mbed-dev library. Release version 163

Who changed what in which revision?

UserRevisionLine numberNew contents of line
<> 149:156823d33999 1 /* mbed Microcontroller Library
<> 149:156823d33999 2 * Copyright (c) 2006-2015 ARM Limited
<> 149:156823d33999 3 *
<> 149:156823d33999 4 * Licensed under the Apache License, Version 2.0 (the "License");
<> 149:156823d33999 5 * you may not use this file except in compliance with the License.
<> 149:156823d33999 6 * You may obtain a copy of the License at
<> 149:156823d33999 7 *
<> 149:156823d33999 8 * http://www.apache.org/licenses/LICENSE-2.0
<> 149:156823d33999 9 *
<> 149:156823d33999 10 * Unless required by applicable law or agreed to in writing, software
<> 149:156823d33999 11 * distributed under the License is distributed on an "AS IS" BASIS,
<> 149:156823d33999 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
<> 149:156823d33999 13 * See the License for the specific language governing permissions and
<> 149:156823d33999 14 * limitations under the License.
<> 149:156823d33999 15 */
<> 149:156823d33999 16 #include "drivers/I2C.h"
<> 149:156823d33999 17
<> 149:156823d33999 18 #if DEVICE_I2C
<> 149:156823d33999 19
AnnaBridge 174:b96e65c34a4d 20 #if DEVICE_I2C_ASYNCH
AnnaBridge 184:08ed48f1de7f 21 #include "platform/mbed_power_mgmt.h"
AnnaBridge 174:b96e65c34a4d 22 #endif
AnnaBridge 174:b96e65c34a4d 23
<> 149:156823d33999 24 namespace mbed {
<> 149:156823d33999 25
<> 149:156823d33999 26 I2C *I2C::_owner = NULL;
<> 149:156823d33999 27 SingletonPtr<PlatformMutex> I2C::_mutex;
<> 149:156823d33999 28
<> 149:156823d33999 29 I2C::I2C(PinName sda, PinName scl) :
<> 149:156823d33999 30 #if DEVICE_I2C_ASYNCH
Anna Bridge 180:96ed750bd169 31 _irq(this), _usage(DMA_USAGE_NEVER), _deep_sleep_locked(false),
<> 149:156823d33999 32 #endif
Anna Bridge 180:96ed750bd169 33 _i2c(), _hz(100000)
Anna Bridge 180:96ed750bd169 34 {
<> 149:156823d33999 35 // No lock needed in the constructor
<> 149:156823d33999 36
<> 149:156823d33999 37 // The init function also set the frequency to 100000
<> 149:156823d33999 38 i2c_init(&_i2c, sda, scl);
<> 149:156823d33999 39
<> 149:156823d33999 40 // Used to avoid unnecessary frequency updates
<> 149:156823d33999 41 _owner = this;
<> 149:156823d33999 42 }
<> 149:156823d33999 43
AnnaBridge 187:0387e8f68319 44 void I2C::frequency(int hz)
AnnaBridge 187:0387e8f68319 45 {
<> 149:156823d33999 46 lock();
<> 149:156823d33999 47 _hz = hz;
<> 149:156823d33999 48
<> 149:156823d33999 49 // We want to update the frequency even if we are already the bus owners
<> 149:156823d33999 50 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 51
<> 149:156823d33999 52 // Updating the frequency of the bus we become the owners of it
<> 149:156823d33999 53 _owner = this;
<> 149:156823d33999 54 unlock();
<> 149:156823d33999 55 }
<> 149:156823d33999 56
AnnaBridge 187:0387e8f68319 57 void I2C::aquire()
AnnaBridge 187:0387e8f68319 58 {
<> 149:156823d33999 59 lock();
<> 149:156823d33999 60 if (_owner != this) {
<> 149:156823d33999 61 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 62 _owner = this;
<> 149:156823d33999 63 }
<> 149:156823d33999 64 unlock();
<> 149:156823d33999 65 }
<> 149:156823d33999 66
<> 149:156823d33999 67 // write - Master Transmitter Mode
AnnaBridge 187:0387e8f68319 68 int I2C::write(int address, const char *data, int length, bool repeated)
AnnaBridge 187:0387e8f68319 69 {
<> 149:156823d33999 70 lock();
<> 149:156823d33999 71 aquire();
<> 149:156823d33999 72
<> 149:156823d33999 73 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 74 int written = i2c_write(&_i2c, address, data, length, stop);
<> 149:156823d33999 75
<> 149:156823d33999 76 unlock();
<> 149:156823d33999 77 return length != written;
<> 149:156823d33999 78 }
<> 149:156823d33999 79
AnnaBridge 187:0387e8f68319 80 int I2C::write(int data)
AnnaBridge 187:0387e8f68319 81 {
<> 149:156823d33999 82 lock();
<> 149:156823d33999 83 int ret = i2c_byte_write(&_i2c, data);
<> 149:156823d33999 84 unlock();
<> 149:156823d33999 85 return ret;
<> 149:156823d33999 86 }
<> 149:156823d33999 87
AnnaBridge 184:08ed48f1de7f 88 // read - Master Receiver Mode
AnnaBridge 187:0387e8f68319 89 int I2C::read(int address, char *data, int length, bool repeated)
AnnaBridge 187:0387e8f68319 90 {
<> 149:156823d33999 91 lock();
<> 149:156823d33999 92 aquire();
<> 149:156823d33999 93
<> 149:156823d33999 94 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 95 int read = i2c_read(&_i2c, address, data, length, stop);
<> 149:156823d33999 96
<> 149:156823d33999 97 unlock();
<> 149:156823d33999 98 return length != read;
<> 149:156823d33999 99 }
<> 149:156823d33999 100
AnnaBridge 187:0387e8f68319 101 int I2C::read(int ack)
AnnaBridge 187:0387e8f68319 102 {
<> 149:156823d33999 103 lock();
<> 149:156823d33999 104 int ret;
<> 149:156823d33999 105 if (ack) {
<> 149:156823d33999 106 ret = i2c_byte_read(&_i2c, 0);
<> 149:156823d33999 107 } else {
<> 149:156823d33999 108 ret = i2c_byte_read(&_i2c, 1);
<> 149:156823d33999 109 }
<> 149:156823d33999 110 unlock();
<> 149:156823d33999 111 return ret;
<> 149:156823d33999 112 }
<> 149:156823d33999 113
AnnaBridge 187:0387e8f68319 114 void I2C::start(void)
AnnaBridge 187:0387e8f68319 115 {
<> 149:156823d33999 116 lock();
<> 149:156823d33999 117 i2c_start(&_i2c);
<> 149:156823d33999 118 unlock();
<> 149:156823d33999 119 }
<> 149:156823d33999 120
AnnaBridge 187:0387e8f68319 121 void I2C::stop(void)
AnnaBridge 187:0387e8f68319 122 {
<> 149:156823d33999 123 lock();
<> 149:156823d33999 124 i2c_stop(&_i2c);
<> 149:156823d33999 125 unlock();
<> 149:156823d33999 126 }
<> 149:156823d33999 127
AnnaBridge 187:0387e8f68319 128 void I2C::lock()
AnnaBridge 187:0387e8f68319 129 {
<> 149:156823d33999 130 _mutex->lock();
<> 149:156823d33999 131 }
<> 149:156823d33999 132
AnnaBridge 187:0387e8f68319 133 void I2C::unlock()
AnnaBridge 187:0387e8f68319 134 {
<> 149:156823d33999 135 _mutex->unlock();
<> 149:156823d33999 136 }
<> 149:156823d33999 137
<> 149:156823d33999 138 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 139
AnnaBridge 187:0387e8f68319 140 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)
<> 149:156823d33999 141 {
<> 149:156823d33999 142 lock();
<> 149:156823d33999 143 if (i2c_active(&_i2c)) {
<> 149:156823d33999 144 unlock();
<> 149:156823d33999 145 return -1; // transaction ongoing
<> 149:156823d33999 146 }
Anna Bridge 180:96ed750bd169 147 lock_deep_sleep();
<> 149:156823d33999 148 aquire();
<> 149:156823d33999 149
<> 149:156823d33999 150 _callback = callback;
<> 149:156823d33999 151 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 152 _irq.callback(&I2C::irq_handler_asynch);
<> 149:156823d33999 153 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
<> 149:156823d33999 154 unlock();
<> 149:156823d33999 155 return 0;
<> 149:156823d33999 156 }
<> 149:156823d33999 157
<> 149:156823d33999 158 void I2C::abort_transfer(void)
<> 149:156823d33999 159 {
<> 149:156823d33999 160 lock();
<> 149:156823d33999 161 i2c_abort_asynch(&_i2c);
Anna Bridge 180:96ed750bd169 162 unlock_deep_sleep();
<> 149:156823d33999 163 unlock();
<> 149:156823d33999 164 }
<> 149:156823d33999 165
<> 149:156823d33999 166 void I2C::irq_handler_asynch(void)
<> 149:156823d33999 167 {
<> 149:156823d33999 168 int event = i2c_irq_handler_asynch(&_i2c);
<> 149:156823d33999 169 if (_callback && event) {
<> 149:156823d33999 170 _callback.call(event);
<> 149:156823d33999 171 }
AnnaBridge 174:b96e65c34a4d 172 if (event) {
Anna Bridge 180:96ed750bd169 173 unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 174 }
<> 149:156823d33999 175
<> 149:156823d33999 176 }
<> 149:156823d33999 177
Anna Bridge 180:96ed750bd169 178 void I2C::lock_deep_sleep()
Anna Bridge 180:96ed750bd169 179 {
Anna Bridge 180:96ed750bd169 180 if (_deep_sleep_locked == false) {
Anna Bridge 180:96ed750bd169 181 sleep_manager_lock_deep_sleep();
Anna Bridge 180:96ed750bd169 182 _deep_sleep_locked = true;
Anna Bridge 180:96ed750bd169 183 }
Anna Bridge 180:96ed750bd169 184 }
Anna Bridge 180:96ed750bd169 185
Anna Bridge 180:96ed750bd169 186 void I2C::unlock_deep_sleep()
Anna Bridge 180:96ed750bd169 187 {
Anna Bridge 180:96ed750bd169 188 if (_deep_sleep_locked == true) {
Anna Bridge 180:96ed750bd169 189 sleep_manager_unlock_deep_sleep();
Anna Bridge 180:96ed750bd169 190 _deep_sleep_locked = false;
Anna Bridge 180:96ed750bd169 191 }
Anna Bridge 180:96ed750bd169 192 }
<> 149:156823d33999 193
<> 149:156823d33999 194 #endif
<> 149:156823d33999 195
<> 149:156823d33999 196 } // namespace mbed
<> 149:156823d33999 197
<> 149:156823d33999 198 #endif