IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Sat Oct 29 15:11:28 2016 +0000
Revision:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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