mbed library for NZ32-SC151

Committer:
modtronix
Date:
Fri Jul 24 21:01:44 2015 +1000
Revision:
1:71204b8406f2
Current mbed v103 (594)

Who changed what in which revision?

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