change some io settings for TWR-K22F-120M

Dependents:   twr_helloworld

Committer:
Jasper_lee
Date:
Tue Dec 23 03:35:08 2014 +0000
Revision:
0:b16d94660a33
change some io setting used in TWR-K22F120M

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Jasper_lee 0:b16d94660a33 1 /* mbed Microcontroller Library
Jasper_lee 0:b16d94660a33 2 * Copyright (c) 2006-2013 ARM Limited
Jasper_lee 0:b16d94660a33 3 *
Jasper_lee 0:b16d94660a33 4 * Licensed under the Apache License, Version 2.0 (the "License");
Jasper_lee 0:b16d94660a33 5 * you may not use this file except in compliance with the License.
Jasper_lee 0:b16d94660a33 6 * You may obtain a copy of the License at
Jasper_lee 0:b16d94660a33 7 *
Jasper_lee 0:b16d94660a33 8 * http://www.apache.org/licenses/LICENSE-2.0
Jasper_lee 0:b16d94660a33 9 *
Jasper_lee 0:b16d94660a33 10 * Unless required by applicable law or agreed to in writing, software
Jasper_lee 0:b16d94660a33 11 * distributed under the License is distributed on an "AS IS" BASIS,
Jasper_lee 0:b16d94660a33 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Jasper_lee 0:b16d94660a33 13 * See the License for the specific language governing permissions and
Jasper_lee 0:b16d94660a33 14 * limitations under the License.
Jasper_lee 0:b16d94660a33 15 */
Jasper_lee 0:b16d94660a33 16 #ifndef MBED_I2C_H
Jasper_lee 0:b16d94660a33 17 #define MBED_I2C_H
Jasper_lee 0:b16d94660a33 18
Jasper_lee 0:b16d94660a33 19 #include "platform.h"
Jasper_lee 0:b16d94660a33 20
Jasper_lee 0:b16d94660a33 21 #if DEVICE_I2C
Jasper_lee 0:b16d94660a33 22
Jasper_lee 0:b16d94660a33 23 #include "i2c_api.h"
Jasper_lee 0:b16d94660a33 24
Jasper_lee 0:b16d94660a33 25 namespace mbed {
Jasper_lee 0:b16d94660a33 26
Jasper_lee 0:b16d94660a33 27 /** An I2C Master, used for communicating with I2C slave devices
Jasper_lee 0:b16d94660a33 28 *
Jasper_lee 0:b16d94660a33 29 * Example:
Jasper_lee 0:b16d94660a33 30 * @code
Jasper_lee 0:b16d94660a33 31 * // Read from I2C slave at address 0x62
Jasper_lee 0:b16d94660a33 32 *
Jasper_lee 0:b16d94660a33 33 * #include "mbed.h"
Jasper_lee 0:b16d94660a33 34 *
Jasper_lee 0:b16d94660a33 35 * I2C i2c(p28, p27);
Jasper_lee 0:b16d94660a33 36 *
Jasper_lee 0:b16d94660a33 37 * int main() {
Jasper_lee 0:b16d94660a33 38 * int address = 0x62;
Jasper_lee 0:b16d94660a33 39 * char data[2];
Jasper_lee 0:b16d94660a33 40 * i2c.read(address, data, 2);
Jasper_lee 0:b16d94660a33 41 * }
Jasper_lee 0:b16d94660a33 42 * @endcode
Jasper_lee 0:b16d94660a33 43 */
Jasper_lee 0:b16d94660a33 44 class I2C {
Jasper_lee 0:b16d94660a33 45
Jasper_lee 0:b16d94660a33 46 public:
Jasper_lee 0:b16d94660a33 47 enum RxStatus {
Jasper_lee 0:b16d94660a33 48 NoData,
Jasper_lee 0:b16d94660a33 49 MasterGeneralCall,
Jasper_lee 0:b16d94660a33 50 MasterWrite,
Jasper_lee 0:b16d94660a33 51 MasterRead
Jasper_lee 0:b16d94660a33 52 };
Jasper_lee 0:b16d94660a33 53
Jasper_lee 0:b16d94660a33 54 enum Acknowledge {
Jasper_lee 0:b16d94660a33 55 NoACK = 0,
Jasper_lee 0:b16d94660a33 56 ACK = 1
Jasper_lee 0:b16d94660a33 57 };
Jasper_lee 0:b16d94660a33 58
Jasper_lee 0:b16d94660a33 59 /** Create an I2C Master interface, connected to the specified pins
Jasper_lee 0:b16d94660a33 60 *
Jasper_lee 0:b16d94660a33 61 * @param sda I2C data line pin
Jasper_lee 0:b16d94660a33 62 * @param scl I2C clock line pin
Jasper_lee 0:b16d94660a33 63 */
Jasper_lee 0:b16d94660a33 64 I2C(PinName sda, PinName scl);
Jasper_lee 0:b16d94660a33 65
Jasper_lee 0:b16d94660a33 66 /** Set the frequency of the I2C interface
Jasper_lee 0:b16d94660a33 67 *
Jasper_lee 0:b16d94660a33 68 * @param hz The bus frequency in hertz
Jasper_lee 0:b16d94660a33 69 */
Jasper_lee 0:b16d94660a33 70 void frequency(int hz);
Jasper_lee 0:b16d94660a33 71
Jasper_lee 0:b16d94660a33 72 /** Read from an I2C slave
Jasper_lee 0:b16d94660a33 73 *
Jasper_lee 0:b16d94660a33 74 * Performs a complete read transaction. The bottom bit of
Jasper_lee 0:b16d94660a33 75 * the address is forced to 1 to indicate a read.
Jasper_lee 0:b16d94660a33 76 *
Jasper_lee 0:b16d94660a33 77 * @param address 8-bit I2C slave address [ addr | 1 ]
Jasper_lee 0:b16d94660a33 78 * @param data Pointer to the byte-array to read data in to
Jasper_lee 0:b16d94660a33 79 * @param length Number of bytes to read
Jasper_lee 0:b16d94660a33 80 * @param repeated Repeated start, true - don't send stop at end
Jasper_lee 0:b16d94660a33 81 *
Jasper_lee 0:b16d94660a33 82 * @returns
Jasper_lee 0:b16d94660a33 83 * 0 on success (ack),
Jasper_lee 0:b16d94660a33 84 * non-0 on failure (nack)
Jasper_lee 0:b16d94660a33 85 */
Jasper_lee 0:b16d94660a33 86 int read(int address, char *data, int length, bool repeated = false);
Jasper_lee 0:b16d94660a33 87
Jasper_lee 0:b16d94660a33 88 /** Read a single byte from the I2C bus
Jasper_lee 0:b16d94660a33 89 *
Jasper_lee 0:b16d94660a33 90 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
Jasper_lee 0:b16d94660a33 91 *
Jasper_lee 0:b16d94660a33 92 * @returns
Jasper_lee 0:b16d94660a33 93 * the byte read
Jasper_lee 0:b16d94660a33 94 */
Jasper_lee 0:b16d94660a33 95 int read(int ack);
Jasper_lee 0:b16d94660a33 96
Jasper_lee 0:b16d94660a33 97 /** Write to an I2C slave
Jasper_lee 0:b16d94660a33 98 *
Jasper_lee 0:b16d94660a33 99 * Performs a complete write transaction. The bottom bit of
Jasper_lee 0:b16d94660a33 100 * the address is forced to 0 to indicate a write.
Jasper_lee 0:b16d94660a33 101 *
Jasper_lee 0:b16d94660a33 102 * @param address 8-bit I2C slave address [ addr | 0 ]
Jasper_lee 0:b16d94660a33 103 * @param data Pointer to the byte-array data to send
Jasper_lee 0:b16d94660a33 104 * @param length Number of bytes to send
Jasper_lee 0:b16d94660a33 105 * @param repeated Repeated start, true - do not send stop at end
Jasper_lee 0:b16d94660a33 106 *
Jasper_lee 0:b16d94660a33 107 * @returns
Jasper_lee 0:b16d94660a33 108 * 0 on success (ack),
Jasper_lee 0:b16d94660a33 109 * non-0 on failure (nack)
Jasper_lee 0:b16d94660a33 110 */
Jasper_lee 0:b16d94660a33 111 int write(int address, const char *data, int length, bool repeated = false);
Jasper_lee 0:b16d94660a33 112
Jasper_lee 0:b16d94660a33 113 /** Write single byte out on the I2C bus
Jasper_lee 0:b16d94660a33 114 *
Jasper_lee 0:b16d94660a33 115 * @param data data to write out on bus
Jasper_lee 0:b16d94660a33 116 *
Jasper_lee 0:b16d94660a33 117 * @returns
Jasper_lee 0:b16d94660a33 118 * '1' if an ACK was received,
Jasper_lee 0:b16d94660a33 119 * '0' otherwise
Jasper_lee 0:b16d94660a33 120 */
Jasper_lee 0:b16d94660a33 121 int write(int data);
Jasper_lee 0:b16d94660a33 122
Jasper_lee 0:b16d94660a33 123 /** Creates a start condition on the I2C bus
Jasper_lee 0:b16d94660a33 124 */
Jasper_lee 0:b16d94660a33 125
Jasper_lee 0:b16d94660a33 126 void start(void);
Jasper_lee 0:b16d94660a33 127
Jasper_lee 0:b16d94660a33 128 /** Creates a stop condition on the I2C bus
Jasper_lee 0:b16d94660a33 129 */
Jasper_lee 0:b16d94660a33 130 void stop(void);
Jasper_lee 0:b16d94660a33 131
Jasper_lee 0:b16d94660a33 132 protected:
Jasper_lee 0:b16d94660a33 133 void aquire();
Jasper_lee 0:b16d94660a33 134
Jasper_lee 0:b16d94660a33 135 i2c_t _i2c;
Jasper_lee 0:b16d94660a33 136 static I2C *_owner;
Jasper_lee 0:b16d94660a33 137 int _hz;
Jasper_lee 0:b16d94660a33 138 };
Jasper_lee 0:b16d94660a33 139
Jasper_lee 0:b16d94660a33 140 } // namespace mbed
Jasper_lee 0:b16d94660a33 141
Jasper_lee 0:b16d94660a33 142 #endif
Jasper_lee 0:b16d94660a33 143
Jasper_lee 0:b16d94660a33 144 #endif