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