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:
Pokitto
Date:
Sat Oct 07 21:31:12 2017 +0000
Revision:
5:7e5c566b1760
mbed-pokitto integrated

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