The prosthetic control(MIT)

Committer:
ganlikun
Date:
Thu Jun 23 05:23:34 2022 +0000
Revision:
0:20e0c61e0684
01

Who changed what in which revision?

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