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