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