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