Nicolas Borla / Mbed OS BBR_1Ebene
Committer:
borlanic
Date:
Mon May 14 11:29:06 2018 +0000
Revision:
0:fbdae7e6d805
BBR

Who changed what in which revision?

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