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