Erste version der Software für der Prototyp

Committer:
borlanic
Date:
Fri Mar 30 14:07:05 2018 +0000
Revision:
4:75df35ef4fb6
Parent:
0:380207fcb5c1
commentar

Who changed what in which revision?

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