test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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