IOTIO

Dependencies:   Nucleo_BLE_API_IOTIO Nucleo_BLE_BlueNRG Nucleo_BLE_DemoApp Nucleo_Sensor_Shield mbed

Dependents:   Nucleo_BLE_Demo_IOTIO

Fork of Nucleo_BLE_Demo by Cortex Challenge Team

Committer:
16038618
Date:
Wed Mar 08 10:24:56 2017 +0000
Revision:
2:c69117fc79da
Parent:
1:4bdfa7d7e8bf
IOTIO

Who changed what in which revision?

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