at32f423_i2c.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  1. /**
  2. **************************************************************************
  3. * @file at32f423_i2c.c
  4. * @brief contains all the functions for the i2c firmware library
  5. **************************************************************************
  6. * Copyright notice & Disclaimer
  7. *
  8. * The software Board Support Package (BSP) that is made available to
  9. * download from Artery official website is the copyrighted work of Artery.
  10. * Artery authorizes customers to use, copy, and distribute the BSP
  11. * software and its related documentation for the purpose of design and
  12. * development in conjunction with Artery microcontrollers. Use of the
  13. * software is governed by this copyright notice and the following disclaimer.
  14. *
  15. * THIS SOFTWARE IS PROVIDED ON "AS IS" BASIS WITHOUT WARRANTIES,
  16. * GUARANTEES OR REPRESENTATIONS OF ANY KIND. ARTERY EXPRESSLY DISCLAIMS,
  17. * TO THE FULLEST EXTENT PERMITTED BY LAW, ALL EXPRESS, IMPLIED OR
  18. * STATUTORY OR OTHER WARRANTIES, GUARANTEES OR REPRESENTATIONS,
  19. * INCLUDING BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
  21. *
  22. **************************************************************************
  23. */
  24. #include "at32f423_conf.h"
  25. /** @addtogroup AT32F423_periph_driver
  26. * @{
  27. */
  28. /** @defgroup I2C
  29. * @brief I2C driver modules
  30. * @{
  31. */
  32. #ifdef I2C_MODULE_ENABLED
  33. /** @defgroup I2C_private_functions
  34. * @{
  35. */
  36. /**
  37. * @brief reset the i2c register
  38. * @param i2c_x: to select the i2c peripheral.
  39. * this parameter can be one of the following values:
  40. * I2C1, I2C2, I2C3.
  41. * @retval none
  42. */
  43. void i2c_reset(i2c_type *i2c_x)
  44. {
  45. if(i2c_x == I2C1)
  46. {
  47. crm_periph_reset(CRM_I2C1_PERIPH_RESET, TRUE);
  48. crm_periph_reset(CRM_I2C1_PERIPH_RESET, FALSE);
  49. }
  50. else if(i2c_x == I2C2)
  51. {
  52. crm_periph_reset(CRM_I2C2_PERIPH_RESET, TRUE);
  53. crm_periph_reset(CRM_I2C2_PERIPH_RESET, FALSE);
  54. }
  55. else if(i2c_x == I2C3)
  56. {
  57. crm_periph_reset(CRM_I2C3_PERIPH_RESET, TRUE);
  58. crm_periph_reset(CRM_I2C3_PERIPH_RESET, FALSE);
  59. }
  60. }
  61. /**
  62. * @brief init i2c digit filters and clock control register.
  63. * @param i2c_x: to select the i2c peripheral.
  64. * this parameter can be one of the following values:
  65. * I2C1, I2C2, I2C3.
  66. * @param dfilters: number of digit filters (0x00~0x0F).
  67. * @param clk: i2c clock control register (0x00000000~0xFFFFFFFF).
  68. * @retval none
  69. */
  70. void i2c_init(i2c_type *i2c_x, uint8_t dfilters, uint32_t clk)
  71. {
  72. /* disable i2c peripheral */
  73. i2c_x->ctrl1_bit.i2cen = FALSE;
  74. /* write clkctrl register*/
  75. i2c_x->clkctrl = clk;
  76. /* write digital filter register*/
  77. i2c_x->ctrl1_bit.dflt = dfilters;
  78. }
  79. /**
  80. * @brief config i2c own address 1.
  81. * @param i2c_x: to select the i2c peripheral.
  82. * this parameter can be one of the following values:
  83. * I2C1, I2C2, I2C3.
  84. * @param mode: i2c address mode.
  85. * this parameter can be one of the following values:
  86. * - I2C_ADDRESS_MODE_7BIT: 7bit address.
  87. * - I2C_ADDRESS_MODE_10BIT: 10bit address.
  88. * @param address: own address 1, such as 0xB0.
  89. * @retval none
  90. */
  91. void i2c_own_address1_set(i2c_type *i2c_x, i2c_address_mode_type mode, uint16_t address)
  92. {
  93. /* config address mode */
  94. i2c_x->oaddr1_bit.addr1mode = mode;
  95. /* config address */
  96. i2c_x->oaddr1_bit.addr1 = address & 0x03FF;
  97. /* enable address */
  98. i2c_x->oaddr1_bit.addr1en = TRUE;
  99. }
  100. /**
  101. * @brief config i2c own address 2 and mask.
  102. * @param i2c_x: to select the i2c peripheral.
  103. * this parameter can be one of the following values:
  104. * I2C1, I2C2, I2C3.
  105. * @param address: own address 1, such as 0xC0.
  106. * @param mask: own address 2 mask.
  107. * this parameter can be one of the following values:
  108. * - I2C_ADDR2_NOMASK: compare bit [7:1].
  109. * - I2C_ADDR2_MASK01: only compare bit [7:2].
  110. * - I2C_ADDR2_MASK02: only compare bit [7:2].
  111. * - I2C_ADDR2_MASK03: only compare bit [7:3].
  112. * - I2C_ADDR2_MASK04: only compare bit [7:4].
  113. * - I2C_ADDR2_MASK05: only compare bit [7:5].
  114. * - I2C_ADDR2_MASK06: only compare bit [7:6].
  115. * - I2C_ADDR2_MASK07: only compare bit [7].
  116. * @retval none
  117. */
  118. void i2c_own_address2_set(i2c_type *i2c_x, uint8_t address, i2c_addr2_mask_type mask)
  119. {
  120. i2c_x->oaddr2_bit.addr2mask = mask;
  121. i2c_x->oaddr2_bit.addr2 = (address >> 1) & 0x7F;
  122. }
  123. /**
  124. * @brief enable or disable own address 2.
  125. * @param i2c_x: to select the i2c peripheral.
  126. * this parameter can be one of the following values:
  127. * I2C1, I2C2, I2C3.
  128. * @param new_state (TRUE or FALSE).
  129. * @retval none
  130. */
  131. void i2c_own_address2_enable(i2c_type *i2c_x, confirm_state new_state)
  132. {
  133. i2c_x->oaddr2_bit.addr2en = new_state;
  134. }
  135. /**
  136. * @brief enable or disable smbus mode.
  137. * @param i2c_x: to select the i2c peripheral.
  138. * this parameter can be one of the following values:
  139. * I2C1, I2C2, I2C3.
  140. * @param mode: smbus device mode.
  141. * this parameter can be one of the following values:
  142. * - I2C_SMBUS_MODE_DEVICE: smbus device.
  143. * - I2C_SMBUS_MODE_HOST: smbus host.
  144. * @param new_state (TRUE or FALSE).
  145. * @retval none
  146. */
  147. void i2c_smbus_enable(i2c_type *i2c_x, i2c_smbus_mode_type mode, confirm_state new_state)
  148. {
  149. switch (mode)
  150. {
  151. case I2C_SMBUS_MODE_DEVICE:
  152. i2c_x->ctrl1_bit.devaddren = new_state;
  153. break;
  154. case I2C_SMBUS_MODE_HOST:
  155. i2c_x->ctrl1_bit.haddren = new_state;
  156. break;
  157. default:
  158. break;
  159. }
  160. }
  161. /**
  162. * @brief enable or disable peripheral.
  163. * @param i2c_x: to select the i2c peripheral.
  164. * this parameter can be one of the following values:
  165. * I2C1, I2C2, I2C3.
  166. * @param new_state (TRUE or FALSE).
  167. * @retval none
  168. */
  169. void i2c_enable(i2c_type *i2c_x, confirm_state new_state)
  170. {
  171. i2c_x->ctrl1_bit.i2cen = new_state;
  172. }
  173. /**
  174. * @brief enable or disable clock stretch.
  175. * @param i2c_x: to select the i2c peripheral.
  176. * this parameter can be one of the following values:
  177. * I2C1, I2C2, I2C3.
  178. * @param new_state (TRUE or FALSE).
  179. * @retval none
  180. */
  181. void i2c_clock_stretch_enable(i2c_type *i2c_x, confirm_state new_state)
  182. {
  183. i2c_x->ctrl1_bit.stretch = (!new_state);
  184. }
  185. /**
  186. * @brief enable or disable ack.
  187. * @param i2c_x: to select the i2c peripheral.
  188. * this parameter can be one of the following values:
  189. * I2C1, I2C2, I2C3.
  190. * @param new_state (TRUE or FALSE).
  191. * @retval none.
  192. */
  193. void i2c_ack_enable(i2c_type *i2c_x, confirm_state new_state)
  194. {
  195. i2c_x->ctrl2_bit.nacken = (!new_state);
  196. }
  197. /**
  198. * @brief enable or disable 10-bit address mode (master transfer).
  199. * @param i2c_x: to select the i2c peripheral.
  200. * this parameter can be one of the following values:
  201. * I2C1, I2C2, I2C3.
  202. * @param new_state (TRUE or FALSE).
  203. * @retval none
  204. */
  205. void i2c_addr10_mode_enable(i2c_type *i2c_x, confirm_state new_state)
  206. {
  207. i2c_x->ctrl2_bit.addr10 = new_state;
  208. }
  209. /**
  210. * @brief config the slave address to be transmitted.
  211. * @param i2c_x: to select the i2c peripheral.
  212. * this parameter can be one of the following values:
  213. * I2C1, I2C2, I2C3.
  214. * @param address: slave address.
  215. * @retval none
  216. */
  217. void i2c_transfer_addr_set(i2c_type *i2c_x, uint16_t address)
  218. {
  219. i2c_x->ctrl2_bit.saddr = address & 0x03FF;
  220. }
  221. /**
  222. * @brief get the slave address to be transmitted.
  223. * @param i2c_x: to select the i2c peripheral.
  224. * this parameter can be one of the following values:
  225. * I2C1, I2C2, I2C3.
  226. * @retval slave address
  227. */
  228. uint16_t i2c_transfer_addr_get(i2c_type *i2c_x)
  229. {
  230. return i2c_x->ctrl2_bit.saddr;
  231. }
  232. /**
  233. * @brief config the master transfer direction.
  234. * @param i2c_x: to select the i2c peripheral.
  235. * this parameter can be one of the following values:
  236. * I2C1, I2C2, I2C3.
  237. * @param i2c_direction: transfer request direction.
  238. * this parameter can be one of the following values:
  239. * - I2C_DIR_TRANSMIT: master request a write transfer.
  240. * - I2C_DIR_RECEIVE: master request a read transfer.
  241. * @retval none
  242. */
  243. void i2c_transfer_dir_set(i2c_type *i2c_x, i2c_transfer_dir_type i2c_direction)
  244. {
  245. i2c_x->ctrl2_bit.dir = i2c_direction;
  246. }
  247. /**
  248. * @brief slave get the i2c transfer direction.
  249. * @param i2c_x: to select the i2c peripheral.
  250. * this parameter can be one of the following values:
  251. * I2C1, I2C2, I2C3.
  252. * @retval the value of the slave direction
  253. * - I2C_DIR_TRANSMIT: master request a write transfer, slave enters receiver mode.
  254. * - I2C_DIR_RECEIVE: master request a read transfer, slave enters transmitter mode.
  255. */
  256. i2c_transfer_dir_type i2c_transfer_dir_get(i2c_type *i2c_x)
  257. {
  258. if (i2c_x->sts_bit.sdir == 0)
  259. {
  260. return I2C_DIR_TRANSMIT;
  261. }
  262. else
  263. {
  264. return I2C_DIR_RECEIVE;
  265. }
  266. }
  267. /**
  268. * @brief get the i2c slave matched address.
  269. * @param i2c_x: to select the i2c peripheral.
  270. * this parameter can be one of the following values:
  271. * I2C1, I2C2, I2C3.
  272. * @retval slave matched address.
  273. */
  274. uint8_t i2c_matched_addr_get(i2c_type *i2c_x)
  275. {
  276. return (i2c_x->sts_bit.addr << 1);
  277. }
  278. /**
  279. * @brief enable or disable auto send stop mode.
  280. * @param i2c_x: to select the i2c peripheral.
  281. * this parameter can be one of the following values:
  282. * I2C1, I2C2, I2C3.
  283. * @param new_state (TRUE or FALSE).
  284. * @retval none
  285. */
  286. void i2c_auto_stop_enable(i2c_type *i2c_x, confirm_state new_state)
  287. {
  288. i2c_x->ctrl2_bit.astopen = new_state;
  289. }
  290. /**
  291. * @brief enable or disable cnt reload mode.
  292. * @param i2c_x: to select the i2c peripheral.
  293. * this parameter can be one of the following values:
  294. * I2C1, I2C2, I2C3.
  295. * @param new_state (TRUE or FALSE).
  296. * @retval none
  297. */
  298. void i2c_reload_enable(i2c_type *i2c_x, confirm_state new_state)
  299. {
  300. i2c_x->ctrl2_bit.rlden = new_state;
  301. }
  302. /**
  303. * @brief config the transfer cnt .
  304. * @param i2c_x: to select the i2c peripheral.
  305. * this parameter can be one of the following values:
  306. * I2C1, I2C2, I2C3.
  307. * @param cnt: transfer cnt.
  308. * @retval none
  309. */
  310. void i2c_cnt_set(i2c_type *i2c_x, uint8_t cnt)
  311. {
  312. i2c_x->ctrl2_bit.cnt = cnt;
  313. }
  314. /**
  315. * @brief enable or disable read 10-bit header, this mode
  316. * only used in 10-bit address mode read.
  317. * @param i2c_x: to select the i2c peripheral.
  318. * this parameter can be one of the following values:
  319. * I2C1, I2C2, I2C3.
  320. * @param new_state (TRUE or FALSE).
  321. * @retval none
  322. */
  323. void i2c_addr10_header_enable(i2c_type *i2c_x, confirm_state new_state)
  324. {
  325. i2c_x->ctrl2_bit.readh10 = new_state;
  326. }
  327. /**
  328. * @brief enable or disable general call mode.
  329. * @param i2c_x: to select the i2c peripheral.
  330. * this parameter can be one of the following values:
  331. * I2C1, I2C2, I2C3.
  332. * @param new_state (TRUE or FALSE).
  333. * @retval none
  334. */
  335. void i2c_general_call_enable(i2c_type *i2c_x, confirm_state new_state)
  336. {
  337. i2c_x->ctrl1_bit.gcaen = new_state;
  338. }
  339. /**
  340. * @brief drives the smbus alert pin high or low.
  341. * @param i2c_x: to select the i2c peripheral.
  342. * this parameter can be one of the following values:
  343. * I2C1, I2C2, I2C3.
  344. * @param level
  345. * this parameter can be one of the following values:
  346. * - I2C_SMBUS_ALERT_LOW: smbus alert set low.
  347. * - I2C_SMBUS_ALERT_HIGH: smbus alert set high.
  348. * @retval none
  349. */
  350. void i2c_smbus_alert_set(i2c_type *i2c_x, i2c_smbus_alert_set_type level)
  351. {
  352. i2c_x->ctrl1_bit.smbalert = level;
  353. }
  354. /**
  355. * @brief enable or disable slave data control.
  356. * @param i2c_x: to select the i2c peripheral.
  357. * this parameter can be one of the following values:
  358. * I2C1, I2C2, I2C3.
  359. * @param new_state (TRUE or FALSE).
  360. * @retval none
  361. */
  362. void i2c_slave_data_ctrl_enable(i2c_type *i2c_x, confirm_state new_state)
  363. {
  364. i2c_x->ctrl1_bit.sctrl = new_state;
  365. }
  366. /**
  367. * @brief enable or disable pec calculate.
  368. * @param i2c_x: to select the i2c peripheral.
  369. * this parameter can be one of the following values:
  370. * I2C1, I2C2, I2C3.
  371. * @param new_state (TRUE or FALSE).
  372. * @retval none
  373. */
  374. void i2c_pec_calculate_enable(i2c_type *i2c_x, confirm_state new_state)
  375. {
  376. i2c_x->ctrl1_bit.pecen = new_state;
  377. }
  378. /**
  379. * @brief enable or disable pec transfer.
  380. * @param i2c_x: to select the i2c peripheral.
  381. * this parameter can be one of the following values:
  382. * I2C1, I2C2, I2C3.
  383. * @param new_state (TRUE or FALSE).
  384. * @retval none
  385. */
  386. void i2c_pec_transmit_enable(i2c_type *i2c_x, confirm_state new_state)
  387. {
  388. i2c_x->ctrl2_bit.pecten = new_state;
  389. }
  390. /**
  391. * @brief get the i2c pec value.
  392. * @param i2c_x: to select the i2c peripheral.
  393. * this parameter can be one of the following values:
  394. * I2C1, I2C2, I2C3.
  395. * @retval the value of the pec.
  396. */
  397. uint8_t i2c_pec_value_get(i2c_type *i2c_x)
  398. {
  399. return (uint8_t)(i2c_x->pec_bit.pecval);
  400. }
  401. /**
  402. * @brief config the i2c bus timeout.
  403. * @param i2c_x: to select the i2c peripheral.
  404. * this parameter can be one of the following values:
  405. * I2C1, I2C2, I2C3.
  406. * @param timeout: timeout (0x0000~0x0FFF).
  407. * @retval none
  408. */
  409. void i2c_timeout_set(i2c_type *i2c_x, uint16_t timeout)
  410. {
  411. i2c_x->timeout_bit.totime = timeout;
  412. }
  413. /**
  414. * @brief config the bus timeout detcet level.
  415. * @param i2c_x: to select the i2c peripheral.
  416. * this parameter can be one of the following values:
  417. * I2C1, I2C2, I2C3.
  418. * @param level
  419. * this parameter can be one of the following values:
  420. * - I2C_TIMEOUT_DETCET_HIGH: detect high level timeout.
  421. * - I2C_TIMEOUT_DETCET_LOW: detect low level timeout.
  422. * @retval none
  423. */
  424. void i2c_timeout_detcet_set(i2c_type *i2c_x, i2c_timeout_detcet_type mode)
  425. {
  426. i2c_x->timeout_bit.tomode = mode;
  427. }
  428. /**
  429. * @brief enable or disable bus timeout.
  430. * @param i2c_x: to select the i2c peripheral.
  431. * this parameter can be one of the following values:
  432. * I2C1, I2C2, I2C3.
  433. * @param new_state (TRUE or FALSE).
  434. * @retval none
  435. */
  436. void i2c_timeout_enable(i2c_type *i2c_x, confirm_state new_state)
  437. {
  438. i2c_x->timeout_bit.toen = new_state;
  439. }
  440. /**
  441. * @brief config the i2c extend bus timeout.
  442. * @param i2c_x: to select the i2c peripheral.
  443. * this parameter can be one of the following values:
  444. * I2C1, I2C2, I2C3.
  445. * @param timeout: extend timeout (0x0000~0x0FFF).
  446. * @retval none
  447. */
  448. void i2c_ext_timeout_set(i2c_type *i2c_x, uint16_t timeout)
  449. {
  450. i2c_x->timeout_bit.exttime = timeout;
  451. }
  452. /**
  453. * @brief enable or disable extend bus timeout.
  454. * @param i2c_x: to select the i2c peripheral.
  455. * this parameter can be one of the following values:
  456. * I2C1, I2C2, I2C3.
  457. * @param new_state (TRUE or FALSE).
  458. * @retval none
  459. */
  460. void i2c_ext_timeout_enable(i2c_type *i2c_x, confirm_state new_state)
  461. {
  462. i2c_x->timeout_bit.exten = new_state;
  463. }
  464. /**
  465. * @brief enable or disable interrupts.
  466. * @param i2c_x: to select the i2c peripheral.
  467. * this parameter can be one of the following values:
  468. * I2C1, I2C2, I2C3.
  469. * @param i2c_int: interrupts sources.
  470. * this parameter can be one of the following values:
  471. * - I2C_TD_INT: transmit data interrupt.
  472. * - I2C_RD_INT: receive data interrupt.
  473. * - I2C_ADDR_INT: address match interrupt.
  474. * - I2C_ACKFIAL_INT: ack fail interrupt.
  475. * - I2C_STOP_INT: stop detect interrupt.
  476. * - I2C_TDC_INT: transmit data complete interrupt.
  477. * - I2C_ERR_INT: bus error interrupt.
  478. * @param new_state (TRUE or FALSE).
  479. * @retval none
  480. */
  481. void i2c_interrupt_enable(i2c_type *i2c_x, uint32_t source, confirm_state new_state)
  482. {
  483. if (new_state != FALSE)
  484. {
  485. i2c_x->ctrl1 |= source;
  486. }
  487. else
  488. {
  489. i2c_x->ctrl1 &= (uint32_t)~source;
  490. }
  491. }
  492. /**
  493. * @brief get interrupt status
  494. * @param i2c_x: to select the i2c peripheral.
  495. * this parameter can be one of the following values:
  496. * I2C1, I2C2, I2C3.
  497. * @param source
  498. * this parameter can be one of the following values:
  499. * - I2C_TD_INT: transmit data interrupt.
  500. * - I2C_RD_INT: receive data interrupt.
  501. * - I2C_ADDR_INT: address match interrupt.
  502. * - I2C_ACKFIAL_INT: ack fail interrupt.
  503. * - I2C_STOP_INT: stop detect interrupt.
  504. * - I2C_TDC_INT: transmit data complete interrupt.
  505. * - I2C_ERR_INT: bus error interrupt.
  506. * @retval flag_status (SET or RESET)
  507. */
  508. flag_status i2c_interrupt_get(i2c_type *i2c_x, uint16_t source)
  509. {
  510. if((i2c_x->ctrl1 & source) != RESET)
  511. {
  512. return SET;
  513. }
  514. else
  515. {
  516. return RESET;
  517. }
  518. }
  519. /**
  520. * @brief enable or disable dma requests.
  521. * @param i2c_x: to select the i2c peripheral.
  522. * this parameter can be one of the following values:
  523. * I2C1, I2C2, I2C3.
  524. * @param dma_req: dma transfer request.
  525. * this parameter can be one of the following values:
  526. * - I2C_DMA_REQUEST_TX: dma transmit request.
  527. * - I2C_DMA_REQUEST_RX: dma receive request.
  528. * @param new_state (TRUE or FALSE).
  529. * @retval none
  530. */
  531. void i2c_dma_enable(i2c_type *i2c_x, i2c_dma_request_type dma_req, confirm_state new_state)
  532. {
  533. if(dma_req == I2C_DMA_REQUEST_TX)
  534. {
  535. i2c_x->ctrl1_bit.dmaten = new_state;
  536. }
  537. else
  538. {
  539. i2c_x->ctrl1_bit.dmaren = new_state;
  540. }
  541. }
  542. /**
  543. * @brief config data transfer.
  544. * @param i2c_x: to select the i2c peripheral.
  545. * this parameter can be one of the following values:
  546. * I2C1, I2C2, I2C3.
  547. * @param address: slave address.
  548. * @param cnt: transfer conuter(0~255)
  549. * @param rld_stop: config reload and gen stop condition mode.
  550. * this parameter can be one of the following values:
  551. * - I2C_AUTO_STOP_MODE: auto generate stop mode.
  552. * - I2C_SOFT_STOP_MODE: soft generate stop mode.
  553. * - I2C_RELOAD_MODE: reload mode.
  554. * @param start: config gen start condition mode.
  555. * this parameter can be one of the following values:
  556. * - I2C_WITHOUT_START: transfer data without start condition.
  557. * - I2C_GEN_START_READ: read data and generate start.
  558. * - I2C_GEN_START_WRITE: send data and generate start.
  559. * @retval none
  560. */
  561. void i2c_transmit_set(i2c_type *i2c_x, uint16_t address, uint8_t cnt, i2c_reload_stop_mode_type rld_stop, i2c_start_mode_type start)
  562. {
  563. uint32_t temp;
  564. /* copy ctrl2 value to temp */
  565. temp = i2c_x->ctrl2;
  566. /* clear ctrl2_bit specific bits */
  567. temp &= ~0x03FF67FF;
  568. /* transfer mode and address set */
  569. temp |= address | rld_stop | start;
  570. /* transfer counter set */
  571. temp |= (uint32_t)cnt << 16;
  572. /* update ctrl2 value */
  573. i2c_x->ctrl2 = temp;
  574. }
  575. /**
  576. * @brief generate start condition.
  577. * @param i2c_x: to select the i2c peripheral.
  578. * this parameter can be one of the following values:
  579. * I2C1, I2C2, I2C3.
  580. * @retval none
  581. */
  582. void i2c_start_generate(i2c_type *i2c_x)
  583. {
  584. i2c_x->ctrl2_bit.genstart = TRUE;
  585. }
  586. /**
  587. * @brief generate stop condition.
  588. * @param i2c_x: to select the i2c peripheral.
  589. * this parameter can be one of the following values:
  590. * I2C1, I2C2, I2C3.
  591. * @retval none
  592. */
  593. void i2c_stop_generate(i2c_type *i2c_x)
  594. {
  595. i2c_x->ctrl2_bit.genstop = TRUE;
  596. }
  597. /**
  598. * @brief send a byte through the i2c periph.
  599. * @param i2c_x: to select the i2c peripheral.
  600. * this parameter can be one of the following values:
  601. * I2C1, I2C2, I2C3.
  602. * @param data: byte to be transmitted.
  603. * @retval none
  604. */
  605. void i2c_data_send(i2c_type *i2c_x, uint8_t data)
  606. {
  607. i2c_x->txdt = data;
  608. }
  609. /**
  610. * @brief receive a byte through the i2c periph.
  611. * @param i2c_x: to select the i2c peripheral.
  612. * this parameter can be one of the following values:
  613. * I2C1, I2C2, I2C3.
  614. * @retval the value of the received data.
  615. */
  616. uint8_t i2c_data_receive(i2c_type *i2c_x)
  617. {
  618. return (uint8_t)i2c_x->rxdt;
  619. }
  620. /**
  621. * @brief get flag status.
  622. * @param i2c_x: to select the i2c peripheral.
  623. * this parameter can be one of the following values:
  624. * I2C1, I2C2, I2C3.
  625. * @param flag: specifies the flag to check.
  626. * this parameter can be one of the following values:
  627. * - I2C_TDBE_FLAG: transmit data buffer empty flag.
  628. * - I2C_TDIS_FLAG: send interrupt status.
  629. * - I2C_RDBF_FLAG: receive data buffer full flag.
  630. * - I2C_ADDRF_FLAG: 0~7 bit address match flag.
  631. * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
  632. * - I2C_STOPF_FLAG: stop condition generation complete flag.
  633. * - I2C_TDC_FLAG: transmit data complete flag.
  634. * - I2C_TCRLD_FLAG: transmission is complete, waiting to load data.
  635. * - I2C_BUSERR_FLAG: bus error flag.
  636. * - I2C_ARLOST_FLAG: arbitration lost flag.
  637. * - I2C_OUF_FLAG: overflow or underflow flag.
  638. * - I2C_PECERR_FLAG: pec receive error flag.
  639. * - I2C_TMOUT_FLAG: smbus timeout flag.
  640. * - I2C_ALERTF_FLAG: smbus alert flag.
  641. * - I2C_BUSYF_FLAG: bus busy flag transmission mode.
  642. * - I2C_SDIR_FLAG: slave data transmit direction.
  643. * @retval the new state of flag (SET or RESET).
  644. */
  645. flag_status i2c_flag_get(i2c_type *i2c_x, uint32_t flag)
  646. {
  647. if((i2c_x->sts & flag) != RESET)
  648. {
  649. return SET;
  650. }
  651. else
  652. {
  653. return RESET;
  654. }
  655. }
  656. /**
  657. * @brief clear flag status
  658. * @param i2c_x: to select the i2c peripheral.
  659. * this parameter can be one of the following values:
  660. * I2C1, I2C2, I2C3.
  661. * @param flag: specifies the flag to clear.
  662. * this parameter can be any combination of the following values:
  663. * - I2C_ADDRF_FLAG: 0~7 bit address match flag.
  664. * - I2C_ACKFAIL_FLAG: acknowledge failure flag.
  665. * - I2C_STOPF_FLAG: stop condition generation complete flag.
  666. * - I2C_BUSERR_FLAG: bus error flag.
  667. * - I2C_ARLOST_FLAG: arbitration lost flag.
  668. * - I2C_OUF_FLAG: overflow or underflow flag.
  669. * - I2C_PECERR_FLAG: pec receive error flag.
  670. * - I2C_TMOUT_FLAG: smbus timeout flag.
  671. * - I2C_ALERTF_FLAG: smbus alert flag.
  672. * @retval none
  673. */
  674. void i2c_flag_clear(i2c_type *i2c_x, uint32_t flag)
  675. {
  676. i2c_x->clr = flag;
  677. }
  678. /**
  679. * @brief enable or disable wakeup from deep sleep mode.
  680. * @param i2c_x: to select the i2c peripheral.
  681. * this parameter can be one of the following values:
  682. * I2C1.
  683. * @param new_state (TRUE or FALSE).
  684. * @retval none
  685. */
  686. void i2c_wakeup_enable(i2c_type *i2c_x, confirm_state new_state)
  687. {
  688. i2c_x->ctrl1_bit.wakeupen = new_state;
  689. }
  690. /**
  691. * @brief enable or disable analog filter.
  692. * @param i2c_x: to select the i2c peripheral.
  693. * this parameter can be one of the following values:
  694. * I2C1.
  695. * @param new_state (TRUE or FALSE).
  696. * @retval none
  697. */
  698. void i2c_analog_filter_enable(i2c_type *i2c_x, confirm_state new_state)
  699. {
  700. i2c_x->ctrl1_bit.angnfoff = !new_state;
  701. }
  702. /**
  703. * @}
  704. */
  705. #endif
  706. /**
  707. * @}
  708. */
  709. /**
  710. * @}
  711. */