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-2013 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 #include "SPI.h"
Pokitto 5:ea7377f3d1af 17
Pokitto 5:ea7377f3d1af 18 #if DEVICE_SPI
Pokitto 5:ea7377f3d1af 19
Pokitto 5:ea7377f3d1af 20 namespace mbed {
Pokitto 5:ea7377f3d1af 21
Pokitto 5:ea7377f3d1af 22 #if DEVICE_SPI_ASYNCH && TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 23 CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_buffer;
Pokitto 5:ea7377f3d1af 24 #endif
Pokitto 5:ea7377f3d1af 25
Pokitto 5:ea7377f3d1af 26 SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
Pokitto 5:ea7377f3d1af 27 _spi(),
Pokitto 5:ea7377f3d1af 28 #if DEVICE_SPI_ASYNCH
Pokitto 5:ea7377f3d1af 29 _irq(this),
Pokitto 5:ea7377f3d1af 30 _usage(DMA_USAGE_NEVER),
Pokitto 5:ea7377f3d1af 31 #endif
Pokitto 5:ea7377f3d1af 32 _bits(8),
Pokitto 5:ea7377f3d1af 33 _mode(0),
Pokitto 5:ea7377f3d1af 34 _hz(1000000) {
Pokitto 5:ea7377f3d1af 35 spi_init(&_spi, mosi, miso, sclk, ssel);
Pokitto 5:ea7377f3d1af 36 spi_format(&_spi, _bits, _mode, 0);
Pokitto 5:ea7377f3d1af 37 spi_frequency(&_spi, _hz);
Pokitto 5:ea7377f3d1af 38 }
Pokitto 5:ea7377f3d1af 39
Pokitto 5:ea7377f3d1af 40 void SPI::format(int bits, int mode) {
Pokitto 5:ea7377f3d1af 41 _bits = bits;
Pokitto 5:ea7377f3d1af 42 _mode = mode;
Pokitto 5:ea7377f3d1af 43 SPI::_owner = NULL; // Not that elegant, but works. rmeyer
Pokitto 5:ea7377f3d1af 44 aquire();
Pokitto 5:ea7377f3d1af 45 }
Pokitto 5:ea7377f3d1af 46
Pokitto 5:ea7377f3d1af 47 void SPI::frequency(int hz) {
Pokitto 5:ea7377f3d1af 48 _hz = hz;
Pokitto 5:ea7377f3d1af 49 SPI::_owner = NULL; // Not that elegant, but works. rmeyer
Pokitto 5:ea7377f3d1af 50 aquire();
Pokitto 5:ea7377f3d1af 51 }
Pokitto 5:ea7377f3d1af 52
Pokitto 5:ea7377f3d1af 53 SPI* SPI::_owner = NULL;
Pokitto 5:ea7377f3d1af 54
Pokitto 5:ea7377f3d1af 55 // ignore the fact there are multiple physical spis, and always update if it wasnt us last
Pokitto 5:ea7377f3d1af 56 void SPI::aquire() {
Pokitto 5:ea7377f3d1af 57 if (_owner != this) {
Pokitto 5:ea7377f3d1af 58 spi_format(&_spi, _bits, _mode, 0);
Pokitto 5:ea7377f3d1af 59 spi_frequency(&_spi, _hz);
Pokitto 5:ea7377f3d1af 60 _owner = this;
Pokitto 5:ea7377f3d1af 61 }
Pokitto 5:ea7377f3d1af 62 }
Pokitto 5:ea7377f3d1af 63
Pokitto 5:ea7377f3d1af 64 int SPI::write(int value) {
Pokitto 5:ea7377f3d1af 65 aquire();
Pokitto 5:ea7377f3d1af 66 return spi_master_write(&_spi, value);
Pokitto 5:ea7377f3d1af 67 }
Pokitto 5:ea7377f3d1af 68
Pokitto 5:ea7377f3d1af 69 #if DEVICE_SPI_ASYNCH
Pokitto 5:ea7377f3d1af 70
Pokitto 5:ea7377f3d1af 71 int SPI::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 72 {
Pokitto 5:ea7377f3d1af 73 if (spi_active(&_spi)) {
Pokitto 5:ea7377f3d1af 74 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event);
Pokitto 5:ea7377f3d1af 75 }
Pokitto 5:ea7377f3d1af 76 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event);
Pokitto 5:ea7377f3d1af 77 return 0;
Pokitto 5:ea7377f3d1af 78 }
Pokitto 5:ea7377f3d1af 79
Pokitto 5:ea7377f3d1af 80 void SPI::abort_transfer()
Pokitto 5:ea7377f3d1af 81 {
Pokitto 5:ea7377f3d1af 82 spi_abort_asynch(&_spi);
Pokitto 5:ea7377f3d1af 83 #if TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 84 dequeue_transaction();
Pokitto 5:ea7377f3d1af 85 #endif
Pokitto 5:ea7377f3d1af 86 }
Pokitto 5:ea7377f3d1af 87
Pokitto 5:ea7377f3d1af 88
Pokitto 5:ea7377f3d1af 89 void SPI::clear_transfer_buffer()
Pokitto 5:ea7377f3d1af 90 {
Pokitto 5:ea7377f3d1af 91 #if TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 92 _transaction_buffer.reset();
Pokitto 5:ea7377f3d1af 93 #endif
Pokitto 5:ea7377f3d1af 94 }
Pokitto 5:ea7377f3d1af 95
Pokitto 5:ea7377f3d1af 96 void SPI::abort_all_transfers()
Pokitto 5:ea7377f3d1af 97 {
Pokitto 5:ea7377f3d1af 98 clear_transfer_buffer();
Pokitto 5:ea7377f3d1af 99 abort_transfer();
Pokitto 5:ea7377f3d1af 100 }
Pokitto 5:ea7377f3d1af 101
Pokitto 5:ea7377f3d1af 102 int SPI::set_dma_usage(DMAUsage usage)
Pokitto 5:ea7377f3d1af 103 {
Pokitto 5:ea7377f3d1af 104 if (spi_active(&_spi)) {
Pokitto 5:ea7377f3d1af 105 return -1;
Pokitto 5:ea7377f3d1af 106 }
Pokitto 5:ea7377f3d1af 107 _usage = usage;
Pokitto 5:ea7377f3d1af 108 return 0;
Pokitto 5:ea7377f3d1af 109 }
Pokitto 5:ea7377f3d1af 110
Pokitto 5:ea7377f3d1af 111 int SPI::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 112 {
Pokitto 5:ea7377f3d1af 113 #if TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 114 transaction_t t;
Pokitto 5:ea7377f3d1af 115
Pokitto 5:ea7377f3d1af 116 t.tx_buffer = const_cast<void *>(tx_buffer);
Pokitto 5:ea7377f3d1af 117 t.tx_length = tx_length;
Pokitto 5:ea7377f3d1af 118 t.rx_buffer = rx_buffer;
Pokitto 5:ea7377f3d1af 119 t.rx_length = rx_length;
Pokitto 5:ea7377f3d1af 120 t.event = event;
Pokitto 5:ea7377f3d1af 121 t.callback = callback;
Pokitto 5:ea7377f3d1af 122 t.width = bit_width;
Pokitto 5:ea7377f3d1af 123 Transaction<SPI> transaction(this, t);
Pokitto 5:ea7377f3d1af 124 if (_transaction_buffer.full()) {
Pokitto 5:ea7377f3d1af 125 return -1; // the buffer is full
Pokitto 5:ea7377f3d1af 126 } else {
Pokitto 5:ea7377f3d1af 127 _transaction_buffer.push(transaction);
Pokitto 5:ea7377f3d1af 128 return 0;
Pokitto 5:ea7377f3d1af 129 }
Pokitto 5:ea7377f3d1af 130 #else
Pokitto 5:ea7377f3d1af 131 return -1;
Pokitto 5:ea7377f3d1af 132 #endif
Pokitto 5:ea7377f3d1af 133 }
Pokitto 5:ea7377f3d1af 134
Pokitto 5:ea7377f3d1af 135 void SPI::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 136 {
Pokitto 5:ea7377f3d1af 137 aquire();
Pokitto 5:ea7377f3d1af 138 _callback = callback;
Pokitto 5:ea7377f3d1af 139 _irq.callback(&SPI::irq_handler_asynch);
Pokitto 5:ea7377f3d1af 140 spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);
Pokitto 5:ea7377f3d1af 141 }
Pokitto 5:ea7377f3d1af 142
Pokitto 5:ea7377f3d1af 143 #if TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 144
Pokitto 5:ea7377f3d1af 145 void SPI::start_transaction(transaction_t *data)
Pokitto 5:ea7377f3d1af 146 {
Pokitto 5:ea7377f3d1af 147 start_transfer(data->tx_buffer, data->tx_length, data->rx_buffer, data->rx_length, data->width, data->callback, data->event);
Pokitto 5:ea7377f3d1af 148 }
Pokitto 5:ea7377f3d1af 149
Pokitto 5:ea7377f3d1af 150 void SPI::dequeue_transaction()
Pokitto 5:ea7377f3d1af 151 {
Pokitto 5:ea7377f3d1af 152 Transaction<SPI> t;
Pokitto 5:ea7377f3d1af 153 if (_transaction_buffer.pop(t)) {
Pokitto 5:ea7377f3d1af 154 SPI* obj = t.get_object();
Pokitto 5:ea7377f3d1af 155 transaction_t* data = t.get_transaction();
Pokitto 5:ea7377f3d1af 156 obj->start_transaction(data);
Pokitto 5:ea7377f3d1af 157 }
Pokitto 5:ea7377f3d1af 158 }
Pokitto 5:ea7377f3d1af 159
Pokitto 5:ea7377f3d1af 160 #endif
Pokitto 5:ea7377f3d1af 161
Pokitto 5:ea7377f3d1af 162 void SPI::irq_handler_asynch(void)
Pokitto 5:ea7377f3d1af 163 {
Pokitto 5:ea7377f3d1af 164 int event = spi_irq_handler_asynch(&_spi);
Pokitto 5:ea7377f3d1af 165 if (_callback && (event & SPI_EVENT_ALL)) {
Pokitto 5:ea7377f3d1af 166 _callback.call(event & SPI_EVENT_ALL);
Pokitto 5:ea7377f3d1af 167 }
Pokitto 5:ea7377f3d1af 168 #if TRANSACTION_QUEUE_SIZE_SPI
Pokitto 5:ea7377f3d1af 169 if (event & (SPI_EVENT_ALL | SPI_EVENT_INTERNAL_TRANSFER_COMPLETE)) {
Pokitto 5:ea7377f3d1af 170 // SPI peripheral is free (event happend), dequeue transaction
Pokitto 5:ea7377f3d1af 171 dequeue_transaction();
Pokitto 5:ea7377f3d1af 172 }
Pokitto 5:ea7377f3d1af 173 #endif
Pokitto 5:ea7377f3d1af 174 }
Pokitto 5:ea7377f3d1af 175
Pokitto 5:ea7377f3d1af 176 #endif
Pokitto 5:ea7377f3d1af 177
Pokitto 5:ea7377f3d1af 178 } // namespace mbed
Pokitto 5:ea7377f3d1af 179
Pokitto 5:ea7377f3d1af 180 #endif