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