mbed library for NZ32-SC151

Committer:
modtronix-com
Date:
Fri Aug 19 15:46:42 2016 +1000
Revision:
17:639ed60ce759
Parent:
1:71204b8406f2
Added tag v1.1 for changeset 076cbe3e55be

Who changed what in which revision?

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