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