Fork of the official mbed C/C SDK provides the software platform and libraries to build your applications for RenBED.

Dependents:   1-RenBuggyTimed RenBED_RGB RenBED_RGB_PWM RenBED_RGB

Fork of mbed by mbed official

Committer:
Kojto
Date:
Tue Jun 09 14:29:26 2015 +0100
Revision:
101:7cff1c4259d7
Parent:
98:8ab26030e058
Child:
102:da0ca467f8b5
Release 101 of the mbed library

Changes:
- new platform: APPNEARME_MICRONFCBOARD, MTS_DRAGONFLY_F411RE, MAX32600MBED, WIZwiki_W7500
- Silabs memory optimization in gpio, pwm fixes
- SPI - ssel documentation fixes and its use

Who changed what in which revision?

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