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