Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 6:40e873bbc5f7 1 /* mbed Microcontroller Library
MACRUM 6:40e873bbc5f7 2 * Copyright (c) 2006-2013 ARM Limited
MACRUM 6:40e873bbc5f7 3 *
MACRUM 6:40e873bbc5f7 4 * Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 6:40e873bbc5f7 5 * you may not use this file except in compliance with the License.
MACRUM 6:40e873bbc5f7 6 * You may obtain a copy of the License at
MACRUM 6:40e873bbc5f7 7 *
MACRUM 6:40e873bbc5f7 8 * http://www.apache.org/licenses/LICENSE-2.0
MACRUM 6:40e873bbc5f7 9 *
MACRUM 6:40e873bbc5f7 10 * Unless required by applicable law or agreed to in writing, software
MACRUM 6:40e873bbc5f7 11 * distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 6:40e873bbc5f7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 6:40e873bbc5f7 13 * See the License for the specific language governing permissions and
MACRUM 6:40e873bbc5f7 14 * limitations under the License.
MACRUM 6:40e873bbc5f7 15 */
MACRUM 6:40e873bbc5f7 16 #ifndef MBED_I2C_SLAVE_H
MACRUM 6:40e873bbc5f7 17 #define MBED_I2C_SLAVE_H
MACRUM 6:40e873bbc5f7 18
MACRUM 6:40e873bbc5f7 19 #include "platform/platform.h"
MACRUM 6:40e873bbc5f7 20
MACRUM 6:40e873bbc5f7 21 #if DEVICE_I2CSLAVE
MACRUM 6:40e873bbc5f7 22
MACRUM 6:40e873bbc5f7 23 #include "hal/i2c_api.h"
MACRUM 6:40e873bbc5f7 24
MACRUM 6:40e873bbc5f7 25 namespace mbed {
MACRUM 6:40e873bbc5f7 26 /** \addtogroup drivers */
MACRUM 6:40e873bbc5f7 27 /** @{*/
MACRUM 6:40e873bbc5f7 28
MACRUM 6:40e873bbc5f7 29 /** An I2C Slave, used for communicating with an I2C Master device
MACRUM 6:40e873bbc5f7 30 *
MACRUM 6:40e873bbc5f7 31 * @Note Synchronization level: Not protected
MACRUM 6:40e873bbc5f7 32 *
MACRUM 6:40e873bbc5f7 33 * Example:
MACRUM 6:40e873bbc5f7 34 * @code
MACRUM 6:40e873bbc5f7 35 * // Simple I2C responder
MACRUM 6:40e873bbc5f7 36 * #include <mbed.h>
MACRUM 6:40e873bbc5f7 37 *
MACRUM 6:40e873bbc5f7 38 * I2CSlave slave(p9, p10);
MACRUM 6:40e873bbc5f7 39 *
MACRUM 6:40e873bbc5f7 40 * int main() {
MACRUM 6:40e873bbc5f7 41 * char buf[10];
MACRUM 6:40e873bbc5f7 42 * char msg[] = "Slave!";
MACRUM 6:40e873bbc5f7 43 *
MACRUM 6:40e873bbc5f7 44 * slave.address(0xA0);
MACRUM 6:40e873bbc5f7 45 * while (1) {
MACRUM 6:40e873bbc5f7 46 * int i = slave.receive();
MACRUM 6:40e873bbc5f7 47 * switch (i) {
MACRUM 6:40e873bbc5f7 48 * case I2CSlave::ReadAddressed:
MACRUM 6:40e873bbc5f7 49 * slave.write(msg, strlen(msg) + 1); // Includes null char
MACRUM 6:40e873bbc5f7 50 * break;
MACRUM 6:40e873bbc5f7 51 * case I2CSlave::WriteGeneral:
MACRUM 6:40e873bbc5f7 52 * slave.read(buf, 10);
MACRUM 6:40e873bbc5f7 53 * printf("Read G: %s\n", buf);
MACRUM 6:40e873bbc5f7 54 * break;
MACRUM 6:40e873bbc5f7 55 * case I2CSlave::WriteAddressed:
MACRUM 6:40e873bbc5f7 56 * slave.read(buf, 10);
MACRUM 6:40e873bbc5f7 57 * printf("Read A: %s\n", buf);
MACRUM 6:40e873bbc5f7 58 * break;
MACRUM 6:40e873bbc5f7 59 * }
MACRUM 6:40e873bbc5f7 60 * for(int i = 0; i < 10; i++) buf[i] = 0; // Clear buffer
MACRUM 6:40e873bbc5f7 61 * }
MACRUM 6:40e873bbc5f7 62 * }
MACRUM 6:40e873bbc5f7 63 * @endcode
MACRUM 6:40e873bbc5f7 64 */
MACRUM 6:40e873bbc5f7 65 class I2CSlave {
MACRUM 6:40e873bbc5f7 66
MACRUM 6:40e873bbc5f7 67 public:
MACRUM 6:40e873bbc5f7 68 enum RxStatus {
MACRUM 6:40e873bbc5f7 69 NoData = 0,
MACRUM 6:40e873bbc5f7 70 ReadAddressed = 1,
MACRUM 6:40e873bbc5f7 71 WriteGeneral = 2,
MACRUM 6:40e873bbc5f7 72 WriteAddressed = 3
MACRUM 6:40e873bbc5f7 73 };
MACRUM 6:40e873bbc5f7 74
MACRUM 6:40e873bbc5f7 75 /** Create an I2C Slave interface, connected to the specified pins.
MACRUM 6:40e873bbc5f7 76 *
MACRUM 6:40e873bbc5f7 77 * @param sda I2C data line pin
MACRUM 6:40e873bbc5f7 78 * @param scl I2C clock line pin
MACRUM 6:40e873bbc5f7 79 */
MACRUM 6:40e873bbc5f7 80 I2CSlave(PinName sda, PinName scl);
MACRUM 6:40e873bbc5f7 81
MACRUM 6:40e873bbc5f7 82 /** Set the frequency of the I2C interface
MACRUM 6:40e873bbc5f7 83 *
MACRUM 6:40e873bbc5f7 84 * @param hz The bus frequency in hertz
MACRUM 6:40e873bbc5f7 85 */
MACRUM 6:40e873bbc5f7 86 void frequency(int hz);
MACRUM 6:40e873bbc5f7 87
MACRUM 6:40e873bbc5f7 88 /** Checks to see if this I2C Slave has been addressed.
MACRUM 6:40e873bbc5f7 89 *
MACRUM 6:40e873bbc5f7 90 * @returns
MACRUM 6:40e873bbc5f7 91 * A status indicating if the device has been addressed, and how
MACRUM 6:40e873bbc5f7 92 * - NoData - the slave has not been addressed
MACRUM 6:40e873bbc5f7 93 * - ReadAddressed - the master has requested a read from this slave
MACRUM 6:40e873bbc5f7 94 * - WriteAddressed - the master is writing to this slave
MACRUM 6:40e873bbc5f7 95 * - WriteGeneral - the master is writing to all slave
MACRUM 6:40e873bbc5f7 96 */
MACRUM 6:40e873bbc5f7 97 int receive(void);
MACRUM 6:40e873bbc5f7 98
MACRUM 6:40e873bbc5f7 99 /** Read from an I2C master.
MACRUM 6:40e873bbc5f7 100 *
MACRUM 6:40e873bbc5f7 101 * @param data pointer to the byte array to read data in to
MACRUM 6:40e873bbc5f7 102 * @param length maximum number of bytes to read
MACRUM 6:40e873bbc5f7 103 *
MACRUM 6:40e873bbc5f7 104 * @returns
MACRUM 6:40e873bbc5f7 105 * 0 on success,
MACRUM 6:40e873bbc5f7 106 * non-0 otherwise
MACRUM 6:40e873bbc5f7 107 */
MACRUM 6:40e873bbc5f7 108 int read(char *data, int length);
MACRUM 6:40e873bbc5f7 109
MACRUM 6:40e873bbc5f7 110 /** Read a single byte from an I2C master.
MACRUM 6:40e873bbc5f7 111 *
MACRUM 6:40e873bbc5f7 112 * @returns
MACRUM 6:40e873bbc5f7 113 * the byte read
MACRUM 6:40e873bbc5f7 114 */
MACRUM 6:40e873bbc5f7 115 int read(void);
MACRUM 6:40e873bbc5f7 116
MACRUM 6:40e873bbc5f7 117 /** Write to an I2C master.
MACRUM 6:40e873bbc5f7 118 *
MACRUM 6:40e873bbc5f7 119 * @param data pointer to the byte array to be transmitted
MACRUM 6:40e873bbc5f7 120 * @param length the number of bytes to transmite
MACRUM 6:40e873bbc5f7 121 *
MACRUM 6:40e873bbc5f7 122 * @returns
MACRUM 6:40e873bbc5f7 123 * 0 on success,
MACRUM 6:40e873bbc5f7 124 * non-0 otherwise
MACRUM 6:40e873bbc5f7 125 */
MACRUM 6:40e873bbc5f7 126 int write(const char *data, int length);
MACRUM 6:40e873bbc5f7 127
MACRUM 6:40e873bbc5f7 128 /** Write a single byte to an I2C master.
MACRUM 6:40e873bbc5f7 129 *
MACRUM 6:40e873bbc5f7 130 * @data the byte to write
MACRUM 6:40e873bbc5f7 131 *
MACRUM 6:40e873bbc5f7 132 * @returns
MACRUM 6:40e873bbc5f7 133 * '1' if an ACK was received,
MACRUM 6:40e873bbc5f7 134 * '0' otherwise
MACRUM 6:40e873bbc5f7 135 */
MACRUM 6:40e873bbc5f7 136 int write(int data);
MACRUM 6:40e873bbc5f7 137
MACRUM 6:40e873bbc5f7 138 /** Sets the I2C slave address.
MACRUM 6:40e873bbc5f7 139 *
MACRUM 6:40e873bbc5f7 140 * @param address The address to set for the slave (ignoring the least
MACRUM 6:40e873bbc5f7 141 * signifcant bit). If set to 0, the slave will only respond to the
MACRUM 6:40e873bbc5f7 142 * general call address.
MACRUM 6:40e873bbc5f7 143 */
MACRUM 6:40e873bbc5f7 144 void address(int address);
MACRUM 6:40e873bbc5f7 145
MACRUM 6:40e873bbc5f7 146 /** Reset the I2C slave back into the known ready receiving state.
MACRUM 6:40e873bbc5f7 147 */
MACRUM 6:40e873bbc5f7 148 void stop(void);
MACRUM 6:40e873bbc5f7 149
MACRUM 6:40e873bbc5f7 150 protected:
MACRUM 6:40e873bbc5f7 151 i2c_t _i2c;
MACRUM 6:40e873bbc5f7 152 };
MACRUM 6:40e873bbc5f7 153
MACRUM 6:40e873bbc5f7 154 } // namespace mbed
MACRUM 6:40e873bbc5f7 155
MACRUM 6:40e873bbc5f7 156 #endif
MACRUM 6:40e873bbc5f7 157
MACRUM 6:40e873bbc5f7 158 #endif
MACRUM 6:40e873bbc5f7 159
MACRUM 6:40e873bbc5f7 160 /** @}*/