mbed libraries for KL25Z

Dependents:   FRDM_RGBLED

Committer:
emilmont
Date:
Mon Feb 18 09:41:56 2013 +0000
Revision:
9:663789d7729f
Parent:
8:c14af7958ef5
Update mbed-KL25Z to latest build

Who changed what in which revision?

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