You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

806 lines
24 KiB

  1. /*
  2. ChibiOS - Copyright (C) 2006..2015 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. I2C Slave API for Chibios V2.x V1 I2C originally contributed
  16. by Brent Roman (brent@mbari.org), ported to Chibios V3, V2 I2C by steved
  17. */
  18. /**
  19. * @file STM32/I2Cv2/i2c_lld.h
  20. * @brief STM32 I2C subsystem low level driver header.
  21. *
  22. * @addtogroup I2C
  23. * @{
  24. */
  25. #ifndef _I2C_LLD_H_
  26. #define _I2C_LLD_H_
  27. #if HAL_USE_I2C || defined(__DOXYGEN__)
  28. /*===========================================================================*/
  29. /* Driver constants. */
  30. /*===========================================================================*/
  31. /**
  32. * @name TIMINGR register definitions
  33. * @{
  34. */
  35. #define STM32_TIMINGR_PRESC_MASK (15U << 28)
  36. #define STM32_TIMINGR_PRESC(n) ((n) << 28)
  37. #define STM32_TIMINGR_SCLDEL_MASK (15U << 20)
  38. #define STM32_TIMINGR_SCLDEL(n) ((n) << 20)
  39. #define STM32_TIMINGR_SDADEL_MASK (15U << 16)
  40. #define STM32_TIMINGR_SDADEL(n) ((n) << 16)
  41. #define STM32_TIMINGR_SCLH_MASK (255U << 8)
  42. #define STM32_TIMINGR_SCLH(n) ((n) << 8)
  43. #define STM32_TIMINGR_SCLL_MASK (255U << 0)
  44. #define STM32_TIMINGR_SCLL(n) ((n) << 0)
  45. /** @} */
  46. /**
  47. * Driver clears down tidily after a timeout
  48. */
  49. #define I2C_SUPPORT_BUS_CLEAR TRUE
  50. /**
  51. * @brief Invalid I2C bus address
  52. */
  53. #define i2cInvalidAdr ((i2caddr_t) -1)
  54. /*===========================================================================*/
  55. /* Driver pre-compile time settings. */
  56. /*===========================================================================*/
  57. /**
  58. * @name Configuration options
  59. * @{
  60. */
  61. /**
  62. * @brief I2C1 driver enable switch.
  63. * @details If set to @p TRUE the support for I2C1 is included.
  64. * @note The default is @p FALSE.
  65. */
  66. #if !defined(STM32_I2C_USE_I2C1) || defined(__DOXYGEN__)
  67. #define STM32_I2C_USE_I2C1 FALSE
  68. #endif
  69. /**
  70. * @brief I2C2 driver enable switch.
  71. * @details If set to @p TRUE the support for I2C2 is included.
  72. * @note The default is @p FALSE.
  73. */
  74. #if !defined(STM32_I2C_USE_I2C2) || defined(__DOXYGEN__)
  75. #define STM32_I2C_USE_I2C2 FALSE
  76. #endif
  77. /**
  78. * @brief I2C3 driver enable switch.
  79. * @details If set to @p TRUE the support for I2C3 is included.
  80. * @note The default is @p FALSE.
  81. */
  82. #if !defined(STM32_I2C_USE_I2C3) || defined(__DOXYGEN__)
  83. #define STM32_I2C_USE_I2C3 FALSE
  84. #endif
  85. /**
  86. * @brief I2C4 driver enable switch.
  87. * @details If set to @p TRUE the support for I2C4 is included.
  88. * @note The default is @p FALSE.
  89. */
  90. #if !defined(STM32_I2C_USE_I2C4) || defined(__DOXYGEN__)
  91. #define STM32_I2C_USE_I2C4 FALSE
  92. #endif
  93. /**
  94. * @brief Enables support for I2C slave mode operation
  95. */
  96. #if !defined(HAL_USE_I2C_SLAVE) || defined(__DOXYGEN__)
  97. #define HAL_USE_I2C_SLAVE FALSE
  98. #endif
  99. /**
  100. * @brief Turns on some debugging options
  101. */
  102. #if !defined(STM32_I2C_DEBUG_ENABLE) || defined(__DOXYGEN__)
  103. #define STM32_I2C_DEBUG_ENABLE FALSE
  104. #endif
  105. /**
  106. * @brief I2C timeout on busy condition in milliseconds.
  107. */
  108. #if !defined(STM32_I2C_BUSY_TIMEOUT) || defined(__DOXYGEN__)
  109. #define STM32_I2C_BUSY_TIMEOUT 50
  110. #endif
  111. /**
  112. * @brief I2C1 interrupt priority level setting.
  113. */
  114. #if !defined(STM32_I2C_I2C1_IRQ_PRIORITY) || defined(__DOXYGEN__)
  115. #define STM32_I2C_I2C1_IRQ_PRIORITY 10
  116. #endif
  117. /**
  118. * @brief I2C2 interrupt priority level setting.
  119. */
  120. #if !defined(STM32_I2C_I2C2_IRQ_PRIORITY) || defined(__DOXYGEN__)
  121. #define STM32_I2C_I2C2_IRQ_PRIORITY 10
  122. #endif
  123. /**
  124. * @brief I2C3 interrupt priority level setting.
  125. */
  126. #if !defined(STM32_I2C_I2C3_IRQ_PRIORITY) || defined(__DOXYGEN__)
  127. #define STM32_I2C_I2C3_IRQ_PRIORITY 10
  128. #endif
  129. /**
  130. * @brief I2C4 interrupt priority level setting.
  131. */
  132. #if !defined(STM32_I2C_I2C4_IRQ_PRIORITY) || defined(__DOXYGEN__)
  133. #define STM32_I2C_I2C4_IRQ_PRIORITY 10
  134. #endif
  135. /**
  136. * @brief DMA use switch.
  137. */
  138. #if !defined(STM32_I2C_USE_DMA) || defined(__DOXYGEN__)
  139. #define STM32_I2C_USE_DMA TRUE
  140. #endif
  141. /**
  142. * @brief I2C1 DMA priority (0..3|lowest..highest).
  143. * @note The priority level is used for both the TX and RX DMA streams but
  144. * because of the streams ordering the RX stream has always priority
  145. * over the TX stream.
  146. */
  147. #if !defined(STM32_I2C_I2C1_DMA_PRIORITY) || defined(__DOXYGEN__)
  148. #define STM32_I2C_I2C1_DMA_PRIORITY 1
  149. #endif
  150. /**
  151. * @brief I2C2 DMA priority (0..3|lowest..highest).
  152. * @note The priority level is used for both the TX and RX DMA streams but
  153. * because of the streams ordering the RX stream has always priority
  154. * over the TX stream.
  155. */
  156. #if !defined(STM32_I2C_I2C2_DMA_PRIORITY) || defined(__DOXYGEN__)
  157. #define STM32_I2C_I2C2_DMA_PRIORITY 1
  158. #endif
  159. /**
  160. * @brief I2C3 DMA priority (0..3|lowest..highest).
  161. * @note The priority level is used for both the TX and RX DMA streams but
  162. * because of the streams ordering the RX stream has always priority
  163. * over the TX stream.
  164. */
  165. #if !defined(STM32_I2C_I2C3_DMA_PRIORITY) || defined(__DOXYGEN__)
  166. #define STM32_I2C_I2C3_DMA_PRIORITY 1
  167. #endif
  168. /**
  169. * @brief I2C4 DMA priority (0..3|lowest..highest).
  170. * @note The priority level is used for both the TX and RX DMA streams but
  171. * because of the streams ordering the RX stream has always priority
  172. * over the TX stream.
  173. */
  174. #if !defined(STM32_I2C_I2C4_DMA_PRIORITY) || defined(__DOXYGEN__)
  175. #define STM32_I2C_I2C4_DMA_PRIORITY 1
  176. #endif
  177. /**
  178. * @brief I2C DMA error hook.
  179. * @note The default action for DMA errors is a system halt because DMA
  180. * error can only happen because programming errors.
  181. */
  182. #if !defined(STM32_I2C_DMA_ERROR_HOOK) || defined(__DOXYGEN__)
  183. #define STM32_I2C_DMA_ERROR_HOOK(i2cp) osalSysHalt("DMA failure")
  184. #endif
  185. /** @} */
  186. /*===========================================================================*/
  187. /* Derived constants and error checks. */
  188. /*===========================================================================*/
  189. /** @brief error checks */
  190. #if STM32_I2C_USE_I2C1 && !STM32_HAS_I2C1
  191. #error "I2C1 not present in the selected device"
  192. #endif
  193. #if STM32_I2C_USE_I2C2 && !STM32_HAS_I2C2
  194. #error "I2C2 not present in the selected device"
  195. #endif
  196. #if STM32_I2C_USE_I2C3 && !STM32_HAS_I2C3
  197. #error "I2C3 not present in the selected device"
  198. #endif
  199. #if STM32_I2C_USE_I2C4 && !STM32_HAS_I2C4
  200. #error "I2C4 not present in the selected device"
  201. #endif
  202. #if !STM32_I2C_USE_I2C1 && !STM32_I2C_USE_I2C2 && !STM32_I2C_USE_I2C3 && \
  203. !STM32_I2C_USE_I2C4
  204. #error "I2C driver activated but no I2C peripheral assigned"
  205. #endif
  206. #if STM32_I2C_USE_I2C1 && \
  207. !OSAL_IRQ_IS_VALID_PRIORITY(STM32_I2C_I2C1_IRQ_PRIORITY)
  208. #error "Invalid IRQ priority assigned to I2C1"
  209. #endif
  210. #if STM32_I2C_USE_I2C2 && \
  211. !OSAL_IRQ_IS_VALID_PRIORITY(STM32_I2C_I2C2_IRQ_PRIORITY)
  212. #error "Invalid IRQ priority assigned to I2C2"
  213. #endif
  214. #if STM32_I2C_USE_I2C3 && \
  215. !OSAL_IRQ_IS_VALID_PRIORITY(STM32_I2C_I2C3_IRQ_PRIORITY)
  216. #error "Invalid IRQ priority assigned to I2C3"
  217. #endif
  218. #if STM32_I2C_USE_I2C4 && \
  219. !OSAL_IRQ_IS_VALID_PRIORITY(STM32_I2C_I2C4_IRQ_PRIORITY)
  220. #error "Invalid IRQ priority assigned to I2C4"
  221. #endif
  222. #if STM32_I2C_USE_DMA == TRUE
  223. #if STM32_I2C_USE_I2C1 && \
  224. !STM32_DMA_IS_VALID_PRIORITY(STM32_I2C_I2C1_DMA_PRIORITY)
  225. #error "Invalid DMA priority assigned to I2C1"
  226. #endif
  227. #if STM32_I2C_USE_I2C2 && \
  228. !STM32_DMA_IS_VALID_PRIORITY(STM32_I2C_I2C2_DMA_PRIORITY)
  229. #error "Invalid DMA priority assigned to I2C2"
  230. #endif
  231. #if STM32_I2C_USE_I2C3 && \
  232. !STM32_DMA_IS_VALID_PRIORITY(STM32_I2C_I2C3_DMA_PRIORITY)
  233. #error "Invalid DMA priority assigned to I2C3"
  234. #endif
  235. #if STM32_I2C_USE_I2C4 && \
  236. !STM32_DMA_IS_VALID_PRIORITY(STM32_I2C_I2C4_DMA_PRIORITY)
  237. #error "Invalid DMA priority assigned to I2C4"
  238. #endif
  239. /* The following checks are only required when there is a DMA able to
  240. reassign streams to different channels.*/
  241. #if STM32_ADVANCED_DMA
  242. /* Check on the presence of the DMA streams settings in mcuconf.h.*/
  243. #if STM32_I2C_USE_I2C1 && (!defined(STM32_I2C_I2C1_RX_DMA_STREAM) || \
  244. !defined(STM32_I2C_I2C1_TX_DMA_STREAM))
  245. #error "I2C1 DMA streams not defined"
  246. #endif
  247. #if STM32_I2C_USE_I2C2 && (!defined(STM32_I2C_I2C2_RX_DMA_STREAM) || \
  248. !defined(STM32_I2C_I2C2_TX_DMA_STREAM))
  249. #error "I2C2 DMA streams not defined"
  250. #endif
  251. #if STM32_I2C_USE_I2C3 && (!defined(STM32_I2C_I2C3_RX_DMA_STREAM) || \
  252. !defined(STM32_I2C_I2C3_TX_DMA_STREAM))
  253. #error "I2C3 DMA streams not defined"
  254. #endif
  255. #if STM32_I2C_USE_I2C4 && (!defined(STM32_I2C_I2C4_RX_DMA_STREAM) || \
  256. !defined(STM32_I2C_I2C4_TX_DMA_STREAM))
  257. #error "I2C4 DMA streams not defined"
  258. #endif
  259. /* Check on the validity of the assigned DMA channels.*/
  260. #if STM32_I2C_USE_I2C1 && \
  261. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C1_RX_DMA_STREAM, \
  262. STM32_I2C1_RX_DMA_MSK)
  263. #error "invalid DMA stream associated to I2C1 RX"
  264. #endif
  265. #if STM32_I2C_USE_I2C1 && \
  266. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C1_TX_DMA_STREAM, \
  267. STM32_I2C1_TX_DMA_MSK)
  268. #error "invalid DMA stream associated to I2C1 TX"
  269. #endif
  270. #if STM32_I2C_USE_I2C2 && \
  271. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C2_RX_DMA_STREAM, \
  272. STM32_I2C2_RX_DMA_MSK)
  273. #error "invalid DMA stream associated to I2C2 RX"
  274. #endif
  275. #if STM32_I2C_USE_I2C2 && \
  276. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C2_TX_DMA_STREAM, \
  277. STM32_I2C2_TX_DMA_MSK)
  278. #error "invalid DMA stream associated to I2C2 TX"
  279. #endif
  280. #if STM32_I2C_USE_I2C3 && \
  281. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C3_RX_DMA_STREAM, \
  282. STM32_I2C3_RX_DMA_MSK)
  283. #error "invalid DMA stream associated to I2C3 RX"
  284. #endif
  285. #if STM32_I2C_USE_I2C3 && \
  286. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C3_TX_DMA_STREAM, \
  287. STM32_I2C3_TX_DMA_MSK)
  288. #error "invalid DMA stream associated to I2C3 TX"
  289. #endif
  290. #if STM32_I2C_USE_I2C4 && \
  291. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C4_RX_DMA_STREAM, \
  292. STM32_I2C4_RX_DMA_MSK)
  293. #error "invalid DMA stream associated to I2C4 RX"
  294. #endif
  295. #if STM32_I2C_USE_I2C4 && \
  296. !STM32_DMA_IS_VALID_ID(STM32_I2C_I2C4_TX_DMA_STREAM, \
  297. STM32_I2C4_TX_DMA_MSK)
  298. #error "invalid DMA stream associated to I2C4 TX"
  299. #endif
  300. #endif /* STM32_ADVANCED_DMA */
  301. #if !defined(STM32_DMA_REQUIRED)
  302. #define STM32_DMA_REQUIRED
  303. #endif
  304. #endif /* STM32_I2C_USE_DMA == TRUE */
  305. /*===========================================================================*/
  306. /* Driver data structures and types. */
  307. /*===========================================================================*/
  308. /**
  309. * @brief Type representing an I2C address.
  310. * @note For a 7-bit address, this takes values 0..0x7f, which are then
  311. * shifted left one and the R/W bit added when required
  312. */
  313. typedef uint16_t i2caddr_t;
  314. /**
  315. * @brief Type of I2C driver condition flags.
  316. */
  317. typedef uint32_t i2cflags_t;
  318. /**
  319. * @brief Type of a structure representing an I2C driver.
  320. */
  321. typedef struct I2CDriver I2CDriver;
  322. /**
  323. * @brief Supported modes for the I2C bus.
  324. * @note Currently not used; retained for future enhancements
  325. */
  326. typedef enum {
  327. OPMODE_I2C = 1,
  328. OPMODE_SMBUS_DEVICE = 2,
  329. OPMODE_SMBUS_HOST = 3,
  330. } i2copmode_t;
  331. /**
  332. * @brief Character received I2C notification callback type.
  333. *
  334. * @param[in] i2cp pointer to the @p I2CDriver object
  335. * @param[in] c received character
  336. *
  337. * @param[out] Return 0 if transfer to continue. 1 if transfer to be stopped
  338. *
  339. * @note Use only in master mode, to stop a read transaction
  340. * once a particular character (or sequence of characters) has been received
  341. */
  342. typedef uint8_t (*i2cccb_t)(I2CDriver *i2cp, uint16_t c);
  343. /**
  344. * @brief Type of I2C driver configuration structure.
  345. */
  346. typedef struct {
  347. /**
  348. * @brief TIMINGR register initialization.
  349. * @note Refer to the STM32 reference manual, the values are affected
  350. * by the system clock settings in mcuconf.h.
  351. */
  352. uint32_t timingr;
  353. /**
  354. * @brief CR1 register initialization.
  355. * @note Leave to zero unless you know what you are doing.
  356. */
  357. uint32_t cr1;
  358. /**
  359. * @brief CR2 register initialization.
  360. * @note Leave at zero except in special circumstances - most bits controlled via API
  361. */
  362. uint32_t cr2;
  363. /**
  364. * @brief Character received callback. Return 0 if transfer to continue. 1 if transfer to be stopped
  365. * @note Use only in master mode. Set to NULL if not used.
  366. */
  367. i2cccb_t rxchar_cb;
  368. } I2CConfig;
  369. #if HAL_USE_I2C_SLAVE /* I2C slave mode support */
  370. typedef struct I2CSlaveMsg I2CSlaveMsg;
  371. /*
  372. returns the current I2C slave message receive configuration
  373. */
  374. I2CSlaveMsg *i2cSlaveGetReceiveMsg(I2CDriver *i2cp);
  375. /*
  376. returns the current I2C slave message reply configuration
  377. */
  378. I2CSlaveMsg *i2cSlaveGetReplyMsg(I2CDriver *i2cp);
  379. /*
  380. I2C Slave Message Call Back.
  381. Invoked from interrupt context just after
  382. the last byte of the message is transferred or slaveAdr is matched.
  383. Use i2cSlaveReceiveMsg() or i2cSlaveReplyMsg() to access
  384. the relevant message handling configuration
  385. */
  386. typedef void I2CSlaveMsgCB(I2CDriver *i2cp);
  387. /*
  388. I2CSlaveMsg message handling configurations are normally
  389. stored in read-only memory.
  390. They describe either a buffer to contain incoming messages from
  391. a bus master and associated callback functions, or one
  392. preloaded with an outgoing reply to a read request and its callbacks.
  393. */
  394. struct I2CSlaveMsg {
  395. size_t size; /* sizeof(body) -- zero if master must wait */
  396. uint8_t *body; /* message contents -- or NULL if master must wait */
  397. I2CSlaveMsgCB *adrMatched; /* invoked when slave address matches */
  398. I2CSlaveMsgCB *processMsg; /* invoked after message is transferred */
  399. I2CSlaveMsgCB *exception; /* invoked if error or timeout during transfer */
  400. };
  401. /*
  402. dummy callback -- placeholder to ignore event
  403. */
  404. I2CSlaveMsgCB I2CSlaveDummyCB;
  405. /* lock bus on receive or reply -- force master to wait */
  406. extern const I2CSlaveMsg I2CSlaveLockOnMsg;
  407. #endif /* HAL_USE_I2C_SLAVE */
  408. /**
  409. * @brief Structure representing an I2C driver.
  410. */
  411. struct I2CDriver {
  412. /**
  413. * @brief Driver state.
  414. */
  415. i2cstate_t state;
  416. /**
  417. * @brief Current configuration data.
  418. */
  419. const I2CConfig *config;
  420. /**
  421. * @brief Error flags.
  422. */
  423. i2cflags_t errors;
  424. #if I2C_USE_MUTUAL_EXCLUSION || defined(__DOXYGEN__)
  425. mutex_t mutex;
  426. #endif /* I2C_USE_MUTUAL_EXCLUSION */
  427. #if defined(I2C_DRIVER_EXT_FIELDS)
  428. I2C_DRIVER_EXT_FIELDS
  429. #endif
  430. /* End of the mandatory fields.*/
  431. /**
  432. * @brief Thread waiting for I/O completion.
  433. */
  434. thread_reference_t thread;
  435. #if (STM32_I2C_USE_DMA == TRUE) || defined(__DOXYGEN__)
  436. /**
  437. * @brief RX DMA mode bit mask.
  438. */
  439. uint32_t rxdmamode;
  440. /**
  441. * @brief TX DMA mode bit mask.
  442. */
  443. uint32_t txdmamode;
  444. /**
  445. * @brief Receive DMA channel.
  446. */
  447. const stm32_dma_stream_t *dmarx;
  448. /**
  449. * @brief Transmit DMA channel.
  450. */
  451. const stm32_dma_stream_t *dmatx;
  452. #else /* STM32_I2C_USE_DMA == FALSE */
  453. /**
  454. * @brief Pointer to the next TX buffer location.
  455. */
  456. const uint8_t *txptr;
  457. /**
  458. * @brief Number of bytes in TX phase.
  459. */
  460. size_t txbytes;
  461. #endif /* STM32_I2C_USE_DMA == FALSE */
  462. /**
  463. * @brief Pointer to the next RX buffer location.
  464. */
  465. uint8_t *rxptr;
  466. /**
  467. * @brief Number of bytes in RX phase.
  468. */
  469. size_t rxbytes;
  470. /**
  471. * @brief Pointer to the I2Cx registers block.
  472. */
  473. I2C_TypeDef *i2c;
  474. /**
  475. * @brief low level I2C interface / protocol state
  476. */
  477. enum i2cMode {
  478. i2cStopped = 0, /* Port not initialised, or not started */
  479. i2cIdle=1, /* awaiting address or inactive */
  480. i2cSlaveRxing, /* receiving message */
  481. i2cLockedRxing, /* stretching clock before receiving message - Rx buffer might be full */
  482. i2cSlaveReplying, /* replying to query (transmitting, slave mode) */
  483. i2cLockedReplying, /* stretching clock before replying to query (no response available from main code) */
  484. i2cIsMaster=0x11, /* sent start bit (mastering bus) */
  485. i2cMasterStarted, /* repeated start after write */
  486. i2cMasterSelecting, /* sending slave address */
  487. i2cMasterRxing, /* receiving reply from slave */
  488. i2cMasterTxing /* sending message to slave */
  489. } mode;
  490. #if HAL_USE_I2C_LOCK || HAL_USE_I2C_SLAVE
  491. /**
  492. * @brief I2C transaction timer
  493. * @note USed for slave mode, lock
  494. */
  495. virtual_timer_t timer;
  496. #endif
  497. #if HAL_USE_I2C_LOCK
  498. /**
  499. * @brief I2C bus lock duration
  500. */
  501. systime_t lockDuration;
  502. #endif
  503. #if HAL_USE_I2C_SLAVE
  504. /* additional fields to support I2C slave transactions */
  505. /**
  506. * @brief slave address of message being processed
  507. */
  508. i2caddr_t targetAdr;
  509. /**
  510. * @brief Error Mask for last slave message
  511. */
  512. i2cflags_t slaveErrors;
  513. /**
  514. * @brief Length of most recently transferred slave message
  515. */
  516. size_t slaveBytes;
  517. /**
  518. * @brief Maximum # of ticks slave may stretch the I2C clock
  519. */
  520. systime_t slaveTimeout;
  521. /**
  522. * @brief Pointer to slave message reception handler
  523. */
  524. const I2CSlaveMsg *slaveRx;
  525. /**
  526. * @brief Pointer to slave message Reply (transmit) handler
  527. *
  528. * @note This is the currently active/just completed reply
  529. */
  530. const I2CSlaveMsg *slaveReply;
  531. /**
  532. * @brief Pointer to handler for next slave received message
  533. */
  534. const I2CSlaveMsg *slaveNextRx;
  535. /**
  536. * @brief Pointer to handler for next slave reply (transmit) message
  537. *
  538. * @note This is used for a reply if no message received first
  539. */
  540. const I2CSlaveMsg *slaveNextReply;
  541. #endif
  542. };
  543. /*===========================================================================*/
  544. /* Driver macros. */
  545. /*===========================================================================*/
  546. /**
  547. * @brief Get errors from I2C driver.
  548. *
  549. * @param[in] i2cp pointer to the @p I2CDriver object
  550. *
  551. * @notapi
  552. */
  553. #define i2c_lld_get_errors(i2cp) ((i2cp)->errors)
  554. #if HAL_USE_I2C_LOCK
  555. /**
  556. * @brief Unlock I2C bus after the end of the next transaction
  557. *
  558. * @param[in] i2cp pointer to the @p I2CDriver object
  559. *
  560. * @notapi
  561. **/
  562. #define i2c_lld_unlock(i2cp) (i2cp->lockDuration = TIME_IMMEDIATE)
  563. #endif
  564. #if HAL_USE_I2C_SLAVE /* I2C slave mode support */
  565. /**
  566. * @brief Get slave errors from I2C driver.
  567. *
  568. * @param[in] i2cp pointer to the @p I2CDriver object
  569. *
  570. * @notapi
  571. */
  572. #define i2c_lld_get_slaveErrors(i2cp) ((i2cp)->slaveErrors)
  573. /**
  574. * @brief Get slave message bytes transferred from I2C driver.
  575. *
  576. * @param[in] i2cp pointer to the @p I2CDriver object
  577. *
  578. * @notapi
  579. */
  580. #define i2c_lld_get_slaveBytes(i2cp) ((i2cp)->slaveBytes)
  581. /**
  582. * @brief Get slave timeout in ticks from I2C driver.
  583. *
  584. * @param[in] i2cp pointer to the @p I2CDriver object
  585. *
  586. * @notapi
  587. */
  588. #define i2c_lld_get_slaveTimeout(i2cp) ((i2cp)->slaveTimeout)
  589. /**
  590. * @brief Set slave timeout in ticks for I2C driver.
  591. *
  592. * @param[in] i2cp pointer to the @p I2CDriver object
  593. *
  594. * @notapi
  595. */
  596. #define i2c_lld_set_slaveTimeout(i2cp,ticks) ((i2cp)->slaveTimeout=(ticks))
  597. /**
  598. * @brief Get slave target address from I2C driver.
  599. *
  600. * @param[in] i2cp pointer to the @p I2CDriver object
  601. *
  602. * @notapi
  603. */
  604. #define i2c_lld_get_slaveTargetAdr(i2cp) ((i2cp)->targetAdr)
  605. /**
  606. * @brief Get slave receive message descriptor from I2C driver.
  607. *
  608. * @param[in] i2cp pointer to the @p I2CDriver object
  609. *
  610. * @notapi
  611. */
  612. #define i2c_lld_get_slaveReceive(i2cp) ((i2cp)->slaveNextRx)
  613. /**
  614. * @brief Get slave reply message descriptor from I2C driver.
  615. *
  616. * @param[in] i2cp pointer to the @p I2CDriver object
  617. *
  618. * @notapi
  619. */
  620. #define i2c_lld_get_slaveReply(i2cp) ((i2cp)->slaveNextReply)
  621. #endif
  622. /*===========================================================================*/
  623. /* External declarations. */
  624. /*===========================================================================*/
  625. #if !defined(__DOXYGEN__)
  626. #if STM32_I2C_USE_I2C1
  627. extern I2CDriver I2CD1;
  628. #endif
  629. #if STM32_I2C_USE_I2C2
  630. extern I2CDriver I2CD2;
  631. #endif
  632. #if STM32_I2C_USE_I2C3
  633. extern I2CDriver I2CD3;
  634. #endif
  635. #if STM32_I2C_USE_I2C4
  636. extern I2CDriver I2CD4;
  637. #endif
  638. #endif /* !defined(__DOXYGEN__) */
  639. #ifdef __cplusplus
  640. extern "C" {
  641. #endif
  642. void i2c_lld_init(void);
  643. void i2c_lld_start(I2CDriver *i2cp);
  644. void i2c_lld_stop(I2CDriver *i2cp);
  645. msg_t i2c_lld_master_transmit_timeout(I2CDriver *i2cp, i2caddr_t addr,
  646. const uint8_t *txbuf, size_t txbytes,
  647. uint8_t *rxbuf, size_t rxbytes,
  648. systime_t timeout);
  649. msg_t i2c_lld_master_receive_timeout(I2CDriver *i2cp, i2caddr_t addr,
  650. uint8_t *rxbuf, size_t rxbytes,
  651. systime_t timeout);
  652. #if HAL_USE_I2C_LOCK /* I2C slave mode support */
  653. void i2c_lld_lock(I2CDriver *i2cp, systime_t lockDuration);
  654. #endif
  655. #if HAL_USE_I2C_SLAVE /* I2C slave mode support */
  656. msg_t i2c_lld_matchAddress(I2CDriver *i2cp, i2caddr_t i2cadr);
  657. void i2c_lld_unmatchAddress(I2CDriver *i2cp, i2caddr_t i2cadr);
  658. void i2c_lld_unmatchAll(I2CDriver *i2cp);
  659. void i2c_lld_slaveReceive(I2CDriver *i2cp, const I2CSlaveMsg *rxMsg);
  660. void i2c_lld_slaveReply(I2CDriver *i2cp, const I2CSlaveMsg *replyMsg);
  661. #if STM32_I2C_DEBUG_ENABLE
  662. void i2cPrintQ(BaseSequentialStream *chp); // Debugging routine
  663. #endif /* STM32_I2C_DEBUG_ENABLE */
  664. #endif /* HAL_USE_I2C_SLAVE */
  665. #ifdef __cplusplus
  666. }
  667. #endif
  668. #endif /* HAL_USE_I2C */
  669. #endif /* _I2C_LLD_H_ */
  670. /** @} */