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