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