at32f423_i2c.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  1. /**
  2. **************************************************************************
  3. * @file at32f423_i2c.h
  4. * @brief at32f423 i2c header file
  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. /* define to prevent recursive inclusion -------------------------------------*/
  25. #ifndef __AT32F423_I2C_H
  26. #define __AT32F423_I2C_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* includes ------------------------------------------------------------------*/
  31. #include "at32f423.h"
  32. /** @addtogroup AT32F423_periph_driver
  33. * @{
  34. */
  35. /** @addtogroup I2C
  36. * @{
  37. */
  38. /**
  39. * @brief maximum number of single transfers
  40. */
  41. #define MAX_TRANSFER_CNT 255 /*!< maximum number of single transfers */
  42. /** @defgroup I2C_interrupts_definition
  43. * @brief i2c interrupt
  44. * @{
  45. */
  46. #define I2C_TD_INT ((uint32_t)0x00000002) /*!< i2c transmit data interrupt */
  47. #define I2C_RD_INT ((uint32_t)0x00000004) /*!< i2c receive data interrupt */
  48. #define I2C_ADDR_INT ((uint32_t)0x00000008) /*!< i2c address match interrupt */
  49. #define I2C_ACKFIAL_INT ((uint32_t)0x00000010) /*!< i2c ack fail interrupt */
  50. #define I2C_STOP_INT ((uint32_t)0x00000020) /*!< i2c stop detect interrupt */
  51. #define I2C_TDC_INT ((uint32_t)0x00000040) /*!< i2c transmit data complete interrupt */
  52. #define I2C_ERR_INT ((uint32_t)0x00000080) /*!< i2c bus error interrupt */
  53. /**
  54. * @}
  55. */
  56. /** @defgroup I2C_flags_definition
  57. * @brief i2c flag
  58. * @{
  59. */
  60. #define I2C_TDBE_FLAG ((uint32_t)0x00000001) /*!< i2c transmit data buffer empty flag */
  61. #define I2C_TDIS_FLAG ((uint32_t)0x00000002) /*!< i2c send interrupt status */
  62. #define I2C_RDBF_FLAG ((uint32_t)0x00000004) /*!< i2c receive data buffer full flag */
  63. #define I2C_ADDRF_FLAG ((uint32_t)0x00000008) /*!< i2c 0~7 bit address match flag */
  64. #define I2C_ACKFAIL_FLAG ((uint32_t)0x00000010) /*!< i2c acknowledge failure flag */
  65. #define I2C_STOPF_FLAG ((uint32_t)0x00000020) /*!< i2c stop condition generation complete flag */
  66. #define I2C_TDC_FLAG ((uint32_t)0x00000040) /*!< i2c transmit data complete flag */
  67. #define I2C_TCRLD_FLAG ((uint32_t)0x00000080) /*!< i2c transmission is complete, waiting to load data */
  68. #define I2C_BUSERR_FLAG ((uint32_t)0x00000100) /*!< i2c bus error flag */
  69. #define I2C_ARLOST_FLAG ((uint32_t)0x00000200) /*!< i2c arbitration lost flag */
  70. #define I2C_OUF_FLAG ((uint32_t)0x00000400) /*!< i2c overflow or underflow flag */
  71. #define I2C_PECERR_FLAG ((uint32_t)0x00000800) /*!< i2c pec receive error flag */
  72. #define I2C_TMOUT_FLAG ((uint32_t)0x00001000) /*!< i2c smbus timeout flag */
  73. #define I2C_ALERTF_FLAG ((uint32_t)0x00002000) /*!< i2c smbus alert flag */
  74. #define I2C_BUSYF_FLAG ((uint32_t)0x00008000) /*!< i2c bus busy flag transmission mode */
  75. #define I2C_SDIR_FLAG ((uint32_t)0x00010000) /*!< i2c slave data transmit direction */
  76. /**
  77. * @}
  78. */
  79. /** @defgroup I2C_exported_types
  80. * @{
  81. */
  82. /**
  83. * @brief i2c smbus mode set
  84. */
  85. typedef enum
  86. {
  87. I2C_SMBUS_MODE_DEVICE = 0x00, /*!< smbus device mode */
  88. I2C_SMBUS_MODE_HOST = 0x01 /*!< smbus host mode */
  89. } i2c_smbus_mode_type;
  90. /**
  91. * @brief i2c address mode
  92. */
  93. typedef enum
  94. {
  95. I2C_ADDRESS_MODE_7BIT = 0x00, /*!< 7bit address mode */
  96. I2C_ADDRESS_MODE_10BIT = 0x01 /*!< 10bit address mode */
  97. } i2c_address_mode_type;
  98. /**
  99. * @brief i2c transfer direction
  100. */
  101. typedef enum
  102. {
  103. I2C_DIR_TRANSMIT = 0x00, /*!< master request a write transfer */
  104. I2C_DIR_RECEIVE = 0x01 /*!< master request a read transfer */
  105. } i2c_transfer_dir_type;
  106. /**
  107. * @brief i2c dma requests direction
  108. */
  109. typedef enum
  110. {
  111. I2C_DMA_REQUEST_TX = 0x00, /*!< dma transmit request */
  112. I2C_DMA_REQUEST_RX = 0x01 /*!< dma receive request */
  113. } i2c_dma_request_type;
  114. /**
  115. * @brief i2c smbus alert pin set
  116. */
  117. typedef enum
  118. {
  119. I2C_SMBUS_ALERT_HIGH = 0x00, /*!< smbus alert pin set high */
  120. I2C_SMBUS_ALERT_LOW = 0x01 /*!< smbus alert pin set low */
  121. } i2c_smbus_alert_set_type;
  122. /**
  123. * @brief i2c clock timeout detection mode
  124. */
  125. typedef enum
  126. {
  127. I2C_TIMEOUT_DETCET_LOW = 0x00, /*!< detect low level timeout */
  128. I2C_TIMEOUT_DETCET_HIGH = 0x01 /*!< detect high level timeout */
  129. } i2c_timeout_detcet_type;
  130. /**
  131. * @brief i2c own address2 mask
  132. */
  133. typedef enum
  134. {
  135. I2C_ADDR2_NOMASK = 0x00, /*!< compare bit [7:1] */
  136. I2C_ADDR2_MASK01 = 0x01, /*!< only compare bit [7:2] */
  137. I2C_ADDR2_MASK02 = 0x02, /*!< only compare bit [7:2] */
  138. I2C_ADDR2_MASK03 = 0x03, /*!< only compare bit [7:3] */
  139. I2C_ADDR2_MASK04 = 0x04, /*!< only compare bit [7:4] */
  140. I2C_ADDR2_MASK05 = 0x05, /*!< only compare bit [7:5] */
  141. I2C_ADDR2_MASK06 = 0x06, /*!< only compare bit [7:6] */
  142. I2C_ADDR2_MASK07 = 0x07 /*!< only compare bit [7] */
  143. } i2c_addr2_mask_type;
  144. /**
  145. * @brief i2c reload end mode
  146. */
  147. typedef enum
  148. {
  149. I2C_AUTO_STOP_MODE = 0x02000000, /*!< auto generate stop mode */
  150. I2C_SOFT_STOP_MODE = 0x00000000, /*!< soft generate stop mode */
  151. I2C_RELOAD_MODE = 0x01000000 /*!< reload mode */
  152. } i2c_reload_stop_mode_type;
  153. /**
  154. * @brief i2c start mode
  155. */
  156. typedef enum
  157. {
  158. I2C_WITHOUT_START = 0x00000000, /*!< transfer data without start condition */
  159. I2C_GEN_START_READ = 0x00002400, /*!< read data and generate start */
  160. I2C_GEN_START_WRITE = 0x00002000 /*!< send data and generate start */
  161. } i2c_start_mode_type;
  162. /**
  163. * @brief type define i2c register all
  164. */
  165. typedef struct
  166. {
  167. /**
  168. * @brief i2c ctrl1 register, offset:0x00
  169. */
  170. union
  171. {
  172. __IO uint32_t ctrl1;
  173. struct
  174. {
  175. __IO uint32_t i2cen : 1; /* [0] */
  176. __IO uint32_t tdien : 1; /* [1] */
  177. __IO uint32_t rdien : 1; /* [2] */
  178. __IO uint32_t addrien : 1; /* [3] */
  179. __IO uint32_t ackfailien : 1; /* [4] */
  180. __IO uint32_t stopien : 1; /* [5] */
  181. __IO uint32_t tdcien : 1; /* [6] */
  182. __IO uint32_t errien : 1; /* [7] */
  183. __IO uint32_t dflt : 4; /* [11:8] */
  184. __IO uint32_t angnfoff : 1; /* [12] */
  185. __IO uint32_t reserved1 : 1; /* [13] */
  186. __IO uint32_t dmaten : 1; /* [14] */
  187. __IO uint32_t dmaren : 1; /* [15] */
  188. __IO uint32_t sctrl : 1; /* [16] */
  189. __IO uint32_t stretch : 1; /* [17] */
  190. __IO uint32_t wakeupen : 1; /* [18] */
  191. __IO uint32_t gcaen : 1; /* [19] */
  192. __IO uint32_t haddren : 1; /* [20] */
  193. __IO uint32_t devaddren : 1; /* [21] */
  194. __IO uint32_t smbalert : 1; /* [22] */
  195. __IO uint32_t pecen : 1; /* [23] */
  196. __IO uint32_t reserved3 : 8; /* [31:24] */
  197. } ctrl1_bit;
  198. };
  199. /**
  200. * @brief i2c ctrl2 register, offset:0x04
  201. */
  202. union
  203. {
  204. __IO uint32_t ctrl2;
  205. struct
  206. {
  207. __IO uint32_t saddr : 10;/* [9:0] */
  208. __IO uint32_t dir : 1; /* [10] */
  209. __IO uint32_t addr10 : 1; /* [11] */
  210. __IO uint32_t readh10 : 1; /* [12] */
  211. __IO uint32_t genstart : 1; /* [13] */
  212. __IO uint32_t genstop : 1; /* [14] */
  213. __IO uint32_t nacken : 1; /* [15] */
  214. __IO uint32_t cnt : 8; /* [23:16] */
  215. __IO uint32_t rlden : 1; /* [24] */
  216. __IO uint32_t astopen : 1; /* [25] */
  217. __IO uint32_t pecten : 1; /* [26] */
  218. __IO uint32_t reserved1 : 5; /* [31:27] */
  219. } ctrl2_bit;
  220. };
  221. /**
  222. * @brief i2c oaddr1 register, offset:0x08
  223. */
  224. union
  225. {
  226. __IO uint32_t oaddr1;
  227. struct
  228. {
  229. __IO uint32_t addr1 : 10;/* [9:0] */
  230. __IO uint32_t addr1mode : 1; /* [10] */
  231. __IO uint32_t reserved1 : 4; /* [14:11] */
  232. __IO uint32_t addr1en : 1; /* [15] */
  233. __IO uint32_t reserved2 : 16;/* [31:16] */
  234. } oaddr1_bit;
  235. };
  236. /**
  237. * @brief i2c oaddr2 register, offset:0x0c
  238. */
  239. union
  240. {
  241. __IO uint32_t oaddr2;
  242. struct
  243. {
  244. __IO uint32_t reserved1 : 1; /* [0] */
  245. __IO uint32_t addr2 : 7; /* [7:1] */
  246. __IO uint32_t addr2mask : 3; /* [10:8] */
  247. __IO uint32_t reserved2 : 4; /* [14:11] */
  248. __IO uint32_t addr2en : 1; /* [15] */
  249. __IO uint32_t reserved3 : 16;/* [31:16] */
  250. } oaddr2_bit;
  251. };
  252. /**
  253. * @brief i2c clkctrl register, offset:0x10
  254. */
  255. union
  256. {
  257. __IO uint32_t clkctrl;
  258. struct
  259. {
  260. __IO uint32_t scll : 8; /* [7:0] */
  261. __IO uint32_t sclh : 8; /* [15:8] */
  262. __IO uint32_t sdad : 4; /* [19:16] */
  263. __IO uint32_t scld : 4; /* [23:20] */
  264. __IO uint32_t divh : 4; /* [27:24] */
  265. __IO uint32_t divl : 4; /* [31:28] */
  266. } clkctrl_bit;
  267. };
  268. /**
  269. * @brief i2c timeout register, offset:0x14
  270. */
  271. union
  272. {
  273. __IO uint32_t timeout;
  274. struct
  275. {
  276. __IO uint32_t totime : 12;/* [11:0] */
  277. __IO uint32_t tomode : 1; /* [12] */
  278. __IO uint32_t reserved1 : 2; /* [14:13] */
  279. __IO uint32_t toen : 1; /* [15] */
  280. __IO uint32_t exttime : 12;/* [27:16] */
  281. __IO uint32_t reserved2 : 3; /* [30:28] */
  282. __IO uint32_t exten : 1; /* [31] */
  283. } timeout_bit;
  284. };
  285. /**
  286. * @brief i2c sts register, offset:0x18
  287. */
  288. union
  289. {
  290. __IO uint32_t sts;
  291. struct
  292. {
  293. __IO uint32_t tdbe : 1; /* [0] */
  294. __IO uint32_t tdis : 1; /* [1] */
  295. __IO uint32_t rdbf : 1; /* [2] */
  296. __IO uint32_t addrf : 1; /* [3] */
  297. __IO uint32_t ackfail : 1; /* [4] */
  298. __IO uint32_t stopf : 1; /* [5] */
  299. __IO uint32_t tdc : 1; /* [6] */
  300. __IO uint32_t tcrld : 1; /* [7] */
  301. __IO uint32_t buserr : 1; /* [8] */
  302. __IO uint32_t arlost : 1; /* [9] */
  303. __IO uint32_t ouf : 1; /* [10] */
  304. __IO uint32_t pecerr : 1; /* [11] */
  305. __IO uint32_t tmout : 1; /* [12] */
  306. __IO uint32_t alertf : 1; /* [13] */
  307. __IO uint32_t reserved1 : 1; /* [14] */
  308. __IO uint32_t busyf : 1; /* [15] */
  309. __IO uint32_t sdir : 1; /* [16] */
  310. __IO uint32_t addr : 7; /* [23:17] */
  311. __IO uint32_t reserved2 : 8; /* [31:24] */
  312. } sts_bit;
  313. };
  314. /**
  315. * @brief i2c clr register, offset:0x1c
  316. */
  317. union
  318. {
  319. __IO uint32_t clr;
  320. struct
  321. {
  322. __IO uint32_t reserved1 : 3; /* [2:0] */
  323. __IO uint32_t addrc : 1; /* [3] */
  324. __IO uint32_t ackfailc : 1; /* [4] */
  325. __IO uint32_t stopc : 1; /* [5] */
  326. __IO uint32_t reserved2 : 2; /* [6:7] */
  327. __IO uint32_t buserrc : 1; /* [8] */
  328. __IO uint32_t arlostc : 1; /* [9] */
  329. __IO uint32_t oufc : 1; /* [10] */
  330. __IO uint32_t pecerrc : 1; /* [11] */
  331. __IO uint32_t tmoutc : 1; /* [12] */
  332. __IO uint32_t alertc : 1; /* [13] */
  333. __IO uint32_t reserved3 : 18;/* [31:14] */
  334. } clr_bit;
  335. };
  336. /**
  337. * @brief i2c pec register, offset:0x20
  338. */
  339. union
  340. {
  341. __IO uint32_t pec;
  342. struct
  343. {
  344. __IO uint32_t pecval : 8; /* [7:0] */
  345. __IO uint32_t reserved1 : 24;/* [31:8] */
  346. } pec_bit;
  347. };
  348. /**
  349. * @brief i2c rxdt register, offset:0x20
  350. */
  351. union
  352. {
  353. __IO uint32_t rxdt;
  354. struct
  355. {
  356. __IO uint32_t dt : 8; /* [7:0] */
  357. __IO uint32_t reserved1 : 24;/* [31:8] */
  358. } rxdt_bit;
  359. };
  360. /**
  361. * @brief i2c txdt register, offset:0x20
  362. */
  363. union
  364. {
  365. __IO uint32_t txdt;
  366. struct
  367. {
  368. __IO uint32_t dt : 8; /* [7:0] */
  369. __IO uint32_t reserved1 : 24;/* [31:8] */
  370. } txdt_bit;
  371. };
  372. } i2c_type;
  373. /**
  374. * @}
  375. */
  376. #define I2C1 ((i2c_type *) I2C1_BASE)
  377. #define I2C2 ((i2c_type *) I2C2_BASE)
  378. #define I2C3 ((i2c_type *) I2C3_BASE)
  379. /** @defgroup I2C_exported_functions
  380. * @{
  381. */
  382. void i2c_reset(i2c_type *i2c_x);
  383. void i2c_init(i2c_type *i2c_x, uint8_t dfilters, uint32_t clk);
  384. void i2c_own_address1_set(i2c_type *i2c_x, i2c_address_mode_type mode, uint16_t address);
  385. void i2c_own_address2_set(i2c_type *i2c_x, uint8_t address, i2c_addr2_mask_type mask);
  386. void i2c_own_address2_enable(i2c_type *i2c_x, confirm_state new_state);
  387. void i2c_smbus_enable(i2c_type *i2c_x, i2c_smbus_mode_type mode, confirm_state new_state);
  388. void i2c_enable(i2c_type *i2c_x, confirm_state new_state);
  389. void i2c_clock_stretch_enable(i2c_type *i2c_x, confirm_state new_state);
  390. void i2c_ack_enable(i2c_type *i2c_x, confirm_state new_state);
  391. void i2c_addr10_mode_enable(i2c_type *i2c_x, confirm_state new_state);
  392. void i2c_transfer_addr_set(i2c_type *i2c_x, uint16_t address);
  393. uint16_t i2c_transfer_addr_get(i2c_type *i2c_x);
  394. void i2c_transfer_dir_set(i2c_type *i2c_x, i2c_transfer_dir_type i2c_direction);
  395. i2c_transfer_dir_type i2c_transfer_dir_get(i2c_type *i2c_x);
  396. uint8_t i2c_matched_addr_get(i2c_type *i2c_x);
  397. void i2c_auto_stop_enable(i2c_type *i2c_x, confirm_state new_state);
  398. void i2c_reload_enable(i2c_type *i2c_x, confirm_state new_state);
  399. void i2c_cnt_set(i2c_type *i2c_x, uint8_t cnt);
  400. void i2c_addr10_header_enable(i2c_type *i2c_x, confirm_state new_state);
  401. void i2c_general_call_enable(i2c_type *i2c_x, confirm_state new_state);
  402. void i2c_smbus_alert_set(i2c_type *i2c_x, i2c_smbus_alert_set_type level);
  403. void i2c_slave_data_ctrl_enable(i2c_type *i2c_x, confirm_state new_state);
  404. void i2c_pec_calculate_enable(i2c_type *i2c_x, confirm_state new_state);
  405. void i2c_pec_transmit_enable(i2c_type *i2c_x, confirm_state new_state);
  406. uint8_t i2c_pec_value_get(i2c_type *i2c_x);
  407. void i2c_timeout_set(i2c_type *i2c_x, uint16_t timeout);
  408. void i2c_timeout_detcet_set(i2c_type *i2c_x, i2c_timeout_detcet_type mode);
  409. void i2c_timeout_enable(i2c_type *i2c_x, confirm_state new_state);
  410. void i2c_ext_timeout_set(i2c_type *i2c_x, uint16_t timeout);
  411. void i2c_ext_timeout_enable(i2c_type *i2c_x, confirm_state new_state);
  412. void i2c_interrupt_enable(i2c_type *i2c_x, uint32_t source, confirm_state new_state);
  413. flag_status i2c_interrupt_get(i2c_type *i2c_x, uint16_t source);
  414. void i2c_dma_enable(i2c_type *i2c_x, i2c_dma_request_type dma_req, confirm_state new_state);
  415. 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);
  416. void i2c_start_generate(i2c_type *i2c_x);
  417. void i2c_stop_generate(i2c_type *i2c_x);
  418. void i2c_data_send(i2c_type *i2c_x, uint8_t data);
  419. uint8_t i2c_data_receive(i2c_type *i2c_x);
  420. flag_status i2c_flag_get(i2c_type *i2c_x, uint32_t flag);
  421. void i2c_flag_clear(i2c_type *i2c_x, uint32_t flag);
  422. void i2c_wakeup_enable(i2c_type *i2c_x, confirm_state new_state);
  423. void i2c_analog_filter_enable(i2c_type *i2c_x, confirm_state new_state);
  424. /**
  425. * @}
  426. */
  427. /**
  428. * @}
  429. */
  430. /**
  431. * @}
  432. */
  433. #ifdef __cplusplus
  434. }
  435. #endif
  436. #endif