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