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