Describes predefine macros for mbed online compiler (armcc)

Committer:
MACRUM
Date:
Thu Mar 16 21:58:09 2017 +0900
Revision:
6:40e873bbc5f7
Add licence header info

Who changed what in which revision?

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