Pinned to some recent date

Committer:
Simon Cooksey
Date:
Thu Nov 17 16:43:53 2016 +0000
Revision:
0:fb7af294d5d9
Initial commit

Who changed what in which revision?

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