drv_spi.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. /*
  2. * Copyright (C) 2017 C-SKY Microsystems Co., Ltd. All rights reserved.
  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. /******************************************************************************
  17. * @file drv_spi.h
  18. * @brief header file for spi driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_SPI_H_
  23. #define _CSI_SPI_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <drv_common.h>
  29. #include <config.h>
  30. #ifdef CONFIG_SPI_DMA
  31. #include <drv_dmac.h>
  32. #endif
  33. /// definition for spi handle.
  34. typedef void *spi_handle_t;
  35. /****** SPI specific error codes *****/
  36. typedef enum {
  37. EDRV_SPI_MODE = (EDRV_SPECIFIC + 1), ///< Specified Mode not supported
  38. EDRV_SPI_FRAME_FORMAT, ///< Specified Frame Format not supported
  39. EDRV_SPI_DATA_BITS, ///< Specified number of Data bits not supported
  40. EDRV_SPI_BIT_ORDER, ///< Specified Bit order not supported
  41. EDRV_SPI_SS_MODE ///< Specified Slave Select Mode not supported
  42. } drv_spi_err_e;
  43. /*----- SPI Control Codes: Mode -----*/
  44. typedef enum {
  45. SPI_MODE_INACTIVE = 0, ///< SPI Inactive
  46. SPI_MODE_MASTER, ///< SPI Master (Output on MOSI, Input on MISO); arg = Bus Speed in bps
  47. SPI_MODE_SLAVE, ///< SPI Slave (Output on MISO, Input on MOSI)
  48. SPI_MODE_MASTER_SIMPLEX, ///< SPI Master (Output/Input on MOSI); arg = Bus Speed in bps
  49. SPI_MODE_SLAVE_SIMPLEX ///< SPI Slave (Output/Input on MISO)
  50. } spi_mode_e;
  51. /*----- SPI Control Codes: Mode Parameters: Frame Format -----*/
  52. typedef enum {
  53. SPI_FORMAT_CPOL0_CPHA0 = 0, ///< Clock Polarity 0, Clock Phase 0 (default)
  54. SPI_FORMAT_CPOL0_CPHA1, ///< Clock Polarity 0, Clock Phase 1
  55. SPI_FORMAT_CPOL1_CPHA0, ///< Clock Polarity 1, Clock Phase 0
  56. SPI_FORMAT_CPOL1_CPHA1, ///< Clock Polarity 1, Clock Phase 1
  57. } spi_format_e;
  58. /*----- SPI Control Codes: Mode Parameters: Bit Order -----*/
  59. typedef enum {
  60. SPI_ORDER_MSB2LSB = 0, ///< SPI Bit order from MSB to LSB (default)
  61. SPI_ORDER_LSB2MSB ///< SPI Bit order from LSB to MSB
  62. } spi_bit_order_e;
  63. /*----- SPI Control Codes: Mode Parameters: Data Width in bits -----*/
  64. #define SPI_DATAWIDTH_MAX 32 /* 1 ~ 32 bit*/
  65. /*----- SPI Control Codes: Mode Parameters: Slave Select Mode -----*/
  66. typedef enum {
  67. /*options for SPI_MODE_MASTER/SPI_MODE_MASTER_SIMPLEX */
  68. SPI_SS_MASTER_UNUSED = 0, ///< SPI Slave Select when Master: Not used (default).SS line is not controlled by master, For example,SS line connected to a fixed low level
  69. SPI_SS_MASTER_SW, ///< SPI Slave Select when Master: Software controlled. SS line is configured by software
  70. SPI_SS_MASTER_HW_OUTPUT, ///< SPI Slave Select when Master: Hardware controlled Output.SS line is activated or deactivated automatically by hardware
  71. SPI_SS_MASTER_HW_INPUT, ///< SPI Slave Select when Master: Hardware monitored Input.Used in multi-master configuration where a master does not drive the Slave Select when driving the bus, but rather monitors it
  72. /*options for SPI_MODE_SLAVE/SPI_MODE_SLAVE_SIMPLEX */
  73. SPI_SS_SLAVE_HW, ///< SPI Slave Select when Slave: Hardware monitored (default).Hardware monitors the Slave Select line and accepts transfers only when the line is active
  74. SPI_SS_SLAVE_SW ///< SPI Slave Select when Slave: Software controlled.Used only when the Slave Select line is not used. Software controls if the slave is responding or not(enables or disables transfers)
  75. } spi_ss_mode_e;
  76. /****** SPI Slave Select Signal definitions *****/
  77. typedef enum {
  78. SPI_SS_INACTIVE = 0, ///< SPI Slave Select Signal/line Inactive
  79. SPI_SS_ACTIVE ///< SPI Slave Select Signal/line Active
  80. } spi_ss_stat_e;
  81. /**
  82. \brief SPI Status
  83. */
  84. typedef struct {
  85. uint32_t busy : 1; ///< Transmitter/Receiver busy flag
  86. uint32_t data_lost : 1; ///< Data lost: Receive overflow / Transmit underflow (cleared on start of transfer operation)
  87. uint32_t mode_fault : 1; ///< Mode fault detected; optional (cleared on start of transfer operation)
  88. } spi_status_t;
  89. /****** SPI Event *****/
  90. typedef enum {
  91. SPI_EVENT_TRANSFER_COMPLETE = 0, ///< Data Transfer completed. Occurs after call to ARM_SPI_Send, ARM_SPI_Receive, or ARM_SPI_Transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
  92. SPI_EVENT_TX_COMPLETE, ///< Data Transfer completed. Occurs after call to ARM_SPI_Send, ARM_SPI_Receive, or ARM_SPI_Transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
  93. SPI_EVENT_RX_COMPLETE, ///< Data Transfer completed. Occurs after call to ARM_SPI_Send, ARM_SPI_Receive, or ARM_SPI_Transfer to indicate that all the data has been transferred. The driver is ready for the next transfer operation
  94. SPI_EVENT_DATA_LOST, ///< Data lost: Receive overflow / Transmit underflow. Occurs in slave mode when data is requested/sent by master but send/receive/transfer operation has not been started and indicates that data is lost. Occurs also in master mode when driver cannot transfer data fast enough.
  95. SPI_EVENT_MODE_FAULT ///< Master Mode Fault (SS deactivated when Master).Occurs in master mode when Slave Select is deactivated and indicates Master Mode Fault. The driver is ready for the next transfer operation.
  96. } spi_event_e;
  97. typedef void (*spi_event_cb_t)(spi_event_e event, void *arg); ///< Pointer to \ref spi_event_cb_t : SPI Event call back.
  98. /**
  99. \brief SPI Driver Capabilities.
  100. */
  101. typedef struct {
  102. uint32_t simplex : 1; ///< supports Simplex Mode (Master and Slave)
  103. uint32_t ti_ssi : 1; ///< supports TI Synchronous Serial Interface
  104. uint32_t microwire : 1; ///< supports Microwire Interface
  105. uint32_t event_mode_fault : 1; ///< Signal Mode Fault event: \ref spi_event_e
  106. } spi_capabilities_t;
  107. /**
  108. \brief Initialize SPI Interface. 1. Initializes the resources needed for the SPI interface 2.registers event callback function
  109. \param[in] mosi spi pin of mosi
  110. \param[in] miso spi pin of miso
  111. \param[in] sclk spi pin of sclk
  112. \param[in] ssel spi pin of ssel
  113. \param[in] cb_event event call back function \ref spi_event_cb_t
  114. \param[in] cb_arg argument for call back function
  115. \return return spi handle if success
  116. */
  117. spi_handle_t csi_spi_initialize(pin_t mosi, pin_t miso, pin_t sclk, pin_t ssel, spi_event_cb_t cb_event, void *cb_arg);
  118. /**
  119. \brief De-initialize SPI Interface. stops operation and releases the software resources used by the interface
  120. \param[in] handle spi handle to operate.
  121. \return error code
  122. */
  123. int32_t csi_spi_uninitialize(spi_handle_t handle);
  124. /**
  125. \brief Get driver capabilities.
  126. \param[in] handle spi handle to operate.
  127. \return \ref spi_capabilities_t
  128. */
  129. spi_capabilities_t csi_spi_get_capabilities(spi_handle_t handle);
  130. /**
  131. \brief config spi mode.
  132. \param[in] handle spi handle to operate.
  133. \param[in] sysclk sysclk for spi module.
  134. \param[in] baud spi baud rate. if negative, then this attribute not changed
  135. \param[in] mode \ref spi_mode_e . if negative, then this attribute not changed
  136. \param[in] format \ref spi_format_e . if negative, then this attribute not changed
  137. \param[in] order \ref spi_bit_order_e . if negative, then this attribute not changed
  138. \param[in] ss_mode \ref spi_ss_mode_e . if negative, then this attribute not changed
  139. \param[in] bit_width spi data bitwidth: (1 ~ SPI_DATAWIDTH_MAX) . if negative, then this attribute not changed
  140. \return error code
  141. */
  142. int32_t csi_spi_config(spi_handle_t handle,
  143. int32_t sysclk,
  144. int32_t baud,
  145. spi_mode_e mode,
  146. spi_format_e format,
  147. spi_bit_order_e order,
  148. spi_ss_mode_e ss_mode,
  149. int32_t bit_width);
  150. /**
  151. \brief config spi default tx value.
  152. \param[in] handle spi handle to operate.
  153. \param[in] value default tx value
  154. \return error code
  155. */
  156. int32_t csi_spi_set_default_tx_value(spi_handle_t handle, uint32_t value);
  157. /**
  158. \brief sending data to SPI transmitter,(received data is ignored).
  159. if non-blocking mode, this function only start the sending,
  160. \ref spi_event_e is signaled when operation completes or error happens.
  161. \ref csi_spi_get_status can indicates operation status.
  162. if blocking mode, this function return after operation completes or error happens.
  163. \param[in] handle spi handle to operate.
  164. \param[in] data Pointer to buffer with data to send to SPI transmitter. data_type is : uint8_t for 1..8 data bits, uint16_t for 9..16 data bits,uint32_t for 17..32 data bits,
  165. \param[in] num Number of data items to send.
  166. \param[in] block_mode blocking and non_blocking to selcect
  167. \return error code
  168. */
  169. int32_t csi_spi_send(spi_handle_t handle, const void *data, uint32_t num, uint8_t block_mode);
  170. /**
  171. \brief receiving data from SPI receiver.transmits the default value as specified by csi_spi_set_default_tx_value
  172. if non-blocking mode, this function only start the receiving,
  173. \ref spi_event_e is signaled when operation completes or error happens.
  174. \ref csi_spi_get_status can indicates operation status.
  175. if blocking mode, this function return after operation completes or error happens.
  176. \param[in] handle spi handle to operate.
  177. \param[out] data Pointer to buffer for data to receive from SPI receiver
  178. \param[in] num Number of data items to receive
  179. \param[in] block_mode blocking and non_blocking to selcect
  180. \return error code
  181. */
  182. int32_t csi_spi_receive(spi_handle_t handle, void *data, uint32_t num, uint8_t block_mode);
  183. /**
  184. \brief sending/receiving data to/from SPI transmitter/receiver.
  185. if non-blocking mode, this function only start the transfer,
  186. \ref spi_event_e is signaled when operation completes or error happens.
  187. \ref csi_spi_get_status can indicates operation status.
  188. if blocking mode, this function return after operation completes or error happens.
  189. \param[in] handle spi handle to operate.
  190. \param[in] data_out Pointer to buffer with data to send to SPI transmitter
  191. \param[out] data_in Pointer to buffer for data to receive from SPI receiver
  192. \param[in] num_out Number of data items to send
  193. \param[in] num_in Number of data items to receive
  194. \param[in] block_mode blocking and non_blocking to selcect
  195. \return error code
  196. */
  197. int32_t csi_spi_transfer(spi_handle_t handle, const void *data_out, void *data_in, uint32_t num_out, uint32_t num_in, uint8_t block_mode);
  198. /**
  199. \brief abort spi transfer.
  200. \param[in] handle spi handle to operate.
  201. \return error code
  202. */
  203. int32_t csi_spi_abort_transfer(spi_handle_t handle);
  204. /**
  205. \brief Get SPI status.
  206. \param[in] handle spi handle to operate.
  207. \return SPI status \ref spi_status_t
  208. */
  209. spi_status_t csi_spi_get_status(spi_handle_t handle);
  210. #ifdef __cplusplus
  211. }
  212. #endif
  213. #endif /* _CSI_SPI_H_ */