Birkbeck College Mobile and Ubiquitous Computing IoT Lab Exercise 2

Dependencies:   BLE_API_Native_blog

Committer:
gkroussos
Date:
Sat Mar 07 16:34:53 2015 +0000
Revision:
0:e8fdba0ed044
MUC IoT Workshop v1.0

Who changed what in which revision?

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