this is i2c library on wallbot BLE. wallbot BLE using BL600. BL600 has one of nRF51822 module. but P0_20 is not use any I/O. this library has use P0_20:SCL P0_21:SDA. based on mbed-src i2c class.

Committer:
sibu2
Date:
Sun Aug 31 12:52:08 2014 +0000
Revision:
0:c2a44165fec8
this is my testing i2c library for BL600; BL600 has one of nRF51822 module.; but P0_20 is not use any I/O.; this library use SCL:P0_21 and SDA:P0_22.; based on mbed-src i2c class.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sibu2 0:c2a44165fec8 1 /* mbed Microcontroller Library
sibu2 0:c2a44165fec8 2 * Copyright (c) 2006-2013 ARM Limited
sibu2 0:c2a44165fec8 3 *
sibu2 0:c2a44165fec8 4 * Licensed under the Apache License, Version 2.0 (the "License");
sibu2 0:c2a44165fec8 5 * you may not use this file except in compliance with the License.
sibu2 0:c2a44165fec8 6 * You may obtain a copy of the License at
sibu2 0:c2a44165fec8 7 *
sibu2 0:c2a44165fec8 8 * http://www.apache.org/licenses/LICENSE-2.0
sibu2 0:c2a44165fec8 9 *
sibu2 0:c2a44165fec8 10 * Unless required by applicable law or agreed to in writing, software
sibu2 0:c2a44165fec8 11 * distributed under the License is distributed on an "AS IS" BASIS,
sibu2 0:c2a44165fec8 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
sibu2 0:c2a44165fec8 13 * See the License for the specific language governing permissions and
sibu2 0:c2a44165fec8 14 * limitations under the License.
sibu2 0:c2a44165fec8 15 */
sibu2 0:c2a44165fec8 16 #include <stdio.h>
sibu2 0:c2a44165fec8 17 #include "myi2c.h"
sibu2 0:c2a44165fec8 18
sibu2 0:c2a44165fec8 19 //#if DEVICE_I2C
sibu2 0:c2a44165fec8 20
sibu2 0:c2a44165fec8 21 namespace mbed {
sibu2 0:c2a44165fec8 22
sibu2 0:c2a44165fec8 23 myI2C *myI2C::_owner = NULL;
sibu2 0:c2a44165fec8 24
sibu2 0:c2a44165fec8 25 myI2C::myI2C(PinName sda, PinName scl) : _i2c(), _hz(100000) {
sibu2 0:c2a44165fec8 26 // The init function also set the frequency to 100000
sibu2 0:c2a44165fec8 27 i2c_init(&_i2c, sda, scl);
sibu2 0:c2a44165fec8 28
sibu2 0:c2a44165fec8 29 // Used to avoid unnecessary frequency updates
sibu2 0:c2a44165fec8 30 _owner = this;
sibu2 0:c2a44165fec8 31 }
sibu2 0:c2a44165fec8 32
sibu2 0:c2a44165fec8 33 void myI2C::frequency(int hz) {
sibu2 0:c2a44165fec8 34 _hz = hz;
sibu2 0:c2a44165fec8 35
sibu2 0:c2a44165fec8 36 // We want to update the frequency even if we are already the bus owners
sibu2 0:c2a44165fec8 37 i2c_frequency(&_i2c, _hz);
sibu2 0:c2a44165fec8 38
sibu2 0:c2a44165fec8 39 // Updating the frequency of the bus we become the owners of it
sibu2 0:c2a44165fec8 40 _owner = this;
sibu2 0:c2a44165fec8 41 }
sibu2 0:c2a44165fec8 42
sibu2 0:c2a44165fec8 43 void myI2C::aquire() {
sibu2 0:c2a44165fec8 44 if (_owner != this) {
sibu2 0:c2a44165fec8 45 i2c_frequency(&_i2c, _hz);
sibu2 0:c2a44165fec8 46 _owner = this;
sibu2 0:c2a44165fec8 47 }
sibu2 0:c2a44165fec8 48 }
sibu2 0:c2a44165fec8 49
sibu2 0:c2a44165fec8 50 // write - Master Transmitter Mode
sibu2 0:c2a44165fec8 51 int myI2C::write(int address, const char* data, int length, bool repeated) {
sibu2 0:c2a44165fec8 52 aquire();
sibu2 0:c2a44165fec8 53
sibu2 0:c2a44165fec8 54 int stop = (repeated) ? 0 : 1;
sibu2 0:c2a44165fec8 55 int written = i2c_write(&_i2c, address, data, length, stop);
sibu2 0:c2a44165fec8 56
sibu2 0:c2a44165fec8 57 return length != written;
sibu2 0:c2a44165fec8 58 }
sibu2 0:c2a44165fec8 59
sibu2 0:c2a44165fec8 60 int myI2C::write(int data) {
sibu2 0:c2a44165fec8 61 return i2c_byte_write(&_i2c, data);
sibu2 0:c2a44165fec8 62 }
sibu2 0:c2a44165fec8 63
sibu2 0:c2a44165fec8 64 // read - Master Reciever Mode
sibu2 0:c2a44165fec8 65 int myI2C::read(int address, char* data, int length, bool repeated) {
sibu2 0:c2a44165fec8 66 aquire();
sibu2 0:c2a44165fec8 67
sibu2 0:c2a44165fec8 68 int stop = (repeated) ? 0 : 1;
sibu2 0:c2a44165fec8 69 int read = i2c_read(&_i2c, address, data, length, stop);
sibu2 0:c2a44165fec8 70
sibu2 0:c2a44165fec8 71 return length != read;
sibu2 0:c2a44165fec8 72 }
sibu2 0:c2a44165fec8 73
sibu2 0:c2a44165fec8 74 int myI2C::read(int ack) {
sibu2 0:c2a44165fec8 75 if (ack) {
sibu2 0:c2a44165fec8 76 return i2c_byte_read(&_i2c, 0);
sibu2 0:c2a44165fec8 77 } else {
sibu2 0:c2a44165fec8 78 return i2c_byte_read(&_i2c, 1);
sibu2 0:c2a44165fec8 79 }
sibu2 0:c2a44165fec8 80 }
sibu2 0:c2a44165fec8 81
sibu2 0:c2a44165fec8 82 void myI2C::start(void) {
sibu2 0:c2a44165fec8 83 i2c_start(&_i2c);
sibu2 0:c2a44165fec8 84 }
sibu2 0:c2a44165fec8 85
sibu2 0:c2a44165fec8 86 void myI2C::stop(void) {
sibu2 0:c2a44165fec8 87 i2c_stop(&_i2c);
sibu2 0:c2a44165fec8 88 }
sibu2 0:c2a44165fec8 89
sibu2 0:c2a44165fec8 90 } // namespace mbed
sibu2 0:c2a44165fec8 91
sibu2 0:c2a44165fec8 92 //#endif