PokittoLib is the library needed for programming the Pokitto DIY game console (www.pokitto.com)

Dependents:   YATTT sd_map_test cPong SnowDemo ... more

PokittoLib

Library for programming Pokitto hardware

How to Use

  1. Import this library to online compiler (see button "import" on the right hand side
  2. DO NOT import mbed-src anymore, a better version is now included inside PokittoLib
  3. Change My_settings.h according to your project
  4. Start coding!
Committer:
Pokitto
Date:
Wed Dec 25 23:59:52 2019 +0000
Revision:
71:531419862202
Parent:
5:ea7377f3d1af
Changed Mode2 C++ refresh code (graphical errors)

Who changed what in which revision?

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