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