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