SPI.cpp 6.4 KB

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