SPKT

Dependencies:   F746_GUI SD_PlayerSkeleton F746_SAI_IO

Committer:
phungductung
Date:
Tue Jun 04 21:37:21 2019 +0000
Revision:
0:8ede47d38d10
SPKT

Who changed what in which revision?

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