Nicolas Borla / Mbed OS ROME2_Robot_Firmware
Committer:
boro
Date:
Mon Mar 16 13:12:31 2020 +0000
Revision:
0:4beb2ea291ec
a

Who changed what in which revision?

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