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