mbed lib with startup delay fixed for Nucleo401RE
Fork of mbed-src by
cpp/I2CSlave.h@0:fd0d7bdfcdc2, 2012-11-20 (annotated)
- Committer:
- mbed_official
- Date:
- Tue Nov 20 17:24:08 2012 +0000
- Revision:
- 0:fd0d7bdfcdc2
- Child:
- 2:143cac498751
mbed sources
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 0:fd0d7bdfcdc2 | 1 | /* mbed Microcontroller Library |
mbed_official | 0:fd0d7bdfcdc2 | 2 | * Copyright (c) 2006-2012 ARM Limited |
mbed_official | 0:fd0d7bdfcdc2 | 3 | * |
mbed_official | 0:fd0d7bdfcdc2 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
mbed_official | 0:fd0d7bdfcdc2 | 5 | * of this software and associated documentation files (the "Software"), to deal |
mbed_official | 0:fd0d7bdfcdc2 | 6 | * in the Software without restriction, including without limitation the rights |
mbed_official | 0:fd0d7bdfcdc2 | 7 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
mbed_official | 0:fd0d7bdfcdc2 | 8 | * copies of the Software, and to permit persons to whom the Software is |
mbed_official | 0:fd0d7bdfcdc2 | 9 | * furnished to do so, subject to the following conditions: |
mbed_official | 0:fd0d7bdfcdc2 | 10 | * |
mbed_official | 0:fd0d7bdfcdc2 | 11 | * The above copyright notice and this permission notice shall be included in |
mbed_official | 0:fd0d7bdfcdc2 | 12 | * all copies or substantial portions of the Software. |
mbed_official | 0:fd0d7bdfcdc2 | 13 | * |
mbed_official | 0:fd0d7bdfcdc2 | 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
mbed_official | 0:fd0d7bdfcdc2 | 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
mbed_official | 0:fd0d7bdfcdc2 | 16 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
mbed_official | 0:fd0d7bdfcdc2 | 17 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
mbed_official | 0:fd0d7bdfcdc2 | 18 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
mbed_official | 0:fd0d7bdfcdc2 | 19 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
mbed_official | 0:fd0d7bdfcdc2 | 20 | * SOFTWARE. |
mbed_official | 0:fd0d7bdfcdc2 | 21 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 22 | #ifndef MBED_I2C_SLAVE_H |
mbed_official | 0:fd0d7bdfcdc2 | 23 | #define MBED_I2C_SLAVE_H |
mbed_official | 0:fd0d7bdfcdc2 | 24 | |
mbed_official | 0:fd0d7bdfcdc2 | 25 | #include "platform.h" |
mbed_official | 0:fd0d7bdfcdc2 | 26 | |
mbed_official | 0:fd0d7bdfcdc2 | 27 | #if DEVICE_I2CSLAVE |
mbed_official | 0:fd0d7bdfcdc2 | 28 | |
mbed_official | 0:fd0d7bdfcdc2 | 29 | #include "i2c_api.h" |
mbed_official | 0:fd0d7bdfcdc2 | 30 | |
mbed_official | 0:fd0d7bdfcdc2 | 31 | namespace mbed { |
mbed_official | 0:fd0d7bdfcdc2 | 32 | |
mbed_official | 0:fd0d7bdfcdc2 | 33 | /** An I2C Slave, used for communicating with an I2C Master device |
mbed_official | 0:fd0d7bdfcdc2 | 34 | * |
mbed_official | 0:fd0d7bdfcdc2 | 35 | * Example: |
mbed_official | 0:fd0d7bdfcdc2 | 36 | * @code |
mbed_official | 0:fd0d7bdfcdc2 | 37 | * // Simple I2C responder |
mbed_official | 0:fd0d7bdfcdc2 | 38 | * #include <mbed.h> |
mbed_official | 0:fd0d7bdfcdc2 | 39 | * |
mbed_official | 0:fd0d7bdfcdc2 | 40 | * I2CSlave slave(p9, p10); |
mbed_official | 0:fd0d7bdfcdc2 | 41 | * |
mbed_official | 0:fd0d7bdfcdc2 | 42 | * int main() { |
mbed_official | 0:fd0d7bdfcdc2 | 43 | * char buf[10]; |
mbed_official | 0:fd0d7bdfcdc2 | 44 | * char msg[] = "Slave!"; |
mbed_official | 0:fd0d7bdfcdc2 | 45 | * |
mbed_official | 0:fd0d7bdfcdc2 | 46 | * slave.address(0xA0); |
mbed_official | 0:fd0d7bdfcdc2 | 47 | * while (1) { |
mbed_official | 0:fd0d7bdfcdc2 | 48 | * int i = slave.receive(); |
mbed_official | 0:fd0d7bdfcdc2 | 49 | * switch (i) { |
mbed_official | 0:fd0d7bdfcdc2 | 50 | * case I2CSlave::ReadAddressed: |
mbed_official | 0:fd0d7bdfcdc2 | 51 | * slave.write(msg, strlen(msg) + 1); // Includes null char |
mbed_official | 0:fd0d7bdfcdc2 | 52 | * break; |
mbed_official | 0:fd0d7bdfcdc2 | 53 | * case I2CSlave::WriteGeneral: |
mbed_official | 0:fd0d7bdfcdc2 | 54 | * slave.read(buf, 10); |
mbed_official | 0:fd0d7bdfcdc2 | 55 | * printf("Read G: %s\n", buf); |
mbed_official | 0:fd0d7bdfcdc2 | 56 | * break; |
mbed_official | 0:fd0d7bdfcdc2 | 57 | * case I2CSlave::WriteAddressed: |
mbed_official | 0:fd0d7bdfcdc2 | 58 | * slave.read(buf, 10); |
mbed_official | 0:fd0d7bdfcdc2 | 59 | * printf("Read A: %s\n", buf); |
mbed_official | 0:fd0d7bdfcdc2 | 60 | * break; |
mbed_official | 0:fd0d7bdfcdc2 | 61 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 62 | * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer |
mbed_official | 0:fd0d7bdfcdc2 | 63 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 64 | * } |
mbed_official | 0:fd0d7bdfcdc2 | 65 | * @endcode |
mbed_official | 0:fd0d7bdfcdc2 | 66 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 67 | class I2CSlave { |
mbed_official | 0:fd0d7bdfcdc2 | 68 | |
mbed_official | 0:fd0d7bdfcdc2 | 69 | public: |
mbed_official | 0:fd0d7bdfcdc2 | 70 | enum RxStatus { |
mbed_official | 0:fd0d7bdfcdc2 | 71 | NoData = 0, |
mbed_official | 0:fd0d7bdfcdc2 | 72 | ReadAddressed = 1, |
mbed_official | 0:fd0d7bdfcdc2 | 73 | WriteGeneral = 2, |
mbed_official | 0:fd0d7bdfcdc2 | 74 | WriteAddressed = 3 |
mbed_official | 0:fd0d7bdfcdc2 | 75 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 76 | |
mbed_official | 0:fd0d7bdfcdc2 | 77 | /** Create an I2C Slave interface, connected to the specified pins. |
mbed_official | 0:fd0d7bdfcdc2 | 78 | * |
mbed_official | 0:fd0d7bdfcdc2 | 79 | * @param sda I2C data line pin |
mbed_official | 0:fd0d7bdfcdc2 | 80 | * @param scl I2C clock line pin |
mbed_official | 0:fd0d7bdfcdc2 | 81 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 82 | I2CSlave(PinName sda, PinName scl); |
mbed_official | 0:fd0d7bdfcdc2 | 83 | |
mbed_official | 0:fd0d7bdfcdc2 | 84 | /** Set the frequency of the I2C interface |
mbed_official | 0:fd0d7bdfcdc2 | 85 | * |
mbed_official | 0:fd0d7bdfcdc2 | 86 | * @param hz The bus frequency in hertz |
mbed_official | 0:fd0d7bdfcdc2 | 87 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 88 | void frequency(int hz); |
mbed_official | 0:fd0d7bdfcdc2 | 89 | |
mbed_official | 0:fd0d7bdfcdc2 | 90 | /** Checks to see if this I2C Slave has been addressed. |
mbed_official | 0:fd0d7bdfcdc2 | 91 | * |
mbed_official | 0:fd0d7bdfcdc2 | 92 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 93 | * A status indicating if the device has been addressed, and how |
mbed_official | 0:fd0d7bdfcdc2 | 94 | * - NoData - the slave has not been addressed |
mbed_official | 0:fd0d7bdfcdc2 | 95 | * - ReadAddressed - the master has requested a read from this slave |
mbed_official | 0:fd0d7bdfcdc2 | 96 | * - WriteAddressed - the master is writing to this slave |
mbed_official | 0:fd0d7bdfcdc2 | 97 | * - WriteGeneral - the master is writing to all slave |
mbed_official | 0:fd0d7bdfcdc2 | 98 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 99 | int receive(void); |
mbed_official | 0:fd0d7bdfcdc2 | 100 | |
mbed_official | 0:fd0d7bdfcdc2 | 101 | /** Read from an I2C master. |
mbed_official | 0:fd0d7bdfcdc2 | 102 | * |
mbed_official | 0:fd0d7bdfcdc2 | 103 | * @param data pointer to the byte array to read data in to |
mbed_official | 0:fd0d7bdfcdc2 | 104 | * @param length maximum number of bytes to read |
mbed_official | 0:fd0d7bdfcdc2 | 105 | * |
mbed_official | 0:fd0d7bdfcdc2 | 106 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 107 | * 0 on success, |
mbed_official | 0:fd0d7bdfcdc2 | 108 | * non-0 otherwise |
mbed_official | 0:fd0d7bdfcdc2 | 109 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 110 | int read(char *data, int length); |
mbed_official | 0:fd0d7bdfcdc2 | 111 | |
mbed_official | 0:fd0d7bdfcdc2 | 112 | /** Read a single byte from an I2C master. |
mbed_official | 0:fd0d7bdfcdc2 | 113 | * |
mbed_official | 0:fd0d7bdfcdc2 | 114 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 115 | * the byte read |
mbed_official | 0:fd0d7bdfcdc2 | 116 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 117 | int read(void); |
mbed_official | 0:fd0d7bdfcdc2 | 118 | |
mbed_official | 0:fd0d7bdfcdc2 | 119 | /** Write to an I2C master. |
mbed_official | 0:fd0d7bdfcdc2 | 120 | * |
mbed_official | 0:fd0d7bdfcdc2 | 121 | * @param data pointer to the byte array to be transmitted |
mbed_official | 0:fd0d7bdfcdc2 | 122 | * @param length the number of bytes to transmite |
mbed_official | 0:fd0d7bdfcdc2 | 123 | * |
mbed_official | 0:fd0d7bdfcdc2 | 124 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 125 | * 0 on success, |
mbed_official | 0:fd0d7bdfcdc2 | 126 | * non-0 otherwise |
mbed_official | 0:fd0d7bdfcdc2 | 127 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 128 | int write(const char *data, int length); |
mbed_official | 0:fd0d7bdfcdc2 | 129 | |
mbed_official | 0:fd0d7bdfcdc2 | 130 | /** Write a single byte to an I2C master. |
mbed_official | 0:fd0d7bdfcdc2 | 131 | * |
mbed_official | 0:fd0d7bdfcdc2 | 132 | * @data the byte to write |
mbed_official | 0:fd0d7bdfcdc2 | 133 | * |
mbed_official | 0:fd0d7bdfcdc2 | 134 | * @returns |
mbed_official | 0:fd0d7bdfcdc2 | 135 | * '1' if an ACK was received, |
mbed_official | 0:fd0d7bdfcdc2 | 136 | * '0' otherwise |
mbed_official | 0:fd0d7bdfcdc2 | 137 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 138 | int write(int data); |
mbed_official | 0:fd0d7bdfcdc2 | 139 | |
mbed_official | 0:fd0d7bdfcdc2 | 140 | /** Sets the I2C slave address. |
mbed_official | 0:fd0d7bdfcdc2 | 141 | * |
mbed_official | 0:fd0d7bdfcdc2 | 142 | * @param address The address to set for the slave (ignoring the least |
mbed_official | 0:fd0d7bdfcdc2 | 143 | * signifcant bit). If set to 0, the slave will only respond to the |
mbed_official | 0:fd0d7bdfcdc2 | 144 | * general call address. |
mbed_official | 0:fd0d7bdfcdc2 | 145 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 146 | void address(int address); |
mbed_official | 0:fd0d7bdfcdc2 | 147 | |
mbed_official | 0:fd0d7bdfcdc2 | 148 | /** Reset the I2C slave back into the known ready receiving state. |
mbed_official | 0:fd0d7bdfcdc2 | 149 | */ |
mbed_official | 0:fd0d7bdfcdc2 | 150 | void stop(void); |
mbed_official | 0:fd0d7bdfcdc2 | 151 | |
mbed_official | 0:fd0d7bdfcdc2 | 152 | protected: |
mbed_official | 0:fd0d7bdfcdc2 | 153 | i2c_t _i2c; |
mbed_official | 0:fd0d7bdfcdc2 | 154 | }; |
mbed_official | 0:fd0d7bdfcdc2 | 155 | |
mbed_official | 0:fd0d7bdfcdc2 | 156 | } // namespace mbed |
mbed_official | 0:fd0d7bdfcdc2 | 157 | |
mbed_official | 0:fd0d7bdfcdc2 | 158 | #endif |
mbed_official | 0:fd0d7bdfcdc2 | 159 | |
mbed_official | 0:fd0d7bdfcdc2 | 160 | #endif |