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