Modified I2C api to work with NRF's dynamic pin asignment.
Dependents: Seed_Barometer_Sensor_custom_I2C_lib
Note that the only changes are in the constructor. I tried extending the original I2C class, but could not find away to avoid the i2c_init from being called there.
I2CX.cpp@2:9d9fc94594e3, 2014-10-22 (annotated)
- Committer:
- jaerts
- Date:
- Wed Oct 22 11:18:05 2014 +0000
- Revision:
- 2:9d9fc94594e3
- Parent:
- 0:3d97d631d8dd
Reset after changing pins on init
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jaerts | 0:3d97d631d8dd | 1 | /* mbed Microcontroller Library |
jaerts | 0:3d97d631d8dd | 2 | * Copyright (c) 2006-2013 ARM Limited |
jaerts | 0:3d97d631d8dd | 3 | * |
jaerts | 0:3d97d631d8dd | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
jaerts | 0:3d97d631d8dd | 5 | * you may not use this file except in compliance with the License. |
jaerts | 0:3d97d631d8dd | 6 | * You may obtain a copy of the License at |
jaerts | 0:3d97d631d8dd | 7 | * |
jaerts | 0:3d97d631d8dd | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
jaerts | 0:3d97d631d8dd | 9 | * |
jaerts | 0:3d97d631d8dd | 10 | * Unless required by applicable law or agreed to in writing, software |
jaerts | 0:3d97d631d8dd | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
jaerts | 0:3d97d631d8dd | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
jaerts | 0:3d97d631d8dd | 13 | * See the License for the specific language governing permissions and |
jaerts | 0:3d97d631d8dd | 14 | * limitations under the License. |
jaerts | 0:3d97d631d8dd | 15 | */ |
jaerts | 0:3d97d631d8dd | 16 | #include "I2CX.h" |
jaerts | 0:3d97d631d8dd | 17 | |
jaerts | 0:3d97d631d8dd | 18 | #if DEVICE_I2C |
jaerts | 0:3d97d631d8dd | 19 | |
jaerts | 0:3d97d631d8dd | 20 | namespace mbed { |
jaerts | 0:3d97d631d8dd | 21 | |
jaerts | 0:3d97d631d8dd | 22 | I2CX *I2CX::_owner = NULL; |
jaerts | 0:3d97d631d8dd | 23 | |
jaerts | 0:3d97d631d8dd | 24 | I2CX::I2CX(I2CName peripheral, PinName sda, PinName scl) : _i2c(), _hz(100000) { |
jaerts | 0:3d97d631d8dd | 25 | |
jaerts | 0:3d97d631d8dd | 26 | // Hacky way to get around pin mapping |
jaerts | 0:3d97d631d8dd | 27 | if(peripheral == I2C_0) { |
jaerts | 0:3d97d631d8dd | 28 | i2c_init(&_i2c, p22, p20); |
jaerts | 0:3d97d631d8dd | 29 | } |
jaerts | 0:3d97d631d8dd | 30 | else if(peripheral == I2C_1) { |
jaerts | 0:3d97d631d8dd | 31 | i2c_init(&_i2c, p13, p15); |
jaerts | 0:3d97d631d8dd | 32 | } |
jaerts | 0:3d97d631d8dd | 33 | else { |
jaerts | 0:3d97d631d8dd | 34 | i2c_init(&_i2c, NC, NC); |
jaerts | 0:3d97d631d8dd | 35 | } |
jaerts | 0:3d97d631d8dd | 36 | |
jaerts | 0:3d97d631d8dd | 37 | _i2c.sda = sda; |
jaerts | 0:3d97d631d8dd | 38 | _i2c.scl = scl; |
jaerts | 0:3d97d631d8dd | 39 | |
jaerts | 2:9d9fc94594e3 | 40 | i2c_reset(&_i2c); |
jaerts | 2:9d9fc94594e3 | 41 | |
jaerts | 0:3d97d631d8dd | 42 | // Used to avoid unnecessary frequency updates |
jaerts | 0:3d97d631d8dd | 43 | _owner = this; |
jaerts | 0:3d97d631d8dd | 44 | } |
jaerts | 0:3d97d631d8dd | 45 | |
jaerts | 0:3d97d631d8dd | 46 | void I2CX::frequency(int hz) { |
jaerts | 0:3d97d631d8dd | 47 | _hz = hz; |
jaerts | 0:3d97d631d8dd | 48 | |
jaerts | 0:3d97d631d8dd | 49 | // We want to update the frequency even if we are already the bus owners |
jaerts | 0:3d97d631d8dd | 50 | i2c_frequency(&_i2c, _hz); |
jaerts | 0:3d97d631d8dd | 51 | |
jaerts | 0:3d97d631d8dd | 52 | // Updating the frequency of the bus we become the owners of it |
jaerts | 0:3d97d631d8dd | 53 | _owner = this; |
jaerts | 0:3d97d631d8dd | 54 | } |
jaerts | 0:3d97d631d8dd | 55 | |
jaerts | 0:3d97d631d8dd | 56 | void I2CX::aquire() { |
jaerts | 0:3d97d631d8dd | 57 | if (_owner != this) { |
jaerts | 0:3d97d631d8dd | 58 | i2c_frequency(&_i2c, _hz); |
jaerts | 0:3d97d631d8dd | 59 | _owner = this; |
jaerts | 0:3d97d631d8dd | 60 | } |
jaerts | 0:3d97d631d8dd | 61 | } |
jaerts | 0:3d97d631d8dd | 62 | |
jaerts | 0:3d97d631d8dd | 63 | // write - Master Transmitter Mode |
jaerts | 0:3d97d631d8dd | 64 | int I2CX::write(int address, const char* data, int length, bool repeated) { |
jaerts | 0:3d97d631d8dd | 65 | aquire(); |
jaerts | 0:3d97d631d8dd | 66 | |
jaerts | 0:3d97d631d8dd | 67 | int stop = (repeated) ? 0 : 1; |
jaerts | 0:3d97d631d8dd | 68 | int written = i2c_write(&_i2c, address, data, length, stop); |
jaerts | 0:3d97d631d8dd | 69 | |
jaerts | 0:3d97d631d8dd | 70 | return length != written; |
jaerts | 0:3d97d631d8dd | 71 | } |
jaerts | 0:3d97d631d8dd | 72 | |
jaerts | 0:3d97d631d8dd | 73 | int I2CX::write(int data) { |
jaerts | 0:3d97d631d8dd | 74 | return i2c_byte_write(&_i2c, data); |
jaerts | 0:3d97d631d8dd | 75 | } |
jaerts | 0:3d97d631d8dd | 76 | |
jaerts | 0:3d97d631d8dd | 77 | // read - Master Reciever Mode |
jaerts | 0:3d97d631d8dd | 78 | int I2CX::read(int address, char* data, int length, bool repeated) { |
jaerts | 0:3d97d631d8dd | 79 | aquire(); |
jaerts | 0:3d97d631d8dd | 80 | |
jaerts | 0:3d97d631d8dd | 81 | int stop = (repeated) ? 0 : 1; |
jaerts | 0:3d97d631d8dd | 82 | int read = i2c_read(&_i2c, address, data, length, stop); |
jaerts | 0:3d97d631d8dd | 83 | |
jaerts | 0:3d97d631d8dd | 84 | return length != read; |
jaerts | 0:3d97d631d8dd | 85 | } |
jaerts | 0:3d97d631d8dd | 86 | |
jaerts | 0:3d97d631d8dd | 87 | int I2CX::read(int ack) { |
jaerts | 0:3d97d631d8dd | 88 | if (ack) { |
jaerts | 0:3d97d631d8dd | 89 | return i2c_byte_read(&_i2c, 0); |
jaerts | 0:3d97d631d8dd | 90 | } else { |
jaerts | 0:3d97d631d8dd | 91 | return i2c_byte_read(&_i2c, 1); |
jaerts | 0:3d97d631d8dd | 92 | } |
jaerts | 0:3d97d631d8dd | 93 | } |
jaerts | 0:3d97d631d8dd | 94 | |
jaerts | 0:3d97d631d8dd | 95 | void I2CX::start(void) { |
jaerts | 0:3d97d631d8dd | 96 | i2c_start(&_i2c); |
jaerts | 0:3d97d631d8dd | 97 | } |
jaerts | 0:3d97d631d8dd | 98 | |
jaerts | 0:3d97d631d8dd | 99 | void I2CX::stop(void) { |
jaerts | 0:3d97d631d8dd | 100 | i2c_stop(&_i2c); |
jaerts | 0:3d97d631d8dd | 101 | } |
jaerts | 0:3d97d631d8dd | 102 | |
jaerts | 0:3d97d631d8dd | 103 | } // namespace mbed |
jaerts | 0:3d97d631d8dd | 104 | |
jaerts | 0:3d97d631d8dd | 105 | #endif |