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