5.2.1 - Updated I2C files

Dependents:   mbed-TFT-example-NCS36510 mbed-Accelerometer-example-NCS36510 mbed-Accelerometer-example-NCS36510

Committer:
group-onsemi
Date:
Wed Jan 25 20:34:15 2017 +0000
Revision:
0:098463de4c5d
Initial commit

Who changed what in which revision?

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