Magnificent7 / Hackathon
Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

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