hal_i2c.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. ////////////////////////////////////////////////////////////////////////////////
  2. /// @file hal_i2c.c
  3. /// @author AE TEAM
  4. /// @brief THIS FILE PROVIDES ALL THE I2C FIRMWARE FUNCTIONS.
  5. ////////////////////////////////////////////////////////////////////////////////
  6. /// @attention
  7. ///
  8. /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
  9. /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
  10. /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
  11. /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
  12. /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
  13. /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
  14. ///
  15. /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
  16. ////////////////////////////////////////////////////////////////////////////////
  17. // Define to prevent recursive inclusion
  18. #define _HAL_I2C_C_
  19. // Files includes
  20. #include "hal_i2c.h"
  21. #include "hal_rcc.h"
  22. ////////////////////////////////////////////////////////////////////////////////
  23. /// @addtogroup MM32_Hardware_Abstract_Layer
  24. /// @{
  25. ////////////////////////////////////////////////////////////////////////////////
  26. /// @addtogroup I2C_HAL
  27. /// @{
  28. ////////////////////////////////////////////////////////////////////////////////
  29. /// @addtogroup I2C_Exported_Functions
  30. /// @{
  31. ////////////////////////////////////////////////////////////////////////////////
  32. /// @brief Deinitializes the i2c peripheral registers to their default
  33. /// reset values.
  34. /// @param i2c: where n can be 1 or 2 to select the I2C peripheral.
  35. /// @retval None.
  36. ////////////////////////////////////////////////////////////////////////////////
  37. void I2C_DeInit(I2C_TypeDef* i2c)
  38. {
  39. switch (*(vu32*)&i2c) {
  40. case (u32)I2C1: // I2C1_BASE:
  41. exRCC_APB1PeriphReset(RCC_APB1ENR_I2C1);
  42. break;
  43. case (u32)I2C2: // I2C2_BASE:
  44. exRCC_APB1PeriphReset(RCC_APB1ENR_I2C2);
  45. break;
  46. default:
  47. break;
  48. }
  49. }
  50. ////////////////////////////////////////////////////////////////////////////////
  51. /// @brief Initializes the i2c peripheral according to the specified
  52. /// parameters in the init_struct.
  53. /// @param i2c: select the I2C peripheral.
  54. /// @param init_struct: pointer to a I2C_InitTypeDef structure that
  55. /// contains the configuration information for the specified
  56. /// I2C peripheral.
  57. /// @retval None.
  58. ////////////////////////////////////////////////////////////////////////////////
  59. void I2C_Init(I2C_TypeDef* i2c, I2C_InitTypeDef* init_struct)
  60. {
  61. u32 pclk1 = HSI_VALUE;
  62. u32 minSclLowTime = 0;
  63. u32 i2cPeriod = 0;
  64. u32 pclk1Period = 0;
  65. i2c->IC_ENABLE &= ~I2C_ENR_ENABLE;
  66. pclk1 = RCC_GetPCLK1Freq();
  67. pclk1Period = 1000000000 / pclk1;
  68. i2cPeriod = 1000000000 / init_struct->I2C_ClockSpeed;
  69. minSclLowTime = pclk1 / init_struct->I2C_ClockSpeed ;
  70. i2cPeriod = 82 / pclk1Period;
  71. if (init_struct->I2C_ClockSpeed <= 100000) {
  72. i2c->IC_SS_SCL_LCNT = (minSclLowTime - 13 - i2cPeriod) / 2;
  73. i2c->IC_SS_SCL_HCNT = (minSclLowTime - 13 - i2cPeriod - i2c->IC_SS_SCL_LCNT);
  74. }
  75. else {
  76. i2c->IC_FS_SCL_LCNT = (minSclLowTime - 13 - i2cPeriod ) / 2 + 4;
  77. i2c->IC_FS_SCL_HCNT = (minSclLowTime - 13 - i2c->IC_FS_SCL_LCNT - i2cPeriod);
  78. }
  79. i2c->IC_CON &= ~(I2C_CR_EMPINT | \
  80. I2C_CR_SLAVEDIS | \
  81. I2C_CR_REPEN | \
  82. I2C_CR_MASTER10 | \
  83. I2C_CR_SLAVE10 | \
  84. I2C_CR_FAST | \
  85. I2C_CR_MASTER);
  86. i2c->IC_CON = I2C_CR_EMPINT | \
  87. I2C_CR_REPEN | \
  88. ((init_struct->I2C_Speed == I2C_CR_FAST) ? I2C_CR_FAST : I2C_CR_STD) | \
  89. ((init_struct->I2C_Mode) ? I2C_CR_MASTER : 0x00);
  90. i2c->IC_INTR_MASK &= INTR_MASK;
  91. i2c->IC_RX_TL = 0x00;
  92. i2c->IC_TX_TL = 0x00;
  93. }
  94. ////////////////////////////////////////////////////////////////////////////////
  95. /// @brief Fills each init_struct member with its default value.
  96. /// @param init_struct: pointer to an I2C_InitTypeDef structure
  97. /// which will be initialized.
  98. /// @retval None.
  99. ////////////////////////////////////////////////////////////////////////////////
  100. void I2C_StructInit(I2C_InitTypeDef* init_struct)
  101. {
  102. init_struct->I2C_Mode = I2C_CR_MASTER;
  103. init_struct->I2C_OwnAddress = I2C_OWN_ADDRESS;
  104. init_struct->I2C_Speed = I2C_CR_STD;
  105. init_struct->I2C_ClockSpeed = 100000;
  106. }
  107. ////////////////////////////////////////////////////////////////////////////////
  108. /// @brief Enables or disables the specified I2C peripheral.
  109. /// @param i2c: select the I2C peripheral.
  110. /// @param state: new state of the i2c peripheral. This parameter
  111. /// can be: ENABLE or DISABLE.
  112. /// @retval None.
  113. ////////////////////////////////////////////////////////////////////////////////
  114. void I2C_Cmd(I2C_TypeDef* i2c, FunctionalState state)
  115. {
  116. (state) ? (i2c->IC_ENABLE |= I2C_ENR_ENABLE) : (i2c->IC_ENABLE &= ~I2C_ENR_ENABLE);
  117. }
  118. ////////////////////////////////////////////////////////////////////////////////
  119. /// @brief Enables or disables the specified I2C DMA requests.
  120. /// @param i2c: select the I2C peripheral.
  121. /// @param state: new state of the I2C DMA transfer.
  122. /// This parameter can be: ENABLE or DISABLE.
  123. /// @retval None.
  124. ////////////////////////////////////////////////////////////////////////////////
  125. void I2C_DMACmd(I2C_TypeDef* i2c, FunctionalState state)
  126. {
  127. if (state) {
  128. if (I2C_DMA_DIR == TDMAE_SET)
  129. i2c->IC_DMA_CR |= TDMAE_SET;
  130. else
  131. i2c->IC_DMA_CR |= RDMAE_SET;
  132. }
  133. else
  134. i2c->IC_DMA_CR &= ~(I2C_DMA_RXEN | I2C_DMA_TXEN);
  135. }
  136. ////////////////////////////////////////////////////////////////////////////////
  137. /// @brief Generates i2c communication START condition.
  138. /// @param i2c: select the I2C peripheral.
  139. /// @param state: new state of the I2C START condition generation.
  140. /// This parameter can be: ENABLE or DISABLE.
  141. /// @retval None.
  142. ////////////////////////////////////////////////////////////////////////////////
  143. void I2C_GenerateSTART(I2C_TypeDef* i2c, FunctionalState state)
  144. {
  145. (state) ? (i2c->IC_CON |= I2C_CR_REPEN) : (i2c->IC_CON &= ~I2C_CR_REPEN);
  146. }
  147. ////////////////////////////////////////////////////////////////////////////////
  148. /// @brief Generates i2c communication STOP condition.
  149. /// @param i2c: select the I2C peripheral.
  150. /// @param state: new state of the I2C STOP condition generation.
  151. /// This parameter can be: ENABLE or DISABLE.
  152. /// @retval None.
  153. ////////////////////////////////////////////////////////////////////////////////
  154. void I2C_GenerateSTOP(I2C_TypeDef* i2c, FunctionalState state)
  155. {
  156. u16 overTime = 3000;
  157. i2c->IC_ENABLE |= I2C_ENR_ABORT;
  158. while (i2c->IC_ENABLE & I2C_ENR_ABORT) {
  159. if (overTime-- == 0)
  160. break;
  161. }
  162. i2c->IC_CLR_TX_ABRT;
  163. }
  164. ////////////////////////////////////////////////////////////////////////////////
  165. /// @brief Configures the specified I2C own address2.
  166. /// @param i2c: select the I2C peripheral.
  167. /// @param addr: specifies the 7bit I2C own address2.
  168. /// @retval None.
  169. ////////////////////////////////////////////////////////////////////////////////
  170. void I2C_OwnAddress2Config(I2C_TypeDef* i2c, u8 addr)
  171. {
  172. MODIFY_REG(i2c->IC_TAR, (u16)I2C_TAR_ADDR, (u16)(addr >> 1));
  173. }
  174. ////////////////////////////////////////////////////////////////////////////////
  175. /// @brief Enables or disables the specified I2C dual addressing mode.
  176. /// @param i2c: select the I2C peripheral.
  177. /// @param state: new state of the I2C dual addressing mode.
  178. /// This parameter can be: ENABLE or DISABLE.
  179. /// @retval None.
  180. ////////////////////////////////////////////////////////////////////////////////
  181. void I2C_DualAddressCmd(I2C_TypeDef* i2c, FunctionalState state)
  182. {
  183. (state) ? (i2c->IC_TAR |= IC_TAR_ENDUAL_Set) : (i2c->IC_TAR &= IC_TAR_ENDUAL_Reset);
  184. }
  185. ////////////////////////////////////////////////////////////////////////////////
  186. /// @brief Enables or disables the specified I2C general call feature.
  187. /// @param i2c: select the I2C peripheral.
  188. /// @param state: new state of the I2C General call.
  189. /// This parameter can be: ENABLE or DISABLE.
  190. /// @retval None.
  191. ////////////////////////////////////////////////////////////////////////////////
  192. void I2C_GeneralCallCmd(I2C_TypeDef* i2c, FunctionalState state)
  193. {
  194. (state) ? (i2c->IC_TAR |= I2C_TAR_SPECIAL) : (i2c->IC_TAR &= ~I2C_TAR_SPECIAL);
  195. }
  196. ////////////////////////////////////////////////////////////////////////////////
  197. /// @brief Enables or disables the specified I2C interrupts.
  198. /// @param i2c: select the I2C peripheral.
  199. /// @param it: specifies the I2C interrupts sources to be enabled
  200. /// or disabled.
  201. /// This parameter can be any combination of the following values:
  202. /// @arg I2C_IT_RX_UNDER : Rx Buffer is empty interrupt mask
  203. /// @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt mask
  204. /// @arg I2C_IT_RX_FULL : Rx buffer full interrupt mask
  205. /// @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt mask
  206. /// @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt mask
  207. /// @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt mask
  208. /// @arg I2C_IT_TX_ABRT : TX error interrupt mask(Master mode)
  209. /// @arg I2C_IT_RX_DONE : Master not ack interrupt mask(slave mode)
  210. /// @arg I2C_IT_ACTIVITY : I2C activity interrupt mask
  211. /// @arg I2C_IT_STOP_DET : stop condition interrupt mask
  212. /// @arg I2C_IT_START_DET : start condition interrupt mask
  213. /// @arg I2C_IT_GEN_CALL : a general call address and ack interrupt mask
  214. /// @param state: new state of the specified I2C interrupts.
  215. /// This parameter can be: ENABLE or DISABLE.
  216. /// @retval None.
  217. ////////////////////////////////////////////////////////////////////////////////
  218. void I2C_ITConfig(I2C_TypeDef* i2c, u16 it, FunctionalState state)
  219. {
  220. if (it == I2C_IT_RX_FULL)
  221. I2C_ReadCmd(i2c);
  222. (state) ? SET_BIT(i2c->IC_INTR_MASK, it) : CLEAR_BIT(i2c->IC_INTR_MASK, (u16)it);
  223. }
  224. ////////////////////////////////////////////////////////////////////////////////
  225. /// @brief Sends a data byte through the i2c peripheral.
  226. /// @param i2c: select the I2C peripheral.
  227. /// @param dat: Byte to be transmitted..
  228. /// @retval None.
  229. ////////////////////////////////////////////////////////////////////////////////
  230. void I2C_SendData(I2C_TypeDef* i2c, u8 dat)
  231. {
  232. i2c->IC_DATA_CMD = dat;
  233. }
  234. ////////////////////////////////////////////////////////////////////////////////
  235. /// @brief Returns the most recent received data by the i2c peripheral.
  236. /// @param i2c: select the I2C peripheral.
  237. /// @retval The value of the received data.
  238. ////////////////////////////////////////////////////////////////////////////////
  239. void I2C_ReadCmd(I2C_TypeDef* i2c)
  240. {
  241. i2c->IC_DATA_CMD = I2C_DR_CMD;
  242. }
  243. ////////////////////////////////////////////////////////////////////////////////
  244. /// @brief Returns the most recent received data by the i2c peripheral.
  245. /// @param i2c: select the I2C peripheral.
  246. /// @retval The value of the received data.
  247. ////////////////////////////////////////////////////////////////////////////////
  248. u8 I2C_ReceiveData(I2C_TypeDef* i2c)
  249. {
  250. I2C_CMD_DIR = 0;
  251. return (u8)i2c->IC_DATA_CMD;
  252. }
  253. ////////////////////////////////////////////////////////////////////////////////
  254. /// @brief Transmits the address byte to select the slave device.
  255. /// @param i2c: select the I2C peripheral.
  256. /// @param addr: specifies the slave address which will be transmitted
  257. /// @param dir: specifies whether the I2C device will be a
  258. /// Transmitter or a Receiver.
  259. /// This parameter can be one of the following values
  260. /// @arg I2C_Direction_Transmitter: Transmitter mode
  261. /// @arg I2C_Direction_Receiver: Receiver mode
  262. /// @retval None.
  263. ////////////////////////////////////////////////////////////////////////////////
  264. void I2C_Send7bitAddress(I2C_TypeDef* i2c, u8 addr, u8 dir)
  265. {
  266. i2c->IC_TAR = addr >> 1;
  267. }
  268. ////////////////////////////////////////////////////////////////////////////////
  269. /// @brief Reads the specified I2C register and returns its value.
  270. /// @param i2c: select the I2C peripheral.
  271. /// @param reg: specifies the register to read.
  272. /// This parameter can be one of the following values:
  273. /// @retval The value of the read register.
  274. ////////////////////////////////////////////////////////////////////////////////
  275. u16 I2C_ReadRegister(I2C_TypeDef* i2c, u8 reg)
  276. {
  277. return (*(vu16*)(*((u32*)&i2c) + reg));
  278. }
  279. ////////////////////////////////////////////////////////////////////////////////
  280. /// @brief Returns the last i2c Event.
  281. /// @param i2c: select the I2C peripheral.
  282. /// @retval The last event
  283. ////////////////////////////////////////////////////////////////////////////////
  284. u32 I2C_GetLastEvent(I2C_TypeDef* i2c)
  285. {
  286. return (u32)i2c->IC_RAW_INTR_STAT & FLAG_Mask;
  287. }
  288. ////////////////////////////////////////////////////////////////////////////////
  289. /// @brief Checks whether the last i2c Event is equal to the one passed
  290. /// as parameter.
  291. /// @param i2c: select the I2C peripheral.
  292. /// @param event: specifies the event to be checked.
  293. /// This parameter can be one of the following values:
  294. /// @arg I2C_EVENT_RX_UNDER : Rx Buffer is empty event
  295. /// @arg I2C_EVENT_RX_OVER : RX Buffer Overrun event
  296. /// @arg I2C_EVENTT_RX_FULL : Rx buffer full event
  297. /// @arg I2C_EVENT_TX_OVER : TX Buffer Overrun event
  298. /// @arg I2C_EVENT_TX_EMPTY : TX_FIFO empty event
  299. /// @arg I2C_EVENT_RD_REQ : I2C work as slave or master event
  300. /// @arg I2C_EVENT_TX_ABRT : TX error event(Master mode)
  301. /// @arg I2C_EVENT_RX_DONE : Master not ack event(slave mode)
  302. /// @arg I2C_EVENT_ACTIVITY : I2C activity event
  303. /// @arg I2C_EVENT_STOP_DET : stop condition event
  304. /// @arg I2C_EVENT_START_DET: start condition event
  305. /// @arg I2C_EVENT_GEN_CALL : a general call address and ack event
  306. /// - SUCCESS: Last event is equal to the I2C_EVENT
  307. /// - ERROR: Last event is different from the I2C_EVENT
  308. ////////////////////////////////////////////////////////////////////////////////
  309. ErrorStatus I2C_CheckEvent(I2C_TypeDef* i2c, u32 event)
  310. {
  311. if ((event == I2C_EVENT_RX_FULL) && (I2C_CMD_DIR == 0)) {
  312. i2c->IC_DATA_CMD = I2C_DR_CMD;
  313. I2C_CMD_DIR = 1;
  314. }
  315. return (ErrorStatus)((i2c->IC_RAW_INTR_STAT & event) == event);
  316. }
  317. ////////////////////////////////////////////////////////////////////////////////
  318. /// @brief Checks whether the specified I2C flag is set or not.
  319. /// @param i2c: select the I2C peripheral.
  320. /// @param flag: specifies the flag to check.
  321. /// This parameter can be one of the following values:
  322. /// @arg I2C_FLAG_RX_UNDER : Rx Buffer is empty flag
  323. /// @arg I2C_FLAG_RX_OVER : RX Buffer Overrun flag
  324. /// @arg I2C_FLAG_RX_FULL : Rx buffer full flag
  325. /// @arg I2C_FLAG_TX_OVER : TX Buffer Overrun flag
  326. /// @arg I2C_FLAG_TX_EMPTY : TX_FIFO empty flag
  327. /// @arg I2C_FLAG_RD_REQ : I2C work as slave or master flag
  328. /// @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
  329. /// @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
  330. /// @arg I2C_FLAG_ACTIVITY : I2C activity flag
  331. /// @arg I2C_FLAG_STOP_DET : stop condition flag
  332. /// @arg I2C_FLAG_START_DET: start condition flag
  333. /// @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
  334. /// @retval The new state of I2C_FLAG (SET or RESET).
  335. ////////////////////////////////////////////////////////////////////////////////
  336. FlagStatus I2C_GetFlagStatus(I2C_TypeDef* i2c, u32 flag)
  337. {
  338. if (flag & 0x8000)
  339. return ((i2c->IC_STATUS & flag) ? SET : RESET);
  340. if ((flag == I2C_FLAG_RX_FULL) && (I2C_CMD_DIR == 0)) {
  341. i2c->IC_DATA_CMD = I2C_DR_CMD;
  342. I2C_CMD_DIR = 1;
  343. }
  344. return (((i2c->IC_RAW_INTR_STAT & flag)) ? SET : RESET);
  345. }
  346. ////////////////////////////////////////////////////////////////////////////////
  347. /// @brief Clears the i2c's pending flags.
  348. /// @param i2c: select the I2C peripheral.
  349. /// @param flag: specifies the flag to clear.
  350. /// This parameter can be any combination of the following values:
  351. /// @arg I2C_FLAG_RX_UNDER : Rx Buffer is empty flag
  352. /// @arg I2C_FLAG_RX_OVER : RX Buffer Overrun flag
  353. /// @arg I2C_FLAG_RX_FULL : Rx buffer full flag
  354. /// @arg I2C_FLAG_TX_OVER : TX Buffer Overrun flag
  355. /// @arg I2C_FLAG_TX_EMPTY : TX_FIFO empty flag
  356. /// @arg I2C_FLAG_RD_REQ : I2C work as slave or master flag
  357. /// @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
  358. /// @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
  359. /// @arg I2C_FLAG_ACTIVITY : I2C activity flag
  360. /// @arg I2C_FLAG_STOP_DET : stop condition flag
  361. /// @arg I2C_FLAG_START_DET: start condition flag
  362. /// @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
  363. /// @retval None.
  364. ////////////////////////////////////////////////////////////////////////////////
  365. void I2C_ClearFlag(I2C_TypeDef* i2c, u32 flag)
  366. {
  367. if ((flag & I2C_FLAG_RX_UNDER) == I2C_FLAG_RX_UNDER)
  368. i2c->IC_CLR_RX_UNDER;
  369. if ((flag & I2C_FLAG_RX_OVER) == I2C_FLAG_RX_OVER)
  370. i2c->IC_CLR_RX_OVER;
  371. if ((flag & I2C_FLAG_TX_OVER) == I2C_FLAG_TX_OVER)
  372. i2c->IC_CLR_TX_OVER;
  373. if ((flag & I2C_FLAG_RD_REQ) == I2C_FLAG_RD_REQ)
  374. i2c->IC_CLR_RD_REQ;
  375. if ((flag & I2C_FLAG_TX_ABRT) == I2C_FLAG_TX_ABRT)
  376. i2c->IC_CLR_TX_ABRT;
  377. if ((flag & I2C_FLAG_RX_DONE) == I2C_FLAG_RX_DONE)
  378. i2c->IC_CLR_RX_DONE;
  379. if ((flag & I2C_FLAG_ACTIVITY) == I2C_FLAG_ACTIVITY)
  380. i2c->IC_CLR_ACTIVITY;
  381. if ((flag & I2C_FLAG_STOP_DET) == I2C_FLAG_STOP_DET)
  382. i2c->IC_CLR_STOP_DET;
  383. if ((flag & I2C_FLAG_START_DET) == I2C_FLAG_START_DET)
  384. i2c->IC_CLR_START_DET;
  385. if ((flag & I2C_FLAG_GEN_CALL) == I2C_FLAG_GEN_CALL)
  386. i2c->IC_CLR_GEN_CALL;
  387. }
  388. ////////////////////////////////////////////////////////////////////////////////
  389. /// @brief Checks whether the specified I2C interrupt has occurred or not.
  390. /// @param i2c: select the I2C peripheral.
  391. /// @param it: specifies the interrupt source to check.
  392. /// This parameter can be one of the following values:
  393. /// @arg I2C_IT_RX_UNDER : Rx Buffer is empty interrupt
  394. /// @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt
  395. /// @arg I2C_IT_RX_FULL : Rx buffer full interrupt
  396. /// @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt
  397. /// @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
  398. /// @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt
  399. /// @arg I2C_IT_TX_ABRT : TX error interrupt (Master mode)
  400. /// @arg I2C_IT_RX_DONE : Master not ack interrupt (slave mode)
  401. /// @arg I2C_IT_ACTIVITY : I2C activity interrupt
  402. /// @arg I2C_IT_STOP_DET : stop condition interrupt
  403. /// @arg I2C_IT_START_DET: start condition interrupt
  404. /// @arg I2C_IT_GEN_CALL : a general call address and ack interrupt
  405. /// @retval The new state of I2C_IT (SET or RESET).
  406. ////////////////////////////////////////////////////////////////////////////////
  407. ITStatus I2C_GetITStatus(I2C_TypeDef* i2c, u32 it)
  408. {
  409. return ((i2c->IC_RAW_INTR_STAT & it) ? SET : RESET);
  410. }
  411. ////////////////////////////////////////////////////////////////////////////////
  412. /// @brief Clears the i2c interrupt pending bits.
  413. /// @param i2c: select the I2C peripheral.
  414. /// @param it: specifies the interrupt pending bit to clear.
  415. /// This parameter can be any combination of the following values:
  416. /// @arg I2C_IT_RX_UNDER : Rx Buffer is empty interrupt
  417. /// @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt
  418. /// @arg I2C_IT_RX_FULL : Rx buffer full interrupt
  419. /// @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt
  420. /// @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
  421. /// @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt
  422. /// @arg I2C_IT_TX_ABRT : TX error interrupt (Master mode)
  423. /// @arg I2C_IT_RX_DONE : Master not ack interrupt (slave mode)
  424. /// @arg I2C_IT_ACTIVITY : I2C activity interrupt
  425. /// @arg I2C_IT_STOP_DET : stop condition interrupt
  426. /// @arg I2C_IT_START_DET: start condition interrupt
  427. /// @arg I2C_IT_GEN_CALL : a general call address and ack interrupt
  428. /// @retval None.
  429. ////////////////////////////////////////////////////////////////////////////////
  430. void I2C_ClearITPendingBit(I2C_TypeDef* i2c, u32 it)
  431. {
  432. if ((it & I2C_IT_RX_UNDER) == I2C_FLAG_RX_UNDER)
  433. i2c->IC_CLR_RX_UNDER;
  434. if ((it & I2C_IT_RX_OVER) == I2C_FLAG_RX_OVER)
  435. i2c->IC_CLR_RX_OVER;
  436. if ((it & I2C_IT_TX_OVER) == I2C_FLAG_TX_OVER)
  437. i2c->IC_CLR_TX_OVER;
  438. if ((it & I2C_IT_RD_REQ) == I2C_FLAG_RD_REQ)
  439. i2c->IC_CLR_RD_REQ;
  440. if ((it & I2C_IT_TX_ABRT) == I2C_FLAG_TX_ABRT)
  441. i2c->IC_CLR_TX_ABRT;
  442. if ((it & I2C_IT_RX_DONE) == I2C_FLAG_RX_DONE)
  443. i2c->IC_CLR_RX_DONE;
  444. if ((it & I2C_IT_ACTIVITY) == I2C_FLAG_ACTIVITY)
  445. i2c->IC_CLR_ACTIVITY;
  446. if ((it & I2C_IT_STOP_DET) == I2C_FLAG_STOP_DET)
  447. i2c->IC_CLR_STOP_DET;
  448. if ((it & I2C_IT_START_DET) == I2C_FLAG_START_DET)
  449. i2c->IC_CLR_START_DET;
  450. if ((it & I2C_IT_GEN_CALL) == I2C_FLAG_GEN_CALL)
  451. i2c->IC_CLR_GEN_CALL;
  452. }
  453. ////////////////////////////////////////////////////////////////////////////////
  454. //
  455. // New Function Interface
  456. //
  457. ////////////////////////////////////////////////////////////////////////////////
  458. ////////////////////////////////////////////////////////////////////////////////
  459. /// @brief Configures slave address.
  460. /// @param i2c: select the I2C peripheral.
  461. /// @param addr: specifies the slave address which will be transmitted
  462. /// This parameter can be one of the following values
  463. /// @retval None.
  464. ////////////////////////////////////////////////////////////////////////////////
  465. void I2C_SendSlaveAddress(I2C_TypeDef* i2c, u8 addr)
  466. {
  467. WRITE_REG(i2c->IC_SAR, addr >> 1);
  468. }
  469. ////////////////////////////////////////////////////////////////////////////////
  470. /// @brief Enables or disables the I2C slave mode.
  471. /// @param i2c: select the I2C peripheral.
  472. /// @param state: new state of the specified I2C interrupts.
  473. /// This parameter can be: ENABLE or DISABLE.
  474. /// @retval None.
  475. ////////////////////////////////////////////////////////////////////////////////
  476. void I2C_SlaveConfigure(I2C_TypeDef* i2c, FunctionalState state)
  477. {
  478. (state) ? CLEAR_BIT(i2c->IC_CON, I2C_CR_SLAVEDIS) : SET_BIT(i2c->IC_CON, I2C_CR_SLAVEDIS);
  479. }
  480. /// @}
  481. /// @}
  482. /// @}