at32f425_i2c.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. /**
  2. **************************************************************************
  3. * @file at32f425_i2c.h
  4. * @brief at32f425 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 __AT32F425_I2C_H
  26. #define __AT32F425_I2C_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* includes ------------------------------------------------------------------*/
  31. #include "at32f425.h"
  32. /** @addtogroup AT32F425_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 reserved1 : 2; /* [13:12] */
  185. __IO uint32_t dmaten : 1; /* [14] */
  186. __IO uint32_t dmaren : 1; /* [15] */
  187. __IO uint32_t sctrl : 1; /* [16] */
  188. __IO uint32_t stretch : 1; /* [17] */
  189. __IO uint32_t reserved2 : 1; /* [18] */
  190. __IO uint32_t gcaen : 1; /* [19] */
  191. __IO uint32_t haddren : 1; /* [20] */
  192. __IO uint32_t devaddren : 1; /* [21] */
  193. __IO uint32_t smbalert : 1; /* [22] */
  194. __IO uint32_t pecen : 1; /* [23] */
  195. __IO uint32_t reserved3 : 8; /* [31:24] */
  196. } ctrl1_bit;
  197. };
  198. /**
  199. * @brief i2c ctrl2 register, offset:0x04
  200. */
  201. union
  202. {
  203. __IO uint32_t ctrl2;
  204. struct
  205. {
  206. __IO uint32_t saddr : 10;/* [9:0] */
  207. __IO uint32_t dir : 1; /* [10] */
  208. __IO uint32_t addr10 : 1; /* [11] */
  209. __IO uint32_t readh10 : 1; /* [12] */
  210. __IO uint32_t genstart : 1; /* [13] */
  211. __IO uint32_t genstop : 1; /* [14] */
  212. __IO uint32_t nacken : 1; /* [15] */
  213. __IO uint32_t cnt : 8; /* [23:16] */
  214. __IO uint32_t rlden : 1; /* [24] */
  215. __IO uint32_t astopen : 1; /* [25] */
  216. __IO uint32_t pecten : 1; /* [26] */
  217. __IO uint32_t reserved1 : 5; /* [31:27] */
  218. } ctrl2_bit;
  219. };
  220. /**
  221. * @brief i2c oaddr1 register, offset:0x08
  222. */
  223. union
  224. {
  225. __IO uint32_t oaddr1;
  226. struct
  227. {
  228. __IO uint32_t addr1 : 10;/* [9:0] */
  229. __IO uint32_t addr1mode : 1; /* [10] */
  230. __IO uint32_t reserved1 : 4; /* [14:11] */
  231. __IO uint32_t addr1en : 1; /* [15] */
  232. __IO uint32_t reserved2 : 16;/* [31:16] */
  233. } oaddr1_bit;
  234. };
  235. /**
  236. * @brief i2c oaddr2 register, offset:0x0c
  237. */
  238. union
  239. {
  240. __IO uint32_t oaddr2;
  241. struct
  242. {
  243. __IO uint32_t reserved1 : 1; /* [0] */
  244. __IO uint32_t addr2 : 7; /* [7:1] */
  245. __IO uint32_t addr2mask : 3; /* [10:8] */
  246. __IO uint32_t reserved2 : 4; /* [14:11] */
  247. __IO uint32_t addr2en : 1; /* [15] */
  248. __IO uint32_t reserved3 : 16;/* [31:16] */
  249. } oaddr2_bit;
  250. };
  251. /**
  252. * @brief i2c clkctrl register, offset:0x10
  253. */
  254. union
  255. {
  256. __IO uint32_t clkctrl;
  257. struct
  258. {
  259. __IO uint32_t scll : 8; /* [7:0] */
  260. __IO uint32_t sclh : 8; /* [15:8] */
  261. __IO uint32_t sdad : 4; /* [19:16] */
  262. __IO uint32_t scld : 4; /* [23:20] */
  263. __IO uint32_t divh : 4; /* [27:24] */
  264. __IO uint32_t divl : 4; /* [31:28] */
  265. } clkctrl_bit;
  266. };
  267. /**
  268. * @brief i2c timeout register, offset:0x14
  269. */
  270. union
  271. {
  272. __IO uint32_t timeout;
  273. struct
  274. {
  275. __IO uint32_t totime : 12;/* [11:0] */
  276. __IO uint32_t tomode : 1; /* [12] */
  277. __IO uint32_t reserved1 : 2; /* [14:13] */
  278. __IO uint32_t toen : 1; /* [15] */
  279. __IO uint32_t exttime : 12;/* [27:16] */
  280. __IO uint32_t reserved2 : 3; /* [30:28] */
  281. __IO uint32_t exten : 1; /* [31] */
  282. } timeout_bit;
  283. };
  284. /**
  285. * @brief i2c sts register, offset:0x18
  286. */
  287. union
  288. {
  289. __IO uint32_t sts;
  290. struct
  291. {
  292. __IO uint32_t tdbe : 1; /* [0] */
  293. __IO uint32_t tdis : 1; /* [1] */
  294. __IO uint32_t rdbf : 1; /* [2] */
  295. __IO uint32_t addrf : 1; /* [3] */
  296. __IO uint32_t ackfail : 1; /* [4] */
  297. __IO uint32_t stopf : 1; /* [5] */
  298. __IO uint32_t tdc : 1; /* [6] */
  299. __IO uint32_t tcrld : 1; /* [7] */
  300. __IO uint32_t buserr : 1; /* [8] */
  301. __IO uint32_t arlost : 1; /* [9] */
  302. __IO uint32_t ouf : 1; /* [10] */
  303. __IO uint32_t pecerr : 1; /* [11] */
  304. __IO uint32_t tmout : 1; /* [12] */
  305. __IO uint32_t alertf : 1; /* [13] */
  306. __IO uint32_t reserved1 : 1; /* [14] */
  307. __IO uint32_t busyf : 1; /* [15] */
  308. __IO uint32_t sdir : 1; /* [16] */
  309. __IO uint32_t addr : 7; /* [23:17] */
  310. __IO uint32_t reserved2 : 8; /* [31:24] */
  311. } sts_bit;
  312. };
  313. /**
  314. * @brief i2c clr register, offset:0x1c
  315. */
  316. union
  317. {
  318. __IO uint32_t clr;
  319. struct
  320. {
  321. __IO uint32_t reserved1 : 3; /* [2:0] */
  322. __IO uint32_t addrc : 1; /* [3] */
  323. __IO uint32_t ackfailc : 1; /* [4] */
  324. __IO uint32_t stopc : 1; /* [5] */
  325. __IO uint32_t reserved2 : 2; /* [6:7] */
  326. __IO uint32_t buserrc : 1; /* [8] */
  327. __IO uint32_t arlostc : 1; /* [9] */
  328. __IO uint32_t oufc : 1; /* [10] */
  329. __IO uint32_t pecerrc : 1; /* [11] */
  330. __IO uint32_t tmoutc : 1; /* [12] */
  331. __IO uint32_t alertc : 1; /* [13] */
  332. __IO uint32_t reserved3 : 18;/* [31:14] */
  333. } clr_bit;
  334. };
  335. /**
  336. * @brief i2c pec register, offset:0x20
  337. */
  338. union
  339. {
  340. __IO uint32_t pec;
  341. struct
  342. {
  343. __IO uint32_t pecval : 8; /* [7:0] */
  344. __IO uint32_t reserved1 : 24;/* [31:8] */
  345. } pec_bit;
  346. };
  347. /**
  348. * @brief i2c rxdt register, offset:0x20
  349. */
  350. union
  351. {
  352. __IO uint32_t rxdt;
  353. struct
  354. {
  355. __IO uint32_t dt : 8; /* [7:0] */
  356. __IO uint32_t reserved1 : 24;/* [31:8] */
  357. } rxdt_bit;
  358. };
  359. /**
  360. * @brief i2c txdt register, offset:0x20
  361. */
  362. union
  363. {
  364. __IO uint32_t txdt;
  365. struct
  366. {
  367. __IO uint32_t dt : 8; /* [7:0] */
  368. __IO uint32_t reserved1 : 24;/* [31:8] */
  369. } txdt_bit;
  370. };
  371. } i2c_type;
  372. /**
  373. * @}
  374. */
  375. #define I2C1 ((i2c_type *) I2C1_BASE)
  376. #define I2C2 ((i2c_type *) I2C2_BASE)
  377. /** @defgroup I2C_exported_functions
  378. * @{
  379. */
  380. void i2c_reset(i2c_type *i2c_x);
  381. void i2c_init(i2c_type *i2c_x, uint8_t dfilters, uint32_t clk);
  382. void i2c_own_address1_set(i2c_type *i2c_x, i2c_address_mode_type mode, uint16_t address);
  383. void i2c_own_address2_set(i2c_type *i2c_x, uint8_t address, i2c_addr2_mask_type mask);
  384. void i2c_own_address2_enable(i2c_type *i2c_x, confirm_state new_state);
  385. void i2c_smbus_enable(i2c_type *i2c_x, i2c_smbus_mode_type mode, confirm_state new_state);
  386. void i2c_enable(i2c_type *i2c_x, confirm_state new_state);
  387. void i2c_clock_stretch_enable(i2c_type *i2c_x, confirm_state new_state);
  388. void i2c_ack_enable(i2c_type *i2c_x, confirm_state new_state);
  389. void i2c_addr10_mode_enable(i2c_type *i2c_x, confirm_state new_state);
  390. void i2c_transfer_addr_set(i2c_type *i2c_x, uint16_t address);
  391. uint16_t i2c_transfer_addr_get(i2c_type *i2c_x);
  392. void i2c_transfer_dir_set(i2c_type *i2c_x, i2c_transfer_dir_type i2c_direction);
  393. i2c_transfer_dir_type i2c_transfer_dir_get(i2c_type *i2c_x);
  394. uint8_t i2c_matched_addr_get(i2c_type *i2c_x);
  395. void i2c_auto_stop_enable(i2c_type *i2c_x, confirm_state new_state);
  396. void i2c_reload_enable(i2c_type *i2c_x, confirm_state new_state);
  397. void i2c_cnt_set(i2c_type *i2c_x, uint8_t cnt);
  398. void i2c_addr10_header_enable(i2c_type *i2c_x, confirm_state new_state);
  399. void i2c_general_call_enable(i2c_type *i2c_x, confirm_state new_state);
  400. void i2c_smbus_alert_set(i2c_type *i2c_x, i2c_smbus_alert_set_type level);
  401. void i2c_slave_data_ctrl_enable(i2c_type *i2c_x, confirm_state new_state);
  402. void i2c_pec_calculate_enable(i2c_type *i2c_x, confirm_state new_state);
  403. void i2c_pec_transmit_enable(i2c_type *i2c_x, confirm_state new_state);
  404. uint8_t i2c_pec_value_get(i2c_type *i2c_x);
  405. void i2c_timeout_set(i2c_type *i2c_x, uint16_t timeout);
  406. void i2c_timeout_detcet_set(i2c_type *i2c_x, i2c_timeout_detcet_type mode);
  407. void i2c_timeout_enable(i2c_type *i2c_x, confirm_state new_state);
  408. void i2c_ext_timeout_set(i2c_type *i2c_x, uint16_t timeout);
  409. void i2c_ext_timeout_enable(i2c_type *i2c_x, confirm_state new_state);
  410. void i2c_interrupt_enable(i2c_type *i2c_x, uint32_t source, confirm_state new_state);
  411. flag_status i2c_interrupt_get(i2c_type *i2c_x, uint16_t source);
  412. void i2c_dma_enable(i2c_type *i2c_x, i2c_dma_request_type dma_req, confirm_state new_state);
  413. 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);
  414. void i2c_start_generate(i2c_type *i2c_x);
  415. void i2c_stop_generate(i2c_type *i2c_x);
  416. void i2c_data_send(i2c_type *i2c_x, uint8_t data);
  417. uint8_t i2c_data_receive(i2c_type *i2c_x);
  418. flag_status i2c_flag_get(i2c_type *i2c_x, uint32_t flag);
  419. void i2c_flag_clear(i2c_type *i2c_x, uint32_t flag);
  420. /**
  421. * @}
  422. */
  423. /**
  424. * @}
  425. */
  426. /**
  427. * @}
  428. */
  429. #ifdef __cplusplus
  430. }
  431. #endif
  432. #endif