Use MPU9250 with nRF51822

Dependencies:   eMPL_MPU

Fork of Seeed_Tiny_BLE_Flash by Darren Huang

Committer:
yihui
Date:
Thu Dec 10 08:00:18 2015 +0000
Revision:
5:9b240c1d5251
get 9dof data from mpu9250

Who changed what in which revision?

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