Dataloger

Committer:
jhon309
Date:
Thu Aug 20 00:37:14 2015 +0000
Revision:
0:666850d06c9f
ok

Who changed what in which revision?

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