PokittoLib with changes to lcd refresh etc.

Dependents:   Pokittris

Fork of Pokitto by Pokitto Community Team

This is a fork by user @Spinal, and is used in Pokittris for testing. Do not import this to your own program.

Committer:
spinal
Date:
Sun Oct 15 18:03:02 2017 +0000
Revision:
11:02ad9c807a21
Parent:
5:7e5c566b1760
fixed 4color refreshRegion code

Who changed what in which revision?

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