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