Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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