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