mbed library sources. Supersedes mbed-src.

Dependents:   Nucleo_Hello_Encoder BLE_iBeaconScan AM1805_DEMO DISCO-F429ZI_ExportTemplate1 ... more

Committer:
AnnaBridge
Date:
Wed Feb 20 22:31:08 2019 +0000
Revision:
189:f392fc9709a3
Parent:
188:bcfe06ba3d64
mbed library release version 165

Who changed what in which revision?

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