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