1
0

HAL_I2C.c 40 KB

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