BA
/
BaBoRo_test2
Backup 1
mbed-os/drivers/SPI.cpp@0:02dd72d1d465, 2018-04-24 (annotated)
- Committer:
- borlanic
- Date:
- Tue Apr 24 11:45:18 2018 +0000
- Revision:
- 0:02dd72d1d465
BaBoRo_test2 - backup 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
borlanic | 0:02dd72d1d465 | 1 | /* mbed Microcontroller Library |
borlanic | 0:02dd72d1d465 | 2 | * Copyright (c) 2006-2013 ARM Limited |
borlanic | 0:02dd72d1d465 | 3 | * |
borlanic | 0:02dd72d1d465 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
borlanic | 0:02dd72d1d465 | 5 | * you may not use this file except in compliance with the License. |
borlanic | 0:02dd72d1d465 | 6 | * You may obtain a copy of the License at |
borlanic | 0:02dd72d1d465 | 7 | * |
borlanic | 0:02dd72d1d465 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
borlanic | 0:02dd72d1d465 | 9 | * |
borlanic | 0:02dd72d1d465 | 10 | * Unless required by applicable law or agreed to in writing, software |
borlanic | 0:02dd72d1d465 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
borlanic | 0:02dd72d1d465 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
borlanic | 0:02dd72d1d465 | 13 | * See the License for the specific language governing permissions and |
borlanic | 0:02dd72d1d465 | 14 | * limitations under the License. |
borlanic | 0:02dd72d1d465 | 15 | */ |
borlanic | 0:02dd72d1d465 | 16 | #include "drivers/SPI.h" |
borlanic | 0:02dd72d1d465 | 17 | #include "platform/mbed_critical.h" |
borlanic | 0:02dd72d1d465 | 18 | |
borlanic | 0:02dd72d1d465 | 19 | #if DEVICE_SPI_ASYNCH |
borlanic | 0:02dd72d1d465 | 20 | #include "platform/mbed_power_mgmt.h" |
borlanic | 0:02dd72d1d465 | 21 | #endif |
borlanic | 0:02dd72d1d465 | 22 | |
borlanic | 0:02dd72d1d465 | 23 | #if DEVICE_SPI |
borlanic | 0:02dd72d1d465 | 24 | |
borlanic | 0:02dd72d1d465 | 25 | namespace mbed { |
borlanic | 0:02dd72d1d465 | 26 | |
borlanic | 0:02dd72d1d465 | 27 | #if DEVICE_SPI_ASYNCH && TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 28 | CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_buffer; |
borlanic | 0:02dd72d1d465 | 29 | #endif |
borlanic | 0:02dd72d1d465 | 30 | |
borlanic | 0:02dd72d1d465 | 31 | SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) : |
borlanic | 0:02dd72d1d465 | 32 | _spi(), |
borlanic | 0:02dd72d1d465 | 33 | #if DEVICE_SPI_ASYNCH |
borlanic | 0:02dd72d1d465 | 34 | _irq(this), |
borlanic | 0:02dd72d1d465 | 35 | _usage(DMA_USAGE_NEVER), |
borlanic | 0:02dd72d1d465 | 36 | _deep_sleep_locked(false), |
borlanic | 0:02dd72d1d465 | 37 | #endif |
borlanic | 0:02dd72d1d465 | 38 | _bits(8), |
borlanic | 0:02dd72d1d465 | 39 | _mode(0), |
borlanic | 0:02dd72d1d465 | 40 | _hz(1000000), |
borlanic | 0:02dd72d1d465 | 41 | _write_fill(SPI_FILL_CHAR) { |
borlanic | 0:02dd72d1d465 | 42 | // No lock needed in the constructor |
borlanic | 0:02dd72d1d465 | 43 | |
borlanic | 0:02dd72d1d465 | 44 | spi_init(&_spi, mosi, miso, sclk, ssel); |
borlanic | 0:02dd72d1d465 | 45 | _acquire(); |
borlanic | 0:02dd72d1d465 | 46 | } |
borlanic | 0:02dd72d1d465 | 47 | |
borlanic | 0:02dd72d1d465 | 48 | void SPI::format(int bits, int mode) { |
borlanic | 0:02dd72d1d465 | 49 | lock(); |
borlanic | 0:02dd72d1d465 | 50 | _bits = bits; |
borlanic | 0:02dd72d1d465 | 51 | _mode = mode; |
borlanic | 0:02dd72d1d465 | 52 | // If changing format while you are the owner then just |
borlanic | 0:02dd72d1d465 | 53 | // update format, but if owner is changed then even frequency should be |
borlanic | 0:02dd72d1d465 | 54 | // updated which is done by acquire. |
borlanic | 0:02dd72d1d465 | 55 | if (_owner == this) { |
borlanic | 0:02dd72d1d465 | 56 | spi_format(&_spi, _bits, _mode, 0); |
borlanic | 0:02dd72d1d465 | 57 | } else { |
borlanic | 0:02dd72d1d465 | 58 | _acquire(); |
borlanic | 0:02dd72d1d465 | 59 | } |
borlanic | 0:02dd72d1d465 | 60 | unlock(); |
borlanic | 0:02dd72d1d465 | 61 | } |
borlanic | 0:02dd72d1d465 | 62 | |
borlanic | 0:02dd72d1d465 | 63 | void SPI::frequency(int hz) { |
borlanic | 0:02dd72d1d465 | 64 | lock(); |
borlanic | 0:02dd72d1d465 | 65 | _hz = hz; |
borlanic | 0:02dd72d1d465 | 66 | // If changing format while you are the owner then just |
borlanic | 0:02dd72d1d465 | 67 | // update frequency, but if owner is changed then even frequency should be |
borlanic | 0:02dd72d1d465 | 68 | // updated which is done by acquire. |
borlanic | 0:02dd72d1d465 | 69 | if (_owner == this) { |
borlanic | 0:02dd72d1d465 | 70 | spi_frequency(&_spi, _hz); |
borlanic | 0:02dd72d1d465 | 71 | } else { |
borlanic | 0:02dd72d1d465 | 72 | _acquire(); |
borlanic | 0:02dd72d1d465 | 73 | } |
borlanic | 0:02dd72d1d465 | 74 | unlock(); |
borlanic | 0:02dd72d1d465 | 75 | } |
borlanic | 0:02dd72d1d465 | 76 | |
borlanic | 0:02dd72d1d465 | 77 | SPI* SPI::_owner = NULL; |
borlanic | 0:02dd72d1d465 | 78 | SingletonPtr<PlatformMutex> SPI::_mutex; |
borlanic | 0:02dd72d1d465 | 79 | |
borlanic | 0:02dd72d1d465 | 80 | // ignore the fact there are multiple physical spis, and always update if it wasn't us last |
borlanic | 0:02dd72d1d465 | 81 | void SPI::aquire() { |
borlanic | 0:02dd72d1d465 | 82 | lock(); |
borlanic | 0:02dd72d1d465 | 83 | if (_owner != this) { |
borlanic | 0:02dd72d1d465 | 84 | spi_format(&_spi, _bits, _mode, 0); |
borlanic | 0:02dd72d1d465 | 85 | spi_frequency(&_spi, _hz); |
borlanic | 0:02dd72d1d465 | 86 | _owner = this; |
borlanic | 0:02dd72d1d465 | 87 | } |
borlanic | 0:02dd72d1d465 | 88 | unlock(); |
borlanic | 0:02dd72d1d465 | 89 | } |
borlanic | 0:02dd72d1d465 | 90 | |
borlanic | 0:02dd72d1d465 | 91 | // Note: Private function with no locking |
borlanic | 0:02dd72d1d465 | 92 | void SPI::_acquire() { |
borlanic | 0:02dd72d1d465 | 93 | if (_owner != this) { |
borlanic | 0:02dd72d1d465 | 94 | spi_format(&_spi, _bits, _mode, 0); |
borlanic | 0:02dd72d1d465 | 95 | spi_frequency(&_spi, _hz); |
borlanic | 0:02dd72d1d465 | 96 | _owner = this; |
borlanic | 0:02dd72d1d465 | 97 | } |
borlanic | 0:02dd72d1d465 | 98 | } |
borlanic | 0:02dd72d1d465 | 99 | |
borlanic | 0:02dd72d1d465 | 100 | int SPI::write(int value) { |
borlanic | 0:02dd72d1d465 | 101 | lock(); |
borlanic | 0:02dd72d1d465 | 102 | _acquire(); |
borlanic | 0:02dd72d1d465 | 103 | int ret = spi_master_write(&_spi, value); |
borlanic | 0:02dd72d1d465 | 104 | unlock(); |
borlanic | 0:02dd72d1d465 | 105 | return ret; |
borlanic | 0:02dd72d1d465 | 106 | } |
borlanic | 0:02dd72d1d465 | 107 | |
borlanic | 0:02dd72d1d465 | 108 | int SPI::write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length) { |
borlanic | 0:02dd72d1d465 | 109 | lock(); |
borlanic | 0:02dd72d1d465 | 110 | _acquire(); |
borlanic | 0:02dd72d1d465 | 111 | int ret = spi_master_block_write(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, _write_fill); |
borlanic | 0:02dd72d1d465 | 112 | unlock(); |
borlanic | 0:02dd72d1d465 | 113 | return ret; |
borlanic | 0:02dd72d1d465 | 114 | } |
borlanic | 0:02dd72d1d465 | 115 | |
borlanic | 0:02dd72d1d465 | 116 | void SPI::lock() { |
borlanic | 0:02dd72d1d465 | 117 | _mutex->lock(); |
borlanic | 0:02dd72d1d465 | 118 | } |
borlanic | 0:02dd72d1d465 | 119 | |
borlanic | 0:02dd72d1d465 | 120 | void SPI::unlock() { |
borlanic | 0:02dd72d1d465 | 121 | _mutex->unlock(); |
borlanic | 0:02dd72d1d465 | 122 | } |
borlanic | 0:02dd72d1d465 | 123 | |
borlanic | 0:02dd72d1d465 | 124 | void SPI::set_default_write_value(char data) { |
borlanic | 0:02dd72d1d465 | 125 | lock(); |
borlanic | 0:02dd72d1d465 | 126 | _write_fill = data; |
borlanic | 0:02dd72d1d465 | 127 | unlock(); |
borlanic | 0:02dd72d1d465 | 128 | } |
borlanic | 0:02dd72d1d465 | 129 | |
borlanic | 0:02dd72d1d465 | 130 | #if DEVICE_SPI_ASYNCH |
borlanic | 0:02dd72d1d465 | 131 | |
borlanic | 0:02dd72d1d465 | 132 | 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) |
borlanic | 0:02dd72d1d465 | 133 | { |
borlanic | 0:02dd72d1d465 | 134 | if (spi_active(&_spi)) { |
borlanic | 0:02dd72d1d465 | 135 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event); |
borlanic | 0:02dd72d1d465 | 136 | } |
borlanic | 0:02dd72d1d465 | 137 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event); |
borlanic | 0:02dd72d1d465 | 138 | return 0; |
borlanic | 0:02dd72d1d465 | 139 | } |
borlanic | 0:02dd72d1d465 | 140 | |
borlanic | 0:02dd72d1d465 | 141 | void SPI::abort_transfer() |
borlanic | 0:02dd72d1d465 | 142 | { |
borlanic | 0:02dd72d1d465 | 143 | spi_abort_asynch(&_spi); |
borlanic | 0:02dd72d1d465 | 144 | unlock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 145 | #if TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 146 | dequeue_transaction(); |
borlanic | 0:02dd72d1d465 | 147 | #endif |
borlanic | 0:02dd72d1d465 | 148 | } |
borlanic | 0:02dd72d1d465 | 149 | |
borlanic | 0:02dd72d1d465 | 150 | |
borlanic | 0:02dd72d1d465 | 151 | void SPI::clear_transfer_buffer() |
borlanic | 0:02dd72d1d465 | 152 | { |
borlanic | 0:02dd72d1d465 | 153 | #if TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 154 | _transaction_buffer.reset(); |
borlanic | 0:02dd72d1d465 | 155 | #endif |
borlanic | 0:02dd72d1d465 | 156 | } |
borlanic | 0:02dd72d1d465 | 157 | |
borlanic | 0:02dd72d1d465 | 158 | void SPI::abort_all_transfers() |
borlanic | 0:02dd72d1d465 | 159 | { |
borlanic | 0:02dd72d1d465 | 160 | clear_transfer_buffer(); |
borlanic | 0:02dd72d1d465 | 161 | abort_transfer(); |
borlanic | 0:02dd72d1d465 | 162 | } |
borlanic | 0:02dd72d1d465 | 163 | |
borlanic | 0:02dd72d1d465 | 164 | int SPI::set_dma_usage(DMAUsage usage) |
borlanic | 0:02dd72d1d465 | 165 | { |
borlanic | 0:02dd72d1d465 | 166 | if (spi_active(&_spi)) { |
borlanic | 0:02dd72d1d465 | 167 | return -1; |
borlanic | 0:02dd72d1d465 | 168 | } |
borlanic | 0:02dd72d1d465 | 169 | _usage = usage; |
borlanic | 0:02dd72d1d465 | 170 | return 0; |
borlanic | 0:02dd72d1d465 | 171 | } |
borlanic | 0:02dd72d1d465 | 172 | |
borlanic | 0:02dd72d1d465 | 173 | 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) |
borlanic | 0:02dd72d1d465 | 174 | { |
borlanic | 0:02dd72d1d465 | 175 | #if TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 176 | transaction_t t; |
borlanic | 0:02dd72d1d465 | 177 | |
borlanic | 0:02dd72d1d465 | 178 | t.tx_buffer = const_cast<void *>(tx_buffer); |
borlanic | 0:02dd72d1d465 | 179 | t.tx_length = tx_length; |
borlanic | 0:02dd72d1d465 | 180 | t.rx_buffer = rx_buffer; |
borlanic | 0:02dd72d1d465 | 181 | t.rx_length = rx_length; |
borlanic | 0:02dd72d1d465 | 182 | t.event = event; |
borlanic | 0:02dd72d1d465 | 183 | t.callback = callback; |
borlanic | 0:02dd72d1d465 | 184 | t.width = bit_width; |
borlanic | 0:02dd72d1d465 | 185 | Transaction<SPI> transaction(this, t); |
borlanic | 0:02dd72d1d465 | 186 | if (_transaction_buffer.full()) { |
borlanic | 0:02dd72d1d465 | 187 | return -1; // the buffer is full |
borlanic | 0:02dd72d1d465 | 188 | } else { |
borlanic | 0:02dd72d1d465 | 189 | core_util_critical_section_enter(); |
borlanic | 0:02dd72d1d465 | 190 | _transaction_buffer.push(transaction); |
borlanic | 0:02dd72d1d465 | 191 | if (!spi_active(&_spi)) { |
borlanic | 0:02dd72d1d465 | 192 | dequeue_transaction(); |
borlanic | 0:02dd72d1d465 | 193 | } |
borlanic | 0:02dd72d1d465 | 194 | core_util_critical_section_exit(); |
borlanic | 0:02dd72d1d465 | 195 | return 0; |
borlanic | 0:02dd72d1d465 | 196 | } |
borlanic | 0:02dd72d1d465 | 197 | #else |
borlanic | 0:02dd72d1d465 | 198 | return -1; |
borlanic | 0:02dd72d1d465 | 199 | #endif |
borlanic | 0:02dd72d1d465 | 200 | } |
borlanic | 0:02dd72d1d465 | 201 | |
borlanic | 0:02dd72d1d465 | 202 | 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) |
borlanic | 0:02dd72d1d465 | 203 | { |
borlanic | 0:02dd72d1d465 | 204 | lock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 205 | _acquire(); |
borlanic | 0:02dd72d1d465 | 206 | _callback = callback; |
borlanic | 0:02dd72d1d465 | 207 | _irq.callback(&SPI::irq_handler_asynch); |
borlanic | 0:02dd72d1d465 | 208 | spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage); |
borlanic | 0:02dd72d1d465 | 209 | } |
borlanic | 0:02dd72d1d465 | 210 | |
borlanic | 0:02dd72d1d465 | 211 | void SPI::lock_deep_sleep() |
borlanic | 0:02dd72d1d465 | 212 | { |
borlanic | 0:02dd72d1d465 | 213 | if (_deep_sleep_locked == false) { |
borlanic | 0:02dd72d1d465 | 214 | sleep_manager_lock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 215 | _deep_sleep_locked = true; |
borlanic | 0:02dd72d1d465 | 216 | } |
borlanic | 0:02dd72d1d465 | 217 | } |
borlanic | 0:02dd72d1d465 | 218 | |
borlanic | 0:02dd72d1d465 | 219 | void SPI::unlock_deep_sleep() |
borlanic | 0:02dd72d1d465 | 220 | { |
borlanic | 0:02dd72d1d465 | 221 | if (_deep_sleep_locked == true) { |
borlanic | 0:02dd72d1d465 | 222 | sleep_manager_unlock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 223 | _deep_sleep_locked = false; |
borlanic | 0:02dd72d1d465 | 224 | } |
borlanic | 0:02dd72d1d465 | 225 | } |
borlanic | 0:02dd72d1d465 | 226 | |
borlanic | 0:02dd72d1d465 | 227 | #if TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 228 | |
borlanic | 0:02dd72d1d465 | 229 | void SPI::start_transaction(transaction_t *data) |
borlanic | 0:02dd72d1d465 | 230 | { |
borlanic | 0:02dd72d1d465 | 231 | start_transfer(data->tx_buffer, data->tx_length, data->rx_buffer, data->rx_length, data->width, data->callback, data->event); |
borlanic | 0:02dd72d1d465 | 232 | } |
borlanic | 0:02dd72d1d465 | 233 | |
borlanic | 0:02dd72d1d465 | 234 | void SPI::dequeue_transaction() |
borlanic | 0:02dd72d1d465 | 235 | { |
borlanic | 0:02dd72d1d465 | 236 | Transaction<SPI> t; |
borlanic | 0:02dd72d1d465 | 237 | if (_transaction_buffer.pop(t)) { |
borlanic | 0:02dd72d1d465 | 238 | SPI* obj = t.get_object(); |
borlanic | 0:02dd72d1d465 | 239 | transaction_t* data = t.get_transaction(); |
borlanic | 0:02dd72d1d465 | 240 | obj->start_transaction(data); |
borlanic | 0:02dd72d1d465 | 241 | } |
borlanic | 0:02dd72d1d465 | 242 | } |
borlanic | 0:02dd72d1d465 | 243 | |
borlanic | 0:02dd72d1d465 | 244 | #endif |
borlanic | 0:02dd72d1d465 | 245 | |
borlanic | 0:02dd72d1d465 | 246 | void SPI::irq_handler_asynch(void) |
borlanic | 0:02dd72d1d465 | 247 | { |
borlanic | 0:02dd72d1d465 | 248 | int event = spi_irq_handler_asynch(&_spi); |
borlanic | 0:02dd72d1d465 | 249 | if (_callback && (event & SPI_EVENT_ALL)) { |
borlanic | 0:02dd72d1d465 | 250 | unlock_deep_sleep(); |
borlanic | 0:02dd72d1d465 | 251 | _callback.call(event & SPI_EVENT_ALL); |
borlanic | 0:02dd72d1d465 | 252 | } |
borlanic | 0:02dd72d1d465 | 253 | #if TRANSACTION_QUEUE_SIZE_SPI |
borlanic | 0:02dd72d1d465 | 254 | if (event & (SPI_EVENT_ALL | SPI_EVENT_INTERNAL_TRANSFER_COMPLETE)) { |
borlanic | 0:02dd72d1d465 | 255 | // SPI peripheral is free (event happened), dequeue transaction |
borlanic | 0:02dd72d1d465 | 256 | dequeue_transaction(); |
borlanic | 0:02dd72d1d465 | 257 | } |
borlanic | 0:02dd72d1d465 | 258 | #endif |
borlanic | 0:02dd72d1d465 | 259 | } |
borlanic | 0:02dd72d1d465 | 260 | |
borlanic | 0:02dd72d1d465 | 261 | #endif |
borlanic | 0:02dd72d1d465 | 262 | |
borlanic | 0:02dd72d1d465 | 263 | } // namespace mbed |
borlanic | 0:02dd72d1d465 | 264 | |
borlanic | 0:02dd72d1d465 | 265 | #endif |