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