added prescaler for 16 bit pwm in LPC1347 target

Fork of mbed-dev by mbed official

Committer:
<>
Date:
Fri Sep 02 15:07:44 2016 +0100
Revision:
144:ef7eb2e8f9f7
Parent:
0:9b334a45a8ff
This updates the lib to the mbed lib v125

Who changed what in which revision?

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