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