mbed library sources. Supersedes mbed-src.

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

Committer:
AnnaBridge
Date:
Thu Nov 08 11:46:34 2018 +0000
Revision:
188:bcfe06ba3d64
Parent:
167:e84263d55307
Child:
189:f392fc9709a3
mbed-dev library. Release version 164

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