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