test

Dependents:   robotic_fish_7

Committer:
juansal12
Date:
Tue Jan 14 19:14:29 2020 +0000
Revision:
0:44931ff4a3ed
Sofi 7 code;

Who changed what in which revision?

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