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