inport from local

Dependents:   Hobbyking_Cheetah_0511

Committer:
NYX
Date:
Mon Mar 16 06:35:48 2020 +0000
Revision:
0:85b3fd62ea1a
reinport to mbed;

Who changed what in which revision?

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