SPI.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /* mbed Microcontroller Library
  2. * Copyright (c) 2006-2015 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. #ifndef MBED_SPI_H
  17. #define MBED_SPI_H
  18. #include "platform/platform.h"
  19. #if defined (DEVICE_SPI) || defined(DOXYGEN_ONLY)
  20. #include "platform/PlatformMutex.h"
  21. #include "hal/spi_api.h"
  22. #include "platform/SingletonPtr.h"
  23. #include "platform/NonCopyable.h"
  24. #if DEVICE_SPI_ASYNCH
  25. #include "platform/CThunk.h"
  26. #include "hal/dma_api.h"
  27. #include "platform/CircularBuffer.h"
  28. #include "platform/FunctionPointer.h"
  29. #include "platform/Transaction.h"
  30. #endif
  31. namespace mbed {
  32. /** \addtogroup drivers */
  33. /** A SPI Master, used for communicating with SPI slave devices
  34. *
  35. * The default format is set to 8-bits, mode 0, and a clock frequency of 1MHz
  36. *
  37. * Most SPI devices will also require Chip Select and Reset signals. These
  38. * can be controlled using DigitalOut pins
  39. *
  40. * @note Synchronization level: Thread safe
  41. *
  42. * Example:
  43. * @code
  44. * // Send a byte to a SPI slave, and record the response
  45. *
  46. * #include "mbed.h"
  47. *
  48. * // hardware ssel (where applicable)
  49. * //SPI device(p5, p6, p7, p8); // mosi, miso, sclk, ssel
  50. *
  51. * // software ssel
  52. * SPI device(p5, p6, p7); // mosi, miso, sclk
  53. * DigitalOut cs(p8); // ssel
  54. *
  55. * int main() {
  56. * // hardware ssel (where applicable)
  57. * //int response = device.write(0xFF);
  58. *
  59. * device.lock();
  60. * // software ssel
  61. * cs = 0;
  62. * int response = device.write(0xFF);
  63. * cs = 1;
  64. * device.unlock();
  65. *
  66. * }
  67. * @endcode
  68. * @ingroup drivers
  69. */
  70. class SPI : private NonCopyable<SPI> {
  71. public:
  72. /** Create a SPI master connected to the specified pins
  73. *
  74. * mosi or miso can be specified as NC if not used
  75. *
  76. * @param mosi SPI Master Out, Slave In pin
  77. * @param miso SPI Master In, Slave Out pin
  78. * @param sclk SPI Clock pin
  79. * @param ssel SPI chip select pin
  80. */
  81. SPI(PinName mosi, PinName miso, PinName sclk, PinName ssel = NC);
  82. /** Configure the data transmission format
  83. *
  84. * @param bits Number of bits per SPI frame (4 - 16)
  85. * @param mode Clock polarity and phase mode (0 - 3)
  86. *
  87. * @code
  88. * mode | POL PHA
  89. * -----+--------
  90. * 0 | 0 0
  91. * 1 | 0 1
  92. * 2 | 1 0
  93. * 3 | 1 1
  94. * @endcode
  95. */
  96. void format(int bits, int mode = 0);
  97. /** Set the spi bus clock frequency
  98. *
  99. * @param hz SCLK frequency in hz (default = 1MHz)
  100. */
  101. void frequency(int hz = 1000000);
  102. /** Write to the SPI Slave and return the response
  103. *
  104. * @param value Data to be sent to the SPI slave
  105. *
  106. * @returns
  107. * Response from the SPI slave
  108. */
  109. virtual int write(int value);
  110. /** Write to the SPI Slave and obtain the response
  111. *
  112. * The total number of bytes sent and received will be the maximum of
  113. * tx_length and rx_length. The bytes written will be padded with the
  114. * value 0xff.
  115. *
  116. * @param tx_buffer Pointer to the byte-array of data to write to the device
  117. * @param tx_length Number of bytes to write, may be zero
  118. * @param rx_buffer Pointer to the byte-array of data to read from the device
  119. * @param rx_length Number of bytes to read, may be zero
  120. * @returns
  121. * The number of bytes written and read from the device. This is
  122. * maximum of tx_length and rx_length.
  123. */
  124. virtual int write(const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length);
  125. /** Acquire exclusive access to this SPI bus
  126. */
  127. virtual void lock(void);
  128. /** Release exclusive access to this SPI bus
  129. */
  130. virtual void unlock(void);
  131. /** Set default write data
  132. * SPI requires the master to send some data during a read operation.
  133. * Different devices may require different default byte values.
  134. * For example: A SD Card requires default bytes to be 0xFF.
  135. *
  136. * @param data Default character to be transmitted while read operation
  137. */
  138. void set_default_write_value(char data);
  139. #if DEVICE_SPI_ASYNCH
  140. /** Start non-blocking SPI transfer using 8bit buffers.
  141. *
  142. * This function locks the deep sleep until any event has occurred
  143. *
  144. * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
  145. * the default SPI value is sent
  146. * @param tx_length The length of TX buffer in bytes
  147. * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
  148. * received data are ignored
  149. * @param rx_length The length of RX buffer in bytes
  150. * @param callback The event callback function
  151. * @param event The logical OR of events to modify. Look at spi hal header file for SPI events.
  152. * @return Zero if the transfer has started, or -1 if SPI peripheral is busy
  153. */
  154. template<typename Type>
  155. int transfer(const Type *tx_buffer, int tx_length, Type *rx_buffer, int rx_length, const event_callback_t &callback, int event = SPI_EVENT_COMPLETE)
  156. {
  157. if (spi_active(&_spi)) {
  158. return queue_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event);
  159. }
  160. start_transfer(tx_buffer, tx_length, rx_buffer, rx_length, sizeof(Type) * 8, callback, event);
  161. return 0;
  162. }
  163. /** Abort the on-going SPI transfer, and continue with transfer's in the queue if any.
  164. */
  165. void abort_transfer();
  166. /** Clear the transaction buffer
  167. */
  168. void clear_transfer_buffer();
  169. /** Clear the transaction buffer and abort on-going transfer.
  170. */
  171. void abort_all_transfers();
  172. /** Configure DMA usage suggestion for non-blocking transfers
  173. *
  174. * @param usage The usage DMA hint for peripheral
  175. * @return Zero if the usage was set, -1 if a transaction is on-going
  176. */
  177. int set_dma_usage(DMAUsage usage);
  178. protected:
  179. /** SPI IRQ handler
  180. *
  181. */
  182. void irq_handler_asynch(void);
  183. /** Common transfer method
  184. *
  185. * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
  186. * the default SPI value is sent
  187. * @param tx_length The length of TX buffer in bytes
  188. * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
  189. * received data are ignored
  190. * @param rx_length The length of RX buffer in bytes
  191. * @param bit_width The buffers element width
  192. * @param callback The event callback function
  193. * @param event The logical OR of events to modify
  194. * @return Zero if the transfer has started or was added to the queue, or -1 if SPI peripheral is busy/buffer is full
  195. */
  196. int 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);
  197. /**
  198. *
  199. * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
  200. * the default SPI value is sent
  201. * @param tx_length The length of TX buffer in bytes
  202. * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
  203. * received data are ignored
  204. * @param rx_length The length of RX buffer in bytes
  205. * @param bit_width The buffers element width
  206. * @param callback The event callback function
  207. * @param event The logical OR of events to modify
  208. * @return Zero if a transfer was added to the queue, or -1 if the queue is full
  209. */
  210. int 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);
  211. /** Configures a callback, spi peripheral and initiate a new transfer
  212. *
  213. * @param tx_buffer The TX buffer with data to be transfered. If NULL is passed,
  214. * the default SPI value is sent
  215. * @param tx_length The length of TX buffer in bytes
  216. * @param rx_buffer The RX buffer which is used for received data. If NULL is passed,
  217. * received data are ignored
  218. * @param rx_length The length of RX buffer in bytes
  219. * @param bit_width The buffers element width
  220. * @param callback The event callback function
  221. * @param event The logical OR of events to modify
  222. */
  223. void 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);
  224. private:
  225. /** Lock deep sleep only if it is not yet locked */
  226. void lock_deep_sleep();
  227. /** Unlock deep sleep in case it is locked */
  228. void unlock_deep_sleep();
  229. #if TRANSACTION_QUEUE_SIZE_SPI
  230. /** Start a new transaction
  231. *
  232. * @param data Transaction data
  233. */
  234. void start_transaction(transaction_t *data);
  235. /** Dequeue a transaction
  236. *
  237. */
  238. void dequeue_transaction();
  239. static CircularBuffer<Transaction<SPI>, TRANSACTION_QUEUE_SIZE_SPI> _transaction_buffer;
  240. #endif
  241. #endif
  242. public:
  243. virtual ~SPI()
  244. {
  245. }
  246. protected:
  247. spi_t _spi;
  248. #if DEVICE_SPI_ASYNCH
  249. CThunk<SPI> _irq;
  250. event_callback_t _callback;
  251. DMAUsage _usage;
  252. bool _deep_sleep_locked;
  253. #endif
  254. void aquire(void);
  255. static SPI *_owner;
  256. static SingletonPtr<PlatformMutex> _mutex;
  257. int _bits;
  258. int _mode;
  259. int _hz;
  260. char _write_fill;
  261. private:
  262. /* Private acquire function without locking/unlocking
  263. * Implemented in order to avoid duplicate locking and boost performance
  264. */
  265. void _acquire(void);
  266. };
  267. } // namespace mbed
  268. #endif
  269. #endif