Final version.

Dependencies:   F746_GUI F746_SAI_IO UIT_FFT_Real

Fork of F746_Spectrogram by 不韋 呂

Committer:
MikamiUitOpen
Date:
Sun Oct 02 10:51:10 2016 +0000
Revision:
4:c1beacfc42c7
5

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MikamiUitOpen 4:c1beacfc42c7 1 /* mbed Microcontroller Library
MikamiUitOpen 4:c1beacfc42c7 2 * Copyright (c) 2006-2013 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 #ifndef MBED_I2C_SLAVE_H
MikamiUitOpen 4:c1beacfc42c7 17 #define MBED_I2C_SLAVE_H
MikamiUitOpen 4:c1beacfc42c7 18
MikamiUitOpen 4:c1beacfc42c7 19 #include "platform.h"
MikamiUitOpen 4:c1beacfc42c7 20
MikamiUitOpen 4:c1beacfc42c7 21 #if DEVICE_I2CSLAVE
MikamiUitOpen 4:c1beacfc42c7 22
MikamiUitOpen 4:c1beacfc42c7 23 #include "i2c_api.h"
MikamiUitOpen 4:c1beacfc42c7 24
MikamiUitOpen 4:c1beacfc42c7 25 namespace mbed {
MikamiUitOpen 4:c1beacfc42c7 26
MikamiUitOpen 4:c1beacfc42c7 27 /** An I2C Slave, used for communicating with an I2C Master device
MikamiUitOpen 4:c1beacfc42c7 28 *
MikamiUitOpen 4:c1beacfc42c7 29 * Example:
MikamiUitOpen 4:c1beacfc42c7 30 * @code
MikamiUitOpen 4:c1beacfc42c7 31 * // Simple I2C responder
MikamiUitOpen 4:c1beacfc42c7 32 * #include <mbed.h>
MikamiUitOpen 4:c1beacfc42c7 33 *
MikamiUitOpen 4:c1beacfc42c7 34 * I2CSlave slave(p9, p10);
MikamiUitOpen 4:c1beacfc42c7 35 *
MikamiUitOpen 4:c1beacfc42c7 36 * int main() {
MikamiUitOpen 4:c1beacfc42c7 37 * char buf[10];
MikamiUitOpen 4:c1beacfc42c7 38 * char msg[] = "Slave!";
MikamiUitOpen 4:c1beacfc42c7 39 *
MikamiUitOpen 4:c1beacfc42c7 40 * slave.address(0xA0);
MikamiUitOpen 4:c1beacfc42c7 41 * while (1) {
MikamiUitOpen 4:c1beacfc42c7 42 * int i = slave.receive();
MikamiUitOpen 4:c1beacfc42c7 43 * switch (i) {
MikamiUitOpen 4:c1beacfc42c7 44 * case I2CSlave::ReadAddressed:
MikamiUitOpen 4:c1beacfc42c7 45 * slave.write(msg, strlen(msg) + 1); // Includes null char
MikamiUitOpen 4:c1beacfc42c7 46 * break;
MikamiUitOpen 4:c1beacfc42c7 47 * case I2CSlave::WriteGeneral:
MikamiUitOpen 4:c1beacfc42c7 48 * slave.read(buf, 10);
MikamiUitOpen 4:c1beacfc42c7 49 * printf("Read G: %s\n", buf);
MikamiUitOpen 4:c1beacfc42c7 50 * break;
MikamiUitOpen 4:c1beacfc42c7 51 * case I2CSlave::WriteAddressed:
MikamiUitOpen 4:c1beacfc42c7 52 * slave.read(buf, 10);
MikamiUitOpen 4:c1beacfc42c7 53 * printf("Read A: %s\n", buf);
MikamiUitOpen 4:c1beacfc42c7 54 * break;
MikamiUitOpen 4:c1beacfc42c7 55 * }
MikamiUitOpen 4:c1beacfc42c7 56 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
MikamiUitOpen 4:c1beacfc42c7 57 * }
MikamiUitOpen 4:c1beacfc42c7 58 * }
MikamiUitOpen 4:c1beacfc42c7 59 * @endcode
MikamiUitOpen 4:c1beacfc42c7 60 */
MikamiUitOpen 4:c1beacfc42c7 61 class I2CSlave {
MikamiUitOpen 4:c1beacfc42c7 62
MikamiUitOpen 4:c1beacfc42c7 63 public:
MikamiUitOpen 4:c1beacfc42c7 64 enum RxStatus {
MikamiUitOpen 4:c1beacfc42c7 65 NoData = 0,
MikamiUitOpen 4:c1beacfc42c7 66 ReadAddressed = 1,
MikamiUitOpen 4:c1beacfc42c7 67 WriteGeneral = 2,
MikamiUitOpen 4:c1beacfc42c7 68 WriteAddressed = 3
MikamiUitOpen 4:c1beacfc42c7 69 };
MikamiUitOpen 4:c1beacfc42c7 70
MikamiUitOpen 4:c1beacfc42c7 71 /** Create an I2C Slave interface, connected to the specified pins.
MikamiUitOpen 4:c1beacfc42c7 72 *
MikamiUitOpen 4:c1beacfc42c7 73 * @param sda I2C data line pin
MikamiUitOpen 4:c1beacfc42c7 74 * @param scl I2C clock line pin
MikamiUitOpen 4:c1beacfc42c7 75 */
MikamiUitOpen 4:c1beacfc42c7 76 I2CSlave(PinName sda, PinName scl);
MikamiUitOpen 4:c1beacfc42c7 77
MikamiUitOpen 4:c1beacfc42c7 78 /** Set the frequency of the I2C interface
MikamiUitOpen 4:c1beacfc42c7 79 *
MikamiUitOpen 4:c1beacfc42c7 80 * @param hz The bus frequency in hertz
MikamiUitOpen 4:c1beacfc42c7 81 */
MikamiUitOpen 4:c1beacfc42c7 82 void frequency(int hz);
MikamiUitOpen 4:c1beacfc42c7 83
MikamiUitOpen 4:c1beacfc42c7 84 /** Checks to see if this I2C Slave has been addressed.
MikamiUitOpen 4:c1beacfc42c7 85 *
MikamiUitOpen 4:c1beacfc42c7 86 * @returns
MikamiUitOpen 4:c1beacfc42c7 87 * A status indicating if the device has been addressed, and how
MikamiUitOpen 4:c1beacfc42c7 88 * - NoData - the slave has not been addressed
MikamiUitOpen 4:c1beacfc42c7 89 * - ReadAddressed - the master has requested a read from this slave
MikamiUitOpen 4:c1beacfc42c7 90 * - WriteAddressed - the master is writing to this slave
MikamiUitOpen 4:c1beacfc42c7 91 * - WriteGeneral - the master is writing to all slave
MikamiUitOpen 4:c1beacfc42c7 92 */
MikamiUitOpen 4:c1beacfc42c7 93 int receive(void);
MikamiUitOpen 4:c1beacfc42c7 94
MikamiUitOpen 4:c1beacfc42c7 95 /** Read from an I2C master.
MikamiUitOpen 4:c1beacfc42c7 96 *
MikamiUitOpen 4:c1beacfc42c7 97 * @param data pointer to the byte array to read data in to
MikamiUitOpen 4:c1beacfc42c7 98 * @param length maximum number of bytes to read
MikamiUitOpen 4:c1beacfc42c7 99 *
MikamiUitOpen 4:c1beacfc42c7 100 * @returns
MikamiUitOpen 4:c1beacfc42c7 101 * 0 on success,
MikamiUitOpen 4:c1beacfc42c7 102 * non-0 otherwise
MikamiUitOpen 4:c1beacfc42c7 103 */
MikamiUitOpen 4:c1beacfc42c7 104 int read(char *data, int length);
MikamiUitOpen 4:c1beacfc42c7 105
MikamiUitOpen 4:c1beacfc42c7 106 /** Read a single byte from an I2C master.
MikamiUitOpen 4:c1beacfc42c7 107 *
MikamiUitOpen 4:c1beacfc42c7 108 * @returns
MikamiUitOpen 4:c1beacfc42c7 109 * the byte read
MikamiUitOpen 4:c1beacfc42c7 110 */
MikamiUitOpen 4:c1beacfc42c7 111 int read(void);
MikamiUitOpen 4:c1beacfc42c7 112
MikamiUitOpen 4:c1beacfc42c7 113 /** Write to an I2C master.
MikamiUitOpen 4:c1beacfc42c7 114 *
MikamiUitOpen 4:c1beacfc42c7 115 * @param data pointer to the byte array to be transmitted
MikamiUitOpen 4:c1beacfc42c7 116 * @param length the number of bytes to transmite
MikamiUitOpen 4:c1beacfc42c7 117 *
MikamiUitOpen 4:c1beacfc42c7 118 * @returns
MikamiUitOpen 4:c1beacfc42c7 119 * 0 on success,
MikamiUitOpen 4:c1beacfc42c7 120 * non-0 otherwise
MikamiUitOpen 4:c1beacfc42c7 121 */
MikamiUitOpen 4:c1beacfc42c7 122 int write(const char *data, int length);
MikamiUitOpen 4:c1beacfc42c7 123
MikamiUitOpen 4:c1beacfc42c7 124 /** Write a single byte to an I2C master.
MikamiUitOpen 4:c1beacfc42c7 125 *
MikamiUitOpen 4:c1beacfc42c7 126 * @data the byte to write
MikamiUitOpen 4:c1beacfc42c7 127 *
MikamiUitOpen 4:c1beacfc42c7 128 * @returns
MikamiUitOpen 4:c1beacfc42c7 129 * '1' if an ACK was received,
MikamiUitOpen 4:c1beacfc42c7 130 * '0' otherwise
MikamiUitOpen 4:c1beacfc42c7 131 */
MikamiUitOpen 4:c1beacfc42c7 132 int write(int data);
MikamiUitOpen 4:c1beacfc42c7 133
MikamiUitOpen 4:c1beacfc42c7 134 /** Sets the I2C slave address.
MikamiUitOpen 4:c1beacfc42c7 135 *
MikamiUitOpen 4:c1beacfc42c7 136 * @param address The address to set for the slave (ignoring the least
MikamiUitOpen 4:c1beacfc42c7 137 * signifcant bit). If set to 0, the slave will only respond to the
MikamiUitOpen 4:c1beacfc42c7 138 * general call address.
MikamiUitOpen 4:c1beacfc42c7 139 */
MikamiUitOpen 4:c1beacfc42c7 140 void address(int address);
MikamiUitOpen 4:c1beacfc42c7 141
MikamiUitOpen 4:c1beacfc42c7 142 /** Reset the I2C slave back into the known ready receiving state.
MikamiUitOpen 4:c1beacfc42c7 143 */
MikamiUitOpen 4:c1beacfc42c7 144 void stop(void);
MikamiUitOpen 4:c1beacfc42c7 145
MikamiUitOpen 4:c1beacfc42c7 146 protected:
MikamiUitOpen 4:c1beacfc42c7 147 i2c_t _i2c;
MikamiUitOpen 4:c1beacfc42c7 148 };
MikamiUitOpen 4:c1beacfc42c7 149
MikamiUitOpen 4:c1beacfc42c7 150 } // namespace mbed
MikamiUitOpen 4:c1beacfc42c7 151
MikamiUitOpen 4:c1beacfc42c7 152 #endif
MikamiUitOpen 4:c1beacfc42c7 153
MikamiUitOpen 4:c1beacfc42c7 154 #endif