fix nrf51822 i2c & spi conflict

Dependencies:   BLE_API eMPL_MPU6050 nRF51822

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Tue Nov 17 07:48:56 2015 +0000
Revision:
5:b8c02645e6af
fix i2c & spi conflict

Who changed what in which revision?

UserRevisionLine numberNew contents of line
yihui 5:b8c02645e6af 1 /* mbed Microcontroller Library
yihui 5:b8c02645e6af 2 * Copyright (c) 2006-2015 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_H
yihui 5:b8c02645e6af 17 #define MBED_I2C_H
yihui 5:b8c02645e6af 18
yihui 5:b8c02645e6af 19 #include "platform.h"
yihui 5:b8c02645e6af 20
yihui 5:b8c02645e6af 21 #if DEVICE_I2C
yihui 5:b8c02645e6af 22
yihui 5:b8c02645e6af 23 #include "i2c_api.h"
yihui 5:b8c02645e6af 24
yihui 5:b8c02645e6af 25 #if DEVICE_I2C_ASYNCH
yihui 5:b8c02645e6af 26 #include "CThunk.h"
yihui 5:b8c02645e6af 27 #include "dma_api.h"
yihui 5:b8c02645e6af 28 #include "FunctionPointer.h"
yihui 5:b8c02645e6af 29 #endif
yihui 5:b8c02645e6af 30
yihui 5:b8c02645e6af 31 namespace mbed {
yihui 5:b8c02645e6af 32
yihui 5:b8c02645e6af 33 /** An I2C Master, used for communicating with I2C slave devices
yihui 5:b8c02645e6af 34 *
yihui 5:b8c02645e6af 35 * Example:
yihui 5:b8c02645e6af 36 * @code
yihui 5:b8c02645e6af 37 * // Read from I2C slave at address 0x62
yihui 5:b8c02645e6af 38 *
yihui 5:b8c02645e6af 39 * #include "mbed.h"
yihui 5:b8c02645e6af 40 *
yihui 5:b8c02645e6af 41 * I2C i2c(p28, p27);
yihui 5:b8c02645e6af 42 *
yihui 5:b8c02645e6af 43 * int main() {
yihui 5:b8c02645e6af 44 * int address = 0x62;
yihui 5:b8c02645e6af 45 * char data[2];
yihui 5:b8c02645e6af 46 * i2c.read(address, data, 2);
yihui 5:b8c02645e6af 47 * }
yihui 5:b8c02645e6af 48 * @endcode
yihui 5:b8c02645e6af 49 */
yihui 5:b8c02645e6af 50 class I2C {
yihui 5:b8c02645e6af 51
yihui 5:b8c02645e6af 52 public:
yihui 5:b8c02645e6af 53 enum RxStatus {
yihui 5:b8c02645e6af 54 NoData,
yihui 5:b8c02645e6af 55 MasterGeneralCall,
yihui 5:b8c02645e6af 56 MasterWrite,
yihui 5:b8c02645e6af 57 MasterRead
yihui 5:b8c02645e6af 58 };
yihui 5:b8c02645e6af 59
yihui 5:b8c02645e6af 60 enum Acknowledge {
yihui 5:b8c02645e6af 61 NoACK = 0,
yihui 5:b8c02645e6af 62 ACK = 1
yihui 5:b8c02645e6af 63 };
yihui 5:b8c02645e6af 64
yihui 5:b8c02645e6af 65 /** Create an I2C Master interface, connected to the specified pins
yihui 5:b8c02645e6af 66 *
yihui 5:b8c02645e6af 67 * @param sda I2C data line pin
yihui 5:b8c02645e6af 68 * @param scl I2C clock line pin
yihui 5:b8c02645e6af 69 */
yihui 5:b8c02645e6af 70 I2C(PinName sda, PinName scl);
yihui 5:b8c02645e6af 71
yihui 5:b8c02645e6af 72 /** Set the frequency of the I2C interface
yihui 5:b8c02645e6af 73 *
yihui 5:b8c02645e6af 74 * @param hz The bus frequency in hertz
yihui 5:b8c02645e6af 75 */
yihui 5:b8c02645e6af 76 void frequency(int hz);
yihui 5:b8c02645e6af 77
yihui 5:b8c02645e6af 78 /** Read from an I2C slave
yihui 5:b8c02645e6af 79 *
yihui 5:b8c02645e6af 80 * Performs a complete read transaction. The bottom bit of
yihui 5:b8c02645e6af 81 * the address is forced to 1 to indicate a read.
yihui 5:b8c02645e6af 82 *
yihui 5:b8c02645e6af 83 * @param address 8-bit I2C slave address [ addr | 1 ]
yihui 5:b8c02645e6af 84 * @param data Pointer to the byte-array to read data in to
yihui 5:b8c02645e6af 85 * @param length Number of bytes to read
yihui 5:b8c02645e6af 86 * @param repeated Repeated start, true - don't send stop at end
yihui 5:b8c02645e6af 87 *
yihui 5:b8c02645e6af 88 * @returns
yihui 5:b8c02645e6af 89 * 0 on success (ack),
yihui 5:b8c02645e6af 90 * non-0 on failure (nack)
yihui 5:b8c02645e6af 91 */
yihui 5:b8c02645e6af 92 int read(int address, char *data, int length, bool repeated = false);
yihui 5:b8c02645e6af 93
yihui 5:b8c02645e6af 94 /** Read a single byte from the I2C bus
yihui 5:b8c02645e6af 95 *
yihui 5:b8c02645e6af 96 * @param ack indicates if the byte is to be acknowledged (1 = acknowledge)
yihui 5:b8c02645e6af 97 *
yihui 5:b8c02645e6af 98 * @returns
yihui 5:b8c02645e6af 99 * the byte read
yihui 5:b8c02645e6af 100 */
yihui 5:b8c02645e6af 101 int read(int ack);
yihui 5:b8c02645e6af 102
yihui 5:b8c02645e6af 103 /** Write to an I2C slave
yihui 5:b8c02645e6af 104 *
yihui 5:b8c02645e6af 105 * Performs a complete write transaction. The bottom bit of
yihui 5:b8c02645e6af 106 * the address is forced to 0 to indicate a write.
yihui 5:b8c02645e6af 107 *
yihui 5:b8c02645e6af 108 * @param address 8-bit I2C slave address [ addr | 0 ]
yihui 5:b8c02645e6af 109 * @param data Pointer to the byte-array data to send
yihui 5:b8c02645e6af 110 * @param length Number of bytes to send
yihui 5:b8c02645e6af 111 * @param repeated Repeated start, true - do not send stop at end
yihui 5:b8c02645e6af 112 *
yihui 5:b8c02645e6af 113 * @returns
yihui 5:b8c02645e6af 114 * 0 on success (ack),
yihui 5:b8c02645e6af 115 * non-0 on failure (nack)
yihui 5:b8c02645e6af 116 */
yihui 5:b8c02645e6af 117 int write(int address, const char *data, int length, bool repeated = false);
yihui 5:b8c02645e6af 118
yihui 5:b8c02645e6af 119 /** Write single byte out on the I2C bus
yihui 5:b8c02645e6af 120 *
yihui 5:b8c02645e6af 121 * @param data data to write out on bus
yihui 5:b8c02645e6af 122 *
yihui 5:b8c02645e6af 123 * @returns
yihui 5:b8c02645e6af 124 * '1' if an ACK was received,
yihui 5:b8c02645e6af 125 * '0' otherwise
yihui 5:b8c02645e6af 126 */
yihui 5:b8c02645e6af 127 int write(int data);
yihui 5:b8c02645e6af 128
yihui 5:b8c02645e6af 129 /** Creates a start condition on the I2C bus
yihui 5:b8c02645e6af 130 */
yihui 5:b8c02645e6af 131
yihui 5:b8c02645e6af 132 void start(void);
yihui 5:b8c02645e6af 133
yihui 5:b8c02645e6af 134 /** Creates a stop condition on the I2C bus
yihui 5:b8c02645e6af 135 */
yihui 5:b8c02645e6af 136 void stop(void);
yihui 5:b8c02645e6af 137
yihui 5:b8c02645e6af 138 #if DEVICE_I2C_ASYNCH
yihui 5:b8c02645e6af 139
yihui 5:b8c02645e6af 140 /** Start non-blocking I2C transfer.
yihui 5:b8c02645e6af 141 *
yihui 5:b8c02645e6af 142 * @param address 8/10 bit I2c slave address
yihui 5:b8c02645e6af 143 * @param tx_buffer The TX buffer with data to be transfered
yihui 5:b8c02645e6af 144 * @param tx_length The length of TX buffer in bytes
yihui 5:b8c02645e6af 145 * @param rx_buffer The RX buffer which is used for received data
yihui 5:b8c02645e6af 146 * @param rx_length The length of RX buffer in bytes
yihui 5:b8c02645e6af 147 * @param event The logical OR of events to modify
yihui 5:b8c02645e6af 148 * @param callback The event callback function
yihui 5:b8c02645e6af 149 * @param repeated Repeated start, true - do not send stop at end
yihui 5:b8c02645e6af 150 * @return Zero if the transfer has started, or -1 if I2C peripheral is busy
yihui 5:b8c02645e6af 151 */
yihui 5:b8c02645e6af 152 int transfer(int address, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, const event_callback_t& callback, int event = I2C_EVENT_TRANSFER_COMPLETE, bool repeated = false);
yihui 5:b8c02645e6af 153
yihui 5:b8c02645e6af 154 /** Abort the on-going I2C transfer
yihui 5:b8c02645e6af 155 */
yihui 5:b8c02645e6af 156 void abort_transfer();
yihui 5:b8c02645e6af 157 protected:
yihui 5:b8c02645e6af 158 void irq_handler_asynch(void);
yihui 5:b8c02645e6af 159 event_callback_t _callback;
yihui 5:b8c02645e6af 160 CThunk<I2C> _irq;
yihui 5:b8c02645e6af 161 DMAUsage _usage;
yihui 5:b8c02645e6af 162 #endif
yihui 5:b8c02645e6af 163
yihui 5:b8c02645e6af 164 protected:
yihui 5:b8c02645e6af 165 void aquire();
yihui 5:b8c02645e6af 166
yihui 5:b8c02645e6af 167 i2c_t _i2c;
yihui 5:b8c02645e6af 168 static I2C *_owner;
yihui 5:b8c02645e6af 169 int _hz;
yihui 5:b8c02645e6af 170 };
yihui 5:b8c02645e6af 171
yihui 5:b8c02645e6af 172 } // namespace mbed
yihui 5:b8c02645e6af 173
yihui 5:b8c02645e6af 174 #endif
yihui 5:b8c02645e6af 175
yihui 5:b8c02645e6af 176 #endif