hal_i2c_lld.h 23 KB

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