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