テスト用です。

Dependencies:   mbed

Committer:
jksoft
Date:
Tue Oct 11 11:09:42 2016 +0000
Revision:
0:8468a4403fea
SB??ver;

Who changed what in which revision?

UserRevisionLine numberNew 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 #ifndef MBED_MYI2C_H
jksoft 0:8468a4403fea 17 #define MBED_MYI2C_H
jksoft 0:8468a4403fea 18
jksoft 0:8468a4403fea 19 //#include "platform.h"
jksoft 0:8468a4403fea 20
jksoft 0:8468a4403fea 21 //#if DEVICE_MYI2C
jksoft 0:8468a4403fea 22
jksoft 0:8468a4403fea 23 #include "myi2c_api.h"
jksoft 0:8468a4403fea 24
jksoft 0:8468a4403fea 25 namespace mbed {
jksoft 0:8468a4403fea 26
jksoft 0:8468a4403fea 27 /** An I2C Master, used for communicating with I2C slave devices
jksoft 0:8468a4403fea 28 *
jksoft 0:8468a4403fea 29 * Example:
jksoft 0:8468a4403fea 30 * @code
jksoft 0:8468a4403fea 31 * // Read from I2C slave at address 0x62
jksoft 0:8468a4403fea 32 *
jksoft 0:8468a4403fea 33 * #include "mbed.h"
jksoft 0:8468a4403fea 34 *
jksoft 0:8468a4403fea 35 * I2C i2c(p28, p27);
jksoft 0:8468a4403fea 36 *
jksoft 0:8468a4403fea 37 * int main() {
jksoft 0:8468a4403fea 38 * int address = 0x62;
jksoft 0:8468a4403fea 39 * char data[2];
jksoft 0:8468a4403fea 40 * i2c.read(address, data, 2);
jksoft 0:8468a4403fea 41 * }
jksoft 0:8468a4403fea 42 * @endcode
jksoft 0:8468a4403fea 43 */
jksoft 0:8468a4403fea 44 class myI2C {
jksoft 0:8468a4403fea 45
jksoft 0:8468a4403fea 46 public:
jksoft 0:8468a4403fea 47 enum RxStatus {
jksoft 0:8468a4403fea 48 NoData,
jksoft 0:8468a4403fea 49 MasterGeneralCall,
jksoft 0:8468a4403fea 50 MasterWrite,
jksoft 0:8468a4403fea 51 MasterRead
jksoft 0:8468a4403fea 52 };
jksoft 0:8468a4403fea 53
jksoft 0:8468a4403fea 54 enum Acknowledge {
jksoft 0:8468a4403fea 55 NoACK = 0,
jksoft 0:8468a4403fea 56 ACK = 1
jksoft 0:8468a4403fea 57 };
jksoft 0:8468a4403fea 58
jksoft 0:8468a4403fea 59 /** Create an I2C Master interface, connected to the specified pins
jksoft 0:8468a4403fea 60 *
jksoft 0:8468a4403fea 61 * @param sda I2C data line pin
jksoft 0:8468a4403fea 62 * @param scl I2C clock line pin
jksoft 0:8468a4403fea 63 */
jksoft 0:8468a4403fea 64 myI2C(PinName sda, PinName scl);
jksoft 0:8468a4403fea 65
jksoft 0:8468a4403fea 66 /** Set the frequency of the I2C interface
jksoft 0:8468a4403fea 67 *
jksoft 0:8468a4403fea 68 * @param hz The bus frequency in hertz
jksoft 0:8468a4403fea 69 */
jksoft 0:8468a4403fea 70 void frequency(int hz);
jksoft 0:8468a4403fea 71
jksoft 0:8468a4403fea 72 /** Read from an I2C slave
jksoft 0:8468a4403fea 73 *
jksoft 0:8468a4403fea 74 * Performs a complete read transaction. The bottom bit of
jksoft 0:8468a4403fea 75 * the address is forced to 1 to indicate a read.
jksoft 0:8468a4403fea 76 *
jksoft 0:8468a4403fea 77 * @param address 8-bit I2C slave address [ addr | 1 ]
jksoft 0:8468a4403fea 78 * @param data Pointer to the byte-array to read data in to
jksoft 0:8468a4403fea 79 * @param length Number of bytes to read
jksoft 0:8468a4403fea 80 * @param repeated Repeated start, true - don't send stop at end
jksoft 0:8468a4403fea 81 *
jksoft 0:8468a4403fea 82 * @returns
jksoft 0:8468a4403fea 83 * 0 on success (ack),
jksoft 0:8468a4403fea 84 * non-0 on failure (nack)
jksoft 0:8468a4403fea 85 */
jksoft 0:8468a4403fea 86 int read(int address, char *data, int length, bool repeated = false);
jksoft 0:8468a4403fea 87
jksoft 0:8468a4403fea 88 /** Read a single byte from the I2C bus
jksoft 0:8468a4403fea 89 *
jksoft 0:8468a4403fea 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
jksoft 0:8468a4403fea 91 *
jksoft 0:8468a4403fea 92 * @returns
jksoft 0:8468a4403fea 93 * the byte read
jksoft 0:8468a4403fea 94 */
jksoft 0:8468a4403fea 95 int read(int ack);
jksoft 0:8468a4403fea 96
jksoft 0:8468a4403fea 97 /** Write to an I2C slave
jksoft 0:8468a4403fea 98 *
jksoft 0:8468a4403fea 99 * Performs a complete write transaction. The bottom bit of
jksoft 0:8468a4403fea 100 * the address is forced to 0 to indicate a write.
jksoft 0:8468a4403fea 101 *
jksoft 0:8468a4403fea 102 * @param address 8-bit I2C slave address [ addr | 0 ]
jksoft 0:8468a4403fea 103 * @param data Pointer to the byte-array data to send
jksoft 0:8468a4403fea 104 * @param length Number of bytes to send
jksoft 0:8468a4403fea 105 * @param repeated Repeated start, true - do not send stop at end
jksoft 0:8468a4403fea 106 *
jksoft 0:8468a4403fea 107 * @returns
jksoft 0:8468a4403fea 108 * 0 on success (ack),
jksoft 0:8468a4403fea 109 * non-0 on failure (nack)
jksoft 0:8468a4403fea 110 */
jksoft 0:8468a4403fea 111 int write(int address, const char *data, int length, bool repeated = false);
jksoft 0:8468a4403fea 112
jksoft 0:8468a4403fea 113 /** Write single byte out on the I2C bus
jksoft 0:8468a4403fea 114 *
jksoft 0:8468a4403fea 115 * @param data data to write out on bus
jksoft 0:8468a4403fea 116 *
jksoft 0:8468a4403fea 117 * @returns
jksoft 0:8468a4403fea 118 * '1' if an ACK was received,
jksoft 0:8468a4403fea 119 * '0' otherwise
jksoft 0:8468a4403fea 120 */
jksoft 0:8468a4403fea 121 int write(int data);
jksoft 0:8468a4403fea 122
jksoft 0:8468a4403fea 123 /** Creates a start condition on the I2C bus
jksoft 0:8468a4403fea 124 */
jksoft 0:8468a4403fea 125
jksoft 0:8468a4403fea 126 void start(void);
jksoft 0:8468a4403fea 127
jksoft 0:8468a4403fea 128 /** Creates a stop condition on the I2C bus
jksoft 0:8468a4403fea 129 */
jksoft 0:8468a4403fea 130 void stop(void);
jksoft 0:8468a4403fea 131
jksoft 0:8468a4403fea 132 protected:
jksoft 0:8468a4403fea 133 void aquire();
jksoft 0:8468a4403fea 134
jksoft 0:8468a4403fea 135 i2c_t _i2c;
jksoft 0:8468a4403fea 136 static myI2C *_owner;
jksoft 0:8468a4403fea 137 int _hz;
jksoft 0:8468a4403fea 138 };
jksoft 0:8468a4403fea 139
jksoft 0:8468a4403fea 140 } // namespace mbed
jksoft 0:8468a4403fea 141
jksoft 0:8468a4403fea 142 //#endif
jksoft 0:8468a4403fea 143
jksoft 0:8468a4403fea 144 #endif