Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of mbed-src by
common/SPI.cpp@563:536c9fb088a0, 2015-06-10 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Jun 10 10:00:08 2015 +0100
- Revision:
- 563:536c9fb088a0
- Parent:
- 552:a1b9575155a3
Synchronized with git revision 4778e33fa11602202ae3a57ced5d026e8053cedf
Full URL: https://github.com/mbedmicro/mbed/commit/4778e33fa11602202ae3a57ced5d026e8053cedf/
Fix asynch methods constness
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bogdanm | 13:0645d8841f51 | 1 | /* mbed Microcontroller Library |
bogdanm | 13:0645d8841f51 | 2 | * Copyright (c) 2006-2013 ARM Limited |
bogdanm | 13:0645d8841f51 | 3 | * |
bogdanm | 13:0645d8841f51 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
bogdanm | 13:0645d8841f51 | 5 | * you may not use this file except in compliance with the License. |
bogdanm | 13:0645d8841f51 | 6 | * You may obtain a copy of the License at |
bogdanm | 13:0645d8841f51 | 7 | * |
bogdanm | 13:0645d8841f51 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
bogdanm | 13:0645d8841f51 | 9 | * |
bogdanm | 13:0645d8841f51 | 10 | * Unless required by applicable law or agreed to in writing, software |
bogdanm | 13:0645d8841f51 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
bogdanm | 13:0645d8841f51 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
bogdanm | 13:0645d8841f51 | 13 | * See the License for the specific language governing permissions and |
bogdanm | 13:0645d8841f51 | 14 | * limitations under the License. |
bogdanm | 13:0645d8841f51 | 15 | */ |
bogdanm | 13:0645d8841f51 | 16 | #include "SPI.h" |
bogdanm | 13:0645d8841f51 | 17 | |
bogdanm | 13:0645d8841f51 | 18 | #if DEVICE_SPI |
bogdanm | 13:0645d8841f51 | 19 | |
bogdanm | 13:0645d8841f51 | 20 | namespace mbed { |
bogdanm | 13:0645d8841f51 | 21 | |
mbed_official | 525:c320967f86b9 | 22 | #if DEVICE_SPI_ASYNCH && TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 23 | CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> SPI::_transaction_buffer; |
mbed_official | 525:c320967f86b9 | 24 | #endif |
mbed_official | 525:c320967f86b9 | 25 | |
mbed_official | 552:a1b9575155a3 | 26 | SPI::SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel) : |
mbed_official | 212:34d62c0b2af6 | 27 | _spi(), |
mbed_official | 525:c320967f86b9 | 28 | #if DEVICE_SPI_ASYNCH |
mbed_official | 525:c320967f86b9 | 29 | _irq(this), |
mbed_official | 525:c320967f86b9 | 30 | _usage(DMA_USAGE_NEVER), |
mbed_official | 525:c320967f86b9 | 31 | #endif |
mbed_official | 212:34d62c0b2af6 | 32 | _bits(8), |
mbed_official | 212:34d62c0b2af6 | 33 | _mode(0), |
mbed_official | 212:34d62c0b2af6 | 34 | _hz(1000000) { |
mbed_official | 552:a1b9575155a3 | 35 | spi_init(&_spi, mosi, miso, sclk, ssel); |
bogdanm | 13:0645d8841f51 | 36 | spi_format(&_spi, _bits, _mode, 0); |
bogdanm | 13:0645d8841f51 | 37 | spi_frequency(&_spi, _hz); |
bogdanm | 13:0645d8841f51 | 38 | } |
bogdanm | 13:0645d8841f51 | 39 | |
bogdanm | 13:0645d8841f51 | 40 | void SPI::format(int bits, int mode) { |
bogdanm | 13:0645d8841f51 | 41 | _bits = bits; |
bogdanm | 13:0645d8841f51 | 42 | _mode = mode; |
bogdanm | 13:0645d8841f51 | 43 | SPI::_owner = NULL; // Not that elegant, but works. rmeyer |
bogdanm | 13:0645d8841f51 | 44 | aquire(); |
bogdanm | 13:0645d8841f51 | 45 | } |
bogdanm | 13:0645d8841f51 | 46 | |
bogdanm | 13:0645d8841f51 | 47 | void SPI::frequency(int hz) { |
bogdanm | 13:0645d8841f51 | 48 | _hz = hz; |
bogdanm | 13:0645d8841f51 | 49 | SPI::_owner = NULL; // Not that elegant, but works. rmeyer |
bogdanm | 13:0645d8841f51 | 50 | aquire(); |
bogdanm | 13:0645d8841f51 | 51 | } |
bogdanm | 13:0645d8841f51 | 52 | |
bogdanm | 13:0645d8841f51 | 53 | SPI* SPI::_owner = NULL; |
bogdanm | 13:0645d8841f51 | 54 | |
bogdanm | 13:0645d8841f51 | 55 | // ignore the fact there are multiple physical spis, and always update if it wasnt us last |
bogdanm | 13:0645d8841f51 | 56 | void SPI::aquire() { |
bogdanm | 13:0645d8841f51 | 57 | if (_owner != this) { |
bogdanm | 13:0645d8841f51 | 58 | spi_format(&_spi, _bits, _mode, 0); |
bogdanm | 13:0645d8841f51 | 59 | spi_frequency(&_spi, _hz); |
bogdanm | 13:0645d8841f51 | 60 | _owner = this; |
bogdanm | 13:0645d8841f51 | 61 | } |
bogdanm | 13:0645d8841f51 | 62 | } |
bogdanm | 13:0645d8841f51 | 63 | |
bogdanm | 13:0645d8841f51 | 64 | int SPI::write(int value) { |
bogdanm | 13:0645d8841f51 | 65 | aquire(); |
bogdanm | 13:0645d8841f51 | 66 | return spi_master_write(&_spi, value); |
bogdanm | 13:0645d8841f51 | 67 | } |
bogdanm | 13:0645d8841f51 | 68 | |
mbed_official | 525:c320967f86b9 | 69 | #if DEVICE_SPI_ASYNCH |
mbed_official | 525:c320967f86b9 | 70 | |
mbed_official | 563:536c9fb088a0 | 71 | 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) |
mbed_official | 525:c320967f86b9 | 72 | { |
mbed_official | 525:c320967f86b9 | 73 | if (spi_active(&_spi)) { |
mbed_official | 525:c320967f86b9 | 74 | return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event); |
mbed_official | 525:c320967f86b9 | 75 | } |
mbed_official | 525:c320967f86b9 | 76 | start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, bit_width, callback, event); |
mbed_official | 525:c320967f86b9 | 77 | return 0; |
mbed_official | 525:c320967f86b9 | 78 | } |
mbed_official | 525:c320967f86b9 | 79 | |
mbed_official | 525:c320967f86b9 | 80 | void SPI::abort_transfer() |
mbed_official | 525:c320967f86b9 | 81 | { |
mbed_official | 525:c320967f86b9 | 82 | spi_abort_asynch(&_spi); |
mbed_official | 525:c320967f86b9 | 83 | #if TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 84 | dequeue_transaction(); |
mbed_official | 525:c320967f86b9 | 85 | #endif |
mbed_official | 525:c320967f86b9 | 86 | } |
mbed_official | 525:c320967f86b9 | 87 | |
mbed_official | 525:c320967f86b9 | 88 | |
mbed_official | 525:c320967f86b9 | 89 | void SPI::clear_transfer_buffer() |
mbed_official | 525:c320967f86b9 | 90 | { |
mbed_official | 525:c320967f86b9 | 91 | #if TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 92 | _transaction_buffer.reset(); |
mbed_official | 525:c320967f86b9 | 93 | #endif |
mbed_official | 525:c320967f86b9 | 94 | } |
mbed_official | 525:c320967f86b9 | 95 | |
mbed_official | 525:c320967f86b9 | 96 | void SPI::abort_all_transfers() |
mbed_official | 525:c320967f86b9 | 97 | { |
mbed_official | 525:c320967f86b9 | 98 | clear_transfer_buffer(); |
mbed_official | 525:c320967f86b9 | 99 | abort_transfer(); |
mbed_official | 525:c320967f86b9 | 100 | } |
mbed_official | 525:c320967f86b9 | 101 | |
mbed_official | 525:c320967f86b9 | 102 | int SPI::set_dma_usage(DMAUsage usage) |
mbed_official | 525:c320967f86b9 | 103 | { |
mbed_official | 525:c320967f86b9 | 104 | if (spi_active(&_spi)) { |
mbed_official | 525:c320967f86b9 | 105 | return -1; |
mbed_official | 525:c320967f86b9 | 106 | } |
mbed_official | 525:c320967f86b9 | 107 | _usage = usage; |
mbed_official | 525:c320967f86b9 | 108 | return 0; |
mbed_official | 525:c320967f86b9 | 109 | } |
mbed_official | 525:c320967f86b9 | 110 | |
mbed_official | 563:536c9fb088a0 | 111 | 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) |
mbed_official | 525:c320967f86b9 | 112 | { |
mbed_official | 525:c320967f86b9 | 113 | #if TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 114 | transaction_t t; |
mbed_official | 525:c320967f86b9 | 115 | |
mbed_official | 563:536c9fb088a0 | 116 | t.tx_buffer = const_cast<void *>(tx_buffer); |
mbed_official | 525:c320967f86b9 | 117 | t.tx_length = tx_length; |
mbed_official | 525:c320967f86b9 | 118 | t.rx_buffer = rx_buffer; |
mbed_official | 525:c320967f86b9 | 119 | t.rx_length = rx_length; |
mbed_official | 525:c320967f86b9 | 120 | t.event = event; |
mbed_official | 525:c320967f86b9 | 121 | t.callback = callback; |
mbed_official | 525:c320967f86b9 | 122 | t.width = bit_width; |
mbed_official | 525:c320967f86b9 | 123 | Transaction<SPI> transaction(this, t); |
mbed_official | 525:c320967f86b9 | 124 | if (_transaction_buffer.full()) { |
mbed_official | 525:c320967f86b9 | 125 | return -1; // the buffer is full |
mbed_official | 525:c320967f86b9 | 126 | } else { |
mbed_official | 525:c320967f86b9 | 127 | _transaction_buffer.push(transaction); |
mbed_official | 525:c320967f86b9 | 128 | return 0; |
mbed_official | 525:c320967f86b9 | 129 | } |
mbed_official | 525:c320967f86b9 | 130 | #else |
mbed_official | 525:c320967f86b9 | 131 | return -1; |
mbed_official | 525:c320967f86b9 | 132 | #endif |
mbed_official | 525:c320967f86b9 | 133 | } |
mbed_official | 525:c320967f86b9 | 134 | |
mbed_official | 563:536c9fb088a0 | 135 | 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) |
mbed_official | 525:c320967f86b9 | 136 | { |
mbed_official | 525:c320967f86b9 | 137 | aquire(); |
mbed_official | 525:c320967f86b9 | 138 | _callback = callback; |
mbed_official | 525:c320967f86b9 | 139 | _irq.callback(&SPI::irq_handler_asynch); |
mbed_official | 525:c320967f86b9 | 140 | spi_master_transfer(&_spi, tx_buffer, tx_length, rx_buffer, rx_length, bit_width, _irq.entry(), event , _usage); |
mbed_official | 525:c320967f86b9 | 141 | } |
mbed_official | 525:c320967f86b9 | 142 | |
mbed_official | 525:c320967f86b9 | 143 | #if TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 144 | |
mbed_official | 525:c320967f86b9 | 145 | void SPI::start_transaction(transaction_t *data) |
mbed_official | 525:c320967f86b9 | 146 | { |
mbed_official | 525:c320967f86b9 | 147 | start_transfer(data->tx_buffer, data->tx_length, data->rx_buffer, data->rx_length, data->width, data->callback, data->event); |
mbed_official | 525:c320967f86b9 | 148 | } |
mbed_official | 525:c320967f86b9 | 149 | |
mbed_official | 525:c320967f86b9 | 150 | void SPI::dequeue_transaction() |
mbed_official | 525:c320967f86b9 | 151 | { |
mbed_official | 525:c320967f86b9 | 152 | Transaction<SPI> t; |
mbed_official | 525:c320967f86b9 | 153 | if (_transaction_buffer.pop(t)) { |
mbed_official | 525:c320967f86b9 | 154 | SPI* obj = t.get_object(); |
mbed_official | 525:c320967f86b9 | 155 | transaction_t* data = t.get_transaction(); |
mbed_official | 525:c320967f86b9 | 156 | obj->start_transaction(data); |
mbed_official | 525:c320967f86b9 | 157 | } |
mbed_official | 525:c320967f86b9 | 158 | } |
mbed_official | 525:c320967f86b9 | 159 | |
mbed_official | 525:c320967f86b9 | 160 | #endif |
mbed_official | 525:c320967f86b9 | 161 | |
mbed_official | 525:c320967f86b9 | 162 | void SPI::irq_handler_asynch(void) |
mbed_official | 525:c320967f86b9 | 163 | { |
mbed_official | 525:c320967f86b9 | 164 | int event = spi_irq_handler_asynch(&_spi); |
mbed_official | 525:c320967f86b9 | 165 | if (_callback && (event & SPI_EVENT_ALL)) { |
mbed_official | 525:c320967f86b9 | 166 | _callback.call(event & SPI_EVENT_ALL); |
mbed_official | 525:c320967f86b9 | 167 | } |
mbed_official | 525:c320967f86b9 | 168 | #if TRANSACTION_QUEUE_SIZE_SPI |
mbed_official | 525:c320967f86b9 | 169 | if (event & (SPI_EVENT_ALL | SPI_EVENT_INTERNAL_TRANSFER_COMPLETE)) { |
mbed_official | 525:c320967f86b9 | 170 | // SPI peripheral is free (event happend), dequeue transaction |
mbed_official | 525:c320967f86b9 | 171 | dequeue_transaction(); |
mbed_official | 525:c320967f86b9 | 172 | } |
mbed_official | 525:c320967f86b9 | 173 | #endif |
mbed_official | 525:c320967f86b9 | 174 | } |
mbed_official | 525:c320967f86b9 | 175 | |
mbed_official | 525:c320967f86b9 | 176 | #endif |
mbed_official | 525:c320967f86b9 | 177 | |
bogdanm | 13:0645d8841f51 | 178 | } // namespace mbed |
bogdanm | 13:0645d8841f51 | 179 | |
bogdanm | 13:0645d8841f51 | 180 | #endif |