Driver_I2C.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (c) 2013-2020 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: 31. March 2020
  19. * $Revision: V2.4
  20. *
  21. * Project: I2C (Inter-Integrated Circuit) Driver definitions
  22. */
  23. /* History:
  24. * Version 2.4
  25. * Removed volatile from ARM_I2C_STATUS
  26. * Version 2.3
  27. * ARM_I2C_STATUS made volatile
  28. * Version 2.2
  29. * Removed function ARM_I2C_MasterTransfer in order to simplify drivers
  30. * and added back parameter "xfer_pending" to functions
  31. * ARM_I2C_MasterTransmit and ARM_I2C_MasterReceive
  32. * Version 2.1
  33. * Added function ARM_I2C_MasterTransfer and removed parameter "xfer_pending"
  34. * from functions ARM_I2C_MasterTransmit and ARM_I2C_MasterReceive
  35. * Added function ARM_I2C_GetDataCount
  36. * Removed flag "address_nack" from ARM_I2C_STATUS
  37. * Replaced events ARM_I2C_EVENT_MASTER_DONE and ARM_I2C_EVENT_SLAVE_DONE
  38. * with event ARM_I2C_EVENT_TRANSFER_DONE
  39. * Added event ARM_I2C_EVENT_TRANSFER_INCOMPLETE
  40. * Removed parameter "arg" from function ARM_I2C_SignalEvent
  41. * Version 2.0
  42. * New simplified driver:
  43. * complexity moved to upper layer (especially data handling)
  44. * more unified API for different communication interfaces
  45. * Added:
  46. * Slave Mode
  47. * Changed prefix ARM_DRV -> ARM_DRIVER
  48. * Version 1.10
  49. * Namespace prefix ARM_ added
  50. * Version 1.00
  51. * Initial release
  52. */
  53. #ifndef DRIVER_I2C_H_
  54. #define DRIVER_I2C_H_
  55. #ifdef __cplusplus
  56. extern "C"
  57. {
  58. #endif
  59. #include "Driver_Common.h"
  60. #define ARM_I2C_API_VERSION ARM_DRIVER_VERSION_MAJOR_MINOR(2,4) /* API version */
  61. #define _ARM_Driver_I2C_(n) Driver_I2C##n
  62. #define ARM_Driver_I2C_(n) _ARM_Driver_I2C_(n)
  63. /****** I2C Control Codes *****/
  64. #define ARM_I2C_OWN_ADDRESS (0x01UL) ///< Set Own Slave Address; arg = address
  65. #define ARM_I2C_BUS_SPEED (0x02UL) ///< Set Bus Speed; arg = speed
  66. #define ARM_I2C_BUS_CLEAR (0x03UL) ///< Execute Bus clear: send nine clock pulses
  67. #define ARM_I2C_ABORT_TRANSFER (0x04UL) ///< Abort Master/Slave Transmit/Receive
  68. /*----- I2C Bus Speed -----*/
  69. #define ARM_I2C_BUS_SPEED_STANDARD (0x01UL) ///< Standard Speed (100kHz)
  70. #define ARM_I2C_BUS_SPEED_FAST (0x02UL) ///< Fast Speed (400kHz)
  71. #define ARM_I2C_BUS_SPEED_FAST_PLUS (0x03UL) ///< Fast+ Speed ( 1MHz)
  72. #define ARM_I2C_BUS_SPEED_HIGH (0x04UL) ///< High Speed (3.4MHz)
  73. /****** I2C Address Flags *****/
  74. #define ARM_I2C_ADDRESS_10BIT (0x0400UL) ///< 10-bit address flag
  75. #define ARM_I2C_ADDRESS_GC (0x8000UL) ///< General Call flag
  76. /**
  77. \brief I2C Status
  78. */
  79. typedef struct _ARM_I2C_STATUS {
  80. uint32_t busy : 1; ///< Busy flag
  81. uint32_t mode : 1; ///< Mode: 0=Slave, 1=Master
  82. uint32_t direction : 1; ///< Direction: 0=Transmitter, 1=Receiver
  83. uint32_t general_call : 1; ///< General Call indication (cleared on start of next Slave operation)
  84. uint32_t arbitration_lost : 1; ///< Master lost arbitration (cleared on start of next Master operation)
  85. uint32_t bus_error : 1; ///< Bus error detected (cleared on start of next Master/Slave operation)
  86. uint32_t reserved : 26;
  87. } ARM_I2C_STATUS;
  88. /****** I2C Event *****/
  89. #define ARM_I2C_EVENT_TRANSFER_DONE (1UL << 0) ///< Master/Slave Transmit/Receive finished
  90. #define ARM_I2C_EVENT_TRANSFER_INCOMPLETE (1UL << 1) ///< Master/Slave Transmit/Receive incomplete transfer
  91. #define ARM_I2C_EVENT_SLAVE_TRANSMIT (1UL << 2) ///< Addressed as Slave Transmitter but transmit operation is not set.
  92. #define ARM_I2C_EVENT_SLAVE_RECEIVE (1UL << 3) ///< Addressed as Slave Receiver but receive operation is not set.
  93. #define ARM_I2C_EVENT_ADDRESS_NACK (1UL << 4) ///< Address not acknowledged from Slave
  94. #define ARM_I2C_EVENT_GENERAL_CALL (1UL << 5) ///< Slave addressed with general call address
  95. #define ARM_I2C_EVENT_ARBITRATION_LOST (1UL << 6) ///< Master lost arbitration
  96. #define ARM_I2C_EVENT_BUS_ERROR (1UL << 7) ///< Bus error detected (START/STOP at illegal position)
  97. #define ARM_I2C_EVENT_BUS_CLEAR (1UL << 8) ///< Bus clear finished
  98. // Function documentation
  99. /**
  100. \fn ARM_DRIVER_VERSION ARM_I2C_GetVersion (void)
  101. \brief Get driver version.
  102. \return \ref ARM_DRIVER_VERSION
  103. \fn ARM_I2C_CAPABILITIES ARM_I2C_GetCapabilities (void)
  104. \brief Get driver capabilities.
  105. \return \ref ARM_I2C_CAPABILITIES
  106. \fn int32_t ARM_I2C_Initialize (ARM_I2C_SignalEvent_t cb_event)
  107. \brief Initialize I2C Interface.
  108. \param[in] cb_event Pointer to \ref ARM_I2C_SignalEvent
  109. \return \ref execution_status
  110. \fn int32_t ARM_I2C_Uninitialize (void)
  111. \brief De-initialize I2C Interface.
  112. \return \ref execution_status
  113. \fn int32_t ARM_I2C_PowerControl (ARM_POWER_STATE state)
  114. \brief Control I2C Interface Power.
  115. \param[in] state Power state
  116. \return \ref execution_status
  117. \fn int32_t ARM_I2C_MasterTransmit (uint32_t addr, const uint8_t *data, uint32_t num, bool xfer_pending)
  118. \brief Start transmitting data as I2C Master.
  119. \param[in] addr Slave address (7-bit or 10-bit)
  120. \param[in] data Pointer to buffer with data to transmit to I2C Slave
  121. \param[in] num Number of data bytes to transmit
  122. \param[in] xfer_pending Transfer operation is pending - Stop condition will not be generated
  123. \return \ref execution_status
  124. \fn int32_t ARM_I2C_MasterReceive (uint32_t addr, uint8_t *data, uint32_t num, bool xfer_pending)
  125. \brief Start receiving data as I2C Master.
  126. \param[in] addr Slave address (7-bit or 10-bit)
  127. \param[out] data Pointer to buffer for data to receive from I2C Slave
  128. \param[in] num Number of data bytes to receive
  129. \param[in] xfer_pending Transfer operation is pending - Stop condition will not be generated
  130. \return \ref execution_status
  131. \fn int32_t ARM_I2C_SlaveTransmit (const uint8_t *data, uint32_t num)
  132. \brief Start transmitting data as I2C Slave.
  133. \param[in] data Pointer to buffer with data to transmit to I2C Master
  134. \param[in] num Number of data bytes to transmit
  135. \return \ref execution_status
  136. \fn int32_t ARM_I2C_SlaveReceive (uint8_t *data, uint32_t num)
  137. \brief Start receiving data as I2C Slave.
  138. \param[out] data Pointer to buffer for data to receive from I2C Master
  139. \param[in] num Number of data bytes to receive
  140. \return \ref execution_status
  141. \fn int32_t ARM_I2C_GetDataCount (void)
  142. \brief Get transferred data count.
  143. \return number of data bytes transferred; -1 when Slave is not addressed by Master
  144. \fn int32_t ARM_I2C_Control (uint32_t control, uint32_t arg)
  145. \brief Control I2C Interface.
  146. \param[in] control Operation
  147. \param[in] arg Argument of operation (optional)
  148. \return \ref execution_status
  149. \fn ARM_I2C_STATUS ARM_I2C_GetStatus (void)
  150. \brief Get I2C status.
  151. \return I2C status \ref ARM_I2C_STATUS
  152. \fn void ARM_I2C_SignalEvent (uint32_t event)
  153. \brief Signal I2C Events.
  154. \param[in] event \ref I2C_events notification mask
  155. */
  156. typedef void (*ARM_I2C_SignalEvent_t) (uint32_t event); ///< Pointer to \ref ARM_I2C_SignalEvent : Signal I2C Event.
  157. /**
  158. \brief I2C Driver Capabilities.
  159. */
  160. typedef struct _ARM_I2C_CAPABILITIES {
  161. uint32_t address_10_bit : 1; ///< supports 10-bit addressing
  162. uint32_t reserved : 31; ///< Reserved (must be zero)
  163. } ARM_I2C_CAPABILITIES;
  164. /**
  165. \brief Access structure of the I2C Driver.
  166. */
  167. typedef struct _ARM_DRIVER_I2C {
  168. ARM_DRIVER_VERSION (*GetVersion) (void); ///< Pointer to \ref ARM_I2C_GetVersion : Get driver version.
  169. ARM_I2C_CAPABILITIES (*GetCapabilities)(void); ///< Pointer to \ref ARM_I2C_GetCapabilities : Get driver capabilities.
  170. int32_t (*Initialize) (ARM_I2C_SignalEvent_t cb_event); ///< Pointer to \ref ARM_I2C_Initialize : Initialize I2C Interface.
  171. int32_t (*Uninitialize) (void); ///< Pointer to \ref ARM_I2C_Uninitialize : De-initialize I2C Interface.
  172. int32_t (*PowerControl) (ARM_POWER_STATE state); ///< Pointer to \ref ARM_I2C_PowerControl : Control I2C Interface Power.
  173. int32_t (*MasterTransmit) (uint32_t addr, const uint8_t *data, uint32_t num, bool xfer_pending); ///< Pointer to \ref ARM_I2C_MasterTransmit : Start transmitting data as I2C Master.
  174. int32_t (*MasterReceive) (uint32_t addr, uint8_t *data, uint32_t num, bool xfer_pending); ///< Pointer to \ref ARM_I2C_MasterReceive : Start receiving data as I2C Master.
  175. int32_t (*SlaveTransmit) ( const uint8_t *data, uint32_t num); ///< Pointer to \ref ARM_I2C_SlaveTransmit : Start transmitting data as I2C Slave.
  176. int32_t (*SlaveReceive) ( uint8_t *data, uint32_t num); ///< Pointer to \ref ARM_I2C_SlaveReceive : Start receiving data as I2C Slave.
  177. int32_t (*GetDataCount) (void); ///< Pointer to \ref ARM_I2C_GetDataCount : Get transferred data count.
  178. int32_t (*Control) (uint32_t control, uint32_t arg); ///< Pointer to \ref ARM_I2C_Control : Control I2C Interface.
  179. ARM_I2C_STATUS (*GetStatus) (void); ///< Pointer to \ref ARM_I2C_GetStatus : Get I2C status.
  180. } const ARM_DRIVER_I2C;
  181. #ifdef __cplusplus
  182. }
  183. #endif
  184. #endif /* DRIVER_I2C_H_ */