mbed.h library with any bug fixes AV finds.

Dependents:   micromouse4_encoder_testing PID_Test Lab1_Test WorkingPID ... more

Committer:
aravindsv
Date:
Mon Nov 02 02:26:59 2015 +0000
Revision:
0:ba7650f404af
Reduced HSE_STARTUP_TIMEOUT to 500 ms, fixed some compiler warnings

Who changed what in which revision?

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