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