Driver_SPI.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2013-2017 ARM Limited. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the License); you may
  7. * not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * 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, WITHOUT
  14. * 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. * $Date: 2. Feb 2017
  19. * $Revision: V2.2
  20. *
  21. * Project: SPI (Serial Peripheral Interface) Driver definitions
  22. */
  23. /* History:
  24. * Version 2.2
  25. * ARM_SPI_STATUS made volatile
  26. * Version 2.1
  27. * Renamed status flag "tx_rx_busy" to "busy"
  28. * Version 2.0
  29. * New simplified driver:
  30. * complexity moved to upper layer (especially data handling)
  31. * more unified API for different communication interfaces
  32. * Added:
  33. * Slave Mode
  34. * Half-duplex Modes
  35. * Configurable number of data bits
  36. * Support for TI Mode and Microwire
  37. * Changed prefix ARM_DRV -> ARM_DRIVER
  38. * Version 1.10
  39. * Namespace prefix ARM_ added
  40. * Version 1.01
  41. * Added "send_done_event" to Capabilities
  42. * Version 1.00
  43. * Initial release
  44. */
  45. #ifndef DRIVER_SPI_H_
  46. #define DRIVER_SPI_H_
  47. #ifdef __cplusplus
  48. extern "C"
  49. {
  50. #endif
  51. #include "Driver_Common.h"
  52. #define ARM_SPI_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,2) /* API version */
  53. /****** SPI Control Codes *****/
  54. #define ARM_SPI_CONTROL_Pos 0
  55. #define ARM_SPI_CONTROL_Msk (0xFFUL << ARM_SPI_CONTROL_Pos)
  56. /*----- SPI Control Codes: Mode -----*/
  57. #define ARM_SPI_MODE_INACTIVE (0x00UL << ARM_SPI_CONTROL_Pos) ///< SPI Inactive
  58. #define ARM_SPI_MODE_MASTER (0x01UL << ARM_SPI_CONTROL_Pos) ///< SPI Master (Output on MOSI, Input on MISO); arg = Bus Speed in bps
  59. #define ARM_SPI_MODE_SLAVE (0x02UL << ARM_SPI_CONTROL_Pos) ///< SPI Slave (Output on MISO, Input on MOSI)
  60. #define ARM_SPI_MODE_MASTER_SIMPLEX (0x03UL << ARM_SPI_CONTROL_Pos) ///< SPI Master (Output/Input on MOSI); arg = Bus Speed in bps
  61. #define ARM_SPI_MODE_SLAVE_SIMPLEX (0x04UL << ARM_SPI_CONTROL_Pos) ///< SPI Slave (Output/Input on MISO)
  62. /*----- SPI Control Codes: Mode Parameters: Frame Format -----*/
  63. #define ARM_SPI_FRAME_FORMAT_Pos 8
  64. #define ARM_SPI_FRAME_FORMAT_Msk (7UL << ARM_SPI_FRAME_FORMAT_Pos)
  65. #define ARM_SPI_CPOL0_CPHA0 (0UL << ARM_SPI_FRAME_FORMAT_Pos) ///< Clock Polarity 0, Clock Phase 0 (default)
  66. #define ARM_SPI_CPOL0_CPHA1 (1UL << ARM_SPI_FRAME_FORMAT_Pos) ///< Clock Polarity 0, Clock Phase 1
  67. #define ARM_SPI_CPOL1_CPHA0 (2UL << ARM_SPI_FRAME_FORMAT_Pos) ///< Clock Polarity 1, Clock Phase 0
  68. #define ARM_SPI_CPOL1_CPHA1 (3UL << ARM_SPI_FRAME_FORMAT_Pos) ///< Clock Polarity 1, Clock Phase 1
  69. #define ARM_SPI_TI_SSI (4UL << ARM_SPI_FRAME_FORMAT_Pos) ///< Texas Instruments Frame Format
  70. #define ARM_SPI_MICROWIRE (5UL << ARM_SPI_FRAME_FORMAT_Pos) ///< National Microwire Frame Format
  71. /*----- SPI Control Codes: Mode Parameters: Data Bits -----*/
  72. #define ARM_SPI_DATA_BITS_Pos 12
  73. #define ARM_SPI_DATA_BITS_Msk (0x3FUL << ARM_SPI_DATA_BITS_Pos)
  74. #define ARM_SPI_DATA_BITS(n) (((n) & 0x3F) << ARM_SPI_DATA_BITS_Pos) ///< Number of Data bits
  75. /*----- SPI Control Codes: Mode Parameters: Bit Order -----*/
  76. #define ARM_SPI_BIT_ORDER_Pos 18
  77. #define ARM_SPI_BIT_ORDER_Msk (1UL << ARM_SPI_BIT_ORDER_Pos)
  78. #define ARM_SPI_MSB_LSB (0UL << ARM_SPI_BIT_ORDER_Pos) ///< SPI Bit order from MSB to LSB (default)
  79. #define ARM_SPI_LSB_MSB (1UL << ARM_SPI_BIT_ORDER_Pos) ///< SPI Bit order from LSB to MSB
  80. /*----- SPI Control Codes: Mode Parameters: Slave Select Mode -----*/
  81. #define ARM_SPI_SS_MASTER_MODE_Pos 19
  82. #define ARM_SPI_SS_MASTER_MODE_Msk (3UL << ARM_SPI_SS_MASTER_MODE_Pos)
  83. #define ARM_SPI_SS_MASTER_UNUSED (0UL << ARM_SPI_SS_MASTER_MODE_Pos) ///< SPI Slave Select when Master: Not used (default)
  84. #define ARM_SPI_SS_MASTER_SW (1UL << ARM_SPI_SS_MASTER_MODE_Pos) ///< SPI Slave Select when Master: Software controlled
  85. #define ARM_SPI_SS_MASTER_HW_OUTPUT (2UL << ARM_SPI_SS_MASTER_MODE_Pos) ///< SPI Slave Select when Master: Hardware controlled Output
  86. #define ARM_SPI_SS_MASTER_HW_INPUT (3UL << ARM_SPI_SS_MASTER_MODE_Pos) ///< SPI Slave Select when Master: Hardware monitored Input
  87. #define ARM_SPI_SS_SLAVE_MODE_Pos 21
  88. #define ARM_SPI_SS_SLAVE_MODE_Msk (1UL << ARM_SPI_SS_SLAVE_MODE_Pos)
  89. #define ARM_SPI_SS_SLAVE_HW (0UL << ARM_SPI_SS_SLAVE_MODE_Pos) ///< SPI Slave Select when Slave: Hardware monitored (default)
  90. #define ARM_SPI_SS_SLAVE_SW (1UL << ARM_SPI_SS_SLAVE_MODE_Pos) ///< SPI Slave Select when Slave: Software controlled
  91. /*----- SPI Control Codes: Miscellaneous Controls -----*/
  92. #define ARM_SPI_SET_BUS_SPEED (0x10UL << ARM_SPI_CONTROL_Pos) ///< Set Bus Speed in bps; arg = value
  93. #define ARM_SPI_GET_BUS_SPEED (0x11UL << ARM_SPI_CONTROL_Pos) ///< Get Bus Speed in bps
  94. #define ARM_SPI_SET_DEFAULT_TX_VALUE (0x12UL << ARM_SPI_CONTROL_Pos) ///< Set default Transmit value; arg = value
  95. #define ARM_SPI_CONTROL_SS (0x13UL << ARM_SPI_CONTROL_Pos) ///< Control Slave Select; arg: 0=inactive, 1=active
  96. #define ARM_SPI_ABORT_TRANSFER (0x14UL << ARM_SPI_CONTROL_Pos) ///< Abort current data transfer
  97. /****** SPI Slave Select Signal definitions *****/
  98. #define ARM_SPI_SS_INACTIVE 0 ///< SPI Slave Select Signal Inactive
  99. #define ARM_SPI_SS_ACTIVE 1 ///< SPI Slave Select Signal Active
  100. /****** SPI specific error codes *****/
  101. #define ARM_SPI_ERROR_MODE (ARM_DRIVER_ERROR_SPECIFIC - 1) ///< Specified Mode not supported
  102. #define ARM_SPI_ERROR_FRAME_FORMAT (ARM_DRIVER_ERROR_SPECIFIC - 2) ///< Specified Frame Format not supported
  103. #define ARM_SPI_ERROR_DATA_BITS (ARM_DRIVER_ERROR_SPECIFIC - 3) ///< Specified number of Data bits not supported
  104. #define ARM_SPI_ERROR_BIT_ORDER (ARM_DRIVER_ERROR_SPECIFIC - 4) ///< Specified Bit order not supported
  105. #define ARM_SPI_ERROR_SS_MODE (ARM_DRIVER_ERROR_SPECIFIC - 5) ///< Specified Slave Select Mode not supported
  106. /**
  107. \brief SPI Status
  108. */
  109. typedef volatile struct _ARM_SPI_STATUS {
  110. uint32_t busy : 1; ///< Transmitter/Receiver busy flag
  111. uint32_t data_lost : 1; ///< Data lost: Receive overflow / Transmit underflow (cleared on start of transfer operation)
  112. uint32_t mode_fault : 1; ///< Mode fault detected; optional (cleared on start of transfer operation)
  113. uint32_t reserved : 29;
  114. } ARM_SPI_STATUS;
  115. /****** SPI Event *****/
  116. #define ARM_SPI_EVENT_TRANSFER_COMPLETE (1UL << 0) ///< Data Transfer completed
  117. #define ARM_SPI_EVENT_DATA_LOST (1UL << 1) ///< Data lost: Receive overflow / Transmit underflow
  118. #define ARM_SPI_EVENT_MODE_FAULT (1UL << 2) ///< Master Mode Fault (SS deactivated when Master)
  119. // Function documentation
  120. /**
  121. \fn ARM_DRIVER_VERSION ARM_SPI_GetVersion (void)
  122. \brief Get driver version.
  123. \return \ref ARM_DRIVER_VERSION
  124. \fn ARM_SPI_CAPABILITIES ARM_SPI_GetCapabilities (void)
  125. \brief Get driver capabilities.
  126. \return \ref ARM_SPI_CAPABILITIES
  127. \fn int32_t ARM_SPI_Initialize (ARM_SPI_SignalEvent_t cb_event)
  128. \brief Initialize SPI Interface.
  129. \param[in] cb_event Pointer to \ref ARM_SPI_SignalEvent
  130. \return \ref execution_status
  131. \fn int32_t ARM_SPI_Uninitialize (void)
  132. \brief De-initialize SPI Interface.
  133. \return \ref execution_status
  134. \fn int32_t ARM_SPI_PowerControl (ARM_POWER_STATE state)
  135. \brief Control SPI Interface Power.
  136. \param[in] state Power state
  137. \return \ref execution_status
  138. \fn int32_t ARM_SPI_Send (const void *data, uint32_t num)
  139. \brief Start sending data to SPI transmitter.
  140. \param[in] data Pointer to buffer with data to send to SPI transmitter
  141. \param[in] num Number of data items to send
  142. \return \ref execution_status
  143. \fn int32_t ARM_SPI_Receive (void *data, uint32_t num)
  144. \brief Start receiving data from SPI receiver.
  145. \param[out] data Pointer to buffer for data to receive from SPI receiver
  146. \param[in] num Number of data items to receive
  147. \return \ref execution_status
  148. \fn int32_t ARM_SPI_Transfer (const void *data_out,
  149. void *data_in,
  150. uint32_t num)
  151. \brief Start sending/receiving data to/from SPI transmitter/receiver.
  152. \param[in] data_out Pointer to buffer with data to send to SPI transmitter
  153. \param[out] data_in Pointer to buffer for data to receive from SPI receiver
  154. \param[in] num Number of data items to transfer
  155. \return \ref execution_status
  156. \fn uint32_t ARM_SPI_GetDataCount (void)
  157. \brief Get transferred data count.
  158. \return number of data items transferred
  159. \fn int32_t ARM_SPI_Control (uint32_t control, uint32_t arg)
  160. \brief Control SPI Interface.
  161. \param[in] control Operation
  162. \param[in] arg Argument of operation (optional)
  163. \return common \ref execution_status and driver specific \ref spi_execution_status
  164. \fn ARM_SPI_STATUS ARM_SPI_GetStatus (void)
  165. \brief Get SPI status.
  166. \return SPI status \ref ARM_SPI_STATUS
  167. \fn void ARM_SPI_SignalEvent (uint32_t event)
  168. \brief Signal SPI Events.
  169. \param[in] event \ref SPI_events notification mask
  170. \return none
  171. */
  172. typedef void (*ARM_SPI_SignalEvent_t) (uint32_t event); ///< Pointer to \ref ARM_SPI_SignalEvent : Signal SPI Event.
  173. /**
  174. \brief SPI Driver Capabilities.
  175. */
  176. typedef struct _ARM_SPI_CAPABILITIES {
  177. uint32_t simplex : 1; ///< supports Simplex Mode (Master and Slave)
  178. uint32_t ti_ssi : 1; ///< supports TI Synchronous Serial Interface
  179. uint32_t microwire : 1; ///< supports Microwire Interface
  180. uint32_t event_mode_fault : 1; ///< Signal Mode Fault event: \ref ARM_SPI_EVENT_MODE_FAULT
  181. uint32_t reserved : 28; ///< Reserved (must be zero)
  182. } ARM_SPI_CAPABILITIES;
  183. /**
  184. \brief Access structure of the SPI Driver.
  185. */
  186. typedef struct _ARM_DRIVER_SPI {
  187. ARM_DRIVER_VERSION (*GetVersion) (void); ///< Pointer to \ref ARM_SPI_GetVersion : Get driver version.
  188. ARM_SPI_CAPABILITIES (*GetCapabilities) (void); ///< Pointer to \ref ARM_SPI_GetCapabilities : Get driver capabilities.
  189. int32_t (*Initialize) (ARM_SPI_SignalEvent_t cb_event); ///< Pointer to \ref ARM_SPI_Initialize : Initialize SPI Interface.
  190. int32_t (*Uninitialize) (void); ///< Pointer to \ref ARM_SPI_Uninitialize : De-initialize SPI Interface.
  191. int32_t (*PowerControl) (ARM_POWER_STATE state); ///< Pointer to \ref ARM_SPI_PowerControl : Control SPI Interface Power.
  192. int32_t (*Send) (const void *data, uint32_t num); ///< Pointer to \ref ARM_SPI_Send : Start sending data to SPI Interface.
  193. int32_t (*Receive) ( void *data, uint32_t num); ///< Pointer to \ref ARM_SPI_Receive : Start receiving data from SPI Interface.
  194. int32_t (*Transfer) (const void *data_out,
  195. void *data_in,
  196. uint32_t num); ///< Pointer to \ref ARM_SPI_Transfer : Start sending/receiving data to/from SPI.
  197. uint32_t (*GetDataCount) (void); ///< Pointer to \ref ARM_SPI_GetDataCount : Get transferred data count.
  198. int32_t (*Control) (uint32_t control, uint32_t arg); ///< Pointer to \ref ARM_SPI_Control : Control SPI Interface.
  199. ARM_SPI_STATUS (*GetStatus) (void); ///< Pointer to \ref ARM_SPI_GetStatus : Get SPI status.
  200. } const ARM_DRIVER_SPI;
  201. #ifdef __cplusplus
  202. }
  203. #endif
  204. #endif /* DRIVER_SPI_H_ */