initial

Dependencies:   mbed

Committer:
yihui
Date:
Mon Jan 11 02:32:24 2016 +0000
Revision:
0:638edba3adf6
initial

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 0:638edba3adf6 1 /* mbed Microcontroller Library
yihui 0:638edba3adf6 2 * Copyright (c) 2006-2015 ARM Limited
yihui 0:638edba3adf6 3 *
yihui 0:638edba3adf6 4 * Licensed under the Apache License, Version 2.0 (the "License");
yihui 0:638edba3adf6 5 * you may not use this file except in compliance with the License.
yihui 0:638edba3adf6 6 * You may obtain a copy of the License at
yihui 0:638edba3adf6 7 *
yihui 0:638edba3adf6 8 * http://www.apache.org/licenses/LICENSE-2.0
yihui 0:638edba3adf6 9 *
yihui 0:638edba3adf6 10 * Unless required by applicable law or agreed to in writing, software
yihui 0:638edba3adf6 11 * distributed under the License is distributed on an "AS IS" BASIS,
yihui 0:638edba3adf6 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
yihui 0:638edba3adf6 13 * See the License for the specific language governing permissions and
yihui 0:638edba3adf6 14 * limitations under the License.
yihui 0:638edba3adf6 15 */
yihui 0:638edba3adf6 16 #include "I2C.h"
yihui 0:638edba3adf6 17
yihui 0:638edba3adf6 18 #if DEVICE_I2C
yihui 0:638edba3adf6 19
yihui 0:638edba3adf6 20 namespace mbed {
yihui 0:638edba3adf6 21
yihui 0:638edba3adf6 22 I2C *I2C::_owner = NULL;
yihui 0:638edba3adf6 23
yihui 0:638edba3adf6 24 I2C::I2C(PinName sda, PinName scl) :
yihui 0:638edba3adf6 25 #if DEVICE_I2C_ASYNCH
yihui 0:638edba3adf6 26 _irq(this), _usage(DMA_USAGE_NEVER),
yihui 0:638edba3adf6 27 #endif
yihui 0:638edba3adf6 28 _i2c(), _hz(100000) {
yihui 0:638edba3adf6 29 // The init function also set the frequency to 100000
yihui 0:638edba3adf6 30 i2c_init(&_i2c, sda, scl);
yihui 0:638edba3adf6 31
yihui 0:638edba3adf6 32 // Used to avoid unnecessary frequency updates
yihui 0:638edba3adf6 33 _owner = this;
yihui 0:638edba3adf6 34 }
yihui 0:638edba3adf6 35
yihui 0:638edba3adf6 36 void I2C::frequency(int hz) {
yihui 0:638edba3adf6 37 _hz = hz;
yihui 0:638edba3adf6 38
yihui 0:638edba3adf6 39 // We want to update the frequency even if we are already the bus owners
yihui 0:638edba3adf6 40 i2c_frequency(&_i2c, _hz);
yihui 0:638edba3adf6 41
yihui 0:638edba3adf6 42 // Updating the frequency of the bus we become the owners of it
yihui 0:638edba3adf6 43 _owner = this;
yihui 0:638edba3adf6 44 }
yihui 0:638edba3adf6 45
yihui 0:638edba3adf6 46 void I2C::aquire() {
yihui 0:638edba3adf6 47 if (_owner != this) {
yihui 0:638edba3adf6 48 i2c_frequency(&_i2c, _hz);
yihui 0:638edba3adf6 49 _owner = this;
yihui 0:638edba3adf6 50 }
yihui 0:638edba3adf6 51 }
yihui 0:638edba3adf6 52
yihui 0:638edba3adf6 53 // write - Master Transmitter Mode
yihui 0:638edba3adf6 54 int I2C::write(int address, const char* data, int length, bool repeated) {
yihui 0:638edba3adf6 55 aquire();
yihui 0:638edba3adf6 56
yihui 0:638edba3adf6 57 int stop = (repeated) ? 0 : 1;
yihui 0:638edba3adf6 58 int written = i2c_write(&_i2c, address, data, length, stop);
yihui 0:638edba3adf6 59
yihui 0:638edba3adf6 60 return length != written;
yihui 0:638edba3adf6 61 }
yihui 0:638edba3adf6 62
yihui 0:638edba3adf6 63 int I2C::write(int data) {
yihui 0:638edba3adf6 64 return i2c_byte_write(&_i2c, data);
yihui 0:638edba3adf6 65 }
yihui 0:638edba3adf6 66
yihui 0:638edba3adf6 67 // read - Master Reciever Mode
yihui 0:638edba3adf6 68 int I2C::read(int address, char* data, int length, bool repeated) {
yihui 0:638edba3adf6 69 aquire();
yihui 0:638edba3adf6 70
yihui 0:638edba3adf6 71 int stop = (repeated) ? 0 : 1;
yihui 0:638edba3adf6 72 int read = i2c_read(&_i2c, address, data, length, stop);
yihui 0:638edba3adf6 73
yihui 0:638edba3adf6 74 return length != read;
yihui 0:638edba3adf6 75 }
yihui 0:638edba3adf6 76
yihui 0:638edba3adf6 77 int I2C::read(int ack) {
yihui 0:638edba3adf6 78 if (ack) {
yihui 0:638edba3adf6 79 return i2c_byte_read(&_i2c, 0);
yihui 0:638edba3adf6 80 } else {
yihui 0:638edba3adf6 81 return i2c_byte_read(&_i2c, 1);
yihui 0:638edba3adf6 82 }
yihui 0:638edba3adf6 83 }
yihui 0:638edba3adf6 84
yihui 0:638edba3adf6 85 void I2C::start(void) {
yihui 0:638edba3adf6 86 i2c_start(&_i2c);
yihui 0:638edba3adf6 87 }
yihui 0:638edba3adf6 88
yihui 0:638edba3adf6 89 void I2C::stop(void) {
yihui 0:638edba3adf6 90 i2c_stop(&_i2c);
yihui 0:638edba3adf6 91 }
yihui 0:638edba3adf6 92
yihui 0:638edba3adf6 93 #if DEVICE_I2C_ASYNCH
yihui 0:638edba3adf6 94
yihui 0:638edba3adf6 95 int I2C::transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event, bool repeated)
yihui 0:638edba3adf6 96 {
yihui 0:638edba3adf6 97 if (i2c_active(&_i2c)) {
yihui 0:638edba3adf6 98 return -1; // transaction ongoing
yihui 0:638edba3adf6 99 }
yihui 0:638edba3adf6 100 aquire();
yihui 0:638edba3adf6 101
yihui 0:638edba3adf6 102 _callback = callback;
yihui 0:638edba3adf6 103 int stop = (repeated) ? 0 : 1;
yihui 0:638edba3adf6 104 _irq.callback(&I2C::irq_handler_asynch);
yihui 0:638edba3adf6 105 i2c_transfer_asynch(&_i2c, (void *)tx_buffer, tx_length, (void *)rx_buffer, rx_length, address, stop, _irq.entry(), event, _usage);
yihui 0:638edba3adf6 106 return 0;
yihui 0:638edba3adf6 107 }
yihui 0:638edba3adf6 108
yihui 0:638edba3adf6 109 void I2C::abort_transfer(void)
yihui 0:638edba3adf6 110 {
yihui 0:638edba3adf6 111 i2c_abort_asynch(&_i2c);
yihui 0:638edba3adf6 112 }
yihui 0:638edba3adf6 113
yihui 0:638edba3adf6 114 void I2C::irq_handler_asynch(void)
yihui 0:638edba3adf6 115 {
yihui 0:638edba3adf6 116 int event = i2c_irq_handler_asynch(&_i2c);
yihui 0:638edba3adf6 117 if (_callback && event) {
yihui 0:638edba3adf6 118 _callback.call(event);
yihui 0:638edba3adf6 119 }
yihui 0:638edba3adf6 120
yihui 0:638edba3adf6 121 }
yihui 0:638edba3adf6 122
yihui 0:638edba3adf6 123
yihui 0:638edba3adf6 124 #endif
yihui 0:638edba3adf6 125
yihui 0:638edba3adf6 126 } // namespace mbed
yihui 0:638edba3adf6 127
yihui 0:638edba3adf6 128 #endif