at32f413_i2c.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**
  2. **************************************************************************
  3. * @file at32f413_i2c.h
  4. * @brief at32f413 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 __AT32F413_I2C_H
  26. #define __AT32F413_I2C_H
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* includes ------------------------------------------------------------------*/
  31. #include "at32f413.h"
  32. /** @addtogroup AT32F413_periph_driver
  33. * @{
  34. */
  35. /** @addtogroup I2C
  36. * @{
  37. */
  38. /** @defgroup I2C_sts1_flags_definition
  39. * @brief i2c sts1 flag
  40. * @{
  41. */
  42. #define I2C_STARTF_FLAG ((uint32_t)0x00000001) /*!< i2c start condition generation complete flag */
  43. #define I2C_ADDR7F_FLAG ((uint32_t)0x00000002) /*!< i2c 0~7 bit address match flag */
  44. #define I2C_TDC_FLAG ((uint32_t)0x00000004) /*!< i2c transmit data complete flag */
  45. #define I2C_ADDRHF_FLAG ((uint32_t)0x00000008) /*!< i2c master 9~8 bit address header match flag */
  46. #define I2C_STOPF_FLAG ((uint32_t)0x00000010) /*!< i2c stop condition generation complete flag */
  47. #define I2C_RDBF_FLAG ((uint32_t)0x00000040) /*!< i2c receive data buffer full flag */
  48. #define I2C_TDBE_FLAG ((uint32_t)0x00000080) /*!< i2c transmit data buffer empty flag */
  49. #define I2C_BUSERR_FLAG ((uint32_t)0x00000100) /*!< i2c bus error flag */
  50. #define I2C_ARLOST_FLAG ((uint32_t)0x00000200) /*!< i2c arbitration lost flag */
  51. #define I2C_ACKFAIL_FLAG ((uint32_t)0x00000400) /*!< i2c acknowledge failure flag */
  52. #define I2C_OUF_FLAG ((uint32_t)0x00000800) /*!< i2c overflow or underflow flag */
  53. #define I2C_PECERR_FLAG ((uint32_t)0x00001000) /*!< i2c pec receive error flag */
  54. #define I2C_TMOUT_FLAG ((uint32_t)0x00004000) /*!< i2c smbus timeout flag */
  55. #define I2C_ALERTF_FLAG ((uint32_t)0x00008000) /*!< i2c smbus alert flag */
  56. /**
  57. * @}
  58. */
  59. /** @defgroup I2C_sts2_flags_definition
  60. * @brief i2c sts2 flag
  61. * @{
  62. */
  63. #define I2C_TRMODE_FLAG ((uint32_t)0x10010000) /*!< i2c transmission mode */
  64. #define I2C_BUSYF_FLAG ((uint32_t)0x10020000) /*!< i2c bus busy flag transmission mode */
  65. #define I2C_DIRF_FLAG ((uint32_t)0x10040000) /*!< i2c transmission direction flag */
  66. #define I2C_GCADDRF_FLAG ((uint32_t)0x10100000) /*!< i2c general call address received flag */
  67. #define I2C_DEVADDRF_FLAG ((uint32_t)0x10200000) /*!< i2c smbus device address received flag */
  68. #define I2C_HOSTADDRF_FLAG ((uint32_t)0x10400000) /*!< i2c smbus host address received flag */
  69. #define I2C_ADDR2_FLAG ((uint32_t)0x10800000) /*!< i2c own address 2 received flag */
  70. /**
  71. * @}
  72. */
  73. /** @defgroup I2C_interrupts_definition
  74. * @brief i2c interrupt
  75. * @{
  76. */
  77. #define I2C_DATA_INT ((uint16_t)0x0400) /*!< i2c data transmission interrupt */
  78. #define I2C_EVT_INT ((uint16_t)0x0200) /*!< i2c event interrupt */
  79. #define I2C_ERR_INT ((uint16_t)0x0100) /*!< i2c error interrupt */
  80. /**
  81. * @}
  82. */
  83. /** @defgroup I2C_exported_types
  84. * @{
  85. */
  86. /**
  87. * @brief i2c master receiving mode acknowledge control
  88. */
  89. typedef enum
  90. {
  91. I2C_MASTER_ACK_CURRENT = 0x00, /*!< acken bit acts on the current byte */
  92. I2C_MASTER_ACK_NEXT = 0x01 /*!< acken bit acts on the next byte */
  93. } i2c_master_ack_type;
  94. /**
  95. * @brief i2c pec position set
  96. */
  97. typedef enum
  98. {
  99. I2C_PEC_POSITION_CURRENT = 0x00, /*!< the current byte is pec */
  100. I2C_PEC_POSITION_NEXT = 0x01 /*!< the next byte is pec */
  101. } i2c_pec_position_type;
  102. /**
  103. * @brief i2c smbus alert pin set
  104. */
  105. typedef enum
  106. {
  107. I2C_SMBUS_ALERT_HIGH = 0x00, /*!< smbus alert pin set high */
  108. I2C_SMBUS_ALERT_LOW = 0x01 /*!< smbus alert pin set low */
  109. } i2c_smbus_alert_set_type;
  110. /**
  111. * @brief i2c smbus mode set
  112. */
  113. typedef enum
  114. {
  115. I2C_SMBUS_MODE_DEVICE = 0x00, /*!< smbus device mode */
  116. I2C_SMBUS_MODE_HOST = 0x01 /*!< smbus host mode */
  117. } i2c_smbus_mode_set_type;
  118. /**
  119. * @brief i2c fast mode duty cycle
  120. */
  121. typedef enum
  122. {
  123. I2C_FSMODE_DUTY_2_1 = 0x00, /*!< duty cycle is 2:1 in fast mode */
  124. I2C_FSMODE_DUTY_16_9 = 0x01 /*!< duty cycle is 16:9 in fast mode */
  125. } i2c_fsmode_duty_cycle_type;
  126. /**
  127. * @brief i2c address mode
  128. */
  129. typedef enum
  130. {
  131. I2C_ADDRESS_MODE_7BIT = 0x00, /*!< 7bit address mode */
  132. I2C_ADDRESS_MODE_10BIT = 0x01 /*!< 10bit address mode */
  133. } i2c_address_mode_type;
  134. /**
  135. * @brief i2c address direction
  136. */
  137. typedef enum
  138. {
  139. I2C_DIRECTION_TRANSMIT = 0x00, /*!< transmit mode */
  140. I2C_DIRECTION_RECEIVE = 0x01 /*!< receive mode */
  141. } i2c_direction_type;
  142. /**
  143. * @brief type define i2c register all
  144. */
  145. typedef struct
  146. {
  147. /**
  148. * @brief i2c ctrl1 register, offset:0x00
  149. */
  150. union
  151. {
  152. __IO uint32_t ctrl1;
  153. struct
  154. {
  155. __IO uint32_t i2cen : 1; /* [0] */
  156. __IO uint32_t permode : 1; /* [1] */
  157. __IO uint32_t reserved1 : 1; /* [2] */
  158. __IO uint32_t smbmode : 1; /* [3] */
  159. __IO uint32_t arpen : 1; /* [4] */
  160. __IO uint32_t pecen : 1; /* [5] */
  161. __IO uint32_t gcaen : 1; /* [6] */
  162. __IO uint32_t stretch : 1; /* [7] */
  163. __IO uint32_t genstart : 1; /* [8] */
  164. __IO uint32_t genstop : 1; /* [9] */
  165. __IO uint32_t acken : 1; /* [10] */
  166. __IO uint32_t mackctrl : 1; /* [11] */
  167. __IO uint32_t pecten : 1; /* [12] */
  168. __IO uint32_t smbalert : 1; /* [13] */
  169. __IO uint32_t reserved2 : 1; /* [14] */
  170. __IO uint32_t reset : 1; /* [15] */
  171. __IO uint32_t reserved3 : 16;/* [31:16] */
  172. } ctrl1_bit;
  173. };
  174. /**
  175. * @brief i2c ctrl2 register, offset:0x04
  176. */
  177. union
  178. {
  179. __IO uint32_t ctrl2;
  180. struct
  181. {
  182. __IO uint32_t clkfreq : 8; /* [7:0] */
  183. __IO uint32_t errien : 1; /* [8] */
  184. __IO uint32_t evtien : 1; /* [9] */
  185. __IO uint32_t dataien : 1; /* [10] */
  186. __IO uint32_t dmaen : 1; /* [11] */
  187. __IO uint32_t dmaend : 1; /* [12] */
  188. __IO uint32_t reserved1 : 19;/* [31:13] */
  189. } ctrl2_bit;
  190. };
  191. /**
  192. * @brief i2c oaddr1 register, offset:0x08
  193. */
  194. union
  195. {
  196. __IO uint32_t oaddr1;
  197. struct
  198. {
  199. __IO uint32_t addr1 : 10;/* [9:0] */
  200. __IO uint32_t reserved1 : 5; /* [14:10] */
  201. __IO uint32_t addr1mode : 1; /* [15] */
  202. __IO uint32_t reserved2 : 16;/* [31:16] */
  203. } oaddr1_bit;
  204. };
  205. /**
  206. * @brief i2c oaddr2 register, offset:0x0C
  207. */
  208. union
  209. {
  210. __IO uint32_t oaddr2;
  211. struct
  212. {
  213. __IO uint32_t addr2en : 1; /* [0] */
  214. __IO uint32_t addr2 : 7; /* [7:1] */
  215. __IO uint32_t reserved1 : 24;/* [31:8] */
  216. } oaddr2_bit;
  217. };
  218. /**
  219. * @brief i2c dt register, offset:0x10
  220. */
  221. union
  222. {
  223. __IO uint32_t dt;
  224. struct
  225. {
  226. __IO uint32_t dt : 8; /* [7:0] */
  227. __IO uint32_t reserved1 : 24;/* [31:8] */
  228. } dt_bit;
  229. };
  230. /**
  231. * @brief i2c sts1 register, offset:0x14
  232. */
  233. union
  234. {
  235. __IO uint32_t sts1;
  236. struct
  237. {
  238. __IO uint32_t startf : 1; /* [0] */
  239. __IO uint32_t addr7f : 1; /* [1] */
  240. __IO uint32_t tdc : 1; /* [2] */
  241. __IO uint32_t addrhf : 1; /* [3] */
  242. __IO uint32_t stopf : 1; /* [4] */
  243. __IO uint32_t reserved1 : 1; /* [5] */
  244. __IO uint32_t rdbf : 1; /* [6] */
  245. __IO uint32_t tdbe : 1; /* [7] */
  246. __IO uint32_t buserr : 1; /* [8] */
  247. __IO uint32_t arlost : 1; /* [9] */
  248. __IO uint32_t ackfail : 1; /* [10] */
  249. __IO uint32_t ouf : 1; /* [11] */
  250. __IO uint32_t pecerr : 1; /* [12] */
  251. __IO uint32_t reserved2 : 1; /* [13] */
  252. __IO uint32_t tmout : 1; /* [14] */
  253. __IO uint32_t alertf : 1; /* [15] */
  254. __IO uint32_t reserved3 : 16; /* [31:16] */
  255. } sts1_bit;
  256. };
  257. /**
  258. * @brief i2c sts2 register, offset:0x18
  259. */
  260. union
  261. {
  262. __IO uint32_t sts2;
  263. struct
  264. {
  265. __IO uint32_t trmode : 1; /* [0] */
  266. __IO uint32_t busyf : 1; /* [1] */
  267. __IO uint32_t dirf : 1; /* [2] */
  268. __IO uint32_t reserved1 : 1; /* [3] */
  269. __IO uint32_t gcaddrf : 1; /* [4] */
  270. __IO uint32_t devaddrf : 1; /* [5] */
  271. __IO uint32_t hostaddrf : 1; /* [6] */
  272. __IO uint32_t addr2 : 1; /* [7] */
  273. __IO uint32_t pecval : 8; /* [15:8] */
  274. __IO uint32_t reserved2 : 16;/* [31:16] */
  275. } sts2_bit;
  276. };
  277. /**
  278. * @brief i2c clkctrl register, offset:0x1C
  279. */
  280. union
  281. {
  282. __IO uint32_t clkctrl;
  283. struct
  284. {
  285. __IO uint32_t speed : 12;/* [11:0] */
  286. __IO uint32_t reserved1 : 2; /* [13:12] */
  287. __IO uint32_t dutymode : 1; /* [14] */
  288. __IO uint32_t speedmode : 1; /* [15] */
  289. __IO uint32_t reserved2 : 16;/* [31:16] */
  290. } clkctrl_bit;
  291. };
  292. /**
  293. * @brief i2c tmrise register, offset:0x20
  294. */
  295. union
  296. {
  297. __IO uint32_t tmrise;
  298. struct
  299. {
  300. __IO uint32_t risetime : 6; /* [5:0] */
  301. __IO uint32_t reserved1 : 26;/* [31:6] */
  302. } tmrise_bit;
  303. };
  304. } i2c_type;
  305. /**
  306. * @}
  307. */
  308. #define I2C1 ((i2c_type *) I2C1_BASE)
  309. #define I2C2 ((i2c_type *) I2C2_BASE)
  310. /** @defgroup I2C_exported_functions
  311. * @{
  312. */
  313. void i2c_reset(i2c_type *i2c_x);
  314. void i2c_software_reset(i2c_type *i2c_x, confirm_state new_state);
  315. void i2c_init(i2c_type *i2c_x, i2c_fsmode_duty_cycle_type duty, uint32_t speed);
  316. void i2c_own_address1_set(i2c_type *i2c_x, i2c_address_mode_type mode, uint16_t address);
  317. void i2c_own_address2_set(i2c_type *i2c_x, uint8_t address);
  318. void i2c_own_address2_enable(i2c_type *i2c_x, confirm_state new_state);
  319. void i2c_smbus_enable(i2c_type *i2c_x, confirm_state new_state);
  320. void i2c_enable(i2c_type *i2c_x, confirm_state new_state);
  321. void i2c_fast_mode_duty_set(i2c_type *i2c_x, i2c_fsmode_duty_cycle_type duty);
  322. void i2c_clock_stretch_enable(i2c_type *i2c_x, confirm_state new_state);
  323. void i2c_ack_enable(i2c_type *i2c_x, confirm_state new_state);
  324. void i2c_master_receive_ack_set(i2c_type *i2c_x, i2c_master_ack_type pos);
  325. void i2c_pec_position_set(i2c_type *i2c_x, i2c_pec_position_type pos);
  326. void i2c_general_call_enable(i2c_type *i2c_x, confirm_state new_state);
  327. void i2c_arp_mode_enable(i2c_type *i2c_x, confirm_state new_state);
  328. void i2c_smbus_mode_set(i2c_type *i2c_x, i2c_smbus_mode_set_type mode);
  329. void i2c_smbus_alert_set(i2c_type *i2c_x, i2c_smbus_alert_set_type level);
  330. void i2c_pec_transmit_enable(i2c_type *i2c_x, confirm_state new_state);
  331. void i2c_pec_calculate_enable(i2c_type *i2c_x, confirm_state new_state);
  332. uint8_t i2c_pec_value_get(i2c_type *i2c_x);
  333. void i2c_dma_end_transfer_set(i2c_type *i2c_x, confirm_state new_state);
  334. void i2c_dma_enable(i2c_type *i2c_x, confirm_state new_state);
  335. void i2c_interrupt_enable(i2c_type *i2c_x, uint16_t source, confirm_state new_state);
  336. void i2c_start_generate(i2c_type *i2c_x);
  337. void i2c_stop_generate(i2c_type *i2c_x);
  338. void i2c_7bit_address_send(i2c_type *i2c_x, uint8_t address, i2c_direction_type direction);
  339. void i2c_data_send(i2c_type *i2c_x, uint8_t data);
  340. uint8_t i2c_data_receive(i2c_type *i2c_x);
  341. flag_status i2c_flag_get(i2c_type *i2c_x, uint32_t flag);
  342. flag_status i2c_interrupt_flag_get(i2c_type *i2c_x, uint32_t flag);
  343. void i2c_flag_clear(i2c_type *i2c_x, uint32_t flag);
  344. /**
  345. * @}
  346. */
  347. /**
  348. * @}
  349. */
  350. /**
  351. * @}
  352. */
  353. #ifdef __cplusplus
  354. }
  355. #endif
  356. #endif