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 #include "I2C.h"
yihui 0:700cadd8b708 17
yihui 0:700cadd8b708 18 #if DEVICE_I2C
yihui 0:700cadd8b708 19
yihui 0:700cadd8b708 20 namespace mbed {
yihui 0:700cadd8b708 21
yihui 0:700cadd8b708 22 I2C *I2C::_owner = NULL;
yihui 0:700cadd8b708 23
yihui 0:700cadd8b708 24 I2C::I2C(PinName sda, PinName scl) : _i2c(), _hz(100000) {
yihui 0:700cadd8b708 25 // The init function also set the frequency to 100000
yihui 0:700cadd8b708 26 i2c_init(&_i2c, sda, scl);
yihui 0:700cadd8b708 27
yihui 0:700cadd8b708 28 // Used to avoid unnecessary frequency updates
yihui 0:700cadd8b708 29 _owner = this;
yihui 0:700cadd8b708 30 }
yihui 0:700cadd8b708 31
yihui 0:700cadd8b708 32 void I2C::frequency(int hz) {
yihui 0:700cadd8b708 33 _hz = hz;
yihui 0:700cadd8b708 34
yihui 0:700cadd8b708 35 // We want to update the frequency even if we are already the bus owners
yihui 0:700cadd8b708 36 i2c_frequency(&_i2c, _hz);
yihui 0:700cadd8b708 37
yihui 0:700cadd8b708 38 // Updating the frequency of the bus we become the owners of it
yihui 0:700cadd8b708 39 _owner = this;
yihui 0:700cadd8b708 40 }
yihui 0:700cadd8b708 41
yihui 0:700cadd8b708 42 void I2C::aquire() {
yihui 0:700cadd8b708 43 if (_owner != this) {
yihui 0:700cadd8b708 44 i2c_frequency(&_i2c, _hz);
yihui 0:700cadd8b708 45 _owner = this;
yihui 0:700cadd8b708 46 }
yihui 0:700cadd8b708 47 }
yihui 0:700cadd8b708 48
yihui 0:700cadd8b708 49 // write - Master Transmitter Mode
yihui 0:700cadd8b708 50 int I2C::write(int address, const char* data, int length, bool repeated) {
yihui 0:700cadd8b708 51 aquire();
yihui 0:700cadd8b708 52
yihui 0:700cadd8b708 53 int stop = (repeated) ? 0 : 1;
yihui 0:700cadd8b708 54 int written = i2c_write(&_i2c, address, data, length, stop);
yihui 0:700cadd8b708 55
yihui 0:700cadd8b708 56 return length != written;
yihui 0:700cadd8b708 57 }
yihui 0:700cadd8b708 58
yihui 0:700cadd8b708 59 int I2C::write(int data) {
yihui 0:700cadd8b708 60 return i2c_byte_write(&_i2c, data);
yihui 0:700cadd8b708 61 }
yihui 0:700cadd8b708 62
yihui 0:700cadd8b708 63 // read - Master Reciever Mode
yihui 0:700cadd8b708 64 int I2C::read(int address, char* data, int length, bool repeated) {
yihui 0:700cadd8b708 65 aquire();
yihui 0:700cadd8b708 66
yihui 0:700cadd8b708 67 int stop = (repeated) ? 0 : 1;
yihui 0:700cadd8b708 68 int read = i2c_read(&_i2c, address, data, length, stop);
yihui 0:700cadd8b708 69
yihui 0:700cadd8b708 70 return length != read;
yihui 0:700cadd8b708 71 }
yihui 0:700cadd8b708 72
yihui 0:700cadd8b708 73 int I2C::read(int ack) {
yihui 0:700cadd8b708 74 if (ack) {
yihui 0:700cadd8b708 75 return i2c_byte_read(&_i2c, 0);
yihui 0:700cadd8b708 76 } else {
yihui 0:700cadd8b708 77 return i2c_byte_read(&_i2c, 1);
yihui 0:700cadd8b708 78 }
yihui 0:700cadd8b708 79 }
yihui 0:700cadd8b708 80
yihui 0:700cadd8b708 81 void I2C::start(void) {
yihui 0:700cadd8b708 82 i2c_start(&_i2c);
yihui 0:700cadd8b708 83 }
yihui 0:700cadd8b708 84
yihui 0:700cadd8b708 85 void I2C::stop(void) {
yihui 0:700cadd8b708 86 i2c_stop(&_i2c);
yihui 0:700cadd8b708 87 }
yihui 0:700cadd8b708 88
yihui 0:700cadd8b708 89 } // namespace mbed
yihui 0:700cadd8b708 90
yihui 0:700cadd8b708 91 #endif