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