mbed library sources

Dependents:   Freedman_v2 Nucleo_i2c_OLED_BME280_copy

Fork of mbed-src by mbed official

Committer:
emilmont
Date:
Fri Jun 14 17:49:17 2013 +0100
Revision:
10:3bc89ef62ce7
Parent:
9:0ce32e54c9a7
Child:
13:0645d8841f51
Unify mbed library sources

Who changed what in which revision?

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