HAL_I2C.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143
  1. /*
  2. ******************************************************************************
  3. * @file HAL_I2C.c
  4. * @version V1.0.0
  5. * @date 2020
  6. * @brief I2C HAL module driver.
  7. * This file provides firmware functions to manage the following
  8. * functionalities of the Inter Integrated Circuit (I2C) peripheral:
  9. * @ Initialization and de-initialization functions
  10. * @ IO operation functions
  11. * @ Peripheral Control functions
  12. ******************************************************************************
  13. */
  14. #include "ACM32Fxx_HAL.h"
  15. /* Private functions for I2C */
  16. static HAL_StatusTypeDef I2C_Set_Clock_Speed(I2C_HandleTypeDef *hi2c, uint32_t ClockSpeed);
  17. static HAL_StatusTypeDef I2C_Master_Request_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
  18. static HAL_StatusTypeDef I2C_Master_Request_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
  19. static HAL_StatusTypeDef I2C_Check_Device_Ready(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout);
  20. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout);
  21. /************************************************************************
  22. * function : HAL_I2C_IRQHandler
  23. * Description: This function handles I2C interrupt request.
  24. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  25. * the configuration information for I2C module
  26. ************************************************************************/
  27. __weak void HAL_I2C_IRQHandler(I2C_HandleTypeDef *hi2c)
  28. {
  29. uint32_t i;
  30. /* Slave ADDR1 Interrupt */
  31. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1))
  32. {
  33. /* Clear ADDR1 Interrupt Flag */
  34. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  35. /* Slave Transmit */
  36. if (READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
  37. {
  38. i = 1;
  39. /* Wait for transmission End*/
  40. while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
  41. /* Clear MTF */
  42. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  43. /* BUS BUSY */
  44. while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  45. {
  46. if (i >= hi2c->Tx_Size && hi2c->Tx_Size != 0)
  47. {
  48. break;
  49. }
  50. if (READ_BIT(hi2c->Instance->SR, I2C_SR_MTF))
  51. {
  52. /* Clear MTF */
  53. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  54. }
  55. if (READ_BIT(hi2c->Instance->SR, I2C_SR_TXE))
  56. {
  57. hi2c->Instance->DR = hi2c->Tx_Buffer[i++];
  58. hi2c->Tx_Count++;
  59. }
  60. }
  61. /* Set Slave machine is DILE */
  62. hi2c->Slave_TxState = SLAVE_TX_STATE_IDLE;
  63. }
  64. /* Slave Receive */
  65. else
  66. {
  67. i = 0;
  68. /* Wait for transmission End*/
  69. while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
  70. /* Clear MTF */
  71. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  72. /* BUS BUSY */
  73. while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  74. {
  75. /* Receive Data */
  76. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RXNE))
  77. {
  78. hi2c->Rx_Buffer[i++] = hi2c->Instance->DR;
  79. /* Wait for transmission End*/
  80. while(!READ_BIT(hi2c->Instance->SR, I2C_SR_MTF));
  81. /* Clear MTF */
  82. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  83. hi2c->Rx_Count++;
  84. if (hi2c->Rx_Size != 0)
  85. {
  86. if (i >= hi2c->Rx_Size)
  87. {
  88. break;
  89. }
  90. }
  91. }
  92. }
  93. /* Set Slave machine is DILE */
  94. hi2c->Slave_RxState = SLAVE_RX_STATE_IDLE;
  95. }
  96. if (hi2c->Slave_RxState == SLAVE_RX_STATE_IDLE && hi2c->Slave_TxState == SLAVE_TX_STATE_IDLE)
  97. {
  98. /* Disable RX_ADDR1_INT_EN */
  99. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
  100. }
  101. }
  102. /* STOP Flag Interrupt */
  103. if (READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF))
  104. {
  105. /* Clear STOPF Interrupt Flag */
  106. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF);
  107. /* Clear STOPF */
  108. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_STOPF_INTEN);
  109. if (hi2c->I2C_STOPF_Callback != NULL)
  110. {
  111. hi2c->I2C_STOPF_Callback();
  112. }
  113. }
  114. }
  115. /************************************************************************
  116. * function : HAL_I2C_MspInit
  117. * Description:
  118. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  119. * the configuration information for I2C module
  120. ************************************************************************/
  121. __weak void HAL_I2C_MspInit(I2C_HandleTypeDef *hi2c)
  122. {
  123. /*
  124. NOTE : This function should be modified by the user.
  125. */
  126. /* For Example */
  127. GPIO_InitTypeDef GPIO_Handle;
  128. /* I2C1 */
  129. if (hi2c->Instance == I2C1)
  130. {
  131. /* Enable Clock */
  132. System_Module_Enable(EN_I2C1);
  133. System_Module_Enable(EN_GPIOAB);
  134. /* I2C1 SDA PortB Pin7 */
  135. /* I2C1 SCL PortB Pin6 */
  136. GPIO_Handle.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  137. GPIO_Handle.Mode = GPIO_MODE_AF_PP;
  138. GPIO_Handle.Pull = GPIO_PULLUP;
  139. GPIO_Handle.Alternate = GPIO_FUNCTION_6;
  140. HAL_GPIO_Init(GPIOB, &GPIO_Handle);
  141. /* Clear Pending Interrupt */
  142. NVIC_ClearPendingIRQ(I2C1_IRQn);
  143. /* Enable External Interrupt */
  144. NVIC_EnableIRQ(I2C1_IRQn);
  145. }
  146. /* I2C2 */
  147. else if (hi2c->Instance == I2C2)
  148. {
  149. }
  150. }
  151. /************************************************************************
  152. * function : HAL_I2C_MspDeInit
  153. * Description:
  154. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  155. * the configuration information for I2C module
  156. ************************************************************************/
  157. __weak void HAL_I2C_MspDeInit(I2C_HandleTypeDef *hi2c)
  158. {
  159. /*
  160. NOTE : This function should be modified by the user.
  161. */
  162. /* For Example */
  163. GPIO_InitTypeDef GPIO_Handle;
  164. /* I2C1 */
  165. if (hi2c->Instance == I2C1)
  166. {
  167. /* Disable Clock */
  168. System_Module_Disable(EN_I2C1);
  169. /* I2C1 SDA PortB Pin7 */
  170. /* I2C1 SCL PortB Pin6 */
  171. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_6 | GPIO_PIN_7);
  172. /* Clear Pending Interrupt */
  173. NVIC_ClearPendingIRQ(I2C1_IRQn);
  174. /* Disable External Interrupt */
  175. NVIC_DisableIRQ(I2C1_IRQn);
  176. }
  177. /* I2C2 */
  178. else if (hi2c->Instance == I2C2)
  179. {
  180. }
  181. }
  182. /************************************************************************
  183. * function : HAL_I2C_Init
  184. * Description: I2c initial with parameters.
  185. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  186. * the configuration information for I2C module
  187. ************************************************************************/
  188. HAL_StatusTypeDef HAL_I2C_Init(I2C_HandleTypeDef *hi2c)
  189. {
  190. /* Check I2C Parameter */
  191. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  192. if (!IS_I2C_ALL_MODE(hi2c->Init.I2C_Mode)) return HAL_ERROR;
  193. if (!IS_I2C_CLOCK_SPEED(hi2c->Init.Clock_Speed)) return HAL_ERROR;
  194. if (!IS_I2C_TX_AUTO_EN(hi2c->Init.Tx_Auto_En)) return HAL_ERROR;
  195. if (!IS_I2C_STRETCH_EN(hi2c->Init.No_Stretch_Mode)) return HAL_ERROR;
  196. /* Disable the selected I2C peripheral */
  197. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_MEN);
  198. /* Init the low level hardware : GPIO, CLOCK, NVIC */
  199. HAL_I2C_MspInit(hi2c);
  200. switch (hi2c->Init.I2C_Mode)
  201. {
  202. /* Master Mode */
  203. case I2C_MODE_MASTER:
  204. {
  205. /* Set Master Mode */
  206. SET_BIT(hi2c->Instance->CR, I2C_CR_MASTER);
  207. /* Set Clock Speed */
  208. I2C_Set_Clock_Speed(hi2c, hi2c->Init.Clock_Speed);
  209. /* Set SDA auto change the direction */
  210. if (hi2c->Init.Tx_Auto_En == TX_AUTO_EN_ENABLE)
  211. SET_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
  212. else
  213. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
  214. /* Enable the selected I2C peripheral */
  215. SET_BIT(hi2c->Instance->CR, I2C_CR_MEN);
  216. }break;
  217. /* Slave Mode */
  218. case I2C_MODE_SLAVE:
  219. {
  220. SET_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
  221. /* Set SDA auto change the direction */
  222. if (hi2c->Init.Tx_Auto_En == TX_AUTO_EN_ENABLE)
  223. SET_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
  224. else
  225. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TX_AUTO_EN);
  226. /* Set Clock Stretch Mode */
  227. if (hi2c->Init.No_Stretch_Mode == NO_STRETCH_MODE_NOSTRETCH)
  228. SET_BIT(hi2c->Instance->CR, I2C_CR_NOSTRETCH);
  229. else
  230. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_NOSTRETCH);
  231. /* Set Address 1 */
  232. hi2c->Instance->SLAVE_ADDR1 = hi2c->Init.Own_Address;
  233. /* Enable the selected I2C peripheral */
  234. SET_BIT(hi2c->Instance->CR, I2C_CR_MEN);
  235. }break;
  236. default: break;
  237. }
  238. return HAL_OK;
  239. }
  240. /************************************************************************
  241. * function : HAL_I2C_DeInit
  242. * Description: I2c De-initial with parameters.
  243. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  244. * the configuration information for I2C module
  245. ************************************************************************/
  246. HAL_StatusTypeDef HAL_I2C_DeInit(I2C_HandleTypeDef *hi2c)
  247. {
  248. /* Check I2C Parameter */
  249. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  250. hi2c->Slave_RxState = SLAVE_RX_STATE_IDLE;
  251. hi2c->Slave_TxState = SLAVE_TX_STATE_IDLE;
  252. HAL_I2C_MspDeInit(hi2c);
  253. hi2c->Tx_Size = 0;
  254. hi2c->Rx_Size = 0;
  255. hi2c->Tx_Count = 0;
  256. hi2c->Rx_Count = 0;
  257. return HAL_OK;
  258. }
  259. /************************************************************************
  260. * function : HAL_I2C_Master_Transmit
  261. * Description: Transmits in master mode an amount of data in blocking mode.
  262. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  263. * the configuration information for I2C module
  264. * DevAddress : Target device address
  265. * pData : Pointer to data buffer
  266. * Size : Amount of data to be sent
  267. * Timeout : Timeout value
  268. ************************************************************************/
  269. HAL_StatusTypeDef HAL_I2C_Master_Transmit(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  270. {
  271. uint32_t i;
  272. /* Check I2C Parameter */
  273. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  274. hi2c->Tx_Buffer = pData;
  275. hi2c->Tx_Size = Size;
  276. hi2c->Tx_Count = 0;
  277. /* Send Write Access Request */
  278. if (I2C_Master_Request_Write(hi2c, DevAddress, 0) == HAL_OK)
  279. {
  280. for (i = 0; i < hi2c->Tx_Size; i++)
  281. {
  282. /* Wait TXE Flag */
  283. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TXE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  284. /* Send Data */
  285. hi2c->Instance->DR = hi2c->Tx_Buffer[hi2c->Tx_Count++];
  286. /* Wait for transmission End*/
  287. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  288. /* Clear MTF */
  289. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  290. /* Get NACK */
  291. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  292. {
  293. /* Generate Stop */
  294. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  295. /* Wait for the bus to idle */
  296. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  297. return HAL_ERROR;
  298. }
  299. }
  300. /* Generate Stop */
  301. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  302. /* Wait for the bus to idle */
  303. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  304. }
  305. else
  306. {
  307. return HAL_ERROR;
  308. }
  309. return HAL_OK;
  310. }
  311. /************************************************************************
  312. * function : HAL_I2C_Master_Receive
  313. * Description: Transmits in master mode an amount of data in blocking mode.
  314. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  315. * the configuration information for I2C module
  316. * DevAddress : Target device address
  317. * pData : Pointer to data buffer
  318. * Size : Amount of data to be Receive
  319. * Timeout : Timeout value
  320. ************************************************************************/
  321. HAL_StatusTypeDef HAL_I2C_Master_Receive(I2C_HandleTypeDef *hi2c, uint16_t DevAddress, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  322. {
  323. uint32_t i;
  324. /* Check I2C Parameter */
  325. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  326. hi2c->Rx_Buffer = pData;
  327. hi2c->Rx_Size = Size;
  328. hi2c->Rx_Count = 0;
  329. /* Send Read Access Request */
  330. if (I2C_Master_Request_Read(hi2c, DevAddress, Timeout) == HAL_OK)
  331. {
  332. /* Wait Master Transition receiving state */
  333. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TX_RX_FLAG, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  334. /* Clear TX_RX_FLAG */
  335. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_TX_RX_FLAG);
  336. /* Generate ACK */
  337. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  338. for (i = 0; i < hi2c->Rx_Size - 1; i++)
  339. {
  340. /* Wait RXNE Flag */
  341. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  342. /* Read Data */
  343. hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
  344. /* Wait for transmission End*/
  345. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  346. /* Clear MTF */
  347. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  348. }
  349. /* Prepare for Generate NACK */
  350. SET_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  351. /* Prepare for Generate STOP */
  352. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  353. /* Wait RXNE Flag */
  354. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  355. /* Read Data */
  356. hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
  357. /* Wait for transmission End*/
  358. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  359. /* Clear MTF */
  360. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  361. /* Wait for the bus to idle */
  362. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  363. /* Generate ACK */
  364. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  365. }
  366. else
  367. {
  368. return HAL_ERROR;
  369. }
  370. return HAL_OK;
  371. }
  372. /************************************************************************
  373. * function : HAL_I2C_Slave_Transmit
  374. * Description: Transmits in Slave mode an amount of data in blocking mode.
  375. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  376. * the configuration information for I2C module
  377. * pData : Pointer to data buffer
  378. * Size : Amount of data to be sent
  379. * Timeout : Timeout value
  380. ************************************************************************/
  381. HAL_StatusTypeDef HAL_I2C_Slave_Transmit(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  382. {
  383. uint32_t i = 0;
  384. /* Check I2C Parameter */
  385. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  386. hi2c->Tx_Buffer = pData;
  387. hi2c->Tx_Size = Size;
  388. hi2c->Tx_Count = 0;
  389. /* Clear RX_ADDR1 Flag */
  390. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  391. /* Match the Address 1 */
  392. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RX_ADDR1, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  393. /* Clear RX_ADDR1 Flag */
  394. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  395. /* Slave Transmit */
  396. if (READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
  397. {
  398. /* BUS BUSY */
  399. while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  400. {
  401. if (READ_BIT(hi2c->Instance->SR, I2C_SR_MTF))
  402. {
  403. /* Clear MTF */
  404. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  405. hi2c->Tx_Count++;
  406. }
  407. if (READ_BIT(hi2c->Instance->SR, I2C_SR_TXE))
  408. {
  409. if (i < hi2c->Tx_Size || hi2c->Tx_Size == 0)
  410. {
  411. hi2c->Instance->DR = hi2c->Tx_Buffer[i++];
  412. }
  413. }
  414. }
  415. hi2c->Instance->SR = READ_REG(hi2c->Instance->SR);
  416. }
  417. else
  418. {
  419. return HAL_ERROR;
  420. }
  421. hi2c->Tx_Count--;
  422. return HAL_OK;
  423. }
  424. /************************************************************************
  425. * function : HAL_I2C_Slave_Transmit_IT
  426. * Description: Transmit in slave mode an amount of data in non-blocking mode with Interrupt
  427. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  428. * the configuration information for I2C module
  429. * pData : Pointer to data buffer
  430. * Size : Amount of data to be sent
  431. * return : HAL_StatusTypeDef
  432. ************************************************************************/
  433. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
  434. {
  435. /* Check I2C Parameter */
  436. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  437. /* Rx machine is running */
  438. if (hi2c->Slave_TxState != SLAVE_TX_STATE_IDLE)
  439. return HAL_ERROR;
  440. /* Set Slave machine is sending */
  441. hi2c->Slave_TxState = SLAVE_TX_STATE_SENDING;
  442. hi2c->Tx_Buffer = pData;
  443. hi2c->Tx_Size = Size;
  444. hi2c->Tx_Count = 0;
  445. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
  446. hi2c->Instance->DR = hi2c->Tx_Buffer[0];
  447. hi2c->Tx_Count++;
  448. /* Clear RX ADDR1 Flag */
  449. SET_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  450. /* RX ADDR1 Interrupt Enable */
  451. SET_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
  452. return HAL_OK;
  453. }
  454. /************************************************************************
  455. * function : HAL_I2C_Slave_Receive
  456. * Description: Receive in Slave mode an amount of data in blocking mode.
  457. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  458. * the configuration information for I2C module
  459. * pData : Pointer to data buffer
  460. * Size : Amount of data to be sent
  461. * Timeout : Timeout value
  462. * return : HAL_StatusTypeDef
  463. ************************************************************************/
  464. HAL_StatusTypeDef HAL_I2C_Slave_Receive(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size, uint32_t Timeout)
  465. {
  466. uint32_t i = 0;
  467. HAL_StatusTypeDef Status;
  468. /* Check I2C Parameter */
  469. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;;
  470. hi2c->Rx_Buffer = pData;
  471. hi2c->Rx_Size = Size;
  472. hi2c->Rx_Count = 0;
  473. /* Match the Address 1 */
  474. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RX_ADDR1, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  475. /* Clear RX_ADDR1 Flag */
  476. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  477. /* Slave Receive */
  478. if (!READ_BIT(hi2c->Instance->SR, I2C_SR_SRW))
  479. {
  480. /* Wait for transmission End*/
  481. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  482. /* Clear MTF */
  483. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  484. /* BUS BUSY */
  485. while(READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  486. {
  487. /* Receive Data */
  488. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RXNE))
  489. {
  490. hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
  491. /* Wait for transmission End*/
  492. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  493. /* Clear MTF */
  494. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  495. if (hi2c->Rx_Size != 0)
  496. {
  497. if (hi2c->Rx_Count >= hi2c->Rx_Size)
  498. {
  499. break;
  500. }
  501. }
  502. }
  503. }
  504. /* Generate ACK */
  505. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  506. hi2c->Instance->SR = READ_REG(hi2c->Instance->SR);
  507. }
  508. /* Slave Transmit */
  509. else
  510. {
  511. return HAL_ERROR;
  512. }
  513. return HAL_OK;
  514. }
  515. /************************************************************************
  516. * function : HAL_I2C_Slave_Receive_IT
  517. * Description: Receive in slave mode an amount of data in non-blocking mode with Interrupt
  518. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  519. * the configuration information for I2C module
  520. * pData : Pointer to data buffer
  521. * Size : Amount of data to be sent
  522. ************************************************************************/
  523. HAL_StatusTypeDef HAL_I2C_Slave_Receive_IT(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
  524. {
  525. /* Check I2C Parameter */
  526. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  527. /* Rx machine is running */
  528. if (hi2c->Slave_RxState != SLAVE_RX_STATE_IDLE)
  529. return HAL_ERROR;
  530. /* Set Slave machine is receiving */
  531. hi2c->Slave_RxState = SLAVE_RX_STATE_RECEIVING;
  532. hi2c->Rx_Buffer = pData;
  533. hi2c->Rx_Size = Size;
  534. hi2c->Rx_Count = 0;
  535. /* Clear RX ADDR1 Flag */
  536. SET_BIT(hi2c->Instance->SR, I2C_SR_RX_ADDR1);
  537. /* RX ADDR1 Interrupt Enable */
  538. SET_BIT(hi2c->Instance->CR, I2C_CR_RX_ADDR1_INT_EN);
  539. return HAL_OK;
  540. }
  541. /************************************************************************
  542. * function : HAL_I2C_Slave_Receive_DMA
  543. * Description: Receive in slave mode an amount of data in non-blocking mode with DMA
  544. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  545. * the configuration information for I2C module
  546. * pData : Pointer to data buffer
  547. * Size : Amount of data to be sent
  548. ************************************************************************/
  549. HAL_StatusTypeDef HAL_I2C_Slave_Receive_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
  550. {
  551. /* Check I2C Parameter */
  552. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  553. hi2c->Rx_Buffer = pData;
  554. hi2c->Rx_Size = Size;
  555. hi2c->Rx_Count = Size;
  556. /* DMA Enable */
  557. SET_BIT(hi2c->Instance->CR, I2C_CR_DMA_EN);
  558. /* Clear STOPF Interrupt Flag */
  559. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_STOPF);
  560. /* STOPF Interrupt Enable */
  561. SET_BIT(hi2c->Instance->CR, I2C_CR_STOPF_INTEN);
  562. HAL_DMA_Start(hi2c->HDMA_Rx, (uint32_t)&hi2c->Instance->DR, (uint32_t)hi2c->Rx_Buffer, hi2c->Rx_Size);
  563. return HAL_OK;
  564. }
  565. /************************************************************************
  566. * function : HAL_I2C_Slave_Transmit_DMA
  567. * Description: Transmit in slave mode an amount of data in non-blocking mode with DMA
  568. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  569. * the configuration information for I2C module
  570. * pData : Pointer to data buffer
  571. * Size : Amount of data to be sent
  572. ************************************************************************/
  573. HAL_StatusTypeDef HAL_I2C_Slave_Transmit_DMA(I2C_HandleTypeDef *hi2c, uint8_t *pData, uint32_t Size)
  574. {
  575. /* Check I2C Parameter */
  576. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  577. hi2c->Tx_Buffer = pData;
  578. hi2c->Tx_Size = Size;
  579. hi2c->Tx_Count = Size;
  580. /* Must Set TXE_SEL In DMA Mode !!! */
  581. SET_BIT(hi2c->Instance->CR, I2C_CR_TXE_SEL);
  582. /* DMA Enable */
  583. SET_BIT(hi2c->Instance->CR, I2C_CR_DMA_EN);
  584. HAL_DMA_Start_IT(hi2c->HDMA_Tx, (uint32_t)hi2c->Tx_Buffer, (uint32_t)&hi2c->Instance->DR, hi2c->Tx_Size);
  585. return HAL_OK;
  586. }
  587. /************************************************************************
  588. * function : HAL_I2C_Mem_Write
  589. * Description: Write an amount of data in blocking mode to a specific memory address
  590. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  591. * the configuration information for I2C module
  592. * DevAddress : Target device address
  593. * MemAddress : MemAddress Internal memory address
  594. * MemAddSize : MemAddSize Size of internal memory address
  595. * pData : Pointer to data buffer
  596. * Size : Amount of data to be sent
  597. * Timeout : Timeout value
  598. ************************************************************************/
  599. HAL_StatusTypeDef HAL_I2C_Mem_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  600. {
  601. uint32_t i;
  602. /* Check I2C Parameter */
  603. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  604. hi2c->Tx_Buffer = pData;
  605. hi2c->Tx_Size = Size;
  606. hi2c->Tx_Count = 0;
  607. /* Bus Busy */
  608. if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  609. return HAL_ERROR;
  610. /* Send Write Access Request */
  611. if (I2C_Master_Request_Write(hi2c, DevAddress,0) == HAL_OK)
  612. {
  613. /* If Memory address size is 8Bit */
  614. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  615. {
  616. /* Send Memory Address */
  617. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  618. }
  619. /* If Memory address size is 16Bit */
  620. else
  621. {
  622. /* Send Memory Address MSB*/
  623. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  624. /* Wait for transmission End*/
  625. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  626. /* Clear MTF */
  627. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  628. /* Send Memory Address LSB*/
  629. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  630. }
  631. /* Wait for transmission End*/
  632. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  633. /* Clear MTF */
  634. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  635. /* Get NACK */
  636. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  637. {
  638. /* Generate Stop */
  639. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  640. /* Wait for the bus to idle */
  641. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  642. return HAL_ERROR;
  643. }
  644. /* Get ACK */
  645. else
  646. {
  647. for (i = 0; i < hi2c->Tx_Size; i++)
  648. {
  649. /* Wait TXE Flag */
  650. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TXE, RESET, Timeout)!= HAL_OK) return HAL_ERROR;
  651. /* Send Data */
  652. hi2c->Instance->DR = hi2c->Tx_Buffer[hi2c->Tx_Count++];
  653. /* Wait for transmission End*/
  654. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  655. /* Clear MTF */
  656. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  657. /* Get NACK */
  658. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  659. {
  660. /* Generate Stop */
  661. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  662. /* Wait for the bus to idle */
  663. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  664. return HAL_ERROR;
  665. }
  666. }
  667. /* Generate Stop */
  668. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  669. /* Wait for the bus to idle */
  670. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  671. /* Check Device Ready */
  672. while(I2C_Check_Device_Ready(hi2c, DevAddress, Timeout) != HAL_OK);
  673. }
  674. }
  675. else
  676. {
  677. return HAL_ERROR;
  678. }
  679. return HAL_OK;
  680. }
  681. /************************************************************************
  682. * function : HAL_I2C_Mem_Read
  683. * Description: Read an amount of data in blocking mode to a specific memory address
  684. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  685. * the configuration information for I2C module
  686. * DevAddress : Target device address
  687. * MemAddress : MemAddress Internal memory address
  688. * MemAddSize : MemAddSize Size of internal memory address
  689. * pData : Pointer to data buffer
  690. * Size : Amount of data to be sent
  691. * Timeout : Timeout value
  692. ************************************************************************/
  693. HAL_StatusTypeDef HAL_I2C_Mem_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint16_t MemAddress, uint16_t MemAddSize, uint8_t *pData, uint16_t Size, uint32_t Timeout)
  694. {
  695. uint32_t i;
  696. /* Check I2C Parameter */
  697. if (!IS_I2C_ALL_INSTANCE(hi2c->Instance)) return HAL_ERROR;
  698. hi2c->Rx_Buffer = pData;
  699. hi2c->Rx_Size = Size;
  700. hi2c->Rx_Count = 0;
  701. /* Bus Busy */
  702. if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  703. return HAL_ERROR;
  704. /* Send Write Access Request */
  705. if (I2C_Master_Request_Write(hi2c, DevAddress,0) == HAL_OK)
  706. {
  707. /* If Memory address size is 8Bit */
  708. if (MemAddSize == I2C_MEMADD_SIZE_8BIT)
  709. {
  710. /* Send Memory Address */
  711. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  712. }
  713. /* If Memory address size is 16Bit */
  714. else
  715. {
  716. /* Send Memory Address MSB*/
  717. hi2c->Instance->DR = I2C_MEM_ADD_MSB(MemAddress);
  718. /* Wait for transmission End*/
  719. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  720. /* Clear MTF */
  721. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  722. /* Send Memory Address LSB*/
  723. hi2c->Instance->DR = I2C_MEM_ADD_LSB(MemAddress);
  724. }
  725. /* Wait for transmission End*/
  726. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  727. /* Clear MTF */
  728. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  729. /* Get NACK */
  730. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  731. {
  732. /* Generate Stop */
  733. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  734. /* Wait for the bus to idle */
  735. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  736. return HAL_ERROR;
  737. }
  738. /* Get ACK */
  739. else
  740. {
  741. /* Send Write Read Request */
  742. if (I2C_Master_Request_Read(hi2c, DevAddress, Timeout) == HAL_OK)
  743. {
  744. /* Wait Master Transition receiving state */
  745. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_TX_RX_FLAG, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  746. /* Clear TX_RX_FLAG */
  747. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_TX_RX_FLAG);
  748. /* Generate ACK */
  749. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  750. for (i = 0; i < hi2c->Rx_Size - 1; i++)
  751. {
  752. /* Wait RXNE Flag */
  753. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  754. /* Read Data */
  755. hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
  756. /* Wait for transmission End*/
  757. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  758. /* Clear MTF */
  759. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  760. }
  761. /* Prepare for Generate NACK */
  762. SET_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  763. /* Prepare for Generate STOP */
  764. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  765. /* Wait RXNE Flag */
  766. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_RXNE, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  767. /* Read Data */
  768. hi2c->Rx_Buffer[hi2c->Rx_Count++] = hi2c->Instance->DR;
  769. /* Wait for transmission End*/
  770. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  771. /* Clear MTF */
  772. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  773. /* Wait for the bus to idle */
  774. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  775. /* Generate ACK */
  776. CLEAR_BIT(hi2c->Instance->CR, I2C_CR_TACK);
  777. }
  778. else
  779. {
  780. /* Get NACK */
  781. return HAL_ERROR;
  782. }
  783. }
  784. }
  785. else
  786. {
  787. return HAL_ERROR;
  788. }
  789. return HAL_OK;
  790. }
  791. /************************************************************************
  792. * function : HAL_I2C_GetSlaveRxState
  793. * Description: Get Slave Rx State
  794. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  795. * the configuration information for I2C module
  796. * return : Slave State
  797. ************************************************************************/
  798. uint8_t HAL_I2C_GetSlaveRxState(I2C_HandleTypeDef *hi2c)
  799. {
  800. return hi2c->Slave_RxState;
  801. }
  802. /************************************************************************
  803. * function : HAL_I2C_GetSlaveTxState
  804. * Description: Get Slave Tx State
  805. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  806. * the configuration information for I2C module
  807. * return : Slave State
  808. ************************************************************************/
  809. uint8_t HAL_I2C_GetSlaveTxState(I2C_HandleTypeDef *hi2c)
  810. {
  811. return hi2c->Slave_TxState;
  812. }
  813. /************************************************************************
  814. * function : I2C_Set_Clock_Speed
  815. * Description: Set I2C Clock Speed
  816. * input : hi2c : pointer to a I2C_HandleTypeDef structure that contains
  817. * the configuration information for I2C module
  818. * ClockSpeed: I2C Clock Speed
  819. ************************************************************************/
  820. static HAL_StatusTypeDef I2C_Set_Clock_Speed(I2C_HandleTypeDef *hi2c, uint32_t ClockSpeed)
  821. {
  822. uint32_t APB_Clock;
  823. APB_Clock = System_Get_APBClock();
  824. hi2c->Instance->CLK_DIV = APB_Clock / (4 * ClockSpeed) - 1;
  825. return HAL_OK;
  826. }
  827. /**
  828. * @brief This function handles I2C Communication Timeout.
  829. * @param hi2c Pointer to a I2C_HandleTypeDef structure that contains
  830. * the configuration information for I2C module
  831. * @param Flag specifies the I2C flag to check.
  832. * @param Status The new Flag status (SET or RESET).
  833. * @param Timeout Timeout duration
  834. * @param Tickstart Tick start value
  835. * @retval HAL status
  836. */
  837. static HAL_StatusTypeDef I2C_WaitOnFlagUntilTimeout(I2C_HandleTypeDef *hi2c, uint32_t Flag, FlagStatus Status, uint32_t Timeout)
  838. {
  839. __IO uint32_t lu32_Timeout;
  840. /* have no timeout */
  841. if (Timeout == 0)
  842. {
  843. while (__HAL_I2C_GET_FLAG(hi2c, Flag)==Status);
  844. }
  845. else
  846. {
  847. lu32_Timeout = Timeout * 0xFF;
  848. while (__HAL_I2C_GET_FLAG(hi2c, Flag)==Status)
  849. {
  850. if (lu32_Timeout-- == 0)
  851. {
  852. return HAL_ERROR;
  853. }
  854. }
  855. }
  856. return HAL_OK;
  857. }
  858. /************************************************************************
  859. * function : I2C_Master_Request_Write
  860. * Description: I2C Write Access Request
  861. * input : hi2c : pointer to a I2C_HandleTypeDef structure
  862. * DevAddress: Device address
  863. * Timeout: Timeout value
  864. ************************************************************************/
  865. static HAL_StatusTypeDef I2C_Master_Request_Write(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
  866. {
  867. /* Generate Start */
  868. SET_BIT(hi2c->Instance->CR, I2C_CR_START);
  869. /* Clear MTF, To Prevent Errors */
  870. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  871. /* Send Device Address */
  872. hi2c->Instance->DR = DevAddress & 0xFE;
  873. /* Wait for transmission End*/
  874. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  875. /* Clear MTF */
  876. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  877. /* Get NACK */
  878. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  879. {
  880. /* Generate Stop */
  881. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  882. /* Wait for the bus to idle */
  883. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  884. return HAL_ERROR;
  885. }
  886. /* Get ACK */
  887. else
  888. {
  889. return HAL_OK;
  890. }
  891. }
  892. /************************************************************************
  893. * function : I2C_Master_Request_Read
  894. * Description: I2C Read Access Request
  895. * input : hi2c : pointer to a I2C_HandleTypeDef structure
  896. * DevAddress: Device address
  897. * Timeout: Timeout value
  898. ************************************************************************/
  899. static HAL_StatusTypeDef I2C_Master_Request_Read(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
  900. {
  901. /* Generate Start */
  902. SET_BIT(hi2c->Instance->CR, I2C_CR_START);
  903. /* Clear MTF, To Prevent Errors */
  904. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  905. /* Send Device Address */
  906. hi2c->Instance->DR = DevAddress | 0x01;
  907. /* Wait for transmission End */
  908. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  909. /* Clear MTF */
  910. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  911. /* Get NACK */
  912. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  913. {
  914. /* Generate Stop */
  915. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  916. /* Wait for the bus to idle */
  917. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  918. return HAL_ERROR;
  919. }
  920. /* Get ACK */
  921. else
  922. {
  923. return HAL_OK;
  924. }
  925. }
  926. /************************************************************************
  927. * function : I2C_Check_Device_Ready
  928. * Description: Check Device Ready
  929. * input : hi2c : pointer to a I2C_HandleTypeDef structure
  930. * DevAddress: Device address
  931. * Timeout: Timeout value
  932. ************************************************************************/
  933. static HAL_StatusTypeDef I2C_Check_Device_Ready(I2C_HandleTypeDef *hi2c, uint8_t DevAddress, uint32_t Timeout)
  934. {
  935. /* Bus Busy */
  936. if (READ_BIT(hi2c->Instance->SR, I2C_SR_BUS_BUSY))
  937. return HAL_ERROR;
  938. /* Generate Start */
  939. SET_BIT(hi2c->Instance->CR, I2C_CR_START);
  940. /* Send Device Address */
  941. hi2c->Instance->DR = DevAddress;
  942. /* Wait for transmission End */
  943. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_MTF, RESET, Timeout) != HAL_OK) return HAL_ERROR;
  944. /* Clear MTF */
  945. hi2c->Instance->SR = READ_BIT(hi2c->Instance->SR, I2C_SR_MTF);
  946. /* Get NACK */
  947. if (READ_BIT(hi2c->Instance->SR, I2C_SR_RACK))
  948. {
  949. /* Generate Stop */
  950. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  951. /* Wait for the bus to idle */
  952. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  953. return HAL_ERROR;
  954. }
  955. /* Get ACK */
  956. else
  957. {
  958. /* Generate Stop */
  959. SET_BIT(hi2c->Instance->CR, I2C_CR_STOP);
  960. /* Wait for the bus to idle */
  961. if(I2C_WaitOnFlagUntilTimeout(hi2c, I2C_SR_BUS_BUSY, SET, Timeout) != HAL_OK) return HAL_ERROR;
  962. return HAL_OK;
  963. }
  964. }