The official mbed C/C SDK provides the software platform and libraries to build your applications.

Fork of mbed by mbed official

Committer:
ldyz
Date:
Fri Jul 05 13:16:13 2013 +0000
Revision:
64:75c1708b266b
Parent:
59:0883845fe643
test

Who changed what in which revision?

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