initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #ifndef MBED_I2C_SLAVE_H
yihui 0:638edba3adf6 17 #define MBED_I2C_SLAVE_H
yihui 0:638edba3adf6 18
yihui 0:638edba3adf6 19 #include "platform.h"
yihui 0:638edba3adf6 20
yihui 0:638edba3adf6 21 #if DEVICE_I2CSLAVE
yihui 0:638edba3adf6 22
yihui 0:638edba3adf6 23 #include "i2c_api.h"
yihui 0:638edba3adf6 24
yihui 0:638edba3adf6 25 namespace mbed {
yihui 0:638edba3adf6 26
yihui 0:638edba3adf6 27 /** An I2C Slave, used for communicating with an I2C Master device
yihui 0:638edba3adf6 28 *
yihui 0:638edba3adf6 29 * Example:
yihui 0:638edba3adf6 30 * @code
yihui 0:638edba3adf6 31 * // Simple I2C responder
yihui 0:638edba3adf6 32 * #include <mbed.h>
yihui 0:638edba3adf6 33 *
yihui 0:638edba3adf6 34 * I2CSlave slave(p9, p10);
yihui 0:638edba3adf6 35 *
yihui 0:638edba3adf6 36 * int main() {
yihui 0:638edba3adf6 37 * char buf[10];
yihui 0:638edba3adf6 38 * char msg[] = "Slave!";
yihui 0:638edba3adf6 39 *
yihui 0:638edba3adf6 40 * slave.address(0xA0);
yihui 0:638edba3adf6 41 * while (1) {
yihui 0:638edba3adf6 42 * int i = slave.receive();
yihui 0:638edba3adf6 43 * switch (i) {
yihui 0:638edba3adf6 44 * case I2CSlave::ReadAddressed:
yihui 0:638edba3adf6 45 * slave.write(msg, strlen(msg) + 1); // Includes null char
yihui 0:638edba3adf6 46 * break;
yihui 0:638edba3adf6 47 * case I2CSlave::WriteGeneral:
yihui 0:638edba3adf6 48 * slave.read(buf, 10);
yihui 0:638edba3adf6 49 * printf("Read G: %s\n", buf);
yihui 0:638edba3adf6 50 * break;
yihui 0:638edba3adf6 51 * case I2CSlave::WriteAddressed:
yihui 0:638edba3adf6 52 * slave.read(buf, 10);
yihui 0:638edba3adf6 53 * printf("Read A: %s\n", buf);
yihui 0:638edba3adf6 54 * break;
yihui 0:638edba3adf6 55 * }
yihui 0:638edba3adf6 56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
yihui 0:638edba3adf6 57 * }
yihui 0:638edba3adf6 58 * }
yihui 0:638edba3adf6 59 * @endcode
yihui 0:638edba3adf6 60 */
yihui 0:638edba3adf6 61 class I2CSlave {
yihui 0:638edba3adf6 62
yihui 0:638edba3adf6 63 public:
yihui 0:638edba3adf6 64 enum RxStatus {
yihui 0:638edba3adf6 65 NoData = 0,
yihui 0:638edba3adf6 66 ReadAddressed = 1,
yihui 0:638edba3adf6 67 WriteGeneral = 2,
yihui 0:638edba3adf6 68 WriteAddressed = 3
yihui 0:638edba3adf6 69 };
yihui 0:638edba3adf6 70
yihui 0:638edba3adf6 71 /** Create an I2C Slave interface, connected to the specified pins.
yihui 0:638edba3adf6 72 *
yihui 0:638edba3adf6 73 * @param sda I2C data line pin
yihui 0:638edba3adf6 74 * @param scl I2C clock line pin
yihui 0:638edba3adf6 75 */
yihui 0:638edba3adf6 76 I2CSlave(PinName sda, PinName scl);
yihui 0:638edba3adf6 77
yihui 0:638edba3adf6 78 /** Set the frequency of the I2C interface
yihui 0:638edba3adf6 79 *
yihui 0:638edba3adf6 80 * @param hz The bus frequency in hertz
yihui 0:638edba3adf6 81 */
yihui 0:638edba3adf6 82 void frequency(int hz);
yihui 0:638edba3adf6 83
yihui 0:638edba3adf6 84 /** Checks to see if this I2C Slave has been addressed.
yihui 0:638edba3adf6 85 *
yihui 0:638edba3adf6 86 * @returns
yihui 0:638edba3adf6 87 * A status indicating if the device has been addressed, and how
yihui 0:638edba3adf6 88 * - NoData - the slave has not been addressed
yihui 0:638edba3adf6 89 * - ReadAddressed - the master has requested a read from this slave
yihui 0:638edba3adf6 90 * - WriteAddressed - the master is writing to this slave
yihui 0:638edba3adf6 91 * - WriteGeneral - the master is writing to all slave
yihui 0:638edba3adf6 92 */
yihui 0:638edba3adf6 93 int receive(void);
yihui 0:638edba3adf6 94
yihui 0:638edba3adf6 95 /** Read from an I2C master.
yihui 0:638edba3adf6 96 *
yihui 0:638edba3adf6 97 * @param data pointer to the byte array to read data in to
yihui 0:638edba3adf6 98 * @param length maximum number of bytes to read
yihui 0:638edba3adf6 99 *
yihui 0:638edba3adf6 100 * @returns
yihui 0:638edba3adf6 101 * 0 on success,
yihui 0:638edba3adf6 102 * non-0 otherwise
yihui 0:638edba3adf6 103 */
yihui 0:638edba3adf6 104 int read(char *data, int length);
yihui 0:638edba3adf6 105
yihui 0:638edba3adf6 106 /** Read a single byte from an I2C master.
yihui 0:638edba3adf6 107 *
yihui 0:638edba3adf6 108 * @returns
yihui 0:638edba3adf6 109 * the byte read
yihui 0:638edba3adf6 110 */
yihui 0:638edba3adf6 111 int read(void);
yihui 0:638edba3adf6 112
yihui 0:638edba3adf6 113 /** Write to an I2C master.
yihui 0:638edba3adf6 114 *
yihui 0:638edba3adf6 115 * @param data pointer to the byte array to be transmitted
yihui 0:638edba3adf6 116 * @param length the number of bytes to transmite
yihui 0:638edba3adf6 117 *
yihui 0:638edba3adf6 118 * @returns
yihui 0:638edba3adf6 119 * 0 on success,
yihui 0:638edba3adf6 120 * non-0 otherwise
yihui 0:638edba3adf6 121 */
yihui 0:638edba3adf6 122 int write(const char *data, int length);
yihui 0:638edba3adf6 123
yihui 0:638edba3adf6 124 /** Write a single byte to an I2C master.
yihui 0:638edba3adf6 125 *
yihui 0:638edba3adf6 126 * @data the byte to write
yihui 0:638edba3adf6 127 *
yihui 0:638edba3adf6 128 * @returns
yihui 0:638edba3adf6 129 * '1' if an ACK was received,
yihui 0:638edba3adf6 130 * '0' otherwise
yihui 0:638edba3adf6 131 */
yihui 0:638edba3adf6 132 int write(int data);
yihui 0:638edba3adf6 133
yihui 0:638edba3adf6 134 /** Sets the I2C slave address.
yihui 0:638edba3adf6 135 *
yihui 0:638edba3adf6 136 * @param address The address to set for the slave (ignoring the least
yihui 0:638edba3adf6 137 * signifcant bit). If set to 0, the slave will only respond to the
yihui 0:638edba3adf6 138 * general call address.
yihui 0:638edba3adf6 139 */
yihui 0:638edba3adf6 140 void address(int address);
yihui 0:638edba3adf6 141
yihui 0:638edba3adf6 142 /** Reset the I2C slave back into the known ready receiving state.
yihui 0:638edba3adf6 143 */
yihui 0:638edba3adf6 144 void stop(void);
yihui 0:638edba3adf6 145
yihui 0:638edba3adf6 146 protected:
yihui 0:638edba3adf6 147 i2c_t _i2c;
yihui 0:638edba3adf6 148 };
yihui 0:638edba3adf6 149
yihui 0:638edba3adf6 150 } // namespace mbed
yihui 0:638edba3adf6 151
yihui 0:638edba3adf6 152 #endif
yihui 0:638edba3adf6 153
yihui 0:638edba3adf6 154 #endif