n32g45x_i2c.h 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_i2c.h
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_I2C_H__
  35. #define __N32G45X_I2C_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "n32g45x.h"
  40. /** @addtogroup N32G45X_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @addtogroup I2C
  44. * @{
  45. */
  46. /** @addtogroup I2C_Exported_Types
  47. * @{
  48. */
  49. /**
  50. * @brief I2C Init structure definition
  51. */
  52. typedef struct
  53. {
  54. uint32_t ClkSpeed; /*!< Specifies the clock frequency.
  55. This parameter must be set to a value lower than 400kHz */
  56. uint16_t BusMode; /*!< Specifies the I2C mode.
  57. This parameter can be a value of @ref I2C_BusMode */
  58. uint16_t FmDutyCycle; /*!< Specifies the I2C fast mode duty cycle.
  59. This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
  60. uint16_t OwnAddr1; /*!< Specifies the first device own address.
  61. This parameter can be a 7-bit or 10-bit address. */
  62. uint16_t AckEnable; /*!< Enables or disables the acknowledgement.
  63. This parameter can be a value of @ref I2C_acknowledgement */
  64. uint16_t AddrMode; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
  65. This parameter can be a value of @ref I2C_acknowledged_address */
  66. } I2C_InitType;
  67. /**
  68. * @}
  69. */
  70. /** @addtogroup I2C_Exported_Constants
  71. * @{
  72. */
  73. #define IS_I2C_PERIPH(PERIPH) (((PERIPH) == I2C1) || ((PERIPH) == I2C2) || ((PERIPH) == I2C3) || ((PERIPH) == I2C4))
  74. /** @addtogroup I2C_BusMode
  75. * @{
  76. */
  77. #define I2C_BUSMODE_I2C ((uint16_t)0x0000)
  78. #define I2C_BUSMODE_SMBDEVICE ((uint16_t)0x0002)
  79. #define I2C_BUSMODE_SMBHOST ((uint16_t)0x000A)
  80. #define IS_I2C_BUS_MODE(MODE) \
  81. (((MODE) == I2C_BUSMODE_I2C) || ((MODE) == I2C_BUSMODE_SMBDEVICE) || ((MODE) == I2C_BUSMODE_SMBHOST))
  82. /**
  83. * @}
  84. */
  85. /** @addtogroup I2C_duty_cycle_in_fast_mode
  86. * @{
  87. */
  88. #define I2C_FMDUTYCYCLE_16_9 ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
  89. #define I2C_FMDUTYCYCLE_2 ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
  90. #define IS_I2C_FM_DUTY_CYCLE(CYCLE) (((CYCLE) == I2C_FMDUTYCYCLE_16_9) || ((CYCLE) == I2C_FMDUTYCYCLE_2))
  91. /**
  92. * @}
  93. */
  94. /** @addtogroup I2C_acknowledgement
  95. * @{
  96. */
  97. #define I2C_ACKEN ((uint16_t)0x0400)
  98. #define I2C_ACKDIS ((uint16_t)0x0000)
  99. #define IS_I2C_ACK_STATE(STATE) (((STATE) == I2C_ACKEN) || ((STATE) == I2C_ACKDIS))
  100. /**
  101. * @}
  102. */
  103. /** @addtogroup I2C_transfer_direction
  104. * @{
  105. */
  106. #define I2C_DIRECTION_SEND ((uint8_t)0x00)
  107. #define I2C_DIRECTION_RECV ((uint8_t)0x01)
  108. #define IS_I2C_DIRECTION(DIRECTION) (((DIRECTION) == I2C_DIRECTION_SEND) || ((DIRECTION) == I2C_DIRECTION_RECV))
  109. /**
  110. * @}
  111. */
  112. /** @addtogroup I2C_acknowledged_address
  113. * @{
  114. */
  115. #define I2C_ADDR_MODE_7BIT ((uint16_t)0x4000)
  116. #define I2C_ADDR_MODE_10BIT ((uint16_t)0xC000)
  117. #define IS_I2C_ADDR_MODE(ADDRESS) (((ADDRESS) == I2C_ADDR_MODE_7BIT) || ((ADDRESS) == I2C_ADDR_MODE_10BIT))
  118. /**
  119. * @}
  120. */
  121. /** @addtogroup I2C_registers
  122. * @{
  123. */
  124. #define I2C_REG_CTRL1 ((uint8_t)0x00)
  125. #define I2C_REG_CTRL2 ((uint8_t)0x04)
  126. #define I2C_REG_OADDR1 ((uint8_t)0x08)
  127. #define I2C_REG_OADDR2 ((uint8_t)0x0C)
  128. #define I2C_REG_DAT ((uint8_t)0x10)
  129. #define I2C_REG_STS1 ((uint8_t)0x14)
  130. #define I2C_REG_STS2 ((uint8_t)0x18)
  131. #define I2C_REG_CLKCTRL ((uint8_t)0x1C)
  132. #define I2C_REG_TMRISE ((uint8_t)0x20)
  133. #define IS_I2C_REG(REGISTER) \
  134. (((REGISTER) == I2C_REG_CTRL1) || ((REGISTER) == I2C_REG_CTRL2) || ((REGISTER) == I2C_REG_OADDR1) \
  135. || ((REGISTER) == I2C_REG_OADDR2) || ((REGISTER) == I2C_REG_DAT) || ((REGISTER) == I2C_REG_STS1) \
  136. || ((REGISTER) == I2C_REG_STS2) || ((REGISTER) == I2C_REG_CLKCTRL) || ((REGISTER) == I2C_REG_TMRISE))
  137. /**
  138. * @}
  139. */
  140. /** @addtogroup I2C_SMBus_alert_pin_level
  141. * @{
  142. */
  143. #define I2C_SMBALERT_LOW ((uint16_t)0x2000)
  144. #define I2C_SMBALERT_HIGH ((uint16_t)0xDFFF)
  145. #define IS_I2C_SMB_ALERT(ALERT) (((ALERT) == I2C_SMBALERT_LOW) || ((ALERT) == I2C_SMBALERT_HIGH))
  146. /**
  147. * @}
  148. */
  149. /** @addtogroup I2C_PEC_position
  150. * @{
  151. */
  152. #define I2C_PEC_POS_NEXT ((uint16_t)0x0800)
  153. #define I2C_PEC_POS_CURRENT ((uint16_t)0xF7FF)
  154. #define IS_I2C_PEC_POS(POSITION) (((POSITION) == I2C_PEC_POS_NEXT) || ((POSITION) == I2C_PEC_POS_CURRENT))
  155. /**
  156. * @}
  157. */
  158. /** @addtogroup I2C_NCAK_position
  159. * @{
  160. */
  161. #define I2C_NACK_POS_NEXT ((uint16_t)0x0800)
  162. #define I2C_NACK_POS_CURRENT ((uint16_t)0xF7FF)
  163. #define IS_I2C_NACK_POS(POSITION) (((POSITION) == I2C_NACK_POS_NEXT) || ((POSITION) == I2C_NACK_POS_CURRENT))
  164. /**
  165. * @}
  166. */
  167. /** @addtogroup I2C_interrupts_definition
  168. * @{
  169. */
  170. #define I2C_INT_BUF ((uint16_t)0x0400)
  171. #define I2C_INT_EVENT ((uint16_t)0x0200)
  172. #define I2C_INT_ERR ((uint16_t)0x0100)
  173. #define IS_I2C_CFG_INT(IT) ((((IT) & (uint16_t)0xF8FF) == 0x00) && ((IT) != 0x00))
  174. /**
  175. * @}
  176. */
  177. /** @addtogroup I2C_interrupts_definition
  178. * @{
  179. */
  180. #define I2C_INT_SMBALERT ((uint32_t)0x01008000)
  181. #define I2C_INT_TIMOUT ((uint32_t)0x01004000)
  182. #define I2C_INT_PECERR ((uint32_t)0x01001000)
  183. #define I2C_INT_OVERRUN ((uint32_t)0x01000800)
  184. #define I2C_INT_ACKFAIL ((uint32_t)0x01000400)
  185. #define I2C_INT_ARLOST ((uint32_t)0x01000200)
  186. #define I2C_INT_BUSERR ((uint32_t)0x01000100)
  187. #define I2C_INT_TXDATE ((uint32_t)0x06000080)
  188. #define I2C_INT_RXDATNE ((uint32_t)0x06000040)
  189. #define I2C_INT_STOPF ((uint32_t)0x02000010)
  190. #define I2C_INT_ADDR10F ((uint32_t)0x02000008)
  191. #define I2C_INT_BYTEF ((uint32_t)0x02000004)
  192. #define I2C_INT_ADDRF ((uint32_t)0x02000002)
  193. #define I2C_INT_STARTBF ((uint32_t)0x02000001)
  194. #define IS_I2C_CLR_INT(IT) ((((IT) & (uint16_t)0x20FF) == 0x00) && ((IT) != (uint16_t)0x00))
  195. #define IS_I2C_GET_INT(IT) \
  196. (((IT) == I2C_INT_SMBALERT) || ((IT) == I2C_INT_TIMOUT) || ((IT) == I2C_INT_PECERR) || ((IT) == I2C_INT_OVERRUN) \
  197. || ((IT) == I2C_INT_ACKFAIL) || ((IT) == I2C_INT_ARLOST) || ((IT) == I2C_INT_BUSERR) || ((IT) == I2C_INT_TXDATE) \
  198. || ((IT) == I2C_INT_RXDATNE) || ((IT) == I2C_INT_STOPF) || ((IT) == I2C_INT_ADDR10F) || ((IT) == I2C_INT_BYTEF) \
  199. || ((IT) == I2C_INT_ADDRF) || ((IT) == I2C_INT_STARTBF))
  200. /**
  201. * @}
  202. */
  203. /** @addtogroup I2C_flags_definition
  204. * @{
  205. */
  206. /**
  207. * @brief STS2 register flags
  208. */
  209. #define I2C_FLAG_DUALFLAG ((uint32_t)0x00800000)
  210. #define I2C_FLAG_SMBHADDR ((uint32_t)0x00400000)
  211. #define I2C_FLAG_SMBDADDR ((uint32_t)0x00200000)
  212. #define I2C_FLAG_GCALLADDR ((uint32_t)0x00100000)
  213. #define I2C_FLAG_TRF ((uint32_t)0x00040000)
  214. #define I2C_FLAG_BUSY ((uint32_t)0x00020000)
  215. #define I2C_FLAG_MSMODE ((uint32_t)0x00010000)
  216. /**
  217. * @brief STS1 register flags
  218. */
  219. #define I2C_FLAG_SMBALERT ((uint32_t)0x10008000)
  220. #define I2C_FLAG_TIMOUT ((uint32_t)0x10004000)
  221. #define I2C_FLAG_PECERR ((uint32_t)0x10001000)
  222. #define I2C_FLAG_OVERRUN ((uint32_t)0x10000800)
  223. #define I2C_FLAG_ACKFAIL ((uint32_t)0x10000400)
  224. #define I2C_FLAG_ARLOST ((uint32_t)0x10000200)
  225. #define I2C_FLAG_BUSERR ((uint32_t)0x10000100)
  226. #define I2C_FLAG_TXDATE ((uint32_t)0x10000080)
  227. #define I2C_FLAG_RXDATNE ((uint32_t)0x10000040)
  228. #define I2C_FLAG_STOPF ((uint32_t)0x10000010)
  229. #define I2C_FLAG_ADDR10F ((uint32_t)0x10000008)
  230. #define I2C_FLAG_BYTEF ((uint32_t)0x10000004)
  231. #define I2C_FLAG_ADDRF ((uint32_t)0x10000002)
  232. #define I2C_FLAG_STARTBF ((uint32_t)0x10000001)
  233. #define IS_I2C_CLR_FLAG(FLAG) ((((FLAG) & (uint16_t)0x20FF) == 0x00) && ((FLAG) != (uint16_t)0x00))
  234. #define IS_I2C_GET_FLAG(FLAG) \
  235. (((FLAG) == I2C_FLAG_DUALFLAG) || ((FLAG) == I2C_FLAG_SMBHADDR) || ((FLAG) == I2C_FLAG_SMBDADDR) \
  236. || ((FLAG) == I2C_FLAG_GCALLADDR) || ((FLAG) == I2C_FLAG_TRF) || ((FLAG) == I2C_FLAG_BUSY) \
  237. || ((FLAG) == I2C_FLAG_MSMODE) || ((FLAG) == I2C_FLAG_SMBALERT) || ((FLAG) == I2C_FLAG_TIMOUT) \
  238. || ((FLAG) == I2C_FLAG_PECERR) || ((FLAG) == I2C_FLAG_OVERRUN) || ((FLAG) == I2C_FLAG_ACKFAIL) \
  239. || ((FLAG) == I2C_FLAG_ARLOST) || ((FLAG) == I2C_FLAG_BUSERR) || ((FLAG) == I2C_FLAG_TXDATE) \
  240. || ((FLAG) == I2C_FLAG_RXDATNE) || ((FLAG) == I2C_FLAG_STOPF) || ((FLAG) == I2C_FLAG_ADDR10F) \
  241. || ((FLAG) == I2C_FLAG_BYTEF) || ((FLAG) == I2C_FLAG_ADDRF) || ((FLAG) == I2C_FLAG_STARTBF))
  242. /**
  243. * @}
  244. */
  245. /** @addtogroup I2C_Events
  246. * @{
  247. */
  248. /*========================================
  249. I2C Master Events (Events grouped in order of communication)
  250. ==========================================*/
  251. /**
  252. * @brief Communication start
  253. *
  254. * After sending the START condition (I2C_GenerateStart() function) the master
  255. * has to wait for this event. It means that the Start condition has been correctly
  256. * released on the I2C bus (the bus is free, no other devices is communicating).
  257. *
  258. */
  259. /* Master mode */
  260. #define I2C_ROLE_MASTER ((uint32_t)0x00010000) /* MSMODE */
  261. /* --EV5 */
  262. #define I2C_EVT_MASTER_MODE_FLAG ((uint32_t)0x00030001) /* BUSY, MSMODE and STARTBF flag */
  263. /**
  264. * @brief Address Acknowledge
  265. *
  266. * After checking on EV5 (start condition correctly released on the bus), the
  267. * master sends the address of the slave(s) with which it will communicate
  268. * (I2C_SendAddr7bit() function, it also determines the direction of the communication:
  269. * Master transmitter or Receiver). Then the master has to wait that a slave acknowledges
  270. * his address. If an acknowledge is sent on the bus, one of the following events will
  271. * be set:
  272. *
  273. * 1) In case of Master Receiver (7-bit addressing): the I2C_EVT_MASTER_RXMODE_FLAG
  274. * event is set.
  275. *
  276. * 2) In case of Master Transmitter (7-bit addressing): the I2C_EVT_MASTER_TXMODE_FLAG
  277. * is set
  278. *
  279. * 3) In case of 10-Bit addressing mode, the master (just after generating the START
  280. * and checking on EV5) has to send the header of 10-bit addressing mode (I2C_SendData()
  281. * function). Then master should wait on EV9. It means that the 10-bit addressing
  282. * header has been correctly sent on the bus. Then master should send the second part of
  283. * the 10-bit address (LSB) using the function I2C_SendAddr7bit(). Then master
  284. * should wait for event EV6.
  285. *
  286. */
  287. /* --EV6 */
  288. #define I2C_EVT_MASTER_TXMODE_FLAG ((uint32_t)0x00070082) /* BUSY, MSMODE, ADDRF, TXDATE and TRF flags */
  289. #define I2C_EVT_MASTER_RXMODE_FLAG ((uint32_t)0x00030002) /* BUSY, MSMODE and ADDRF flags */
  290. /* --EV9 */
  291. #define I2C_EVT_MASTER_MODE_ADDRESS10_FLAG ((uint32_t)0x00030008) /* BUSY, MSMODE and ADD10RF flags */
  292. /**
  293. * @brief Communication events
  294. *
  295. * If a communication is established (START condition generated and slave address
  296. * acknowledged) then the master has to check on one of the following events for
  297. * communication procedures:
  298. *
  299. * 1) Master Receiver mode: The master has to wait on the event EV7 then to read
  300. * the data received from the slave (I2C_RecvData() function).
  301. *
  302. * 2) Master Transmitter mode: The master has to send data (I2C_SendData()
  303. * function) then to wait on event EV8 or EV8_2.
  304. * These two events are similar:
  305. * - EV8 means that the data has been written in the data register and is
  306. * being shifted out.
  307. * - EV8_2 means that the data has been physically shifted out and output
  308. * on the bus.
  309. * In most cases, using EV8 is sufficient for the application.
  310. * Using EV8_2 leads to a slower communication but ensure more reliable test.
  311. * EV8_2 is also more suitable than EV8 for testing on the last data transmission
  312. * (before Stop condition generation).
  313. *
  314. * @note In case the user software does not guarantee that this event EV7 is
  315. * managed before the current byte end of transfer, then user may check on EV7
  316. * and BSF flag at the same time (ie. (I2C_EVT_MASTER_DATA_RECVD_FLAG | I2C_FLAG_BYTEF)).
  317. * In this case the communication may be slower.
  318. *
  319. */
  320. /* Master RECEIVER mode -----------------------------*/
  321. /* --EV7 */
  322. #define I2C_EVT_MASTER_DATA_RECVD_FLAG ((uint32_t)0x00030040) /* BUSY, MSMODE and RXDATNE flags */
  323. /* EV7x shifter register full */
  324. #define I2C_EVT_MASTER_SFT_DATA_RECVD_FLAG ((uint32_t)0x00030044) /* BUSY, MSMODE, BSF and RXDATNE flags */
  325. /* Master TRANSMITTER mode --------------------------*/
  326. /* --EV8 */
  327. #define I2C_EVT_MASTER_DATA_SENDING ((uint32_t)0x00070080) /* TRF, BUSY, MSMODE, TXDATE flags */
  328. /* --EV8_2 */
  329. #define I2C_EVT_MASTER_DATA_SENDED ((uint32_t)0x00070084) /* TRF, BUSY, MSMODE, TXDATE and BSF flags */
  330. /*========================================
  331. I2C Slave Events (Events grouped in order of communication)
  332. ==========================================*/
  333. /**
  334. * @brief Communication start events
  335. *
  336. * Wait on one of these events at the start of the communication. It means that
  337. * the I2C peripheral detected a Start condition on the bus (generated by master
  338. * device) followed by the peripheral address. The peripheral generates an ACK
  339. * condition on the bus (if the acknowledge feature is enabled through function
  340. * I2C_ConfigAck()) and the events listed above are set :
  341. *
  342. * 1) In normal case (only one address managed by the slave), when the address
  343. * sent by the master matches the own address of the peripheral (configured by
  344. * OwnAddr1 field) the I2C_EVENT_SLAVE_XXX_ADDRESS_MATCHED event is set
  345. * (where XXX could be TRANSMITTER or RECEIVER).
  346. *
  347. * 2) In case the address sent by the master matches the second address of the
  348. * peripheral (configured by the function I2C_ConfigOwnAddr2() and enabled
  349. * by the function I2C_EnableDualAddr()) the events I2C_EVENT_SLAVE_XXX_SECONDADDRESS_MATCHED
  350. * (where XXX could be TRANSMITTER or RECEIVER) are set.
  351. *
  352. * 3) In case the address sent by the master is General Call (address 0x00) and
  353. * if the General Call is enabled for the peripheral (using function I2C_EnableGeneralCall())
  354. * the following event is set I2C_EVT_SLAVE_GCALLADDR_MATCHED.
  355. *
  356. */
  357. /* --EV1 (all the events below are variants of EV1) */
  358. /* 1) Case of One Single Address managed by the slave */
  359. #define I2C_EVT_SLAVE_RECV_ADDR_MATCHED ((uint32_t)0x00020002) /* BUSY and ADDRF flags */
  360. #define I2C_EVT_SLAVE_SEND_ADDR_MATCHED ((uint32_t)0x00060082) /* TRF, BUSY, TXDATE and ADDRF flags */
  361. /* 2) Case of Dual address managed by the slave */
  362. #define I2C_EVT_SLAVE_RECV_ADDR2_MATCHED ((uint32_t)0x00820000) /* DUALF and BUSY flags */
  363. #define I2C_EVT_SLAVE_SEND_ADDR2_MATCHED ((uint32_t)0x00860080) /* DUALF, TRF, BUSY and TXDATE flags */
  364. /* 3) Case of General Call enabled for the slave */
  365. #define I2C_EVT_SLAVE_GCALLADDR_MATCHED ((uint32_t)0x00120000) /* GENCALL and BUSY flags */
  366. /**
  367. * @brief Communication events
  368. *
  369. * Wait on one of these events when EV1 has already been checked and:
  370. *
  371. * - Slave RECEIVER mode:
  372. * - EV2: When the application is expecting a data byte to be received.
  373. * - EV4: When the application is expecting the end of the communication: master
  374. * sends a stop condition and data transmission is stopped.
  375. *
  376. * - Slave Transmitter mode:
  377. * - EV3: When a byte has been transmitted by the slave and the application is expecting
  378. * the end of the byte transmission. The two events I2C_EVT_SLAVE_DATA_SENDED and
  379. * I2C_EVT_SLAVE_DATA_SENDING are similar. The second one can optionally be
  380. * used when the user software doesn't guarantee the EV3 is managed before the
  381. * current byte end of transfer.
  382. * - EV3_2: When the master sends a NACK in order to tell slave that data transmission
  383. * shall end (before sending the STOP condition). In this case slave has to stop sending
  384. * data bytes and expect a Stop condition on the bus.
  385. *
  386. * @note In case the user software does not guarantee that the event EV2 is
  387. * managed before the current byte end of transfer, then user may check on EV2
  388. * and BSF flag at the same time (ie. (I2C_EVT_SLAVE_DATA_RECVD | I2C_FLAG_BYTEF)).
  389. * In this case the communication may be slower.
  390. *
  391. */
  392. /* Slave RECEIVER mode --------------------------*/
  393. /* --EV2 */
  394. #define I2C_EVT_SLAVE_DATA_RECVD ((uint32_t)0x00020040) /* BUSY and RXDATNE flags */
  395. /* --EV2x */
  396. #define I2C_EVT_SLAVE_DATA_RECVD_NOBUSY ((uint32_t)0x00000040) /* no BUSY and RXDATNE flags */
  397. /* --EV4 */
  398. #define I2C_EVT_SLAVE_STOP_RECVD ((uint32_t)0x00000010) /* STOPF flag */
  399. /* Slave TRANSMITTER mode -----------------------*/
  400. /* --EV3 */
  401. #define I2C_EVT_SLAVE_DATA_SENDED ((uint32_t)0x00060084) /* TRF, BUSY, TXDATE and BSF flags */
  402. #define I2C_EVT_SLAVE_DATA_SENDING ((uint32_t)0x00060080) /* TRF, BUSY and TXDATE flags */
  403. /* --EV3_2 */
  404. #define I2C_EVT_SLAVE_ACK_MISS ((uint32_t)0x00000400) /* AF flag */
  405. /*=========================== End of Events Description ==========================================*/
  406. #define IS_I2C_EVT(EVENT) \
  407. (((EVENT) == I2C_EVT_SLAVE_SEND_ADDR_MATCHED) || ((EVENT) == I2C_EVT_SLAVE_RECV_ADDR_MATCHED) \
  408. || ((EVENT) == I2C_EVT_SLAVE_SEND_ADDR2_MATCHED) || ((EVENT) == I2C_EVT_SLAVE_RECV_ADDR2_MATCHED) \
  409. || ((EVENT) == I2C_EVT_SLAVE_GCALLADDR_MATCHED) || ((EVENT) == I2C_EVT_SLAVE_DATA_RECVD) \
  410. || ((EVENT) == (I2C_EVT_SLAVE_DATA_RECVD | I2C_FLAG_DUALFLAG)) \
  411. || ((EVENT) == (I2C_EVT_SLAVE_DATA_RECVD | I2C_FLAG_GCALLADDR)) || ((EVENT) == I2C_EVT_SLAVE_DATA_SENDED) \
  412. || ((EVENT) == (I2C_EVT_SLAVE_DATA_SENDED | I2C_FLAG_DUALFLAG)) \
  413. || ((EVENT) == (I2C_EVT_SLAVE_DATA_SENDED | I2C_FLAG_GCALLADDR)) || ((EVENT) == I2C_EVT_SLAVE_STOP_RECVD) \
  414. || ((EVENT) == I2C_EVT_MASTER_MODE_FLAG) || ((EVENT) == I2C_EVT_MASTER_TXMODE_FLAG) \
  415. || ((EVENT) == I2C_EVT_MASTER_RXMODE_FLAG) || ((EVENT) == I2C_EVT_MASTER_DATA_RECVD_FLAG) \
  416. || ((EVENT) == I2C_EVT_MASTER_DATA_SENDED) || ((EVENT) == I2C_EVT_MASTER_DATA_SENDING) \
  417. || ((EVENT) == I2C_EVT_MASTER_MODE_ADDRESS10_FLAG) || ((EVENT) == I2C_EVT_SLAVE_ACK_MISS) \
  418. || ((EVENT) == I2C_EVT_MASTER_SFT_DATA_RECVD_FLAG) || ((EVENT) == I2C_EVT_SLAVE_DATA_RECVD_NOBUSY))
  419. /**
  420. * @}
  421. */
  422. /** @addtogroup I2C_own_address1
  423. * @{
  424. */
  425. #define IS_I2C_OWN_ADDR1(ADDRESS1) ((ADDRESS1) <= 0x3FF)
  426. /**
  427. * @}
  428. */
  429. /** @addtogroup I2C_clock_speed
  430. * @{
  431. */
  432. //#define IS_I2C_CLK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 400000))
  433. #define IS_I2C_CLK_SPEED(SPEED) (((SPEED) >= 0x1) && ((SPEED) <= 1000000))
  434. /**
  435. * @}
  436. */
  437. /**
  438. * @}
  439. */
  440. /** @addtogroup I2C_Exported_Macros
  441. * @{
  442. */
  443. /**
  444. * @}
  445. */
  446. /** @addtogroup I2C_Exported_Functions
  447. * @{
  448. */
  449. void I2C_DeInit(I2C_Module* I2Cx);
  450. void I2C_Init(I2C_Module* I2Cx, I2C_InitType* I2C_InitStruct);
  451. void I2C_InitStruct(I2C_InitType* I2C_InitStruct);
  452. void I2C_Enable(I2C_Module* I2Cx, FunctionalState Cmd);
  453. void I2C_EnableDMA(I2C_Module* I2Cx, FunctionalState Cmd);
  454. void I2C_EnableDmaLastSend(I2C_Module* I2Cx, FunctionalState Cmd);
  455. void I2C_GenerateStart(I2C_Module* I2Cx, FunctionalState Cmd);
  456. void I2C_GenerateStop(I2C_Module* I2Cx, FunctionalState Cmd);
  457. void I2C_ConfigAck(I2C_Module* I2Cx, FunctionalState Cmd);
  458. void I2C_ConfigOwnAddr2(I2C_Module* I2Cx, uint8_t Address);
  459. void I2C_EnableDualAddr(I2C_Module* I2Cx, FunctionalState Cmd);
  460. void I2C_EnableGeneralCall(I2C_Module* I2Cx, FunctionalState Cmd);
  461. void I2C_ConfigInt(I2C_Module* I2Cx, uint16_t I2C_IT, FunctionalState Cmd);
  462. void I2C_SendData(I2C_Module* I2Cx, uint8_t Data);
  463. uint8_t I2C_RecvData(I2C_Module* I2Cx);
  464. void I2C_SendAddr7bit(I2C_Module* I2Cx, uint8_t Address, uint8_t I2C_Direction);
  465. uint16_t I2C_GetRegister(I2C_Module* I2Cx, uint8_t I2C_Register);
  466. void I2C_EnableSoftwareReset(I2C_Module* I2Cx, FunctionalState Cmd);
  467. void I2C_ConfigNackLocation(I2C_Module* I2Cx, uint16_t I2C_NACKPosition);
  468. void I2C_ConfigSmbusAlert(I2C_Module* I2Cx, uint16_t I2C_SMBusAlert);
  469. void I2C_SendPEC(I2C_Module* I2Cx, FunctionalState Cmd);
  470. void I2C_ConfigPecLocation(I2C_Module* I2Cx, uint16_t I2C_PECPosition);
  471. void I2C_ComputePec(I2C_Module* I2Cx, FunctionalState Cmd);
  472. uint8_t I2C_GetPec(I2C_Module* I2Cx);
  473. void I2C_EnableArp(I2C_Module* I2Cx, FunctionalState Cmd);
  474. void I2C_EnableExtendClk(I2C_Module* I2Cx, FunctionalState Cmd);
  475. void I2C_ConfigFastModeDutyCycle(I2C_Module* I2Cx, uint16_t FmDutyCycle);
  476. /**
  477. * @brief
  478. ****************************************************************************************
  479. *
  480. * I2C State Monitoring Functions
  481. *
  482. ****************************************************************************************
  483. * This I2C driver provides three different ways for I2C state monitoring
  484. * depending on the application requirements and constraints:
  485. *
  486. *
  487. * 1) Basic state monitoring:
  488. * Using I2C_CheckEvent() function:
  489. * It compares the status registers (STS1 and STS2) content to a given event
  490. * (can be the combination of one or more flags).
  491. * It returns SUCCESS if the current status includes the given flags
  492. * and returns ERROR if one or more flags are missing in the current status.
  493. * - When to use:
  494. * - This function is suitable for most applications as well as for startup
  495. * activity since the events are fully described in the product reference manual
  496. * (RM0008).
  497. * - It is also suitable for users who need to define their own events.
  498. * - Limitations:
  499. * - If an error occurs (ie. error flags are set besides to the monitored flags),
  500. * the I2C_CheckEvent() function may return SUCCESS despite the communication
  501. * hold or corrupted real state.
  502. * In this case, it is advised to use error interrupts to monitor the error
  503. * events and handle them in the interrupt IRQ handler.
  504. *
  505. * @note
  506. * For error management, it is advised to use the following functions:
  507. * - I2C_ConfigInt() to configure and enable the error interrupts (I2C_INT_ERR).
  508. * - I2Cx_ER_IRQHandler() which is called when the error interrupt occurs.
  509. * Where x is the peripheral instance (I2C1, I2C2 ...)
  510. * - I2C_GetFlag() or I2C_GetIntStatus() to be called into I2Cx_ER_IRQHandler()
  511. * in order to determine which error occurred.
  512. * - I2C_ClrFlag() or I2C_ClrIntPendingBit() and/or I2C_EnableSoftwareReset()
  513. * and/or I2C_GenerateStop() in order to clear the error flag and source,
  514. * and return to correct communication status.
  515. *
  516. *
  517. * 2) Advanced state monitoring:
  518. * Using the function I2C_GetLastEvent() which returns the image of both status
  519. * registers in a single word (uint32_t) (Status Register 2 value is shifted left
  520. * by 16 bits and concatenated to Status Register 1).
  521. * - When to use:
  522. * - This function is suitable for the same applications above but it allows to
  523. * overcome the limitations of I2C_GetFlag() function (see below).
  524. * The returned value could be compared to events already defined in the
  525. * library (n32g45x_i2c.h) or to custom values defined by user.
  526. * - This function is suitable when multiple flags are monitored at the same time.
  527. * - At the opposite of I2C_CheckEvent() function, this function allows user to
  528. * choose when an event is accepted (when all events flags are set and no
  529. * other flags are set or just when the needed flags are set like
  530. * I2C_CheckEvent() function).
  531. * - Limitations:
  532. * - User may need to define his own events.
  533. * - Same remark concerning the error management is applicable for this
  534. * function if user decides to check only regular communication flags (and
  535. * ignores error flags).
  536. *
  537. *
  538. * 3) Flag-based state monitoring:
  539. * Using the function I2C_GetFlag() which simply returns the status of
  540. * one single flag (ie. I2C_FLAG_RXDATNE ...).
  541. * - When to use:
  542. * - This function could be used for specific applications or in debug phase.
  543. * - It is suitable when only one flag checking is needed (most I2C events
  544. * are monitored through multiple flags).
  545. * - Limitations:
  546. * - When calling this function, the Status register is accessed. Some flags are
  547. * cleared when the status register is accessed. So checking the status
  548. * of one Flag, may clear other ones.
  549. * - Function may need to be called twice or more in order to monitor one
  550. * single event.
  551. *
  552. */
  553. /**
  554. *
  555. * 1) Basic state monitoring
  556. *******************************************************************************
  557. */
  558. ErrorStatus I2C_CheckEvent(I2C_Module* I2Cx, uint32_t I2C_EVENT);
  559. /**
  560. *
  561. * 2) Advanced state monitoring
  562. *******************************************************************************
  563. */
  564. uint32_t I2C_GetLastEvent(I2C_Module* I2Cx);
  565. /**
  566. *
  567. * 3) Flag-based state monitoring
  568. *******************************************************************************
  569. */
  570. FlagStatus I2C_GetFlag(I2C_Module* I2Cx, uint32_t I2C_FLAG);
  571. /**
  572. *
  573. *******************************************************************************
  574. */
  575. void I2C_ClrFlag(I2C_Module* I2Cx, uint32_t I2C_FLAG);
  576. INTStatus I2C_GetIntStatus(I2C_Module* I2Cx, uint32_t I2C_IT);
  577. void I2C_ClrIntPendingBit(I2C_Module* I2Cx, uint32_t I2C_IT);
  578. #ifdef __cplusplus
  579. }
  580. #endif
  581. #endif /*__N32G45X_I2C_H */
  582. /**
  583. * @}
  584. */
  585. /**
  586. * @}
  587. */
  588. /**
  589. * @}
  590. */