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_SLAVE_H
mturner5 0:72480818e4a9 17 #define MBED_I2C_SLAVE_H
mturner5 0:72480818e4a9 18
mturner5 0:72480818e4a9 19 #include "platform.h"
mturner5 0:72480818e4a9 20
mturner5 0:72480818e4a9 21 #if DEVICE_I2CSLAVE
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 Slave, used for communicating with an I2C Master device
mturner5 0:72480818e4a9 28 *
mturner5 0:72480818e4a9 29 * Example:
mturner5 0:72480818e4a9 30 * @code
mturner5 0:72480818e4a9 31 * // Simple I2C responder
mturner5 0:72480818e4a9 32 * #include <mbed.h>
mturner5 0:72480818e4a9 33 *
mturner5 0:72480818e4a9 34 * I2CSlave slave(p9, p10);
mturner5 0:72480818e4a9 35 *
mturner5 0:72480818e4a9 36 * int main() {
mturner5 0:72480818e4a9 37 * char buf[10];
mturner5 0:72480818e4a9 38 * char msg[] = "Slave!";
mturner5 0:72480818e4a9 39 *
mturner5 0:72480818e4a9 40 * slave.address(0xA0);
mturner5 0:72480818e4a9 41 * while (1) {
mturner5 0:72480818e4a9 42 * int i = slave.receive();
mturner5 0:72480818e4a9 43 * switch (i) {
mturner5 0:72480818e4a9 44 * case I2CSlave::ReadAddressed:
mturner5 0:72480818e4a9 45 * slave.write(msg, strlen(msg) + 1); // Includes null char
mturner5 0:72480818e4a9 46 * break;
mturner5 0:72480818e4a9 47 * case I2CSlave::WriteGeneral:
mturner5 0:72480818e4a9 48 * slave.read(buf, 10);
mturner5 0:72480818e4a9 49 * printf("Read G: %s\n", buf);
mturner5 0:72480818e4a9 50 * break;
mturner5 0:72480818e4a9 51 * case I2CSlave::WriteAddressed:
mturner5 0:72480818e4a9 52 * slave.read(buf, 10);
mturner5 0:72480818e4a9 53 * printf("Read A: %s\n", buf);
mturner5 0:72480818e4a9 54 * break;
mturner5 0:72480818e4a9 55 * }
mturner5 0:72480818e4a9 56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
mturner5 0:72480818e4a9 57 * }
mturner5 0:72480818e4a9 58 * }
mturner5 0:72480818e4a9 59 * @endcode
mturner5 0:72480818e4a9 60 */
mturner5 0:72480818e4a9 61 class I2CSlave {
mturner5 0:72480818e4a9 62
mturner5 0:72480818e4a9 63 public:
mturner5 0:72480818e4a9 64 enum RxStatus {
mturner5 0:72480818e4a9 65 NoData = 0,
mturner5 0:72480818e4a9 66 ReadAddressed = 1,
mturner5 0:72480818e4a9 67 WriteGeneral = 2,
mturner5 0:72480818e4a9 68 WriteAddressed = 3
mturner5 0:72480818e4a9 69 };
mturner5 0:72480818e4a9 70
mturner5 0:72480818e4a9 71 /** Create an I2C Slave interface, connected to the specified pins.
mturner5 0:72480818e4a9 72 *
mturner5 0:72480818e4a9 73 * @param sda I2C data line pin
mturner5 0:72480818e4a9 74 * @param scl I2C clock line pin
mturner5 0:72480818e4a9 75 */
mturner5 0:72480818e4a9 76 I2CSlave(PinName sda, PinName scl);
mturner5 0:72480818e4a9 77
mturner5 0:72480818e4a9 78 /** Set the frequency of the I2C interface
mturner5 0:72480818e4a9 79 *
mturner5 0:72480818e4a9 80 * @param hz The bus frequency in hertz
mturner5 0:72480818e4a9 81 */
mturner5 0:72480818e4a9 82 void frequency(int hz);
mturner5 0:72480818e4a9 83
mturner5 0:72480818e4a9 84 /** Checks to see if this I2C Slave has been addressed.
mturner5 0:72480818e4a9 85 *
mturner5 0:72480818e4a9 86 * @returns
mturner5 0:72480818e4a9 87 * A status indicating if the device has been addressed, and how
mturner5 0:72480818e4a9 88 * - NoData - the slave has not been addressed
mturner5 0:72480818e4a9 89 * - ReadAddressed - the master has requested a read from this slave
mturner5 0:72480818e4a9 90 * - WriteAddressed - the master is writing to this slave
mturner5 0:72480818e4a9 91 * - WriteGeneral - the master is writing to all slave
mturner5 0:72480818e4a9 92 */
mturner5 0:72480818e4a9 93 int receive(void);
mturner5 0:72480818e4a9 94
mturner5 0:72480818e4a9 95 /** Read from an I2C master.
mturner5 0:72480818e4a9 96 *
mturner5 0:72480818e4a9 97 * @param data pointer to the byte array to read data in to
mturner5 0:72480818e4a9 98 * @param length maximum number of bytes to read
mturner5 0:72480818e4a9 99 *
mturner5 0:72480818e4a9 100 * @returns
mturner5 0:72480818e4a9 101 * 0 on success,
mturner5 0:72480818e4a9 102 * non-0 otherwise
mturner5 0:72480818e4a9 103 */
mturner5 0:72480818e4a9 104 int read(char *data, int length);
mturner5 0:72480818e4a9 105
mturner5 0:72480818e4a9 106 /** Read a single byte from an I2C master.
mturner5 0:72480818e4a9 107 *
mturner5 0:72480818e4a9 108 * @returns
mturner5 0:72480818e4a9 109 * the byte read
mturner5 0:72480818e4a9 110 */
mturner5 0:72480818e4a9 111 int read(void);
mturner5 0:72480818e4a9 112
mturner5 0:72480818e4a9 113 /** Write to an I2C master.
mturner5 0:72480818e4a9 114 *
mturner5 0:72480818e4a9 115 * @param data pointer to the byte array to be transmitted
mturner5 0:72480818e4a9 116 * @param length the number of bytes to transmite
mturner5 0:72480818e4a9 117 *
mturner5 0:72480818e4a9 118 * @returns
mturner5 0:72480818e4a9 119 * 0 on success,
mturner5 0:72480818e4a9 120 * non-0 otherwise
mturner5 0:72480818e4a9 121 */
mturner5 0:72480818e4a9 122 int write(const char *data, int length);
mturner5 0:72480818e4a9 123
mturner5 0:72480818e4a9 124 /** Write a single byte to an I2C master.
mturner5 0:72480818e4a9 125 *
mturner5 0:72480818e4a9 126 * @data the byte to write
mturner5 0:72480818e4a9 127 *
mturner5 0:72480818e4a9 128 * @returns
mturner5 0:72480818e4a9 129 * '1' if an ACK was received,
mturner5 0:72480818e4a9 130 * '0' otherwise
mturner5 0:72480818e4a9 131 */
mturner5 0:72480818e4a9 132 int write(int data);
mturner5 0:72480818e4a9 133
mturner5 0:72480818e4a9 134 /** Sets the I2C slave address.
mturner5 0:72480818e4a9 135 *
mturner5 0:72480818e4a9 136 * @param address The address to set for the slave (ignoring the least
mturner5 0:72480818e4a9 137 * signifcant bit). If set to 0, the slave will only respond to the
mturner5 0:72480818e4a9 138 * general call address.
mturner5 0:72480818e4a9 139 */
mturner5 0:72480818e4a9 140 void address(int address);
mturner5 0:72480818e4a9 141
mturner5 0:72480818e4a9 142 /** Reset the I2C slave back into the known ready receiving state.
mturner5 0:72480818e4a9 143 */
mturner5 0:72480818e4a9 144 void stop(void);
mturner5 0:72480818e4a9 145
mturner5 0:72480818e4a9 146 protected:
mturner5 0:72480818e4a9 147 i2c_t _i2c;
mturner5 0:72480818e4a9 148 };
mturner5 0:72480818e4a9 149
mturner5 0:72480818e4a9 150 } // namespace mbed
mturner5 0:72480818e4a9 151
mturner5 0:72480818e4a9 152 #endif
mturner5 0:72480818e4a9 153
mturner5 0:72480818e4a9 154 #endif