Elijah Stanger-Jones / mbed-dev-f303
Committer:
elijahsj
Date:
Mon Nov 09 00:02:47 2020 -0500
Revision:
1:8a094db1347f
test

Who changed what in which revision?

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