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