spi_api.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /** \addtogroup hal */
  2. /** @{*/
  3. /* mbed Microcontroller Library
  4. * Copyright (c) 2006-2013 ARM Limited
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #ifndef MBED_SPI_API_H
  19. #define MBED_SPI_API_H
  20. #include "device.h"
  21. #include "hal/dma_api.h"
  22. #include "hal/buffer.h"
  23. #if DEVICE_SPI
  24. #define SPI_EVENT_ERROR (1 << 1)
  25. #define SPI_EVENT_COMPLETE (1 << 2)
  26. #define SPI_EVENT_RX_OVERFLOW (1 << 3)
  27. #define SPI_EVENT_ALL (SPI_EVENT_ERROR | SPI_EVENT_COMPLETE | SPI_EVENT_RX_OVERFLOW)
  28. #define SPI_EVENT_INTERNAL_TRANSFER_COMPLETE (1 << 30) // Internal flag to report that an event occurred
  29. #define SPI_FILL_WORD (0xFFFF)
  30. #define SPI_FILL_CHAR (0xFF)
  31. #if DEVICE_SPI_ASYNCH
  32. /** Asynch SPI HAL structure
  33. */
  34. typedef struct {
  35. struct spi_s spi; /**< Target specific SPI structure */
  36. struct buffer_s tx_buff; /**< Tx buffer */
  37. struct buffer_s rx_buff; /**< Rx buffer */
  38. } spi_t;
  39. #else
  40. /** Non-asynch SPI HAL structure
  41. */
  42. typedef struct spi_s spi_t;
  43. #endif
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. /**
  48. * \defgroup hal_GeneralSPI SPI Configuration Functions
  49. * @{
  50. */
  51. /** Initialize the SPI peripheral
  52. *
  53. * Configures the pins used by SPI, sets a default format and frequency, and enables the peripheral
  54. * @param[out] obj The SPI object to initialize
  55. * @param[in] mosi The pin to use for MOSI
  56. * @param[in] miso The pin to use for MISO
  57. * @param[in] sclk The pin to use for SCLK
  58. * @param[in] ssel The pin to use for SSEL
  59. */
  60. void spi_init(spi_t *obj, PinName mosi, PinName miso, PinName sclk, PinName ssel);
  61. /** Release a SPI object
  62. *
  63. * TODO: spi_free is currently unimplemented
  64. * This will require reference counting at the C++ level to be safe
  65. *
  66. * Return the pins owned by the SPI object to their reset state
  67. * Disable the SPI peripheral
  68. * Disable the SPI clock
  69. * @param[in] obj The SPI object to deinitialize
  70. */
  71. void spi_free(spi_t *obj);
  72. /** Configure the SPI format
  73. *
  74. * Set the number of bits per frame, configure clock polarity and phase, shift order and master/slave mode.
  75. * The default bit order is MSB.
  76. * @param[in,out] obj The SPI object to configure
  77. * @param[in] bits The number of bits per frame
  78. * @param[in] mode The SPI mode (clock polarity, phase, and shift direction)
  79. * @param[in] slave Zero for master mode or non-zero for slave mode
  80. */
  81. void spi_format(spi_t *obj, int bits, int mode, int slave);
  82. /** Set the SPI baud rate
  83. *
  84. * Actual frequency may differ from the desired frequency due to available dividers and bus clock
  85. * Configures the SPI peripheral's baud rate
  86. * @param[in,out] obj The SPI object to configure
  87. * @param[in] hz The baud rate in Hz
  88. */
  89. void spi_frequency(spi_t *obj, int hz);
  90. /**@}*/
  91. /**
  92. * \defgroup SynchSPI Synchronous SPI Hardware Abstraction Layer
  93. * @{
  94. */
  95. /** Write a byte out in master mode and receive a value
  96. *
  97. * @param[in] obj The SPI peripheral to use for sending
  98. * @param[in] value The value to send
  99. * @return Returns the value received during send
  100. */
  101. int spi_master_write(spi_t *obj, int value);
  102. /** Write a block out in master mode and receive a value
  103. *
  104. * The total number of bytes sent and received will be the maximum of
  105. * tx_length and rx_length. The bytes written will be padded with the
  106. * value 0xff.
  107. *
  108. * @param[in] obj The SPI peripheral to use for sending
  109. * @param[in] tx_buffer Pointer to the byte-array of data to write to the device
  110. * @param[in] tx_length Number of bytes to write, may be zero
  111. * @param[in] rx_buffer Pointer to the byte-array of data to read from the device
  112. * @param[in] rx_length Number of bytes to read, may be zero
  113. * @param[in] write_fill Default data transmitted while performing a read
  114. * @returns
  115. * The number of bytes written and read from the device. This is
  116. * maximum of tx_length and rx_length.
  117. */
  118. int spi_master_block_write(spi_t *obj, const char *tx_buffer, int tx_length, char *rx_buffer, int rx_length, char write_fill);
  119. /** Check if a value is available to read
  120. *
  121. * @param[in] obj The SPI peripheral to check
  122. * @return non-zero if a value is available
  123. */
  124. int spi_slave_receive(spi_t *obj);
  125. /** Get a received value out of the SPI receive buffer in slave mode
  126. *
  127. * Blocks until a value is available
  128. * @param[in] obj The SPI peripheral to read
  129. * @return The value received
  130. */
  131. int spi_slave_read(spi_t *obj);
  132. /** Write a value to the SPI peripheral in slave mode
  133. *
  134. * Blocks until the SPI peripheral can be written to
  135. * @param[in] obj The SPI peripheral to write
  136. * @param[in] value The value to write
  137. */
  138. void spi_slave_write(spi_t *obj, int value);
  139. /** Checks if the specified SPI peripheral is in use
  140. *
  141. * @param[in] obj The SPI peripheral to check
  142. * @return non-zero if the peripheral is currently transmitting
  143. */
  144. int spi_busy(spi_t *obj);
  145. /** Get the module number
  146. *
  147. * @param[in] obj The SPI peripheral to check
  148. * @return The module number
  149. */
  150. uint8_t spi_get_module(spi_t *obj);
  151. /**@}*/
  152. #if DEVICE_SPI_ASYNCH
  153. /**
  154. * \defgroup AsynchSPI Asynchronous SPI Hardware Abstraction Layer
  155. * @{
  156. */
  157. /** Begin the SPI transfer. Buffer pointers and lengths are specified in tx_buff and rx_buff
  158. *
  159. * @param[in] obj The SPI object that holds the transfer information
  160. * @param[in] tx The transmit buffer
  161. * @param[in] tx_length The number of bytes to transmit
  162. * @param[in] rx The receive buffer
  163. * @param[in] rx_length The number of bytes to receive
  164. * @param[in] bit_width The bit width of buffer words
  165. * @param[in] event The logical OR of events to be registered
  166. * @param[in] handler SPI interrupt handler
  167. * @param[in] hint A suggestion for how to use DMA with this transfer
  168. */
  169. void spi_master_transfer(spi_t *obj, const void *tx, size_t tx_length, void *rx, size_t rx_length, uint8_t bit_width, uint32_t handler, uint32_t event, DMAUsage hint);
  170. /** The asynchronous IRQ handler
  171. *
  172. * Reads the received values out of the RX FIFO, writes values into the TX FIFO and checks for transfer termination
  173. * conditions, such as buffer overflows or transfer complete.
  174. * @param[in] obj The SPI object that holds the transfer information
  175. * @return Event flags if a transfer termination condition was met; otherwise 0.
  176. */
  177. uint32_t spi_irq_handler_asynch(spi_t *obj);
  178. /** Attempts to determine if the SPI peripheral is already in use
  179. *
  180. * If a temporary DMA channel has been allocated, peripheral is in use.
  181. * If a permanent DMA channel has been allocated, check if the DMA channel is in use. If not, proceed as though no DMA
  182. * channel were allocated.
  183. * If no DMA channel is allocated, check whether tx and rx buffers have been assigned. For each assigned buffer, check
  184. * if the corresponding buffer position is less than the buffer length. If buffers do not indicate activity, check if
  185. * there are any bytes in the FIFOs.
  186. * @param[in] obj The SPI object to check for activity
  187. * @return Non-zero if the SPI port is active or zero if it is not.
  188. */
  189. uint8_t spi_active(spi_t *obj);
  190. /** Abort an SPI transfer
  191. *
  192. * @param obj The SPI peripheral to stop
  193. */
  194. void spi_abort_asynch(spi_t *obj);
  195. #endif
  196. /**@}*/
  197. #ifdef __cplusplus
  198. }
  199. #endif // __cplusplus
  200. #endif // SPI_DEVICE
  201. #endif // MBED_SPI_API_H
  202. /** @}*/