test

Dependents:   robotic_fish_6

Committer:
juansal12
Date:
Fri Dec 03 23:00:34 2021 +0000
Revision:
0:c792b17d9f78
uploaded sofi code ;

Who changed what in which revision?

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