Development mbed library for MAX32630FTHR

Dependents:   blinky_max32630fthr

Committer:
switches
Date:
Fri Dec 16 16:27:57 2016 +0000
Revision:
3:1198227e6421
Parent:
0:5c4d7b2438d3
Changed ADC scale for MAX32625 platforms to 1.2V full scale to match MAX32630 platforms

Who changed what in which revision?

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