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