hal_i2c_lld.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. * @file common/I2C/hal_i2c_lld.h
  15. * @brief SW I2C subsystem low level driver header.
  16. *
  17. * @addtogroup I2C
  18. * @{
  19. */
  20. #ifndef HAL_I2C_LLD_H
  21. #define HAL_I2C_LLD_H
  22. #if HAL_USE_I2C || defined(__DOXYGEN__)
  23. /*===========================================================================*/
  24. /* Driver constants. */
  25. /*===========================================================================*/
  26. /*===========================================================================*/
  27. /* Driver pre-compile time settings. */
  28. /*===========================================================================*/
  29. /**
  30. * @name Configuration options
  31. * @{
  32. */
  33. /**
  34. * @brief Use OSAL delays.
  35. * @details If set to @p TRUE then delays are implemented using the
  36. * thread-friendy delay function else a delay function must
  37. * be provided extenally.
  38. */
  39. #if !defined(SW_I2C_USE_OSAL_DELAY) || defined(__DOXYGEN__)
  40. #define SW_I2C_USE_OSAL_DELAY TRUE
  41. #endif
  42. /**
  43. * @brief I2C1 driver enable switch.
  44. * @details If set to @p TRUE the support for I2C1 is included.
  45. * @note The default is @p FALSE.
  46. */
  47. #if !defined(SW_I2C_USE_I2C1) || defined(__DOXYGEN__)
  48. #define SW_I2C_USE_I2C1 FALSE
  49. #endif
  50. /**
  51. * @brief I2C2 driver enable switch.
  52. * @details If set to @p TRUE the support for I2C2 is included.
  53. * @note The default is @p FALSE.
  54. */
  55. #if !defined(SW_I2C_USE_I2C2) || defined(__DOXYGEN__)
  56. #define SW_I2C_USE_I2C2 FALSE
  57. #endif
  58. /**
  59. * @brief I2C3 driver enable switch.
  60. * @details If set to @p TRUE the support for I2C3 is included.
  61. * @note The default is @p FALSE.
  62. */
  63. #if !defined(SW_I2C_USE_I2C3) || defined(__DOXYGEN__)
  64. #define SW_I2C_USE_I2C3 FALSE
  65. #endif
  66. /**
  67. * @brief I2C4 driver enable switch.
  68. * @details If set to @p TRUE the support for I2C4 is included.
  69. * @note The default is @p FALSE.
  70. */
  71. #if !defined(SW_I2C_USE_I2C4) || defined(__DOXYGEN__)
  72. #define SW_I2C_USE_I2C4 FALSE
  73. #endif
  74. /** @} */
  75. /*===========================================================================*/
  76. /* Derived constants and error checks. */
  77. /*===========================================================================*/
  78. /*===========================================================================*/
  79. /* Driver data structures and types. */
  80. /*===========================================================================*/
  81. /**
  82. * @brief Type representing an I2C address.
  83. */
  84. typedef uint16_t i2caddr_t;
  85. /**
  86. * @brief Type of I2C driver condition flags.
  87. */
  88. typedef uint8_t i2cflags_t;
  89. /**
  90. * @brief Type of a delay function.
  91. */
  92. typedef void (*i2c_delay_t)(void);
  93. /**
  94. * @brief Type of I2C driver configuration structure.
  95. */
  96. typedef struct {
  97. /**
  98. * @brief 10 bits addressing switch.
  99. */
  100. bool addr10;
  101. /**
  102. * @brief I2C clock line.
  103. */
  104. ioline_t scl;
  105. /**
  106. * @brief I2C data line.
  107. */
  108. ioline_t sda;
  109. #if SW_I2C_USE_OSAL_DELAY || defined(__DOXYGEN__)
  110. /**
  111. * @brief Delay of an half bit time in system ticks.
  112. */
  113. systime_t ticks;
  114. #else
  115. /**
  116. * @brief Pointer to an externally defined delay function.
  117. */
  118. i2c_delay_t delay;
  119. #endif
  120. } I2CConfig;
  121. /**
  122. * @brief Type of a structure representing an I2C driver.
  123. */
  124. typedef struct I2CDriver I2CDriver;
  125. /**
  126. * @brief Structure representing an I2C driver.
  127. */
  128. struct I2CDriver {
  129. /**
  130. * @brief Driver state.
  131. */
  132. i2cstate_t state;
  133. /**
  134. * @brief Current configuration data.
  135. */
  136. const I2CConfig *config;
  137. /**
  138. * @brief Error flags.
  139. */
  140. i2cflags_t errors;
  141. #if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
  142. mutex_t mutex;
  143. #endif /* I2C_USE_MUTUAL_EXCLUSION */
  144. #if defined(I2C_DRIVER_EXT_FIELDS)
  145. I2C_DRIVER_EXT_FIELDS
  146. #endif
  147. /* End of the mandatory fields.*/
  148. /**
  149. * @brief Time of operation begin.
  150. */
  151. systime_t start;
  152. /**
  153. * @brief Time of operation timeout.
  154. */
  155. systime_t end;
  156. };
  157. /*===========================================================================*/
  158. /* Driver macros. */
  159. /*===========================================================================*/
  160. /**
  161. * @brief Get errors from I2C driver.
  162. *
  163. * @param[in] i2cp pointer to the @p I2CDriver object
  164. *
  165. * @notapi
  166. */
  167. #define i2c_lld_get_errors(i2cp) ((i2cp)->errors)
  168. /*===========================================================================*/
  169. /* External declarations. */
  170. /*===========================================================================*/
  171. #if !defined(__DOXYGEN__)
  172. #if SW_I2C_USE_I2C1
  173. extern I2CDriver I2CD1;
  174. #endif
  175. #if SW_I2C_USE_I2C2
  176. extern I2CDriver I2CD2;
  177. #endif
  178. #if SW_I2C_USE_I2C3
  179. extern I2CDriver I2CD3;
  180. #endif
  181. #if SW_I2C_USE_I2C4
  182. extern I2CDriver I2CD4;
  183. #endif
  184. #endif /* !defined(__DOXYGEN__) */
  185. #ifdef __cplusplus
  186. extern "C" {
  187. #endif
  188. void i2c_lld_init(void);
  189. void i2c_lld_start(I2CDriver *i2cp);
  190. void i2c_lld_stop(I2CDriver *i2cp);
  191. void i2c_lld_soft_stop(I2CDriver *i2cp);
  192. msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
  193. const uint8_t *txbuf, size_t txbytes,
  194. uint8_t *rxbuf, size_t rxbytes,
  195. systime_t timeout);
  196. msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
  197. uint8_t *rxbuf, size_t rxbytes,
  198. systime_t timeout);
  199. #ifdef __cplusplus
  200. }
  201. #endif
  202. #endif /* HAL_USE_I2C */
  203. #endif /* HAL_I2C_LLD_H */
  204. /** @} */