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