mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Fri Feb 16 16:09:33 2018 +0000
Revision:
181:57724642e740
Parent:
180:96ed750bd169
Child:
184:08ed48f1de7f
mbed-dev library. Release version 159.

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 174:b96e65c34a4d 21 #include "platform/mbed_sleep.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
<> 149:156823d33999 44 void I2C::frequency(int hz) {
<> 149:156823d33999 45 lock();
<> 149:156823d33999 46 _hz = hz;
<> 149:156823d33999 47
<> 149:156823d33999 48 // We want to update the frequency even if we are already the bus owners
<> 149:156823d33999 49 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 50
<> 149:156823d33999 51 // Updating the frequency of the bus we become the owners of it
<> 149:156823d33999 52 _owner = this;
<> 149:156823d33999 53 unlock();
<> 149:156823d33999 54 }
<> 149:156823d33999 55
<> 149:156823d33999 56 void I2C::aquire() {
<> 149:156823d33999 57 lock();
<> 149:156823d33999 58 if (_owner != this) {
<> 149:156823d33999 59 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 60 _owner = this;
<> 149:156823d33999 61 }
<> 149:156823d33999 62 unlock();
<> 149:156823d33999 63 }
<> 149:156823d33999 64
<> 149:156823d33999 65 // write - Master Transmitter Mode
<> 149:156823d33999 66 int I2C::write(int address, const char* data, int length, bool repeated) {
<> 149:156823d33999 67 lock();
<> 149:156823d33999 68 aquire();
<> 149:156823d33999 69
<> 149:156823d33999 70 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 71 int written = i2c_write(&_i2c, address, data, length, stop);
<> 149:156823d33999 72
<> 149:156823d33999 73 unlock();
<> 149:156823d33999 74 return length != written;
<> 149:156823d33999 75 }
<> 149:156823d33999 76
<> 149:156823d33999 77 int I2C::write(int data) {
<> 149:156823d33999 78 lock();
<> 149:156823d33999 79 int ret = i2c_byte_write(&_i2c, data);
<> 149:156823d33999 80 unlock();
<> 149:156823d33999 81 return ret;
<> 149:156823d33999 82 }
<> 149:156823d33999 83
<> 149:156823d33999 84 // read - Master Reciever Mode
<> 149:156823d33999 85 int I2C::read(int address, char* data, int length, bool repeated) {
<> 149:156823d33999 86 lock();
<> 149:156823d33999 87 aquire();
<> 149:156823d33999 88
<> 149:156823d33999 89 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 90 int read = i2c_read(&_i2c, address, data, length, stop);
<> 149:156823d33999 91
<> 149:156823d33999 92 unlock();
<> 149:156823d33999 93 return length != read;
<> 149:156823d33999 94 }
<> 149:156823d33999 95
<> 149:156823d33999 96 int I2C::read(int ack) {
<> 149:156823d33999 97 lock();
<> 149:156823d33999 98 int ret;
<> 149:156823d33999 99 if (ack) {
<> 149:156823d33999 100 ret = i2c_byte_read(&_i2c, 0);
<> 149:156823d33999 101 } else {
<> 149:156823d33999 102 ret = i2c_byte_read(&_i2c, 1);
<> 149:156823d33999 103 }
<> 149:156823d33999 104 unlock();
<> 149:156823d33999 105 return ret;
<> 149:156823d33999 106 }
<> 149:156823d33999 107
<> 149:156823d33999 108 void I2C::start(void) {
<> 149:156823d33999 109 lock();
<> 149:156823d33999 110 i2c_start(&_i2c);
<> 149:156823d33999 111 unlock();
<> 149:156823d33999 112 }
<> 149:156823d33999 113
<> 149:156823d33999 114 void I2C::stop(void) {
<> 149:156823d33999 115 lock();
<> 149:156823d33999 116 i2c_stop(&_i2c);
<> 149:156823d33999 117 unlock();
<> 149:156823d33999 118 }
<> 149:156823d33999 119
<> 149:156823d33999 120 void I2C::lock() {
<> 149:156823d33999 121 _mutex->lock();
<> 149:156823d33999 122 }
<> 149:156823d33999 123
<> 149:156823d33999 124 void I2C::unlock() {
<> 149:156823d33999 125 _mutex->unlock();
<> 149:156823d33999 126 }
<> 149:156823d33999 127
<> 149:156823d33999 128 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 129
<> 149:156823d33999 130 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 131 {
<> 149:156823d33999 132 lock();
<> 149:156823d33999 133 if (i2c_active(&_i2c)) {
<> 149:156823d33999 134 unlock();
<> 149:156823d33999 135 return -1; // transaction ongoing
<> 149:156823d33999 136 }
Anna Bridge 180:96ed750bd169 137 lock_deep_sleep();
<> 149:156823d33999 138 aquire();
<> 149:156823d33999 139
<> 149:156823d33999 140 _callback = callback;
<> 149:156823d33999 141 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 142 _irq.callback(&I2C::irq_handler_asynch);
<> 149:156823d33999 143 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
<> 149:156823d33999 144 unlock();
<> 149:156823d33999 145 return 0;
<> 149:156823d33999 146 }
<> 149:156823d33999 147
<> 149:156823d33999 148 void I2C::abort_transfer(void)
<> 149:156823d33999 149 {
<> 149:156823d33999 150 lock();
<> 149:156823d33999 151 i2c_abort_asynch(&_i2c);
Anna Bridge 180:96ed750bd169 152 unlock_deep_sleep();
<> 149:156823d33999 153 unlock();
<> 149:156823d33999 154 }
<> 149:156823d33999 155
<> 149:156823d33999 156 void I2C::irq_handler_asynch(void)
<> 149:156823d33999 157 {
<> 149:156823d33999 158 int event = i2c_irq_handler_asynch(&_i2c);
<> 149:156823d33999 159 if (_callback && event) {
<> 149:156823d33999 160 _callback.call(event);
<> 149:156823d33999 161 }
AnnaBridge 174:b96e65c34a4d 162 if (event) {
Anna Bridge 180:96ed750bd169 163 unlock_deep_sleep();
AnnaBridge 174:b96e65c34a4d 164 }
<> 149:156823d33999 165
<> 149:156823d33999 166 }
<> 149:156823d33999 167
Anna Bridge 180:96ed750bd169 168 void I2C::lock_deep_sleep()
Anna Bridge 180:96ed750bd169 169 {
Anna Bridge 180:96ed750bd169 170 if (_deep_sleep_locked == false) {
Anna Bridge 180:96ed750bd169 171 sleep_manager_lock_deep_sleep();
Anna Bridge 180:96ed750bd169 172 _deep_sleep_locked = true;
Anna Bridge 180:96ed750bd169 173 }
Anna Bridge 180:96ed750bd169 174 }
Anna Bridge 180:96ed750bd169 175
Anna Bridge 180:96ed750bd169 176 void I2C::unlock_deep_sleep()
Anna Bridge 180:96ed750bd169 177 {
Anna Bridge 180:96ed750bd169 178 if (_deep_sleep_locked == true) {
Anna Bridge 180:96ed750bd169 179 sleep_manager_unlock_deep_sleep();
Anna Bridge 180:96ed750bd169 180 _deep_sleep_locked = false;
Anna Bridge 180:96ed750bd169 181 }
Anna Bridge 180:96ed750bd169 182 }
<> 149:156823d33999 183
<> 149:156823d33999 184 #endif
<> 149:156823d33999 185
<> 149:156823d33999 186 } // namespace mbed
<> 149:156823d33999 187
<> 149:156823d33999 188 #endif