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