Forked.

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Oct 28 11:17:30 2016 +0100
Revision:
149:156823d33999
This updates the lib to the mbed lib v128

NOTE: This release includes a restructuring of the file and directory locations and thus some
include paths in your code may need updating accordingly.

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
<> 149:156823d33999 20 namespace mbed {
<> 149:156823d33999 21
<> 149:156823d33999 22 I2C *I2C::_owner = NULL;
<> 149:156823d33999 23 SingletonPtr<PlatformMutex> I2C::_mutex;
<> 149:156823d33999 24
<> 149:156823d33999 25 I2C::I2C(PinName sda, PinName scl) :
<> 149:156823d33999 26 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 27 _irq(this), _usage(DMA_USAGE_NEVER),
<> 149:156823d33999 28 #endif
<> 149:156823d33999 29 _i2c(), _hz(100000) {
<> 149:156823d33999 30 // No lock needed in the constructor
<> 149:156823d33999 31
<> 149:156823d33999 32 // The init function also set the frequency to 100000
<> 149:156823d33999 33 i2c_init(&_i2c, sda, scl);
<> 149:156823d33999 34
<> 149:156823d33999 35 // Used to avoid unnecessary frequency updates
<> 149:156823d33999 36 _owner = this;
<> 149:156823d33999 37 }
<> 149:156823d33999 38
<> 149:156823d33999 39 void I2C::frequency(int hz) {
<> 149:156823d33999 40 lock();
<> 149:156823d33999 41 _hz = hz;
<> 149:156823d33999 42
<> 149:156823d33999 43 // We want to update the frequency even if we are already the bus owners
<> 149:156823d33999 44 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 45
<> 149:156823d33999 46 // Updating the frequency of the bus we become the owners of it
<> 149:156823d33999 47 _owner = this;
<> 149:156823d33999 48 unlock();
<> 149:156823d33999 49 }
<> 149:156823d33999 50
<> 149:156823d33999 51 void I2C::aquire() {
<> 149:156823d33999 52 lock();
<> 149:156823d33999 53 if (_owner != this) {
<> 149:156823d33999 54 i2c_frequency(&_i2c, _hz);
<> 149:156823d33999 55 _owner = this;
<> 149:156823d33999 56 }
<> 149:156823d33999 57 unlock();
<> 149:156823d33999 58 }
<> 149:156823d33999 59
<> 149:156823d33999 60 // write - Master Transmitter Mode
<> 149:156823d33999 61 int I2C::write(int address, const char* data, int length, bool repeated) {
<> 149:156823d33999 62 lock();
<> 149:156823d33999 63 aquire();
<> 149:156823d33999 64
<> 149:156823d33999 65 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 66 int written = i2c_write(&_i2c, address, data, length, stop);
<> 149:156823d33999 67
<> 149:156823d33999 68 unlock();
<> 149:156823d33999 69 return length != written;
<> 149:156823d33999 70 }
<> 149:156823d33999 71
<> 149:156823d33999 72 int I2C::write(int data) {
<> 149:156823d33999 73 lock();
<> 149:156823d33999 74 int ret = i2c_byte_write(&_i2c, data);
<> 149:156823d33999 75 unlock();
<> 149:156823d33999 76 return ret;
<> 149:156823d33999 77 }
<> 149:156823d33999 78
<> 149:156823d33999 79 // read - Master Reciever Mode
<> 149:156823d33999 80 int I2C::read(int address, char* data, int length, bool repeated) {
<> 149:156823d33999 81 lock();
<> 149:156823d33999 82 aquire();
<> 149:156823d33999 83
<> 149:156823d33999 84 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 85 int read = i2c_read(&_i2c, address, data, length, stop);
<> 149:156823d33999 86
<> 149:156823d33999 87 unlock();
<> 149:156823d33999 88 return length != read;
<> 149:156823d33999 89 }
<> 149:156823d33999 90
<> 149:156823d33999 91 int I2C::read(int ack) {
<> 149:156823d33999 92 lock();
<> 149:156823d33999 93 int ret;
<> 149:156823d33999 94 if (ack) {
<> 149:156823d33999 95 ret = i2c_byte_read(&_i2c, 0);
<> 149:156823d33999 96 } else {
<> 149:156823d33999 97 ret = i2c_byte_read(&_i2c, 1);
<> 149:156823d33999 98 }
<> 149:156823d33999 99 unlock();
<> 149:156823d33999 100 return ret;
<> 149:156823d33999 101 }
<> 149:156823d33999 102
<> 149:156823d33999 103 void I2C::start(void) {
<> 149:156823d33999 104 lock();
<> 149:156823d33999 105 i2c_start(&_i2c);
<> 149:156823d33999 106 unlock();
<> 149:156823d33999 107 }
<> 149:156823d33999 108
<> 149:156823d33999 109 void I2C::stop(void) {
<> 149:156823d33999 110 lock();
<> 149:156823d33999 111 i2c_stop(&_i2c);
<> 149:156823d33999 112 unlock();
<> 149:156823d33999 113 }
<> 149:156823d33999 114
<> 149:156823d33999 115 void I2C::lock() {
<> 149:156823d33999 116 _mutex->lock();
<> 149:156823d33999 117 }
<> 149:156823d33999 118
<> 149:156823d33999 119 void I2C::unlock() {
<> 149:156823d33999 120 _mutex->unlock();
<> 149:156823d33999 121 }
<> 149:156823d33999 122
<> 149:156823d33999 123 #if DEVICE_I2C_ASYNCH
<> 149:156823d33999 124
<> 149:156823d33999 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)
<> 149:156823d33999 126 {
<> 149:156823d33999 127 lock();
<> 149:156823d33999 128 if (i2c_active(&_i2c)) {
<> 149:156823d33999 129 unlock();
<> 149:156823d33999 130 return -1; // transaction ongoing
<> 149:156823d33999 131 }
<> 149:156823d33999 132 aquire();
<> 149:156823d33999 133
<> 149:156823d33999 134 _callback = callback;
<> 149:156823d33999 135 int stop = (repeated) ? 0 : 1;
<> 149:156823d33999 136 _irq.callback(&I2C::irq_handler_asynch);
<> 149:156823d33999 137 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
<> 149:156823d33999 138 unlock();
<> 149:156823d33999 139 return 0;
<> 149:156823d33999 140 }
<> 149:156823d33999 141
<> 149:156823d33999 142 void I2C::abort_transfer(void)
<> 149:156823d33999 143 {
<> 149:156823d33999 144 lock();
<> 149:156823d33999 145 i2c_abort_asynch(&_i2c);
<> 149:156823d33999 146 unlock();
<> 149:156823d33999 147 }
<> 149:156823d33999 148
<> 149:156823d33999 149 void I2C::irq_handler_asynch(void)
<> 149:156823d33999 150 {
<> 149:156823d33999 151 int event = i2c_irq_handler_asynch(&_i2c);
<> 149:156823d33999 152 if (_callback && event) {
<> 149:156823d33999 153 _callback.call(event);
<> 149:156823d33999 154 }
<> 149:156823d33999 155
<> 149:156823d33999 156 }
<> 149:156823d33999 157
<> 149:156823d33999 158
<> 149:156823d33999 159 #endif
<> 149:156823d33999 160
<> 149:156823d33999 161 } // namespace mbed
<> 149:156823d33999 162
<> 149:156823d33999 163 #endif