This is a repository for all my programs or modified programs.

Committer:
mturner5
Date:
Sun Sep 11 23:48:09 2016 +0000
Revision:
0:72480818e4a9
Made delays for the LEDS and button presses

Who changed what in which revision?

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