mbed library sources
common/I2C.cpp@0:0a673c671a56, 2016-07-27 (annotated)
- Committer:
- ebrus
- Date:
- Wed Jul 27 18:35:32 2016 +0000
- Revision:
- 0:0a673c671a56
4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ebrus | 0:0a673c671a56 | 1 | /* mbed Microcontroller Library |
ebrus | 0:0a673c671a56 | 2 | * Copyright (c) 2006-2013 ARM Limited |
ebrus | 0:0a673c671a56 | 3 | * |
ebrus | 0:0a673c671a56 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
ebrus | 0:0a673c671a56 | 5 | * you may not use this file except in compliance with the License. |
ebrus | 0:0a673c671a56 | 6 | * You may obtain a copy of the License at |
ebrus | 0:0a673c671a56 | 7 | * |
ebrus | 0:0a673c671a56 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ebrus | 0:0a673c671a56 | 9 | * |
ebrus | 0:0a673c671a56 | 10 | * Unless required by applicable law or agreed to in writing, software |
ebrus | 0:0a673c671a56 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
ebrus | 0:0a673c671a56 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ebrus | 0:0a673c671a56 | 13 | * See the License for the specific language governing permissions and |
ebrus | 0:0a673c671a56 | 14 | * limitations under the License. |
ebrus | 0:0a673c671a56 | 15 | */ |
ebrus | 0:0a673c671a56 | 16 | #include "I2C.h" |
ebrus | 0:0a673c671a56 | 17 | |
ebrus | 0:0a673c671a56 | 18 | #if DEVICE_I2C |
ebrus | 0:0a673c671a56 | 19 | |
ebrus | 0:0a673c671a56 | 20 | namespace mbed { |
ebrus | 0:0a673c671a56 | 21 | |
ebrus | 0:0a673c671a56 | 22 | I2C *I2C::_owner = NULL; |
ebrus | 0:0a673c671a56 | 23 | |
ebrus | 0:0a673c671a56 | 24 | I2C::I2C(PinName sda, PinName scl) : _i2c(), _hz(100000) { |
ebrus | 0:0a673c671a56 | 25 | // The init function also set the frequency to 100000 |
ebrus | 0:0a673c671a56 | 26 | i2c_init(&_i2c, sda, scl); |
ebrus | 0:0a673c671a56 | 27 | |
ebrus | 0:0a673c671a56 | 28 | // Used to avoid unnecessary frequency updates |
ebrus | 0:0a673c671a56 | 29 | _owner = this; |
ebrus | 0:0a673c671a56 | 30 | } |
ebrus | 0:0a673c671a56 | 31 | |
ebrus | 0:0a673c671a56 | 32 | void I2C::frequency(int hz) { |
ebrus | 0:0a673c671a56 | 33 | _hz = hz; |
ebrus | 0:0a673c671a56 | 34 | |
ebrus | 0:0a673c671a56 | 35 | // We want to update the frequency even if we are already the bus owners |
ebrus | 0:0a673c671a56 | 36 | i2c_frequency(&_i2c, _hz); |
ebrus | 0:0a673c671a56 | 37 | |
ebrus | 0:0a673c671a56 | 38 | // Updating the frequency of the bus we become the owners of it |
ebrus | 0:0a673c671a56 | 39 | _owner = this; |
ebrus | 0:0a673c671a56 | 40 | } |
ebrus | 0:0a673c671a56 | 41 | |
ebrus | 0:0a673c671a56 | 42 | void I2C::aquire() { |
ebrus | 0:0a673c671a56 | 43 | if (_owner != this) { |
ebrus | 0:0a673c671a56 | 44 | i2c_frequency(&_i2c, _hz); |
ebrus | 0:0a673c671a56 | 45 | _owner = this; |
ebrus | 0:0a673c671a56 | 46 | } |
ebrus | 0:0a673c671a56 | 47 | } |
ebrus | 0:0a673c671a56 | 48 | |
ebrus | 0:0a673c671a56 | 49 | // write - Master Transmitter Mode |
ebrus | 0:0a673c671a56 | 50 | int I2C::write(int address, const char* data, int length, bool repeated) { |
ebrus | 0:0a673c671a56 | 51 | aquire(); |
ebrus | 0:0a673c671a56 | 52 | |
ebrus | 0:0a673c671a56 | 53 | int stop = (repeated) ? 0 : 1; |
ebrus | 0:0a673c671a56 | 54 | int written = i2c_write(&_i2c, address, data, length, stop); |
ebrus | 0:0a673c671a56 | 55 | |
ebrus | 0:0a673c671a56 | 56 | return length != written; |
ebrus | 0:0a673c671a56 | 57 | } |
ebrus | 0:0a673c671a56 | 58 | |
ebrus | 0:0a673c671a56 | 59 | int I2C::write(int data) { |
ebrus | 0:0a673c671a56 | 60 | return i2c_byte_write(&_i2c, data); |
ebrus | 0:0a673c671a56 | 61 | } |
ebrus | 0:0a673c671a56 | 62 | |
ebrus | 0:0a673c671a56 | 63 | // read - Master Reciever Mode |
ebrus | 0:0a673c671a56 | 64 | int I2C::read(int address, char* data, int length, bool repeated) { |
ebrus | 0:0a673c671a56 | 65 | aquire(); |
ebrus | 0:0a673c671a56 | 66 | |
ebrus | 0:0a673c671a56 | 67 | int stop = (repeated) ? 0 : 1; |
ebrus | 0:0a673c671a56 | 68 | int read = i2c_read(&_i2c, address, data, length, stop); |
ebrus | 0:0a673c671a56 | 69 | |
ebrus | 0:0a673c671a56 | 70 | return length != read; |
ebrus | 0:0a673c671a56 | 71 | } |
ebrus | 0:0a673c671a56 | 72 | |
ebrus | 0:0a673c671a56 | 73 | int I2C::read(int ack) { |
ebrus | 0:0a673c671a56 | 74 | if (ack) { |
ebrus | 0:0a673c671a56 | 75 | return i2c_byte_read(&_i2c, 0); |
ebrus | 0:0a673c671a56 | 76 | } else { |
ebrus | 0:0a673c671a56 | 77 | return i2c_byte_read(&_i2c, 1); |
ebrus | 0:0a673c671a56 | 78 | } |
ebrus | 0:0a673c671a56 | 79 | } |
ebrus | 0:0a673c671a56 | 80 | |
ebrus | 0:0a673c671a56 | 81 | void I2C::start(void) { |
ebrus | 0:0a673c671a56 | 82 | i2c_start(&_i2c); |
ebrus | 0:0a673c671a56 | 83 | } |
ebrus | 0:0a673c671a56 | 84 | |
ebrus | 0:0a673c671a56 | 85 | void I2C::stop(void) { |
ebrus | 0:0a673c671a56 | 86 | i2c_stop(&_i2c); |
ebrus | 0:0a673c671a56 | 87 | } |
ebrus | 0:0a673c671a56 | 88 | |
ebrus | 0:0a673c671a56 | 89 | } // namespace mbed |
ebrus | 0:0a673c671a56 | 90 | |
ebrus | 0:0a673c671a56 | 91 | #endif |