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