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