Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:27:58 2016 +0000
Revision:
0:6c56fb4bc5f0
Moving to library for sharing updates

Who changed what in which revision?

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