RTC auf true
drivers/I2CSlave.h@0:38ceb79fef03, 2018-11-28 (annotated)
- Committer:
- kevman
- Date:
- Wed Nov 28 15:10:15 2018 +0000
- Revision:
- 0:38ceb79fef03
RTC modified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kevman | 0:38ceb79fef03 | 1 | /* mbed Microcontroller Library |
kevman | 0:38ceb79fef03 | 2 | * Copyright (c) 2006-2013 ARM Limited |
kevman | 0:38ceb79fef03 | 3 | * |
kevman | 0:38ceb79fef03 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
kevman | 0:38ceb79fef03 | 5 | * you may not use this file except in compliance with the License. |
kevman | 0:38ceb79fef03 | 6 | * You may obtain a copy of the License at |
kevman | 0:38ceb79fef03 | 7 | * |
kevman | 0:38ceb79fef03 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
kevman | 0:38ceb79fef03 | 9 | * |
kevman | 0:38ceb79fef03 | 10 | * Unless required by applicable law or agreed to in writing, software |
kevman | 0:38ceb79fef03 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
kevman | 0:38ceb79fef03 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
kevman | 0:38ceb79fef03 | 13 | * See the License for the specific language governing permissions and |
kevman | 0:38ceb79fef03 | 14 | * limitations under the License. |
kevman | 0:38ceb79fef03 | 15 | */ |
kevman | 0:38ceb79fef03 | 16 | #ifndef MBED_I2C_SLAVE_H |
kevman | 0:38ceb79fef03 | 17 | #define MBED_I2C_SLAVE_H |
kevman | 0:38ceb79fef03 | 18 | |
kevman | 0:38ceb79fef03 | 19 | #include "platform/platform.h" |
kevman | 0:38ceb79fef03 | 20 | |
kevman | 0:38ceb79fef03 | 21 | #if defined (DEVICE_I2CSLAVE) || defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 22 | |
kevman | 0:38ceb79fef03 | 23 | #include "hal/i2c_api.h" |
kevman | 0:38ceb79fef03 | 24 | |
kevman | 0:38ceb79fef03 | 25 | namespace mbed { |
kevman | 0:38ceb79fef03 | 26 | /** \addtogroup drivers */ |
kevman | 0:38ceb79fef03 | 27 | |
kevman | 0:38ceb79fef03 | 28 | /** An I2C Slave, used for communicating with an I2C Master device. |
kevman | 0:38ceb79fef03 | 29 | * |
kevman | 0:38ceb79fef03 | 30 | * @note Synchronization level: Not protected |
kevman | 0:38ceb79fef03 | 31 | * |
kevman | 0:38ceb79fef03 | 32 | * Example Simple I2C responder: |
kevman | 0:38ceb79fef03 | 33 | * @code |
kevman | 0:38ceb79fef03 | 34 | * #include <mbed.h> |
kevman | 0:38ceb79fef03 | 35 | * |
kevman | 0:38ceb79fef03 | 36 | * const int SLAVE_ADDRESS = 0xA0; |
kevman | 0:38ceb79fef03 | 37 | * const char message[] = "Slave!"; |
kevman | 0:38ceb79fef03 | 38 | * |
kevman | 0:38ceb79fef03 | 39 | * I2CSlave slave(I2C_SDA, I2C_SCL); |
kevman | 0:38ceb79fef03 | 40 | * |
kevman | 0:38ceb79fef03 | 41 | * int main() { |
kevman | 0:38ceb79fef03 | 42 | * slave.address(SLAVE_ADDRESS); |
kevman | 0:38ceb79fef03 | 43 | * while (1) { |
kevman | 0:38ceb79fef03 | 44 | * int operation = slave.receive(); |
kevman | 0:38ceb79fef03 | 45 | * switch (operation) { |
kevman | 0:38ceb79fef03 | 46 | * case I2CSlave::ReadAddressed: |
kevman | 0:38ceb79fef03 | 47 | * int status = slave.write(message, sizeof(message)); |
kevman | 0:38ceb79fef03 | 48 | * if (status == 0) { |
kevman | 0:38ceb79fef03 | 49 | * printf("Written message: %s\n", message); |
kevman | 0:38ceb79fef03 | 50 | * } else { |
kevman | 0:38ceb79fef03 | 51 | * printf("Failed to write message.\n"); |
kevman | 0:38ceb79fef03 | 52 | * } |
kevman | 0:38ceb79fef03 | 53 | * break; |
kevman | 0:38ceb79fef03 | 54 | * case I2CSlave::WriteGeneral: |
kevman | 0:38ceb79fef03 | 55 | * int byte_read = slave.read(); |
kevman | 0:38ceb79fef03 | 56 | * printf("Read General: %c (%d)\n", byte_read, byte_read); |
kevman | 0:38ceb79fef03 | 57 | * break; |
kevman | 0:38ceb79fef03 | 58 | * case I2CSlave::WriteAddressed: |
kevman | 0:38ceb79fef03 | 59 | * int byte_read = slave.read(); |
kevman | 0:38ceb79fef03 | 60 | * printf("Read Addressed: %c (%d)\n", byte_read, byte_read); |
kevman | 0:38ceb79fef03 | 61 | * break; |
kevman | 0:38ceb79fef03 | 62 | * } |
kevman | 0:38ceb79fef03 | 63 | * } |
kevman | 0:38ceb79fef03 | 64 | * } |
kevman | 0:38ceb79fef03 | 65 | * @endcode |
kevman | 0:38ceb79fef03 | 66 | * @ingroup drivers |
kevman | 0:38ceb79fef03 | 67 | */ |
kevman | 0:38ceb79fef03 | 68 | class I2CSlave { |
kevman | 0:38ceb79fef03 | 69 | |
kevman | 0:38ceb79fef03 | 70 | public: |
kevman | 0:38ceb79fef03 | 71 | enum RxStatus { |
kevman | 0:38ceb79fef03 | 72 | NoData = 0, |
kevman | 0:38ceb79fef03 | 73 | ReadAddressed = 1, |
kevman | 0:38ceb79fef03 | 74 | WriteGeneral = 2, |
kevman | 0:38ceb79fef03 | 75 | WriteAddressed = 3 |
kevman | 0:38ceb79fef03 | 76 | }; |
kevman | 0:38ceb79fef03 | 77 | |
kevman | 0:38ceb79fef03 | 78 | /** Create an I2C Slave interface, connected to the specified pins. |
kevman | 0:38ceb79fef03 | 79 | * |
kevman | 0:38ceb79fef03 | 80 | * @param sda I2C data line pin. |
kevman | 0:38ceb79fef03 | 81 | * @param scl I2C clock line pin. |
kevman | 0:38ceb79fef03 | 82 | */ |
kevman | 0:38ceb79fef03 | 83 | I2CSlave(PinName sda, PinName scl); |
kevman | 0:38ceb79fef03 | 84 | |
kevman | 0:38ceb79fef03 | 85 | /** Set the frequency of the I2C interface. |
kevman | 0:38ceb79fef03 | 86 | * |
kevman | 0:38ceb79fef03 | 87 | * @param hz The bus frequency in Hertz. |
kevman | 0:38ceb79fef03 | 88 | */ |
kevman | 0:38ceb79fef03 | 89 | void frequency(int hz); |
kevman | 0:38ceb79fef03 | 90 | |
kevman | 0:38ceb79fef03 | 91 | /** Check if this I2C Slave has been addressed. |
kevman | 0:38ceb79fef03 | 92 | * |
kevman | 0:38ceb79fef03 | 93 | * @return A status indicating if the device has been addressed and how. |
kevman | 0:38ceb79fef03 | 94 | * @retval NoData The slave has not been addressed. |
kevman | 0:38ceb79fef03 | 95 | * @retval ReadAddressed The master has requested a read from this slave. |
kevman | 0:38ceb79fef03 | 96 | * @retval WriteAddressed The master is writing to this slave. |
kevman | 0:38ceb79fef03 | 97 | * @retval WriteGeneral The master is writing to all slave. |
kevman | 0:38ceb79fef03 | 98 | */ |
kevman | 0:38ceb79fef03 | 99 | int receive(void); |
kevman | 0:38ceb79fef03 | 100 | |
kevman | 0:38ceb79fef03 | 101 | /** Read specified number of bytes from an I2C master. |
kevman | 0:38ceb79fef03 | 102 | * |
kevman | 0:38ceb79fef03 | 103 | * @param data Pointer to the buffer to read data into. |
kevman | 0:38ceb79fef03 | 104 | * @param length Number of bytes to read. |
kevman | 0:38ceb79fef03 | 105 | * |
kevman | 0:38ceb79fef03 | 106 | * @return Result of the operation. |
kevman | 0:38ceb79fef03 | 107 | * @retval 0 If the number of bytes read is equal to length requested. |
kevman | 0:38ceb79fef03 | 108 | * @retval nonzero On error or if the number of bytes read is less than requested. |
kevman | 0:38ceb79fef03 | 109 | */ |
kevman | 0:38ceb79fef03 | 110 | int read(char *data, int length); |
kevman | 0:38ceb79fef03 | 111 | |
kevman | 0:38ceb79fef03 | 112 | /** Read a single byte from an I2C master. |
kevman | 0:38ceb79fef03 | 113 | * |
kevman | 0:38ceb79fef03 | 114 | * @return The byte read. |
kevman | 0:38ceb79fef03 | 115 | */ |
kevman | 0:38ceb79fef03 | 116 | int read(void); |
kevman | 0:38ceb79fef03 | 117 | |
kevman | 0:38ceb79fef03 | 118 | /** Write to an I2C master. |
kevman | 0:38ceb79fef03 | 119 | * |
kevman | 0:38ceb79fef03 | 120 | * @param data Pointer to the buffer containing the data to be sent. |
kevman | 0:38ceb79fef03 | 121 | * @param length Number of bytes to send. |
kevman | 0:38ceb79fef03 | 122 | * |
kevman | 0:38ceb79fef03 | 123 | * @return |
kevman | 0:38ceb79fef03 | 124 | * @retval 0 If written all bytes successfully. |
kevman | 0:38ceb79fef03 | 125 | * @retval nonzero On error or if the number of bytes written is less than requested. |
kevman | 0:38ceb79fef03 | 126 | */ |
kevman | 0:38ceb79fef03 | 127 | int write(const char *data, int length); |
kevman | 0:38ceb79fef03 | 128 | |
kevman | 0:38ceb79fef03 | 129 | /** Write a single byte to an I2C master. |
kevman | 0:38ceb79fef03 | 130 | * |
kevman | 0:38ceb79fef03 | 131 | * @param data Value to write. |
kevman | 0:38ceb79fef03 | 132 | * |
kevman | 0:38ceb79fef03 | 133 | * @return Result of the operation. |
kevman | 0:38ceb79fef03 | 134 | * @retval 0 If a NACK is received. |
kevman | 0:38ceb79fef03 | 135 | * @retval 1 If an ACK is received. |
kevman | 0:38ceb79fef03 | 136 | * @retval 2 On timeout. |
kevman | 0:38ceb79fef03 | 137 | */ |
kevman | 0:38ceb79fef03 | 138 | int write(int data); |
kevman | 0:38ceb79fef03 | 139 | |
kevman | 0:38ceb79fef03 | 140 | /** Set the I2C slave address. |
kevman | 0:38ceb79fef03 | 141 | * |
kevman | 0:38ceb79fef03 | 142 | * @param address The address to set for the slave (least significant bit is ignored). |
kevman | 0:38ceb79fef03 | 143 | * |
kevman | 0:38ceb79fef03 | 144 | * @note If address is set to 0, the slave will only respond to the |
kevman | 0:38ceb79fef03 | 145 | * general call address. |
kevman | 0:38ceb79fef03 | 146 | */ |
kevman | 0:38ceb79fef03 | 147 | void address(int address); |
kevman | 0:38ceb79fef03 | 148 | |
kevman | 0:38ceb79fef03 | 149 | /** Reset the I2C slave back into the known ready receiving state. |
kevman | 0:38ceb79fef03 | 150 | */ |
kevman | 0:38ceb79fef03 | 151 | void stop(void); |
kevman | 0:38ceb79fef03 | 152 | |
kevman | 0:38ceb79fef03 | 153 | #if !defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 154 | |
kevman | 0:38ceb79fef03 | 155 | protected: |
kevman | 0:38ceb79fef03 | 156 | /* Internal i2c object identifying the resources */ |
kevman | 0:38ceb79fef03 | 157 | i2c_t _i2c; |
kevman | 0:38ceb79fef03 | 158 | |
kevman | 0:38ceb79fef03 | 159 | #endif //!defined(DOXYGEN_ONLY) |
kevman | 0:38ceb79fef03 | 160 | }; |
kevman | 0:38ceb79fef03 | 161 | |
kevman | 0:38ceb79fef03 | 162 | } // namespace mbed |
kevman | 0:38ceb79fef03 | 163 | |
kevman | 0:38ceb79fef03 | 164 | #endif |
kevman | 0:38ceb79fef03 | 165 | |
kevman | 0:38ceb79fef03 | 166 | #endif |