This fork captures the mbed lib v125 for ease of integration into older projects.

Fork of mbed-dev by mbed official

Committer:
apluscw
Date:
Fri Jul 20 21:24:42 2018 +0000
Revision:
187:92cbb9eec47b
Mbed library with source code from mbed lib v125. Posted to ease integration with some older projects.

Who changed what in which revision?

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