Entrega 3er corte - sistemas embebidos

Committer:
Bethory
Date:
Wed May 30 04:46:28 2018 +0000
Revision:
1:fcdb45ee95b9
Parent:
0:6ad07c9019fd
Entrega Final

Who changed what in which revision?

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