drv_iic.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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_iic.h
  18. * @brief header file for iic driver
  19. * @version V1.0
  20. * @date 02. June 2017
  21. ******************************************************************************/
  22. #ifndef _CSI_IIC_H_
  23. #define _CSI_IIC_H_
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. #include <stdint.h>
  28. #include <stdbool.h>
  29. #include <drv_common.h>
  30. /// definition for iic handle.
  31. typedef void *iic_handle_t;
  32. /*----- IIC Control Codes: Mode -----*/
  33. typedef enum {
  34. IIC_MODE_MASTER, ///< IIC Master
  35. IIC_MODE_SLAVE ///< IIC Slave
  36. } iic_mode_e;
  37. /*----- IIC Control Codes: IIC Bus Speed -----*/
  38. typedef enum {
  39. I2C_BUS_SPEED_STANDARD = 0, ///< Standard Speed (100kHz)
  40. I2C_BUS_SPEED_FAST = 1, ///< Fast Speed (400kHz)
  41. I2C_BUS_SPEED_FAST_PLUS = 2, ///< Fast+ Speed ( 1MHz)
  42. I2C_BUS_SPEED_HIGH = 3 ///< High Speed (3.4MHz)
  43. } iic_speed_e;
  44. /*----- IIC Control Codes: IIC Address Mode -----*/
  45. typedef enum {
  46. I2C_ADDRESS_7BIT = 0, ///< 7-bit address mode
  47. I2C_ADDRESS_10BIT = 1 ///< 10-bit address mode
  48. } iic_address_mode_e;
  49. /**
  50. \brief IIC Status
  51. */
  52. typedef struct {
  53. uint32_t busy : 1; ///< Transmitter/Receiver busy flag
  54. uint32_t mode : 1; ///< Mode: 0=Slave, 1=Master
  55. uint32_t direction : 1; ///< Direction: 0=Transmitter, 1=Receiver
  56. uint32_t general_call : 1; ///< General Call(address 0) indication (cleared on start of next Slave operation)
  57. uint32_t arbitration_lost : 1; ///< Master lost arbitration(in case of multi-masters) (cleared on start of next Master operation)
  58. uint32_t bus_error : 1; ///< Bus error detected (cleared on start of next Master/Slave operation)
  59. } iic_status_t;
  60. /****** IIC Event *****/
  61. typedef enum {
  62. I2C_EVENT_TRANSFER_DONE = 0, ///< Master/Slave Transmit/Receive finished
  63. I2C_EVENT_TRANSFER_INCOMPLETE = 1, ///< Master/Slave Transmit/Receive incomplete transfer
  64. I2C_EVENT_SLAVE_TRANSMIT = 2, ///< Slave Transmit operation requested
  65. I2C_EVENT_SLAVE_RECEIVE = 3, ///< Slave Receive operation requested
  66. I2C_EVENT_ADDRESS_NACK = 4, ///< Address not acknowledged from Slave
  67. I2C_EVENT_GENERAL_CALL = 5, ///< General Call indication
  68. I2C_EVENT_ARBITRATION_LOST = 6, ///< Master lost arbitration
  69. I2C_EVENT_BUS_ERROR = 7, ///< Bus error detected (START/STOP at illegal position)
  70. I2C_EVENT_BUS_CLEAR = 8 ///< Bus clear finished
  71. } iic_event_e;
  72. typedef void (*iic_event_cb_t)(iic_event_e event, void *arg); ///< Pointer to \ref iic_event_cb_t : IIC Event call back.
  73. /**
  74. \brief IIC Driver Capabilities.
  75. */
  76. typedef struct {
  77. uint32_t address_10_bit : 1; ///< supports 10-bit addressing
  78. } iic_capabilities_t;
  79. /**
  80. \brief Initialize IIC Interface specified by pins. \n
  81. 1. Initializes the resources needed for the IIC interface 2.registers event callback function
  82. \param[in] scl scl pin of iic.
  83. \param[in] sda sda pin of iic.
  84. \param[in] cb_event Pointer to \ref iic_event_cb_t
  85. \param[in] cb_arg argument for call back function
  86. \return 0 for success, negative for error code
  87. */
  88. iic_handle_t csi_iic_initialize(pin_t scl, pin_t sda, iic_event_cb_t cb_event, void *cb_arg);
  89. /**
  90. \brief De-initialize IIC Interface. stops operation and releases the software resources used by the interface
  91. \param[in] handle iic handle to operate.
  92. \return 0 for success, negative for error code
  93. */
  94. int32_t csi_iic_uninitialize(iic_handle_t handle);
  95. /**
  96. \brief Get driver capabilities.
  97. \param[in] handle iic handle to operate.
  98. \return \ref iic_capabilities_t
  99. */
  100. iic_capabilities_t csi_iic_get_capabilities(iic_handle_t handle);
  101. /**
  102. \brief config iic attributes.
  103. \param[in] handle iic handle to operate.
  104. \param[in] mode iic mode \ref iic_mode_e. if negative, then this attribute not changed.
  105. \param[in] speed iic speed \ref iic_speed_e. if negative, then this attribute not changed.
  106. \param[in] addr_mode iic address mode \ref iic_address_mode_e. if negative, then this attribute not changed.
  107. \param[in] slave_addr iic address of slave. if negative, then this attribute not changed.
  108. \return 0 for success, negative for error code
  109. */
  110. int32_t csi_iic_config(iic_handle_t handle,
  111. iic_mode_e mode,
  112. iic_speed_e speed,
  113. iic_address_mode_e addr_mode,
  114. int32_t slave_addr);
  115. /**
  116. \brief Start transmitting data as I2C Master.
  117. This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
  118. \ref csi_iic_get_status can indicates transmission status.
  119. \param[in] handle iic handle to operate.
  120. \param[in] data data to send to I2C Slave
  121. \param[in] num Number of data items to send
  122. \param[in] xfer_pending Transfer operation is pending - Stop condition will not be generated
  123. \return 0 for success, negative for error code
  124. */
  125. int32_t csi_iic_master_send(iic_handle_t handle, const void *data, uint32_t num, bool xfer_pending);
  126. /**
  127. \brief Start receiving data as I2C Master.
  128. This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
  129. \ref csi_iic_get_status can indicates transmission status.
  130. \param[in] handle iic handle to operate.
  131. \param[out] data Pointer to buffer for data to receive from IIC receiver
  132. \param[in] num Number of data items to receive
  133. \param[in] xfer_pending Transfer operation is pending - Stop condition will not be generated
  134. \return 0 for success, negative for error code
  135. */
  136. int32_t csi_iic_master_receive(iic_handle_t handle, const void *data, uint32_t num, bool xfer_pending);
  137. /**
  138. \brief Start transmitting data as I2C Slave.
  139. This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
  140. \ref csi_iic_get_status can indicates transmission status.
  141. \param[in] handle iic handle to operate.
  142. \param[in] data Pointer to buffer with data to transmit to I2C Master
  143. \param[in] num Number of data items to send
  144. \return 0 for success, negative for error code
  145. */
  146. int32_t csi_iic_slave_send(iic_handle_t handle, const void *data, uint32_t num);
  147. /**
  148. \brief Start receiving data as I2C Slave.
  149. This function is non-blocking,\ref iic_event_e is signaled when transfer completes or error happens.
  150. \ref csi_iic_get_status can indicates transmission status.
  151. \param[in] handle iic handle to operate.
  152. \param[out] data Pointer to buffer for data to receive from I2C Master
  153. \param[in] num Number of data items to receive
  154. \return 0 for success, negative for error code
  155. */
  156. int32_t csi_iic_slave_receive(iic_handle_t handle, const void *data, uint32_t num);
  157. /**
  158. \brief abort transfer.
  159. \param[in] handle iic handle to operate.
  160. \return 0 for success, negative for error code
  161. */
  162. int32_t csi_iic_abort_transfer(iic_handle_t handle);
  163. /**
  164. \brief Get IIC status.
  165. \param[in] handle iic handle to operate.
  166. \return IIC status \ref iic_status_t
  167. */
  168. iic_status_t csi_iic_get_status(iic_handle_t handle);
  169. #ifdef __cplusplus
  170. }
  171. #endif
  172. #endif /* _CSI_IIC_H_ */