Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Thu Nov 27 09:30:36 2014 +0000
Revision:
10:22480ac31879
Parent:
9:05f0b5a3a70a
change to new revision hardware

Who changed what in which revision?

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