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_SPI_H
yihui 5:b8c02645e6af 17 #define MBED_SPI_H
yihui 5:b8c02645e6af 18
yihui 5:b8c02645e6af 19 #include "platform.h"
yihui 5:b8c02645e6af 20
yihui 5:b8c02645e6af 21 #if DEVICE_SPI
yihui 5:b8c02645e6af 22
yihui 5:b8c02645e6af 23 #include "spi_api.h"
yihui 5:b8c02645e6af 24
yihui 5:b8c02645e6af 25 #if DEVICE_SPI_ASYNCH
yihui 5:b8c02645e6af 26 #include "CThunk.h"
yihui 5:b8c02645e6af 27 #include "dma_api.h"
yihui 5:b8c02645e6af 28 #include "CircularBuffer.h"
yihui 5:b8c02645e6af 29 #include "FunctionPointer.h"
yihui 5:b8c02645e6af 30 #include "Transaction.h"
yihui 5:b8c02645e6af 31 #endif
yihui 5:b8c02645e6af 32
yihui 5:b8c02645e6af 33 namespace mbed {
yihui 5:b8c02645e6af 34
yihui 5:b8c02645e6af 35 /** A SPI Master, used for communicating with SPI slave devices
yihui 5:b8c02645e6af 36 *
yihui 5:b8c02645e6af 37 * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
yihui 5:b8c02645e6af 38 *
yihui 5:b8c02645e6af 39 * Most SPI devices will also require Chip Select and Reset signals. These
yihui 5:b8c02645e6af 40 * can be controlled using <DigitalOut> pins
yihui 5:b8c02645e6af 41 *
yihui 5:b8c02645e6af 42 * Example:
yihui 5:b8c02645e6af 43 * @code
yihui 5:b8c02645e6af 44 * // Send a byte to a SPI slave, and record the response
yihui 5:b8c02645e6af 45 *
yihui 5:b8c02645e6af 46 * #include "mbed.h"
yihui 5:b8c02645e6af 47 *
yihui 5:b8c02645e6af 48 * // hardware ssel (where applicable)
yihui 5:b8c02645e6af 49 * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
yihui 5:b8c02645e6af 50 *
yihui 5:b8c02645e6af 51 * // software ssel
yihui 5:b8c02645e6af 52 * SPI device(p5, p6, p7); // mosi, miso, sclk
yihui 5:b8c02645e6af 53 * DigitalOut cs(p8); // ssel
yihui 5:b8c02645e6af 54 *
yihui 5:b8c02645e6af 55 * int main() {
yihui 5:b8c02645e6af 56 * // hardware ssel (where applicable)
yihui 5:b8c02645e6af 57 * //int response = device.write(0xFF);
yihui 5:b8c02645e6af 58 *
yihui 5:b8c02645e6af 59 * // software ssel
yihui 5:b8c02645e6af 60 * cs = 0;
yihui 5:b8c02645e6af 61 * int response = device.write(0xFF);
yihui 5:b8c02645e6af 62 * cs = 1;
yihui 5:b8c02645e6af 63 * }
yihui 5:b8c02645e6af 64 * @endcode
yihui 5:b8c02645e6af 65 */
yihui 5:b8c02645e6af 66 class SPI {
yihui 5:b8c02645e6af 67
yihui 5:b8c02645e6af 68 public:
yihui 5:b8c02645e6af 69
yihui 5:b8c02645e6af 70 /** Create a SPI master connected to the specified pins
yihui 5:b8c02645e6af 71 *
yihui 5:b8c02645e6af 72 * mosi or miso can be specfied as NC if not used
yihui 5:b8c02645e6af 73 *
yihui 5:b8c02645e6af 74 * @param mosi SPI Master Out, Slave In pin
yihui 5:b8c02645e6af 75 * @param miso SPI Master In, Slave Out pin
yihui 5:b8c02645e6af 76 * @param sclk SPI Clock pin
yihui 5:b8c02645e6af 77 * @param ssel SPI chip select pin
yihui 5:b8c02645e6af 78 */
yihui 5:b8c02645e6af 79 SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel=NC);
yihui 5:b8c02645e6af 80
yihui 5:b8c02645e6af 81 /** Configure the data transmission format
yihui 5:b8c02645e6af 82 *
yihui 5:b8c02645e6af 83 * @param bits Number of bits per SPI frame (4 - 16)
yihui 5:b8c02645e6af 84 * @param mode Clock polarity and phase mode (0 - 3)
yihui 5:b8c02645e6af 85 *
yihui 5:b8c02645e6af 86 * @code
yihui 5:b8c02645e6af 87 * mode | POL PHA
yihui 5:b8c02645e6af 88 * -----+--------
yihui 5:b8c02645e6af 89 * 0 | 0 0
yihui 5:b8c02645e6af 90 * 1 | 0 1
yihui 5:b8c02645e6af 91 * 2 | 1 0
yihui 5:b8c02645e6af 92 * 3 | 1 1
yihui 5:b8c02645e6af 93 * @endcode
yihui 5:b8c02645e6af 94 */
yihui 5:b8c02645e6af 95 void format(int bits, int mode = 0);
yihui 5:b8c02645e6af 96
yihui 5:b8c02645e6af 97 /** Set the spi bus clock frequency
yihui 5:b8c02645e6af 98 *
yihui 5:b8c02645e6af 99 * @param hz SCLK frequency in hz (default = 1MHz)
yihui 5:b8c02645e6af 100 */
yihui 5:b8c02645e6af 101 void frequency(int hz = 1000000);
yihui 5:b8c02645e6af 102
yihui 5:b8c02645e6af 103 /** Write to the SPI Slave and return the response
yihui 5:b8c02645e6af 104 *
yihui 5:b8c02645e6af 105 * @param value Data to be sent to the SPI slave
yihui 5:b8c02645e6af 106 *
yihui 5:b8c02645e6af 107 * @returns
yihui 5:b8c02645e6af 108 * Response from the SPI slave
yihui 5:b8c02645e6af 109 */
yihui 5:b8c02645e6af 110 virtual int write(int value);
yihui 5:b8c02645e6af 111
yihui 5:b8c02645e6af 112 #if DEVICE_SPI_ASYNCH
yihui 5:b8c02645e6af 113
yihui 5:b8c02645e6af 114 /** Start non-blocking SPI transfer using 8bit buffers.
yihui 5:b8c02645e6af 115 *
yihui 5:b8c02645e6af 116 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
yihui 5:b8c02645e6af 117 * the default SPI value is sent
yihui 5:b8c02645e6af 118 * @param tx_length The length of TX buffer in bytes
yihui 5:b8c02645e6af 119 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
yihui 5:b8c02645e6af 120 * received data are ignored
yihui 5:b8c02645e6af 121 * @param rx_length The length of RX buffer in bytes
yihui 5:b8c02645e6af 122 * @param callback The event callback function
yihui 5:b8c02645e6af 123 * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
yihui 5:b8c02645e6af 124 * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
yihui 5:b8c02645e6af 125 */
yihui 5:b8c02645e6af 126 template<typename Type>
yihui 5:b8c02645e6af 127 int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t& callback, int event = SPI_EVENT_COMPLETE) {
yihui 5:b8c02645e6af 128 if (spi_active(&_spi)) {
yihui 5:b8c02645e6af 129 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
yihui 5:b8c02645e6af 130 }
yihui 5:b8c02645e6af 131 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type)*8, callback, event);
yihui 5:b8c02645e6af 132 return 0;
yihui 5:b8c02645e6af 133 }
yihui 5:b8c02645e6af 134
yihui 5:b8c02645e6af 135 /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
yihui 5:b8c02645e6af 136 */
yihui 5:b8c02645e6af 137 void abort_transfer();
yihui 5:b8c02645e6af 138
yihui 5:b8c02645e6af 139 /** Clear the transaction buffer
yihui 5:b8c02645e6af 140 */
yihui 5:b8c02645e6af 141 void clear_transfer_buffer();
yihui 5:b8c02645e6af 142
yihui 5:b8c02645e6af 143 /** Clear the transaction buffer and abort on-going transfer.
yihui 5:b8c02645e6af 144 */
yihui 5:b8c02645e6af 145 void abort_all_transfers();
yihui 5:b8c02645e6af 146
yihui 5:b8c02645e6af 147 /** Configure DMA usage suggestion for non-blocking transfers
yihui 5:b8c02645e6af 148 *
yihui 5:b8c02645e6af 149 * @param usage The usage DMA hint for peripheral
yihui 5:b8c02645e6af 150 * @return Zero if the usage was set, -1 if a transaction is on-going
yihui 5:b8c02645e6af 151 */
yihui 5:b8c02645e6af 152 int set_dma_usage(DMAUsage usage);
yihui 5:b8c02645e6af 153
yihui 5:b8c02645e6af 154 protected:
yihui 5:b8c02645e6af 155 /** SPI IRQ handler
yihui 5:b8c02645e6af 156 *
yihui 5:b8c02645e6af 157 */
yihui 5:b8c02645e6af 158 void irq_handler_asynch(void);
yihui 5:b8c02645e6af 159
yihui 5:b8c02645e6af 160 /** Common transfer method
yihui 5:b8c02645e6af 161 *
yihui 5:b8c02645e6af 162 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
yihui 5:b8c02645e6af 163 * the default SPI value is sent
yihui 5:b8c02645e6af 164 * @param tx_length The length of TX buffer in bytes
yihui 5:b8c02645e6af 165 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
yihui 5:b8c02645e6af 166 * received data are ignored
yihui 5:b8c02645e6af 167 * @param rx_length The length of RX buffer in bytes
yihui 5:b8c02645e6af 168 * @param bit_width The buffers element width
yihui 5:b8c02645e6af 169 * @param callback The event callback function
yihui 5:b8c02645e6af 170 * @param event The logical OR of events to modify
yihui 5:b8c02645e6af 171 * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
yihui 5:b8c02645e6af 172 */
yihui 5:b8c02645e6af 173 int transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
yihui 5:b8c02645e6af 174
yihui 5:b8c02645e6af 175 /**
yihui 5:b8c02645e6af 176 *
yihui 5:b8c02645e6af 177 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
yihui 5:b8c02645e6af 178 * the default SPI value is sent
yihui 5:b8c02645e6af 179 * @param tx_length The length of TX buffer in bytes
yihui 5:b8c02645e6af 180 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
yihui 5:b8c02645e6af 181 * received data are ignored
yihui 5:b8c02645e6af 182 * @param rx_length The length of RX buffer in bytes
yihui 5:b8c02645e6af 183 * @param bit_width The buffers element width
yihui 5:b8c02645e6af 184 * @param callback The event callback function
yihui 5:b8c02645e6af 185 * @param event The logical OR of events to modify
yihui 5:b8c02645e6af 186 * @return Zero if a transfer was added to the queue, or -1 if the queue is full
yihui 5:b8c02645e6af 187 */
yihui 5:b8c02645e6af 188 int queue_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
yihui 5:b8c02645e6af 189
yihui 5:b8c02645e6af 190 /** Configures a callback, spi peripheral and initiate a new transfer
yihui 5:b8c02645e6af 191 *
yihui 5:b8c02645e6af 192 * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
yihui 5:b8c02645e6af 193 * the default SPI value is sent
yihui 5:b8c02645e6af 194 * @param tx_length The length of TX buffer in bytes
yihui 5:b8c02645e6af 195 * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
yihui 5:b8c02645e6af 196 * received data are ignored
yihui 5:b8c02645e6af 197 * @param rx_length The length of RX buffer in bytes
yihui 5:b8c02645e6af 198 * @param bit_width The buffers element width
yihui 5:b8c02645e6af 199 * @param callback The event callback function
yihui 5:b8c02645e6af 200 * @param event The logical OR of events to modify
yihui 5:b8c02645e6af 201 */
yihui 5:b8c02645e6af 202 void start_transfer(const void *tx_buffer, int tx_length, void *rx_buffer, int rx_length, unsigned char bit_width, const event_callback_t& callback, int event);
yihui 5:b8c02645e6af 203
yihui 5:b8c02645e6af 204 #if TRANSACTION_QUEUE_SIZE_SPI
yihui 5:b8c02645e6af 205
yihui 5:b8c02645e6af 206 /** Start a new transaction
yihui 5:b8c02645e6af 207 *
yihui 5:b8c02645e6af 208 * @param data Transaction data
yihui 5:b8c02645e6af 209 */
yihui 5:b8c02645e6af 210 void start_transaction(transaction_t *data);
yihui 5:b8c02645e6af 211
yihui 5:b8c02645e6af 212 /** Dequeue a transaction
yihui 5:b8c02645e6af 213 *
yihui 5:b8c02645e6af 214 */
yihui 5:b8c02645e6af 215 void dequeue_transaction();
yihui 5:b8c02645e6af 216 static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
yihui 5:b8c02645e6af 217 #endif
yihui 5:b8c02645e6af 218
yihui 5:b8c02645e6af 219 #endif
yihui 5:b8c02645e6af 220
yihui 5:b8c02645e6af 221 public:
yihui 5:b8c02645e6af 222 virtual ~SPI() {
yihui 5:b8c02645e6af 223 }
yihui 5:b8c02645e6af 224
yihui 5:b8c02645e6af 225 protected:
yihui 5:b8c02645e6af 226 spi_t _spi;
yihui 5:b8c02645e6af 227
yihui 5:b8c02645e6af 228 #if DEVICE_SPI_ASYNCH
yihui 5:b8c02645e6af 229 CThunk<SPI> _irq;
yihui 5:b8c02645e6af 230 event_callback_t _callback;
yihui 5:b8c02645e6af 231 DMAUsage _usage;
yihui 5:b8c02645e6af 232 #endif
yihui 5:b8c02645e6af 233
yihui 5:b8c02645e6af 234 void aquire(void);
yihui 5:b8c02645e6af 235 static SPI *_owner;
yihui 5:b8c02645e6af 236 int _bits;
yihui 5:b8c02645e6af 237 int _mode;
yihui 5:b8c02645e6af 238 int _hz;
yihui 5:b8c02645e6af 239 };
yihui 5:b8c02645e6af 240
yihui 5:b8c02645e6af 241 } // namespace mbed
yihui 5:b8c02645e6af 242
yihui 5:b8c02645e6af 243 #endif
yihui 5:b8c02645e6af 244
yihui 5:b8c02645e6af 245 #endif