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