customized mbed library sources for nrf51822

Dependents:   Grove_Node Potentiometer BLE_Beacon I2C_Scanner

Committer:
yihui
Date:
Tue Nov 04 07:38:53 2014 +0000
Revision:
0:700cadd8b708
customized mbed-src library for nrf51822

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:700cadd8b708 1 /* mbed Microcontroller Library
yihui 0:700cadd8b708 2 * Copyright (c) 2006-2013 ARM Limited
yihui 0:700cadd8b708 3 *
yihui 0:700cadd8b708 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:700cadd8b708 5 * you may not use this file except in compliance with the License.
yihui 0:700cadd8b708 6 * You may obtain a copy of the License at
yihui 0:700cadd8b708 7 *
yihui 0:700cadd8b708 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:700cadd8b708 9 *
yihui 0:700cadd8b708 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:700cadd8b708 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:700cadd8b708 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:700cadd8b708 13 * See the License for the specific language governing permissions and
yihui 0:700cadd8b708 14 * limitations under the License.
yihui 0:700cadd8b708 15 */
yihui 0:700cadd8b708 16 #ifndef MBED_I2C_H
yihui 0:700cadd8b708 17 #define MBED_I2C_H
yihui 0:700cadd8b708 18
yihui 0:700cadd8b708 19 #include "platform.h"
yihui 0:700cadd8b708 20
yihui 0:700cadd8b708 21 #if DEVICE_I2C
yihui 0:700cadd8b708 22
yihui 0:700cadd8b708 23 #include "i2c_api.h"
yihui 0:700cadd8b708 24
yihui 0:700cadd8b708 25 namespace mbed {
yihui 0:700cadd8b708 26
yihui 0:700cadd8b708 27 /** An I2C Master, used for communicating with I2C slave devices
yihui 0:700cadd8b708 28 *
yihui 0:700cadd8b708 29 * Example:
yihui 0:700cadd8b708 30 * @code
yihui 0:700cadd8b708 31 * // Read from I2C slave at address 0x62
yihui 0:700cadd8b708 32 *
yihui 0:700cadd8b708 33 * #include "mbed.h"
yihui 0:700cadd8b708 34 *
yihui 0:700cadd8b708 35 * I2C i2c(p28, p27);
yihui 0:700cadd8b708 36 *
yihui 0:700cadd8b708 37 * int main() {
yihui 0:700cadd8b708 38 * int address = 0x62;
yihui 0:700cadd8b708 39 * char data[2];
yihui 0:700cadd8b708 40 * i2c.read(address, data, 2);
yihui 0:700cadd8b708 41 * }
yihui 0:700cadd8b708 42 * @endcode
yihui 0:700cadd8b708 43 */
yihui 0:700cadd8b708 44 class I2C {
yihui 0:700cadd8b708 45
yihui 0:700cadd8b708 46 public:
yihui 0:700cadd8b708 47 enum RxStatus {
yihui 0:700cadd8b708 48 NoData,
yihui 0:700cadd8b708 49 MasterGeneralCall,
yihui 0:700cadd8b708 50 MasterWrite,
yihui 0:700cadd8b708 51 MasterRead
yihui 0:700cadd8b708 52 };
yihui 0:700cadd8b708 53
yihui 0:700cadd8b708 54 enum Acknowledge {
yihui 0:700cadd8b708 55 NoACK = 0,
yihui 0:700cadd8b708 56 ACK = 1
yihui 0:700cadd8b708 57 };
yihui 0:700cadd8b708 58
yihui 0:700cadd8b708 59 /** Create an I2C Master interface, connected to the specified pins
yihui 0:700cadd8b708 60 *
yihui 0:700cadd8b708 61 * @param sda I2C data line pin
yihui 0:700cadd8b708 62 * @param scl I2C clock line pin
yihui 0:700cadd8b708 63 */
yihui 0:700cadd8b708 64 I2C(PinName sda, PinName scl);
yihui 0:700cadd8b708 65
yihui 0:700cadd8b708 66 ~I2C() {
yihui 0:700cadd8b708 67 i2c_free(&_i2c);
yihui 0:700cadd8b708 68 }
yihui 0:700cadd8b708 69
yihui 0:700cadd8b708 70 /** Set the frequency of the I2C interface
yihui 0:700cadd8b708 71 *
yihui 0:700cadd8b708 72 * @param hz The bus frequency in hertz
yihui 0:700cadd8b708 73 */
yihui 0:700cadd8b708 74 void frequency(int hz);
yihui 0:700cadd8b708 75
yihui 0:700cadd8b708 76 /** Read from an I2C slave
yihui 0:700cadd8b708 77 *
yihui 0:700cadd8b708 78 * Performs a complete read transaction. The bottom bit of
yihui 0:700cadd8b708 79 * the address is forced to 1 to indicate a read.
yihui 0:700cadd8b708 80 *
yihui 0:700cadd8b708 81 * @param address 8-bit I2C slave address [ addr | 1 ]
yihui 0:700cadd8b708 82 * @param data Pointer to the byte-array to read data in to
yihui 0:700cadd8b708 83 * @param length Number of bytes to read
yihui 0:700cadd8b708 84 * @param repeated Repeated start, true - don't send stop at end
yihui 0:700cadd8b708 85 *
yihui 0:700cadd8b708 86 * @returns
yihui 0:700cadd8b708 87 * 0 on success (ack),
yihui 0:700cadd8b708 88 * non-0 on failure (nack)
yihui 0:700cadd8b708 89 */
yihui 0:700cadd8b708 90 int read(int address, char *data, int length, bool repeated = false);
yihui 0:700cadd8b708 91
yihui 0:700cadd8b708 92 /** Read a single byte from the I2C bus
yihui 0:700cadd8b708 93 *
yihui 0:700cadd8b708 94 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
yihui 0:700cadd8b708 95 *
yihui 0:700cadd8b708 96 * @returns
yihui 0:700cadd8b708 97 * the byte read
yihui 0:700cadd8b708 98 */
yihui 0:700cadd8b708 99 int read(int ack);
yihui 0:700cadd8b708 100
yihui 0:700cadd8b708 101 /** Write to an I2C slave
yihui 0:700cadd8b708 102 *
yihui 0:700cadd8b708 103 * Performs a complete write transaction. The bottom bit of
yihui 0:700cadd8b708 104 * the address is forced to 0 to indicate a write.
yihui 0:700cadd8b708 105 *
yihui 0:700cadd8b708 106 * @param address 8-bit I2C slave address [ addr | 0 ]
yihui 0:700cadd8b708 107 * @param data Pointer to the byte-array data to send
yihui 0:700cadd8b708 108 * @param length Number of bytes to send
yihui 0:700cadd8b708 109 * @param repeated Repeated start, true - do not send stop at end
yihui 0:700cadd8b708 110 *
yihui 0:700cadd8b708 111 * @returns
yihui 0:700cadd8b708 112 * 0 on success (ack),
yihui 0:700cadd8b708 113 * non-0 on failure (nack)
yihui 0:700cadd8b708 114 */
yihui 0:700cadd8b708 115 int write(int address, const char *data, int length, bool repeated = false);
yihui 0:700cadd8b708 116
yihui 0:700cadd8b708 117 /** Write single byte out on the I2C bus
yihui 0:700cadd8b708 118 *
yihui 0:700cadd8b708 119 * @param data data to write out on bus
yihui 0:700cadd8b708 120 *
yihui 0:700cadd8b708 121 * @returns
yihui 0:700cadd8b708 122 * '1' if an ACK was received,
yihui 0:700cadd8b708 123 * '0' otherwise
yihui 0:700cadd8b708 124 */
yihui 0:700cadd8b708 125 int write(int data);
yihui 0:700cadd8b708 126
yihui 0:700cadd8b708 127 /** Creates a start condition on the I2C bus
yihui 0:700cadd8b708 128 */
yihui 0:700cadd8b708 129
yihui 0:700cadd8b708 130 void start(void);
yihui 0:700cadd8b708 131
yihui 0:700cadd8b708 132 /** Creates a stop condition on the I2C bus
yihui 0:700cadd8b708 133 */
yihui 0:700cadd8b708 134 void stop(void);
yihui 0:700cadd8b708 135
yihui 0:700cadd8b708 136 protected:
yihui 0:700cadd8b708 137 void aquire();
yihui 0:700cadd8b708 138
yihui 0:700cadd8b708 139 i2c_t _i2c;
yihui 0:700cadd8b708 140 static I2C *_owner;
yihui 0:700cadd8b708 141 int _hz;
yihui 0:700cadd8b708 142 };
yihui 0:700cadd8b708 143
yihui 0:700cadd8b708 144 } // namespace mbed
yihui 0:700cadd8b708 145
yihui 0:700cadd8b708 146 #endif
yihui 0:700cadd8b708 147
yihui 0:700cadd8b708 148 #endif