Includes library modifications to allow access to AIN_4 (AIN_0 / 5)

Committer:
bryantaylor
Date:
Tue Sep 20 21:26:12 2016 +0000
Revision:
0:eafc3fd41f75
hackathon

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bryantaylor 0:eafc3fd41f75 1 /* mbed Microcontroller Library
bryantaylor 0:eafc3fd41f75 2 * Copyright (c) 2006-2013 ARM Limited
bryantaylor 0:eafc3fd41f75 3 *
bryantaylor 0:eafc3fd41f75 4 * Licensed under the Apache License, Version 2.0 (the "License");
bryantaylor 0:eafc3fd41f75 5 * you may not use this file except in compliance with the License.
bryantaylor 0:eafc3fd41f75 6 * You may obtain a copy of the License at
bryantaylor 0:eafc3fd41f75 7 *
bryantaylor 0:eafc3fd41f75 8 * http://www.apache.org/licenses/LICENSE-2.0
bryantaylor 0:eafc3fd41f75 9 *
bryantaylor 0:eafc3fd41f75 10 * Unless required by applicable law or agreed to in writing, software
bryantaylor 0:eafc3fd41f75 11 * distributed under the License is distributed on an "AS IS" BASIS,
bryantaylor 0:eafc3fd41f75 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
bryantaylor 0:eafc3fd41f75 13 * See the License for the specific language governing permissions and
bryantaylor 0:eafc3fd41f75 14 * limitations under the License.
bryantaylor 0:eafc3fd41f75 15 */
bryantaylor 0:eafc3fd41f75 16 #include "SPI.h"
bryantaylor 0:eafc3fd41f75 17 #include "critical.h"
bryantaylor 0:eafc3fd41f75 18
bryantaylor 0:eafc3fd41f75 19 #if DEVICE_SPI
bryantaylor 0:eafc3fd41f75 20
bryantaylor 0:eafc3fd41f75 21 namespace mbed {
bryantaylor 0:eafc3fd41f75 22
bryantaylor 0:eafc3fd41f75 23 #if DEVICE_SPI_ASYNCH && TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 24 CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_buffer;
bryantaylor 0:eafc3fd41f75 25 #endif
bryantaylor 0:eafc3fd41f75 26
bryantaylor 0:eafc3fd41f75 27 SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) :
bryantaylor 0:eafc3fd41f75 28 _spi(),
bryantaylor 0:eafc3fd41f75 29 #if DEVICE_SPI_ASYNCH
bryantaylor 0:eafc3fd41f75 30 _irq(this),
bryantaylor 0:eafc3fd41f75 31 _usage(DMA_USAGE_NEVER),
bryantaylor 0:eafc3fd41f75 32 #endif
bryantaylor 0:eafc3fd41f75 33 _bits(8),
bryantaylor 0:eafc3fd41f75 34 _mode(0),
bryantaylor 0:eafc3fd41f75 35 _hz(1000000) {
bryantaylor 0:eafc3fd41f75 36 // No lock needed in the constructor
bryantaylor 0:eafc3fd41f75 37
bryantaylor 0:eafc3fd41f75 38 spi_init(&_spi, mosi, miso, sclk, ssel);
bryantaylor 0:eafc3fd41f75 39 aquire();
bryantaylor 0:eafc3fd41f75 40 }
bryantaylor 0:eafc3fd41f75 41
bryantaylor 0:eafc3fd41f75 42 void SPI::format(int bits, int mode) {
bryantaylor 0:eafc3fd41f75 43 lock();
bryantaylor 0:eafc3fd41f75 44 _bits = bits;
bryantaylor 0:eafc3fd41f75 45 _mode = mode;
bryantaylor 0:eafc3fd41f75 46 SPI::_owner = NULL; // Not that elegant, but works. rmeyer
bryantaylor 0:eafc3fd41f75 47 aquire();
bryantaylor 0:eafc3fd41f75 48 unlock();
bryantaylor 0:eafc3fd41f75 49 }
bryantaylor 0:eafc3fd41f75 50
bryantaylor 0:eafc3fd41f75 51 void SPI::frequency(int hz) {
bryantaylor 0:eafc3fd41f75 52 lock();
bryantaylor 0:eafc3fd41f75 53 _hz = hz;
bryantaylor 0:eafc3fd41f75 54 SPI::_owner = NULL; // Not that elegant, but works. rmeyer
bryantaylor 0:eafc3fd41f75 55 aquire();
bryantaylor 0:eafc3fd41f75 56 unlock();
bryantaylor 0:eafc3fd41f75 57 }
bryantaylor 0:eafc3fd41f75 58
bryantaylor 0:eafc3fd41f75 59 SPI* SPI::_owner = NULL;
bryantaylor 0:eafc3fd41f75 60 SingletonPtr<PlatformMutex> SPI::_mutex;
bryantaylor 0:eafc3fd41f75 61
bryantaylor 0:eafc3fd41f75 62 // ignore the fact there are multiple physical spis, and always update if it wasnt us last
bryantaylor 0:eafc3fd41f75 63 void SPI::aquire() {
bryantaylor 0:eafc3fd41f75 64 lock();
bryantaylor 0:eafc3fd41f75 65 if (_owner != this) {
bryantaylor 0:eafc3fd41f75 66 spi_format(&_spi, _bits, _mode, 0);
bryantaylor 0:eafc3fd41f75 67 spi_frequency(&_spi, _hz);
bryantaylor 0:eafc3fd41f75 68 _owner = this;
bryantaylor 0:eafc3fd41f75 69 }
bryantaylor 0:eafc3fd41f75 70 unlock();
bryantaylor 0:eafc3fd41f75 71 }
bryantaylor 0:eafc3fd41f75 72
bryantaylor 0:eafc3fd41f75 73 int SPI::write(int value) {
bryantaylor 0:eafc3fd41f75 74 lock();
bryantaylor 0:eafc3fd41f75 75 aquire();
bryantaylor 0:eafc3fd41f75 76 int ret = spi_master_write(&_spi, value);
bryantaylor 0:eafc3fd41f75 77 unlock();
bryantaylor 0:eafc3fd41f75 78 return ret;
bryantaylor 0:eafc3fd41f75 79 }
bryantaylor 0:eafc3fd41f75 80
bryantaylor 0:eafc3fd41f75 81 void SPI::lock() {
bryantaylor 0:eafc3fd41f75 82 _mutex->lock();
bryantaylor 0:eafc3fd41f75 83 }
bryantaylor 0:eafc3fd41f75 84
bryantaylor 0:eafc3fd41f75 85 void SPI::unlock() {
bryantaylor 0:eafc3fd41f75 86 _mutex->unlock();
bryantaylor 0:eafc3fd41f75 87 }
bryantaylor 0:eafc3fd41f75 88
bryantaylor 0:eafc3fd41f75 89 #if DEVICE_SPI_ASYNCH
bryantaylor 0:eafc3fd41f75 90
bryantaylor 0:eafc3fd41f75 91 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)
bryantaylor 0:eafc3fd41f75 92 {
bryantaylor 0:eafc3fd41f75 93 if (spi_active(&_spi)) {
bryantaylor 0:eafc3fd41f75 94 return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event);
bryantaylor 0:eafc3fd41f75 95 }
bryantaylor 0:eafc3fd41f75 96 start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event);
bryantaylor 0:eafc3fd41f75 97 return 0;
bryantaylor 0:eafc3fd41f75 98 }
bryantaylor 0:eafc3fd41f75 99
bryantaylor 0:eafc3fd41f75 100 void SPI::abort_transfer()
bryantaylor 0:eafc3fd41f75 101 {
bryantaylor 0:eafc3fd41f75 102 spi_abort_asynch(&_spi);
bryantaylor 0:eafc3fd41f75 103 #if TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 104 dequeue_transaction();
bryantaylor 0:eafc3fd41f75 105 #endif
bryantaylor 0:eafc3fd41f75 106 }
bryantaylor 0:eafc3fd41f75 107
bryantaylor 0:eafc3fd41f75 108
bryantaylor 0:eafc3fd41f75 109 void SPI::clear_transfer_buffer()
bryantaylor 0:eafc3fd41f75 110 {
bryantaylor 0:eafc3fd41f75 111 #if TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 112 _transaction_buffer.reset();
bryantaylor 0:eafc3fd41f75 113 #endif
bryantaylor 0:eafc3fd41f75 114 }
bryantaylor 0:eafc3fd41f75 115
bryantaylor 0:eafc3fd41f75 116 void SPI::abort_all_transfers()
bryantaylor 0:eafc3fd41f75 117 {
bryantaylor 0:eafc3fd41f75 118 clear_transfer_buffer();
bryantaylor 0:eafc3fd41f75 119 abort_transfer();
bryantaylor 0:eafc3fd41f75 120 }
bryantaylor 0:eafc3fd41f75 121
bryantaylor 0:eafc3fd41f75 122 int SPI::set_dma_usage(DMAUsage usage)
bryantaylor 0:eafc3fd41f75 123 {
bryantaylor 0:eafc3fd41f75 124 if (spi_active(&_spi)) {
bryantaylor 0:eafc3fd41f75 125 return -1;
bryantaylor 0:eafc3fd41f75 126 }
bryantaylor 0:eafc3fd41f75 127 _usage = usage;
bryantaylor 0:eafc3fd41f75 128 return 0;
bryantaylor 0:eafc3fd41f75 129 }
bryantaylor 0:eafc3fd41f75 130
bryantaylor 0:eafc3fd41f75 131 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)
bryantaylor 0:eafc3fd41f75 132 {
bryantaylor 0:eafc3fd41f75 133 #if TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 134 transaction_t t;
bryantaylor 0:eafc3fd41f75 135
bryantaylor 0:eafc3fd41f75 136 t.tx_buffer = const_cast<void *>(tx_buffer);
bryantaylor 0:eafc3fd41f75 137 t.tx_length = tx_length;
bryantaylor 0:eafc3fd41f75 138 t.rx_buffer = rx_buffer;
bryantaylor 0:eafc3fd41f75 139 t.rx_length = rx_length;
bryantaylor 0:eafc3fd41f75 140 t.event = event;
bryantaylor 0:eafc3fd41f75 141 t.callback = callback;
bryantaylor 0:eafc3fd41f75 142 t.width = bit_width;
bryantaylor 0:eafc3fd41f75 143 Transaction<SPI> transaction(this, t);
bryantaylor 0:eafc3fd41f75 144 if (_transaction_buffer.full()) {
bryantaylor 0:eafc3fd41f75 145 return -1; // the buffer is full
bryantaylor 0:eafc3fd41f75 146 } else {
bryantaylor 0:eafc3fd41f75 147 core_util_critical_section_enter();
bryantaylor 0:eafc3fd41f75 148 _transaction_buffer.push(transaction);
bryantaylor 0:eafc3fd41f75 149 if (!spi_active(&_spi)) {
bryantaylor 0:eafc3fd41f75 150 dequeue_transaction();
bryantaylor 0:eafc3fd41f75 151 }
bryantaylor 0:eafc3fd41f75 152 core_util_critical_section_exit();
bryantaylor 0:eafc3fd41f75 153 return 0;
bryantaylor 0:eafc3fd41f75 154 }
bryantaylor 0:eafc3fd41f75 155 #else
bryantaylor 0:eafc3fd41f75 156 return -1;
bryantaylor 0:eafc3fd41f75 157 #endif
bryantaylor 0:eafc3fd41f75 158 }
bryantaylor 0:eafc3fd41f75 159
bryantaylor 0:eafc3fd41f75 160 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)
bryantaylor 0:eafc3fd41f75 161 {
bryantaylor 0:eafc3fd41f75 162 aquire();
bryantaylor 0:eafc3fd41f75 163 _callback = callback;
bryantaylor 0:eafc3fd41f75 164 _irq.callback(&SPI::irq_handler_asynch);
bryantaylor 0:eafc3fd41f75 165 spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage);
bryantaylor 0:eafc3fd41f75 166 }
bryantaylor 0:eafc3fd41f75 167
bryantaylor 0:eafc3fd41f75 168 #if TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 169
bryantaylor 0:eafc3fd41f75 170 void SPI::start_transaction(transaction_t *data)
bryantaylor 0:eafc3fd41f75 171 {
bryantaylor 0:eafc3fd41f75 172 start_transfer(data->tx_buffer, data->tx_length, data->rx_buffer, data->rx_length, data->width, data->callback, data->event);
bryantaylor 0:eafc3fd41f75 173 }
bryantaylor 0:eafc3fd41f75 174
bryantaylor 0:eafc3fd41f75 175 void SPI::dequeue_transaction()
bryantaylor 0:eafc3fd41f75 176 {
bryantaylor 0:eafc3fd41f75 177 Transaction<SPI> t;
bryantaylor 0:eafc3fd41f75 178 if (_transaction_buffer.pop(t)) {
bryantaylor 0:eafc3fd41f75 179 SPI* obj = t.get_object();
bryantaylor 0:eafc3fd41f75 180 transaction_t* data = t.get_transaction();
bryantaylor 0:eafc3fd41f75 181 obj->start_transaction(data);
bryantaylor 0:eafc3fd41f75 182 }
bryantaylor 0:eafc3fd41f75 183 }
bryantaylor 0:eafc3fd41f75 184
bryantaylor 0:eafc3fd41f75 185 #endif
bryantaylor 0:eafc3fd41f75 186
bryantaylor 0:eafc3fd41f75 187 void SPI::irq_handler_asynch(void)
bryantaylor 0:eafc3fd41f75 188 {
bryantaylor 0:eafc3fd41f75 189 int event = spi_irq_handler_asynch(&_spi);
bryantaylor 0:eafc3fd41f75 190 if (_callback && (event & SPI_EVENT_ALL)) {
bryantaylor 0:eafc3fd41f75 191 _callback.call(event & SPI_EVENT_ALL);
bryantaylor 0:eafc3fd41f75 192 }
bryantaylor 0:eafc3fd41f75 193 #if TRANSACTION_QUEUE_SIZE_SPI
bryantaylor 0:eafc3fd41f75 194 if (event & (SPI_EVENT_ALL | SPI_EVENT_INTERNAL_TRANSFER_COMPLETE)) {
bryantaylor 0:eafc3fd41f75 195 // SPI peripheral is free (event happend), dequeue transaction
bryantaylor 0:eafc3fd41f75 196 dequeue_transaction();
bryantaylor 0:eafc3fd41f75 197 }
bryantaylor 0:eafc3fd41f75 198 #endif
bryantaylor 0:eafc3fd41f75 199 }
bryantaylor 0:eafc3fd41f75 200
bryantaylor 0:eafc3fd41f75 201 #endif
bryantaylor 0:eafc3fd41f75 202
bryantaylor 0:eafc3fd41f75 203 } // namespace mbed
bryantaylor 0:eafc3fd41f75 204
bryantaylor 0:eafc3fd41f75 205 #endif