.

Dependents:   RTC

Committer:
jhon309
Date:
Thu Aug 13 00:20:09 2015 +0000
Revision:
0:88e313c910d0
RTC Example

Who changed what in which revision?

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