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