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