Mangue Baja Box

Dependencies:   mbed

Committer:
einsteingustavo
Date:
Mon Jul 29 20:38:00 2019 +0000
Revision:
0:0dee8840a1c0
Mangue Baja Box

Who changed what in which revision?

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