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