mbed SDK library sources

Fork of mbed-src by mbed official

Development branch of the mbed library sources. This library is kept in synch with the latest changes from the mbed SDK and it is not guaranteed to work.

If you are looking for a stable and tested release, please import one of the official mbed library releases:

Import librarymbed

The official Mbed 2 C/C++ SDK provides the software platform and libraries to build your applications.

Committer:
bogdanm
Date:
Mon Aug 05 14:12:34 2013 +0300
Revision:
13:0645d8841f51
Parent:
10:3bc89ef62ce7
Update mbed sources to revision 64

Who changed what in which revision?

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