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