hal_i2c.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Concepts and parts of this file have been contributed by Uladzimir Pylinsky
  15. aka barthess.
  16. */
  17. /**
  18. * @file hal_i2c.h
  19. * @brief I2C Driver macros and structures.
  20. *
  21. * @addtogroup I2C
  22. * @{
  23. */
  24. #ifndef HAL_I2C_H
  25. #define HAL_I2C_H
  26. #if (HAL_USE_I2C == TRUE) || defined(__DOXYGEN__)
  27. /*===========================================================================*/
  28. /* Driver constants. */
  29. /*===========================================================================*/
  30. /* TODO: To be reviewed, too STM32-centric.*/
  31. /**
  32. * @name I2C bus error conditions
  33. * @{
  34. */
  35. #define I2C_NO_ERROR 0x00 /**< @brief No error. */
  36. #define I2C_BUS_ERROR 0x01 /**< @brief Bus Error. */
  37. #define I2C_ARBITRATION_LOST 0x02 /**< @brief Arbitration Lost. */
  38. #define I2C_ACK_FAILURE 0x04 /**< @brief Acknowledge Failure. */
  39. #define I2C_OVERRUN 0x08 /**< @brief Overrun/Underrun. */
  40. #define I2C_PEC_ERROR 0x10 /**< @brief PEC Error in
  41. reception. */
  42. #define I2C_TIMEOUT 0x20 /**< @brief Hardware timeout. */
  43. #define I2C_SMB_ALERT 0x40 /**< @brief SMBus Alert. */
  44. #define I2C_ISR_LIMIT 0x80 /**< @brief exceeded maximum ISR limit */
  45. /** @} */
  46. /*===========================================================================*/
  47. /* Driver pre-compile time settings. */
  48. /*===========================================================================*/
  49. /**
  50. * @brief Enables the mutual exclusion APIs on the I2C bus.
  51. */
  52. #if !defined(I2C_USE_MUTUAL_EXCLUSION) || defined(__DOXYGEN__)
  53. #define I2C_USE_MUTUAL_EXCLUSION TRUE
  54. #endif
  55. /*===========================================================================*/
  56. /* Derived constants and error checks. */
  57. /*===========================================================================*/
  58. /*===========================================================================*/
  59. /* Driver data structures and types. */
  60. /*===========================================================================*/
  61. /**
  62. * @brief Driver state machine possible states.
  63. */
  64. typedef enum {
  65. I2C_UNINIT = 0, /**< Not initialized. */
  66. I2C_STOP = 1, /**< Stopped. */
  67. I2C_READY = 2, /**< Ready. */
  68. I2C_ACTIVE_TX = 3, /**< Transmitting. */
  69. I2C_ACTIVE_RX = 4, /**< Receiving. */
  70. I2C_LOCKED = 5 /**> Bus or driver locked. */
  71. } i2cstate_t;
  72. #include "hal_i2c_lld.h"
  73. /*===========================================================================*/
  74. /* Driver macros. */
  75. /*===========================================================================*/
  76. /**
  77. * @brief Wakes up the waiting thread notifying no errors.
  78. *
  79. * @param[in] i2cp pointer to the @p I2CDriver object
  80. *
  81. * @notapi
  82. */
  83. #define _i2c_wakeup_isr(i2cp) do { \
  84. osalSysLockFromISR(); \
  85. osalThreadResumeI(&(i2cp)->thread, MSG_OK); \
  86. osalSysUnlockFromISR(); \
  87. } while(0)
  88. /**
  89. * @brief Wakes up the waiting thread notifying errors.
  90. *
  91. * @param[in] i2cp pointer to the @p I2CDriver object
  92. *
  93. * @notapi
  94. */
  95. #define _i2c_wakeup_error_isr(i2cp) do { \
  96. osalSysLockFromISR(); \
  97. osalThreadResumeI(&(i2cp)->thread, MSG_RESET); \
  98. osalSysUnlockFromISR(); \
  99. } while(0)
  100. /**
  101. * @brief Wrap i2cMasterTransmitTimeout function with TIME_INFINITE timeout.
  102. * @api
  103. */
  104. #define i2cMasterTransmit(i2cp, addr, txbuf, txbytes, rxbuf, rxbytes) \
  105. (i2cMasterTransmitTimeout(i2cp, addr, txbuf, txbytes, rxbuf, rxbytes, \
  106. TIME_INFINITE))
  107. /**
  108. * @brief Wrap i2cMasterReceiveTimeout function with TIME_INFINITE timeout.
  109. * @api
  110. */
  111. #define i2cMasterReceive(i2cp, addr, rxbuf, rxbytes) \
  112. (i2cMasterReceiveTimeout(i2cp, addr, rxbuf, rxbytes, TIME_INFINITE))
  113. /*===========================================================================*/
  114. /* External declarations. */
  115. /*===========================================================================*/
  116. #ifdef __cplusplus
  117. extern "C" {
  118. #endif
  119. void i2cInit(void);
  120. void i2cObjectInit(I2CDriver *i2cp);
  121. void i2cStart(I2CDriver *i2cp, const I2CConfig *config);
  122. void i2cStop(I2CDriver *i2cp);
  123. void i2cSoftStop(I2CDriver *i2cp);
  124. i2cflags_t i2cGetErrors(I2CDriver *i2cp);
  125. msg_t i2cMasterTransmitTimeout(I2CDriver *i2cp,
  126. i2caddr_t addr,
  127. const uint8_t *txbuf, size_t txbytes,
  128. uint8_t *rxbuf, size_t rxbytes,
  129. sysinterval_t timeout);
  130. msg_t i2cMasterReceiveTimeout(I2CDriver *i2cp,
  131. i2caddr_t addr,
  132. uint8_t *rxbuf, size_t rxbytes,
  133. sysinterval_t timeout);
  134. #if I2C_USE_MUTUAL_EXCLUSION == TRUE
  135. void i2cAcquireBus(I2CDriver *i2cp);
  136. void i2cReleaseBus(I2CDriver *i2cp);
  137. #endif
  138. #ifdef __cplusplus
  139. }
  140. #endif
  141. #endif /* HAL_USE_I2C == TRUE */
  142. #endif /* HAL_I2C_H */
  143. /** @} */