stm320518_eval_i2c_ee.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /**
  2. ******************************************************************************
  3. * @file stm320518_eval_i2c_ee.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 an I2C M24LR64
  8. * EEPROM memory.
  9. *
  10. * ===================================================================
  11. * Notes:
  12. * - This driver is intended for STM32F0xx families devices only.
  13. * - The I2C EEPROM memory (M24LR64) is available on RF EEPROM daughter
  14. * board (ANT7-M24LR-A) provided with the EVAL board, to use this
  15. * driver you have to connect the ANT7-M24LR-A to CN12 connector.
  16. * ===================================================================
  17. *
  18. * It implements a high level communication layer for read and write
  19. * from/to this memory. The needed STM32F0xx hardware resources (I2C and
  20. * GPIO) are defined in stm320518_eval.h file, and the initialization is
  21. * performed in sEE_LowLevel_Init() function declared in stm320518_eval.c
  22. * file.
  23. * You can easily tailor this driver to any other development board,
  24. * by just adapting the defines for hardware resources and
  25. * sEE_LowLevel_Init() function.
  26. *
  27. * @note In this driver, basic read and write functions (sEE_ReadBuffer()
  28. * and sEE_WritePage()) use Polling mode to perform the data transfer
  29. * to/from EEPROM memory.
  30. *
  31. * +-----------------------------------------------------------------+
  32. * | Pin assignment |
  33. * +---------------------------------------+-----------+-------------+
  34. * | STM32F0xx I2C Pins | sEE | Pin |
  35. * +---------------------------------------+-----------+-------------+
  36. * | . | E0(GND) | 1 (0V) |
  37. * | . | AC0 | 2 |
  38. * | . | AC1 | 3 |
  39. * | . | VSS | 4 (0V) |
  40. * | sEE_I2C_SDA_PIN/ SDA | SDA | 5 |
  41. * | sEE_I2C_SCL_PIN/ SCL | SCL | 6 |
  42. * | . | E1(GND) | 7 (0V) |
  43. * | . | VDD | 8 (3.3V) |
  44. * +---------------------------------------+-----------+-------------+
  45. ******************************************************************************
  46. * @attention
  47. *
  48. * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  49. *
  50. * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  51. * You may not use this file except in compliance with the License.
  52. * You may obtain a copy of the License at:
  53. *
  54. * http://www.st.com/software_license_agreement_liberty_v2
  55. *
  56. * Unless required by applicable law or agreed to in writing, software
  57. * distributed under the License is distributed on an "AS IS" BASIS,
  58. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  59. * See the License for the specific language governing permissions and
  60. * limitations under the License.
  61. *
  62. ******************************************************************************
  63. */
  64. /* Includes ------------------------------------------------------------------*/
  65. #include "stm320518_eval_i2c_ee.h"
  66. /** @addtogroup Utilities
  67. * @{
  68. */
  69. /** @addtogroup STM32_EVAL
  70. * @{
  71. */
  72. /** @addtogroup STM320518_EVAL
  73. * @{
  74. */
  75. /** @addtogroup STM320518_EVAL_I2C_EE
  76. * @brief This file includes the I2C EEPROM driver of STM320518-EVAL board.
  77. * @{
  78. */
  79. /** @defgroup STM320518_EVAL_I2C_EE_Private_Types
  80. * @{
  81. */
  82. /**
  83. * @}
  84. */
  85. /** @defgroup STM320518_EVAL_I2C_EE_Private_Defines
  86. * @{
  87. */
  88. /**
  89. * @}
  90. */
  91. /** @defgroup STM320518_EVAL_I2C_EE_Private_Macros
  92. * @{
  93. */
  94. /**
  95. * @}
  96. */
  97. /** @defgroup STM320518_EVAL_I2C_EE_Private_Variables
  98. * @{
  99. */
  100. __IO uint16_t sEEAddress = 0;
  101. __IO uint32_t sEETimeout = sEE_LONG_TIMEOUT;
  102. __IO uint16_t sEEDataNum;
  103. /**
  104. * @}
  105. */
  106. /** @defgroup STM320518_EVAL_I2C_EE_Private_Function_Prototypes
  107. * @{
  108. */
  109. /**
  110. * @}
  111. */
  112. /** @defgroup STM320518_EVAL_I2C_EE_Private_Functions
  113. * @{
  114. */
  115. /**
  116. * @brief DeInitializes peripherals used by the I2C EEPROM driver.
  117. * @param None
  118. * @retval None
  119. */
  120. void sEE_DeInit(void)
  121. {
  122. sEE_LowLevel_DeInit();
  123. }
  124. /**
  125. * @brief Initializes peripherals used by the I2C EEPROM driver.
  126. * @param None
  127. * @retval None
  128. */
  129. void sEE_Init(void)
  130. {
  131. I2C_InitTypeDef I2C_InitStructure;
  132. sEE_LowLevel_Init();
  133. /*!< I2C configuration */
  134. /* sEE_I2C configuration */
  135. I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
  136. I2C_InitStructure.I2C_AnalogFilter = I2C_AnalogFilter_Enable;
  137. I2C_InitStructure.I2C_DigitalFilter = 0x00;
  138. I2C_InitStructure.I2C_OwnAddress1 = 0x00;
  139. I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
  140. I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
  141. I2C_InitStructure.I2C_Timing = sEE_I2C_TIMING;
  142. /* Apply sEE_I2C configuration after enabling it */
  143. I2C_Init(sEE_I2C, &I2C_InitStructure);
  144. /* sEE_I2C Peripheral Enable */
  145. I2C_Cmd(sEE_I2C, ENABLE);
  146. /*!< Select the EEPROM address */
  147. sEEAddress = sEE_HW_ADDRESS;
  148. }
  149. /**
  150. * @brief Reads a block of data from the EEPROM.
  151. * @param pBuffer: pointer to the buffer that receives the data read from
  152. * the EEPROM.
  153. * @param ReadAddr: EEPROM's internal address to start reading from.
  154. * @param NumByteToRead: pointer to the variable holding number of bytes to
  155. * be read from the EEPROM.
  156. *
  157. * @retval sEE_OK (0) if operation is correctly performed, else return value
  158. * different from sEE_OK (0) or the timeout user callback.
  159. */
  160. uint32_t sEE_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
  161. {
  162. uint32_t NumbOfSingle = 0, Count = 0, DataNum = 0, StartCom = 0;
  163. /* Get number of reload cycles */
  164. Count = (*NumByteToRead) / 255;
  165. NumbOfSingle = (*NumByteToRead) % 255;
  166. #ifdef sEE_M24C08
  167. /* Configure slave address, nbytes, reload and generate start */
  168. I2C_TransferHandling(sEE_I2C, sEEAddress, 1, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  169. /* Wait until TXIS flag is set */
  170. sEETimeout = sEE_LONG_TIMEOUT;
  171. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  172. {
  173. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  174. }
  175. /* Send memory address */
  176. I2C_SendData(sEE_I2C, (uint8_t)ReadAddr);
  177. #elif defined(sEE_M24M01) || defined(sEE_M24C64_32) || defined (sEE_M24LR64)
  178. /* Configure slave address, nbytes, reload and generate start */
  179. I2C_TransferHandling(sEE_I2C, sEEAddress, 2, I2C_SoftEnd_Mode, I2C_Generate_Start_Write);
  180. /* Wait until TXIS flag is set */
  181. sEETimeout = sEE_LONG_TIMEOUT;
  182. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  183. {
  184. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  185. }
  186. /* Send MSB of memory address */
  187. I2C_SendData(sEE_I2C, (uint8_t)((ReadAddr & 0xFF00) >> 8));
  188. /* Wait until TXIS flag is set */
  189. sEETimeout = sEE_LONG_TIMEOUT;
  190. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  191. {
  192. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  193. }
  194. /* Send LSB of memory address */
  195. I2C_SendData(sEE_I2C, (uint8_t)(ReadAddr & 0x00FF));
  196. #endif /*!< sEE_M24C08 */
  197. /* Wait until TC flag is set */
  198. sEETimeout = sEE_LONG_TIMEOUT;
  199. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TC) == RESET)
  200. {
  201. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  202. }
  203. /* If number of Reload cycles is not equal to 0 */
  204. if (Count != 0)
  205. {
  206. /* Starting communication */
  207. StartCom = 1;
  208. /* Wait until all reload cycles are performed */
  209. while( Count != 0)
  210. {
  211. /* If a read transfer is performed */
  212. if (StartCom == 0)
  213. {
  214. /* Wait until TCR flag is set */
  215. sEETimeout = sEE_LONG_TIMEOUT;
  216. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TCR) == RESET)
  217. {
  218. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  219. }
  220. }
  221. /* if remains one read cycle */
  222. if ((Count == 1) && (NumbOfSingle == 0))
  223. {
  224. /* if starting communication */
  225. if (StartCom != 0)
  226. {
  227. /* Configure slave address, end mode and start condition */
  228. I2C_TransferHandling(sEE_I2C, sEEAddress, 255, I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  229. }
  230. else
  231. {
  232. /* Configure slave address, end mode */
  233. I2C_TransferHandling(sEE_I2C, sEEAddress, 255, I2C_AutoEnd_Mode, I2C_No_StartStop);
  234. }
  235. }
  236. else
  237. {
  238. /* if starting communication */
  239. if (StartCom != 0)
  240. {
  241. /* Configure slave address, end mode and start condition */
  242. I2C_TransferHandling(sEE_I2C, sEEAddress, 255, I2C_Reload_Mode, I2C_Generate_Start_Read);
  243. }
  244. else
  245. {
  246. /* Configure slave address, end mode */
  247. I2C_TransferHandling(sEE_I2C, sEEAddress, 255, I2C_Reload_Mode, I2C_No_StartStop);
  248. }
  249. }
  250. /* Update local variable */
  251. StartCom = 0;
  252. DataNum = 0;
  253. /* Wait until all data are received */
  254. while (DataNum != 255)
  255. {
  256. /* Wait until RXNE flag is set */
  257. sEETimeout = sEE_LONG_TIMEOUT;
  258. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_RXNE) == RESET)
  259. {
  260. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  261. }
  262. /* Read data from RXDR */
  263. pBuffer[DataNum]= I2C_ReceiveData(sEE_I2C);
  264. /* Update number of received data */
  265. DataNum++;
  266. (*NumByteToRead)--;
  267. }
  268. /* Update Pointer of received buffer */
  269. pBuffer += DataNum;
  270. /* update number of reload cycle */
  271. Count--;
  272. }
  273. /* If number of single data is not equal to 0 */
  274. if (NumbOfSingle != 0)
  275. {
  276. /* Wait until TCR flag is set */
  277. sEETimeout = sEE_LONG_TIMEOUT;
  278. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TCR) == RESET)
  279. {
  280. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  281. }
  282. /* Update CR2 : set Nbytes and end mode */
  283. I2C_TransferHandling(sEE_I2C, sEEAddress, (uint8_t)(NumbOfSingle), I2C_AutoEnd_Mode, I2C_No_StartStop);
  284. /* Reset local variable */
  285. DataNum = 0;
  286. /* Wait until all data are received */
  287. while (DataNum != NumbOfSingle)
  288. {
  289. /* Wait until RXNE flag is set */
  290. sEETimeout = sEE_LONG_TIMEOUT;
  291. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_RXNE) == RESET)
  292. {
  293. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  294. }
  295. /* Read data from RXDR */
  296. pBuffer[DataNum]= I2C_ReceiveData(sEE_I2C);
  297. /* Update number of received data */
  298. DataNum++;
  299. (*NumByteToRead)--;
  300. }
  301. }
  302. }
  303. else
  304. {
  305. /* Update CR2 : set Slave Address , set read request, generate Start and set end mode */
  306. I2C_TransferHandling(sEE_I2C, sEEAddress, (uint32_t)(NumbOfSingle), I2C_AutoEnd_Mode, I2C_Generate_Start_Read);
  307. /* Reset local variable */
  308. DataNum = 0;
  309. /* Wait until all data are received */
  310. while (DataNum != NumbOfSingle)
  311. {
  312. /* Wait until RXNE flag is set */
  313. sEETimeout = sEE_LONG_TIMEOUT;
  314. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_RXNE) == RESET)
  315. {
  316. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  317. }
  318. /* Read data from RXDR */
  319. pBuffer[DataNum]= I2C_ReceiveData(sEE_I2C);
  320. /* Update number of received data */
  321. DataNum++;
  322. (*NumByteToRead)--;
  323. }
  324. }
  325. /* Wait until STOPF flag is set */
  326. sEETimeout = sEE_LONG_TIMEOUT;
  327. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_STOPF) == RESET)
  328. {
  329. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  330. }
  331. /* Clear STOPF flag */
  332. I2C_ClearFlag(sEE_I2C, I2C_ICR_STOPCF);
  333. /* If all operations OK, return sEE_OK (0) */
  334. return sEE_OK;
  335. }
  336. /**
  337. * @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
  338. *
  339. * @note The number of bytes (combined to write start address) must not
  340. * cross the EEPROM page boundary. This function can only write into
  341. * the boundaries of an EEPROM page.
  342. * @note This function doesn't check on boundaries condition (in this driver
  343. * the function sEE_WriteBuffer() which calls sEE_WritePage() is
  344. * responsible of checking on Page boundaries).
  345. *
  346. * @param pBuffer: pointer to the buffer containing the data to be written to
  347. * the EEPROM.
  348. * @param WriteAddr: EEPROM's internal address to write to.
  349. * @param NumByteToWrite: pointer to the variable holding number of bytes to
  350. * be written into the EEPROM.
  351. *
  352. * @retval sEE_OK (0) if operation is correctly performed, else return value
  353. * different from sEE_OK (0) or the timeout user callback.
  354. */
  355. uint32_t sEE_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
  356. {
  357. uint32_t DataNum = 0;
  358. #ifdef sEE_M24C08
  359. /* Configure slave address, nbytes, reload and generate start */
  360. I2C_TransferHandling(sEE_I2C, sEEAddress, 1, I2C_Reload_Mode, I2C_Generate_Start_Write);
  361. /* Wait until TXIS flag is set */
  362. sEETimeout = sEE_LONG_TIMEOUT;
  363. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  364. {
  365. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  366. }
  367. /* Send memory address */
  368. I2C_SendData(sEE_I2C, (uint8_t)WriteAddr);
  369. #elif defined(sEE_M24M01) || defined(sEE_M24C64_32) || defined (sEE_M24LR64)
  370. /* Configure slave address, nbytes, reload and generate start */
  371. I2C_TransferHandling(sEE_I2C, sEEAddress, 2, I2C_Reload_Mode, I2C_Generate_Start_Write);
  372. /* Wait until TXIS flag is set */
  373. sEETimeout = sEE_LONG_TIMEOUT;
  374. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  375. {
  376. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  377. }
  378. /* Send MSB of memory address */
  379. I2C_SendData(sEE_I2C, (uint8_t)((WriteAddr & 0xFF00) >> 8));
  380. /* Wait until TXIS flag is set */
  381. sEETimeout = sEE_LONG_TIMEOUT;
  382. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  383. {
  384. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  385. }
  386. /* Send LSB of memory address */
  387. I2C_SendData(sEE_I2C, (uint8_t)(WriteAddr & 0x00FF));
  388. #endif /*!< sEE_M24C08 */
  389. /* Wait until TCR flag is set */
  390. sEETimeout = sEE_LONG_TIMEOUT;
  391. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TCR) == RESET)
  392. {
  393. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  394. }
  395. /* Update CR2 : set Slave Address , set write request, generate Start and set end mode */
  396. I2C_TransferHandling(sEE_I2C, sEEAddress, (uint8_t)(*NumByteToWrite), I2C_AutoEnd_Mode, I2C_No_StartStop);
  397. while (DataNum != (*NumByteToWrite))
  398. {
  399. /* Wait until TXIS flag is set */
  400. sEETimeout = sEE_LONG_TIMEOUT;
  401. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_TXIS) == RESET)
  402. {
  403. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  404. }
  405. /* Write data to TXDR */
  406. I2C_SendData(sEE_I2C, (uint8_t)(pBuffer[DataNum]));
  407. /* Update number of transmitted data */
  408. DataNum++;
  409. }
  410. /* Wait until STOPF flag is set */
  411. sEETimeout = sEE_LONG_TIMEOUT;
  412. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_STOPF) == RESET)
  413. {
  414. if((sEETimeout--) == 0) return sEE_TIMEOUT_UserCallback();
  415. }
  416. /* Clear STOPF flag */
  417. I2C_ClearFlag(sEE_I2C, I2C_ICR_STOPCF);
  418. /* If all operations OK, return sEE_OK (0) */
  419. return sEE_OK;
  420. }
  421. /**
  422. * @brief Writes buffer of data to the I2C EEPROM.
  423. * @param pBuffer: pointer to the buffer containing the data to be written
  424. * to the EEPROM.
  425. * @param WriteAddr: EEPROM's internal address to write to.
  426. * @param NumByteToWrite: number of bytes to write to the EEPROM.
  427. * @retval None
  428. */
  429. void sEE_WriteBuffer(uint8_t* pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
  430. {
  431. uint16_t NumOfPage = 0, NumOfSingle = 0, count = 0;
  432. uint16_t Addr = 0;
  433. Addr = WriteAddr % sEE_PAGESIZE;
  434. count = sEE_PAGESIZE - Addr;
  435. NumOfPage = NumByteToWrite / sEE_PAGESIZE;
  436. NumOfSingle = NumByteToWrite % sEE_PAGESIZE;
  437. /*!< If WriteAddr is sEE_PAGESIZE aligned */
  438. if(Addr == 0)
  439. {
  440. /*!< If NumByteToWrite < sEE_PAGESIZE */
  441. if(NumOfPage == 0)
  442. {
  443. /* Store the number of data to be written */
  444. sEEDataNum = NumOfSingle;
  445. /* Start writing data */
  446. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  447. sEE_WaitEepromStandbyState();
  448. }
  449. /*!< If NumByteToWrite > sEE_PAGESIZE */
  450. else
  451. {
  452. while(NumOfPage--)
  453. {
  454. /* Store the number of data to be written */
  455. sEEDataNum = sEE_PAGESIZE;
  456. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  457. sEE_WaitEepromStandbyState();
  458. WriteAddr += sEE_PAGESIZE;
  459. pBuffer += sEE_PAGESIZE;
  460. }
  461. if(NumOfSingle!=0)
  462. {
  463. /* Store the number of data to be written */
  464. sEEDataNum = NumOfSingle;
  465. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  466. sEE_WaitEepromStandbyState();
  467. }
  468. }
  469. }
  470. /*!< If WriteAddr is not sEE_PAGESIZE aligned */
  471. else
  472. {
  473. /*!< If NumByteToWrite < sEE_PAGESIZE */
  474. if(NumOfPage== 0)
  475. {
  476. /*!< If the number of data to be written is more than the remaining space
  477. in the current page: */
  478. if (NumByteToWrite > count)
  479. {
  480. /* Store the number of data to be written */
  481. sEEDataNum = count;
  482. /*!< Write the data conained in same page */
  483. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  484. sEE_WaitEepromStandbyState();
  485. /* Store the number of data to be written */
  486. sEEDataNum = (NumByteToWrite - count);
  487. /*!< Write the remaining data in the following page */
  488. sEE_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&sEEDataNum));
  489. sEE_WaitEepromStandbyState();
  490. }
  491. else
  492. {
  493. /* Store the number of data to be written */
  494. sEEDataNum = NumOfSingle;
  495. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  496. sEE_WaitEepromStandbyState();
  497. }
  498. }
  499. /*!< If NumByteToWrite > sEE_PAGESIZE */
  500. else
  501. {
  502. NumByteToWrite -= count;
  503. NumOfPage = NumByteToWrite / sEE_PAGESIZE;
  504. NumOfSingle = NumByteToWrite % sEE_PAGESIZE;
  505. if(count != 0)
  506. {
  507. /* Store the number of data to be written */
  508. sEEDataNum = count;
  509. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  510. sEE_WaitEepromStandbyState();
  511. WriteAddr += count;
  512. pBuffer += count;
  513. }
  514. while(NumOfPage--)
  515. {
  516. /* Store the number of data to be written */
  517. sEEDataNum = sEE_PAGESIZE;
  518. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  519. sEETimeout = sEE_LONG_TIMEOUT;
  520. sEE_WaitEepromStandbyState();
  521. WriteAddr += sEE_PAGESIZE;
  522. pBuffer += sEE_PAGESIZE;
  523. }
  524. if(NumOfSingle != 0)
  525. {
  526. /* Store the number of data to be written */
  527. sEEDataNum = NumOfSingle;
  528. sEE_WritePage(pBuffer, WriteAddr, (uint8_t*)(&sEEDataNum));
  529. sEE_WaitEepromStandbyState();
  530. }
  531. }
  532. }
  533. }
  534. /**
  535. * @brief Wait for EEPROM Standby state.
  536. *
  537. * @note This function allows to wait and check that EEPROM has finished the
  538. * last operation. It is mostly used after Write operation: after receiving
  539. * the buffer to be written, the EEPROM may need additional time to actually
  540. * perform the write operation. During this time, it doesn't answer to
  541. * I2C packets addressed to it. Once the write operation is complete
  542. * the EEPROM responds to its address.
  543. *
  544. * @param None
  545. *
  546. * @retval sEE_OK (0) if operation is correctly performed, else return value
  547. * different from sEE_OK (0) or the timeout user callback.
  548. */
  549. uint32_t sEE_WaitEepromStandbyState(void)
  550. {
  551. __IO uint32_t sEETrials = 0;
  552. /* Keep looping till the slave acknowledge his address or maximum number
  553. of trials is reached (this number is defined by sEE_MAX_TRIALS_NUMBER define
  554. in stm32373c_eval_i2c_ee.h file) */
  555. /* Configure CR2 register : set Slave Address and end mode */
  556. I2C_TransferHandling(sEE_I2C, sEEAddress, 0, I2C_AutoEnd_Mode, I2C_No_StartStop);
  557. do
  558. {
  559. /* Initialize sEETimeout */
  560. sEETimeout = sEE_FLAG_TIMEOUT;
  561. /* Clear NACKF */
  562. I2C_ClearFlag(sEE_I2C, I2C_ICR_NACKCF | I2C_ICR_STOPCF);
  563. /* Generate start */
  564. I2C_GenerateSTART(sEE_I2C, ENABLE);
  565. /* Wait until timeout elapsed */
  566. while (sEETimeout-- != 0);
  567. /* Check if the maximum allowed numbe of trials has bee reached */
  568. if (sEETrials++ == sEE_MAX_TRIALS_NUMBER)
  569. {
  570. /* If the maximum number of trials has been reached, exit the function */
  571. return sEE_TIMEOUT_UserCallback();
  572. }
  573. }
  574. while(I2C_GetFlagStatus(sEE_I2C, I2C_ISR_NACKF) != RESET);
  575. /* Clear STOPF */
  576. I2C_ClearFlag(sEE_I2C, I2C_ICR_STOPCF);
  577. /* Return sEE_OK if device is ready */
  578. return sEE_OK;
  579. }
  580. #ifdef USE_DEFAULT_TIMEOUT_CALLBACK
  581. /**
  582. * @brief Basic management of the timeout situation.
  583. * @param None.
  584. * @retval None.
  585. */
  586. uint32_t sEE_TIMEOUT_UserCallback(void)
  587. {
  588. /* Block communication and all processes */
  589. while (1)
  590. {
  591. }
  592. }
  593. #endif /* USE_DEFAULT_TIMEOUT_CALLBACK */
  594. /**
  595. * @}
  596. */
  597. /**
  598. * @}
  599. */
  600. /**
  601. * @}
  602. */
  603. /**
  604. * @}
  605. */
  606. /**
  607. * @}
  608. */
  609. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/