change some io settings for TWR-K22F-120M

Dependents:   twr_helloworld

Committer:
Jasper_lee
Date:
Tue Dec 23 03:35:08 2014 +0000
Revision:
0:b16d94660a33
change some io setting used in TWR-K22F120M

Who changed what in which revision?

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