Test code for Grove Node BLE

Dependencies:   BLE_API nRF51822

Fork of BLE_LoopbackUART by Bluetooth Low Energy

Committer:
yihui
Date:
Wed Oct 29 06:23:47 2014 +0000
Revision:
9:05f0b5a3a70a
initial

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_H
yihui 9:05f0b5a3a70a 17 #define MBED_I2C_H
yihui 9:05f0b5a3a70a 18
yihui 9:05f0b5a3a70a 19 #include "platform.h"
yihui 9:05f0b5a3a70a 20
yihui 9:05f0b5a3a70a 21 #if DEVICE_I2C
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 Master, used for communicating with I2C slave devices
yihui 9:05f0b5a3a70a 28 *
yihui 9:05f0b5a3a70a 29 * Example:
yihui 9:05f0b5a3a70a 30 * @code
yihui 9:05f0b5a3a70a 31 * // Read from I2C slave at address 0x62
yihui 9:05f0b5a3a70a 32 *
yihui 9:05f0b5a3a70a 33 * #include "mbed.h"
yihui 9:05f0b5a3a70a 34 *
yihui 9:05f0b5a3a70a 35 * I2C i2c(p28, p27);
yihui 9:05f0b5a3a70a 36 *
yihui 9:05f0b5a3a70a 37 * int main() {
yihui 9:05f0b5a3a70a 38 * int address = 0x62;
yihui 9:05f0b5a3a70a 39 * char data[2];
yihui 9:05f0b5a3a70a 40 * i2c.read(address, data, 2);
yihui 9:05f0b5a3a70a 41 * }
yihui 9:05f0b5a3a70a 42 * @endcode
yihui 9:05f0b5a3a70a 43 */
yihui 9:05f0b5a3a70a 44 class I2C {
yihui 9:05f0b5a3a70a 45
yihui 9:05f0b5a3a70a 46 public:
yihui 9:05f0b5a3a70a 47 enum RxStatus {
yihui 9:05f0b5a3a70a 48 NoData,
yihui 9:05f0b5a3a70a 49 MasterGeneralCall,
yihui 9:05f0b5a3a70a 50 MasterWrite,
yihui 9:05f0b5a3a70a 51 MasterRead
yihui 9:05f0b5a3a70a 52 };
yihui 9:05f0b5a3a70a 53
yihui 9:05f0b5a3a70a 54 enum Acknowledge {
yihui 9:05f0b5a3a70a 55 NoACK = 0,
yihui 9:05f0b5a3a70a 56 ACK = 1
yihui 9:05f0b5a3a70a 57 };
yihui 9:05f0b5a3a70a 58
yihui 9:05f0b5a3a70a 59 /** Create an I2C Master interface, connected to the specified pins
yihui 9:05f0b5a3a70a 60 *
yihui 9:05f0b5a3a70a 61 * @param sda I2C data line pin
yihui 9:05f0b5a3a70a 62 * @param scl I2C clock line pin
yihui 9:05f0b5a3a70a 63 */
yihui 9:05f0b5a3a70a 64 I2C(PinName sda, PinName scl);
yihui 9:05f0b5a3a70a 65
yihui 9:05f0b5a3a70a 66 ~I2C() {
yihui 9:05f0b5a3a70a 67 i2c_free(&_i2c);
yihui 9:05f0b5a3a70a 68 }
yihui 9:05f0b5a3a70a 69
yihui 9:05f0b5a3a70a 70 /** Set the frequency of the I2C interface
yihui 9:05f0b5a3a70a 71 *
yihui 9:05f0b5a3a70a 72 * @param hz The bus frequency in hertz
yihui 9:05f0b5a3a70a 73 */
yihui 9:05f0b5a3a70a 74 void frequency(int hz);
yihui 9:05f0b5a3a70a 75
yihui 9:05f0b5a3a70a 76 /** Read from an I2C slave
yihui 9:05f0b5a3a70a 77 *
yihui 9:05f0b5a3a70a 78 * Performs a complete read transaction. The bottom bit of
yihui 9:05f0b5a3a70a 79 * the address is forced to 1 to indicate a read.
yihui 9:05f0b5a3a70a 80 *
yihui 9:05f0b5a3a70a 81 * @param address 8-bit I2C slave address [ addr | 1 ]
yihui 9:05f0b5a3a70a 82 * @param data Pointer to the byte-array to read data in to
yihui 9:05f0b5a3a70a 83 * @param length Number of bytes to read
yihui 9:05f0b5a3a70a 84 * @param repeated Repeated start, true - don't send stop at end
yihui 9:05f0b5a3a70a 85 *
yihui 9:05f0b5a3a70a 86 * @returns
yihui 9:05f0b5a3a70a 87 * 0 on success (ack),
yihui 9:05f0b5a3a70a 88 * non-0 on failure (nack)
yihui 9:05f0b5a3a70a 89 */
yihui 9:05f0b5a3a70a 90 int read(int address, char *data, int length, bool repeated = false);
yihui 9:05f0b5a3a70a 91
yihui 9:05f0b5a3a70a 92 /** Read a single byte from the I2C bus
yihui 9:05f0b5a3a70a 93 *
yihui 9:05f0b5a3a70a 94 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
yihui 9:05f0b5a3a70a 95 *
yihui 9:05f0b5a3a70a 96 * @returns
yihui 9:05f0b5a3a70a 97 * the byte read
yihui 9:05f0b5a3a70a 98 */
yihui 9:05f0b5a3a70a 99 int read(int ack);
yihui 9:05f0b5a3a70a 100
yihui 9:05f0b5a3a70a 101 /** Write to an I2C slave
yihui 9:05f0b5a3a70a 102 *
yihui 9:05f0b5a3a70a 103 * Performs a complete write transaction. The bottom bit of
yihui 9:05f0b5a3a70a 104 * the address is forced to 0 to indicate a write.
yihui 9:05f0b5a3a70a 105 *
yihui 9:05f0b5a3a70a 106 * @param address 8-bit I2C slave address [ addr | 0 ]
yihui 9:05f0b5a3a70a 107 * @param data Pointer to the byte-array data to send
yihui 9:05f0b5a3a70a 108 * @param length Number of bytes to send
yihui 9:05f0b5a3a70a 109 * @param repeated Repeated start, true - do not send stop at end
yihui 9:05f0b5a3a70a 110 *
yihui 9:05f0b5a3a70a 111 * @returns
yihui 9:05f0b5a3a70a 112 * 0 on success (ack),
yihui 9:05f0b5a3a70a 113 * non-0 on failure (nack)
yihui 9:05f0b5a3a70a 114 */
yihui 9:05f0b5a3a70a 115 int write(int address, const char *data, int length, bool repeated = false);
yihui 9:05f0b5a3a70a 116
yihui 9:05f0b5a3a70a 117 /** Write single byte out on the I2C bus
yihui 9:05f0b5a3a70a 118 *
yihui 9:05f0b5a3a70a 119 * @param data data to write out on bus
yihui 9:05f0b5a3a70a 120 *
yihui 9:05f0b5a3a70a 121 * @returns
yihui 9:05f0b5a3a70a 122 * '1' if an ACK was received,
yihui 9:05f0b5a3a70a 123 * '0' otherwise
yihui 9:05f0b5a3a70a 124 */
yihui 9:05f0b5a3a70a 125 int write(int data);
yihui 9:05f0b5a3a70a 126
yihui 9:05f0b5a3a70a 127 /** Creates a start condition on the I2C bus
yihui 9:05f0b5a3a70a 128 */
yihui 9:05f0b5a3a70a 129
yihui 9:05f0b5a3a70a 130 void start(void);
yihui 9:05f0b5a3a70a 131
yihui 9:05f0b5a3a70a 132 /** Creates a stop condition on the I2C bus
yihui 9:05f0b5a3a70a 133 */
yihui 9:05f0b5a3a70a 134 void stop(void);
yihui 9:05f0b5a3a70a 135
yihui 9:05f0b5a3a70a 136 protected:
yihui 9:05f0b5a3a70a 137 void aquire();
yihui 9:05f0b5a3a70a 138
yihui 9:05f0b5a3a70a 139 i2c_t _i2c;
yihui 9:05f0b5a3a70a 140 static I2C *_owner;
yihui 9:05f0b5a3a70a 141 int _hz;
yihui 9:05f0b5a3a70a 142 };
yihui 9:05f0b5a3a70a 143
yihui 9:05f0b5a3a70a 144 } // namespace mbed
yihui 9:05f0b5a3a70a 145
yihui 9:05f0b5a3a70a 146 #endif
yihui 9:05f0b5a3a70a 147
yihui 9:05f0b5a3a70a 148 #endif