Opencv 3.1 project on GR-PEACH board

Fork of gr-peach-opencv-project by the do

Committer:
thedo
Date:
Tue Jul 04 06:23:13 2017 +0000
Revision:
170:54ff26da7eb6
Parent:
167:1657b442184c
project opencv 3.1 on GR PEACH board, no use SD card.

Who changed what in which revision?

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