stm320518_eval_i2c_tsensor.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /**
  2. ******************************************************************************
  3. * @file stm320518_eval_i2c_tsensor.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 20-April-2012
  7. * @brief This file provides a set of functions needed to manage the I2C LM75
  8. * temperature sensor mounted on STM320518-EVAL board.
  9. * It implements a high level communication layer for read and write
  10. * from/to this sensor. The needed STM32F0xx hardware resources (I2C and
  11. * GPIO) are defined in stm320518_eval.h file, and the initialization is
  12. * performed in LM75_LowLevel_Init() function declared in stm320518_eval.c
  13. * file.
  14. * You can easily tailor this driver to any other development board,
  15. * by just adapting the defines for hardware resources and
  16. * LM75_LowLevel_Init() function.
  17. *
  18. * +-----------------------------------------------------------------+
  19. * | Pin assignment |
  20. * +---------------------------------------+-----------+-------------+
  21. * | STM32F0xx I2C Pins | STLM75 | Pin |
  22. * +---------------------------------------+-----------+-------------+
  23. * | LM75_I2C_SDA_PIN/ SDA | SDA | 1 |
  24. * | LM75_I2C_SCL_PIN/ SCL | SCL | 2 |
  25. * | LM75_I2C_SMBUSALERT_PIN/ SMBUS ALERT | OS/INT | 3 |
  26. * | . | GND | 4 (0V) |
  27. * | . | GND | 5 (0V) |
  28. * | . | GND | 6 (0V) |
  29. * | . | GND | 7 (0V) |
  30. * | . | VDD | 8 (3.3V)|
  31. * +---------------------------------------+-----------+-------------+
  32. ******************************************************************************
  33. * @attention
  34. *
  35. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  36. *
  37. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  38. * You may not use this file except in compliance with the License.
  39. * You may obtain a copy of the License at:
  40. *
  41. * http://www.st.com/software_license_agreement_liberty_v2
  42. *
  43. * Unless required by applicable law or agreed to in writing, software
  44. * distributed under the License is distributed on an "AS IS" BASIS,
  45. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  46. * See the License for the specific language governing permissions and
  47. * limitations under the License.
  48. *
  49. ******************************************************************************
  50. */
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "stm320518_eval_i2c_tsensor.h"
  53. /** @addtogroup Utilities
  54. * @{
  55. */
  56. /** @addtogroup STM32_EVAL
  57. * @{
  58. */
  59. /** @addtogroup STM320518_EVAL
  60. * @{
  61. */
  62. /** @addtogroup STM320518_EVAL_I2C_TSENSOR
  63. * @brief This file includes the LM75 Temperature Sensor driver of
  64. * STM320518-EVAL boards.
  65. * @{
  66. */
  67. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Types
  68. * @{
  69. */
  70. /**
  71. * @}
  72. */
  73. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Defines
  74. * @{
  75. */
  76. #define LM75_SD_SET 0x01 /*!< Set SD bit in the configuration register */
  77. #define LM75_SD_RESET 0xFE /*!< Reset SD bit in the configuration register */
  78. /**
  79. * @}
  80. */
  81. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Macros
  82. * @{
  83. */
  84. /**
  85. * @}
  86. */
  87. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Variables
  88. * @{
  89. */
  90. __IO uint32_t LM75_Timeout = LM75_LONG_TIMEOUT;
  91. /**
  92. * @}
  93. */
  94. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Function_Prototypes
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /** @defgroup STM320518_EVAL_I2C_TSENSOR_Private_Functions
  101. * @{
  102. */
  103. /**
  104. * @brief DeInitializes the LM75_I2C.
  105. * @param None
  106. * @retval None
  107. */
  108. void LM75_DeInit(void)
  109. {
  110. LM75_LowLevel_DeInit();
  111. }
  112. /**
  113. * @brief Initializes the LM75_I2C.
  114. * @param None
  115. * @retval None
  116. */
  117. void LM75_Init(void)
  118. {
  119. I2C_InitTypeDef I2C_InitStructure;
  120. LM75_LowLevel_Init();
  121. /* LM75_I2C configuration */
  122. I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;
  123. I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  124. I2C_InitStructure.I2C_DigitalFilter = 0x00;
  125. I2C_InitStructure.I2C_OwnAddress1 = 0x00;
  126. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  127. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  128. I2C_InitStructure.I2C_Timing = LM75_I2C_TIMING;
  129. /* Apply LM75_I2C configuration after enabling it */
  130. I2C_Init(LM75_I2C, &I2C_InitStructure);
  131. /* LM75_I2C Peripheral Enable */
  132. I2C_Cmd(LM75_I2C, ENABLE);
  133. }
  134. /**
  135. * @brief Checks the LM75 status.
  136. * @param None
  137. * @retval ErrorStatus: LM75 Status (ERROR or SUCCESS).
  138. */
  139. ErrorStatus LM75_GetStatus(void)
  140. {
  141. uint32_t I2C_TimeOut = I2C_TIMEOUT;
  142. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  143. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 0, I2C_AutoEnd_Mode, I2C_No_StartStop);
  144. /* Clear NACKF and STOPF */
  145. I2C_ClearFlag(LM75_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF);
  146. /* Generate start */
  147. I2C_GenerateSTART(LM75_I2C, ENABLE);
  148. /* Wait until timeout elapsed */
  149. while ((I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET) && (I2C_TimeOut-- != 0));
  150. /* Check if Temp sensor is ready for use */
  151. if ((I2C_GetFlagStatus(LM75_I2C, I2C_ISR_NACKF) != RESET) || (I2C_TimeOut == 0))
  152. {
  153. /* Clear NACKF and STOPF */
  154. I2C_ClearFlag(LM75_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF);
  155. return ERROR;
  156. }
  157. else
  158. {
  159. /* Clear STOPF */
  160. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  161. return SUCCESS;
  162. }
  163. }
  164. /**
  165. * @brief Read the specified register from the LM75.
  166. * @param RegName: specifies the LM75 register to be read.
  167. * This parameter can be one of the following values:
  168. * @arg LM75_REG_TEMP: temperature register
  169. * @arg LM75_REG_TOS: Over-limit temperature register
  170. * @arg LM75_REG_THYS: Hysteresis temperature register
  171. * @retval LM75 register value.
  172. */
  173. uint16_t LM75_ReadReg(uint8_t RegName)
  174. {
  175. uint8_t LM75_BufferRX[2] ={0,0};
  176. uint16_t tmp = 0;
  177. uint32_t DataNum = 0;
  178. /* Test on BUSY Flag */
  179. LM75_Timeout = LM75_LONG_TIMEOUT;
  180. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  181. {
  182. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  183. }
  184. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  185. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  186. /* Wait until TXIS flag is set */
  187. LM75_Timeout = LM75_LONG_TIMEOUT;
  188. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  189. {
  190. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  191. }
  192. /* Send Register address */
  193. I2C_SendData(LM75_I2C, (uint8_t)RegName);
  194. /* Wait until TC flag is set */
  195. LM75_Timeout = LM75_LONG_TIMEOUT;
  196. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET)
  197. {
  198. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  199. }
  200. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  201. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 2, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  202. /* Reset local variable */
  203. DataNum = 0;
  204. /* Wait until all data are received */
  205. while (DataNum != 2)
  206. {
  207. /* Wait until RXNE flag is set */
  208. LM75_Timeout = LM75_LONG_TIMEOUT;
  209. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET)
  210. {
  211. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  212. }
  213. /* Read data from RXDR */
  214. LM75_BufferRX[DataNum]= I2C_ReceiveData(LM75_I2C);
  215. /* Update number of received data */
  216. DataNum++;
  217. }
  218. /* Wait until STOPF flag is set */
  219. LM75_Timeout = LM75_LONG_TIMEOUT;
  220. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  221. {
  222. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  223. }
  224. /* Clear STOPF flag */
  225. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  226. /*!< Store LM75_I2C received data */
  227. tmp = (uint16_t)(LM75_BufferRX[0] << 8);
  228. tmp |= LM75_BufferRX[1];
  229. /* return a Reg value */
  230. return (uint16_t)tmp;
  231. }
  232. /**
  233. * @brief Write to the specified register of the LM75.
  234. * @param RegName: specifies the LM75 register to be written.
  235. * This parameter can be one of the following values:
  236. * @arg LM75_REG_TOS: Over-limit temperature register
  237. * @arg LM75_REG_THYS: Hysteresis temperature register
  238. * @param RegValue: value to be written to LM75 register.
  239. * @retval None
  240. */
  241. uint8_t LM75_WriteReg(uint8_t RegName, uint16_t RegValue)
  242. {
  243. uint32_t DataNum = 0;
  244. uint8_t LM75_BufferTX[2] ={0,0};
  245. LM75_BufferTX[0] = (uint8_t)(RegValue >> 8);
  246. LM75_BufferTX[1] = (uint8_t)(RegValue);
  247. /* Test on BUSY Flag */
  248. LM75_Timeout = LM75_LONG_TIMEOUT;
  249. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  250. {
  251. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  252. }
  253. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  254. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
  255. /* Wait until TXIS flag is set */
  256. LM75_Timeout = LM75_LONG_TIMEOUT;
  257. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  258. {
  259. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  260. }
  261. /* Send Register address */
  262. I2C_SendData(LM75_I2C, (uint8_t)RegName);
  263. /* Wait until TCR flag is set */
  264. LM75_Timeout = LM75_LONG_TIMEOUT;
  265. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TCR) == RESET)
  266. {
  267. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  268. }
  269. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  270. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 2, I2C_AutoEnd_Mode, I2C_No_StartStop);
  271. while (DataNum != 2)
  272. {
  273. /* Wait until TXIS flag is set */
  274. LM75_Timeout = LM75_LONG_TIMEOUT;
  275. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  276. {
  277. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  278. }
  279. /* Write data to TXDR */
  280. I2C_SendData(LM75_I2C, (uint8_t)(LM75_BufferTX[DataNum]));
  281. /* Update number of transmitted data */
  282. DataNum++;
  283. }
  284. /* Wait until STOPF flag is set */
  285. LM75_Timeout = LM75_LONG_TIMEOUT;
  286. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  287. {
  288. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  289. }
  290. /* Clear STOPF flag */
  291. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  292. return LM75_OK;
  293. }
  294. /**
  295. * @brief Read Temperature register of LM75: double temperature value.
  296. * @param None
  297. * @retval LM75 measured temperature value.
  298. */
  299. uint16_t LM75_ReadTemp(void)
  300. {
  301. uint8_t LM75_BufferRX[2] ={0,0};
  302. uint16_t tmp = 0;
  303. uint32_t DataNum = 0;
  304. /* Test on BUSY Flag */
  305. LM75_Timeout = LM75_LONG_TIMEOUT;
  306. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  307. {
  308. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  309. }
  310. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  311. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  312. /* Wait until TXIS flag is set */
  313. LM75_Timeout = LM75_LONG_TIMEOUT;
  314. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  315. {
  316. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  317. }
  318. /* Send Register address */
  319. I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_TEMP);
  320. /* Wait until TC flag is set */
  321. LM75_Timeout = LM75_LONG_TIMEOUT;
  322. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET)
  323. {
  324. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  325. }
  326. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  327. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 2, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  328. /* Reset local variable */
  329. DataNum = 0;
  330. /* Wait until all data are received */
  331. while (DataNum != 2)
  332. {
  333. /* Wait until RXNE flag is set */
  334. LM75_Timeout = LM75_LONG_TIMEOUT;
  335. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET)
  336. {
  337. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  338. }
  339. /* Read data from RXDR */
  340. LM75_BufferRX[DataNum]= I2C_ReceiveData(LM75_I2C);
  341. /* Update number of received data */
  342. DataNum++;
  343. }
  344. /* Wait until STOPF flag is set */
  345. LM75_Timeout = LM75_LONG_TIMEOUT;
  346. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  347. {
  348. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  349. }
  350. /* Clear STOPF flag */
  351. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  352. /*!< Store LM75_I2C received data */
  353. tmp = (uint16_t)(LM75_BufferRX[0] << 8);
  354. tmp |= LM75_BufferRX[1];
  355. /*!< Return Temperature value */
  356. return (uint16_t)(tmp >> 7);
  357. }
  358. /**
  359. * @brief Read the configuration register from the LM75.
  360. * @param None
  361. * @retval LM75 configuration register value.
  362. */
  363. uint8_t LM75_ReadConfReg(void)
  364. {
  365. uint8_t LM75_BufferRX[2] ={0,0};
  366. /* Test on BUSY Flag */
  367. LM75_Timeout = LM75_LONG_TIMEOUT;
  368. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  369. {
  370. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  371. }
  372. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  373. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  374. /* Wait until TXIS flag is set */
  375. LM75_Timeout = LM75_LONG_TIMEOUT;
  376. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  377. {
  378. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  379. }
  380. /* Send Register address */
  381. I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
  382. /* Wait until TC flag is set */
  383. LM75_Timeout = LM75_LONG_TIMEOUT;
  384. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET)
  385. {
  386. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  387. }
  388. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  389. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  390. /* Wait until RXNE flag is set */
  391. LM75_Timeout = LM75_LONG_TIMEOUT;
  392. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET)
  393. {
  394. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  395. }
  396. /* Read data from RXDR */
  397. LM75_BufferRX[0]= I2C_ReceiveData(LM75_I2C);
  398. /* Wait until STOPF flag is set */
  399. LM75_Timeout = LM75_LONG_TIMEOUT;
  400. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  401. {
  402. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  403. }
  404. /* Clear STOPF flag */
  405. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  406. /*!< Return Register value */
  407. return (uint8_t)LM75_BufferRX[0];
  408. }
  409. /**
  410. * @brief Write to the configuration register of the LM75.
  411. * @param RegValue: specifies the value to be written to LM75 configuration
  412. * register.
  413. * @retval None
  414. */
  415. uint8_t LM75_WriteConfReg(uint8_t RegValue)
  416. {
  417. uint8_t LM75_BufferTX = 0;
  418. LM75_BufferTX = (uint8_t)(RegValue);
  419. /* Test on BUSY Flag */
  420. LM75_Timeout = LM75_LONG_TIMEOUT;
  421. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  422. {
  423. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  424. }
  425. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  426. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
  427. /* Wait until TXIS flag is set */
  428. LM75_Timeout = LM75_LONG_TIMEOUT;
  429. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  430. {
  431. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  432. }
  433. /* Send Register address */
  434. I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
  435. /* Wait until TCR flag is set */
  436. LM75_Timeout = LM75_LONG_TIMEOUT;
  437. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TCR) == RESET)
  438. {
  439. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  440. }
  441. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  442. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);
  443. /* Wait until TXIS flag is set */
  444. LM75_Timeout = LM75_LONG_TIMEOUT;
  445. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  446. {
  447. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  448. }
  449. /* Write data to TXDR */
  450. I2C_SendData(LM75_I2C, (uint8_t)LM75_BufferTX);
  451. /* Wait until STOPF flag is set */
  452. LM75_Timeout = LM75_LONG_TIMEOUT;
  453. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  454. {
  455. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  456. }
  457. /* Clear STOPF flag */
  458. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  459. return LM75_OK;
  460. }
  461. /**
  462. * @brief Enables or disables the LM75.
  463. * @param NewState: specifies the LM75 new status. This parameter can be ENABLE
  464. * or DISABLE.
  465. * @retval None
  466. */
  467. uint8_t LM75_ShutDown(FunctionalState NewState)
  468. {
  469. uint8_t LM75_BufferRX[2] ={0,0};
  470. uint8_t LM75_BufferTX = 0;
  471. __IO uint8_t RegValue = 0;
  472. /* Test on BUSY Flag */
  473. LM75_Timeout = LM75_LONG_TIMEOUT;
  474. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  475. {
  476. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  477. }
  478. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  479. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  480. /* Wait until TXIS flag is set */
  481. LM75_Timeout = LM75_LONG_TIMEOUT;
  482. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  483. {
  484. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  485. }
  486. /* Send Register address */
  487. I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
  488. /* Wait until TC flag is set */
  489. LM75_Timeout = LM75_LONG_TIMEOUT;
  490. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TC) == RESET)
  491. {
  492. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  493. }
  494. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  495. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  496. /* Wait until RXNE flag is set */
  497. LM75_Timeout = LM75_LONG_TIMEOUT;
  498. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_RXNE) == RESET)
  499. {
  500. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  501. }
  502. /* Read data from RXDR */
  503. LM75_BufferRX[0]= I2C_ReceiveData(LM75_I2C);
  504. /* Wait until STOPF flag is set */
  505. LM75_Timeout = LM75_LONG_TIMEOUT;
  506. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  507. {
  508. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  509. }
  510. /* Clear STOPF flag */
  511. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  512. /*!< Get received data */
  513. RegValue = (uint8_t)LM75_BufferRX[0];
  514. /*---------------------------- Transmission Phase ---------------------------*/
  515. /*!< Enable or disable SD bit */
  516. if (NewState != DISABLE)
  517. {
  518. /*!< Enable LM75 */
  519. LM75_BufferTX = RegValue & LM75_SD_RESET;
  520. }
  521. else
  522. {
  523. /*!< Disable LM75 */
  524. LM75_BufferTX = RegValue | LM75_SD_SET;
  525. }
  526. /* Test on BUSY Flag */
  527. LM75_Timeout = LM75_LONG_TIMEOUT;
  528. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_BUSY) != RESET)
  529. {
  530. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  531. }
  532. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  533. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
  534. /* Wait until TXIS flag is set */
  535. LM75_Timeout = LM75_LONG_TIMEOUT;
  536. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  537. {
  538. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  539. }
  540. /* Send Register address */
  541. I2C_SendData(LM75_I2C, (uint8_t)LM75_REG_CONF);
  542. /* Wait until TCR flag is set */
  543. LM75_Timeout = LM75_LONG_TIMEOUT;
  544. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TCR) == RESET)
  545. {
  546. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  547. }
  548. /* Configure slave address, nbytes, reload, end mode and start or stop generation */
  549. I2C_TransferHandling(LM75_I2C, LM75_ADDR, 1, I2C_AutoEnd_Mode, I2C_No_StartStop);
  550. /* Wait until TXIS flag is set */
  551. LM75_Timeout = LM75_LONG_TIMEOUT;
  552. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_TXIS) == RESET)
  553. {
  554. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  555. }
  556. /* Write data to TXDR */
  557. I2C_SendData(LM75_I2C, (uint8_t)LM75_BufferTX);
  558. /* Wait until STOPF flag is set */
  559. LM75_Timeout = LM75_LONG_TIMEOUT;
  560. while(I2C_GetFlagStatus(LM75_I2C, I2C_ISR_STOPF) == RESET)
  561. {
  562. if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback();
  563. }
  564. /* Clear STOPF flag */
  565. I2C_ClearFlag(LM75_I2C, I2C_ICR_STOPCF);
  566. return LM75_OK;
  567. }
  568. /**
  569. * @}
  570. */
  571. /**
  572. * @}
  573. */
  574. /**
  575. * @}
  576. */
  577. /**
  578. * @}
  579. */
  580. /**
  581. * @}
  582. */
  583. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/