mbed-os

Fork of mbed-os by erkin yucel

Committer:
xuaner
Date:
Thu Jul 20 14:26:57 2017 +0000
Revision:
1:3deb71413561
Parent:
0:f269e3021894
mbed_os

Who changed what in which revision?

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