001

Committer:
ganlikun
Date:
Sun Jun 12 14:02:44 2022 +0000
Revision:
0:13413ea9a877
00

Who changed what in which revision?

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