mbed-os

Fork of mbed-os by erkin yucel

Committer:
elessair
Date:
Sun Oct 23 15:10:02 2016 +0000
Revision:
0:f269e3021894
Initial commit

Who changed what in which revision?

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