Projet

Dependents:   DISCO-F746NG_Scope_copy

Fork of BSP_DISCO_F746NG by ST

Committer:
bcostm
Date:
Mon Jan 04 15:19:15 2016 +0000
Revision:
1:ee089790cdbb
Add missing files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bcostm 1:ee089790cdbb 1 /**
bcostm 1:ee089790cdbb 2 ******************************************************************************
bcostm 1:ee089790cdbb 3 * @file stm32746g_discovery_eeprom.c
bcostm 1:ee089790cdbb 4 * @author MCD Application Team
bcostm 1:ee089790cdbb 5 * @version V1.0.0
bcostm 1:ee089790cdbb 6 * @date 25-June-2015
bcostm 1:ee089790cdbb 7 * @brief This file provides a set of functions needed to manage an I2C M24LR64
bcostm 1:ee089790cdbb 8 * EEPROM memory.
bcostm 1:ee089790cdbb 9 @verbatim
bcostm 1:ee089790cdbb 10 To be able to use this driver, the switch EE_M24LR64 must be defined
bcostm 1:ee089790cdbb 11 in your toolchain compiler preprocessor
bcostm 1:ee089790cdbb 12
bcostm 1:ee089790cdbb 13 ===================================================================
bcostm 1:ee089790cdbb 14 Notes:
bcostm 1:ee089790cdbb 15 - The I2C EEPROM memory (M24LR64) is available on separate daughter
bcostm 1:ee089790cdbb 16 board ANT7-M24LR-A, which is not provided with the STM32746G_DISCOVERY
bcostm 1:ee089790cdbb 17 board.
bcostm 1:ee089790cdbb 18 To use this driver you have to connect the ANT7-M24LR-A to CN3
bcostm 1:ee089790cdbb 19 connector of STM32746G_DISCOVERY board.
bcostm 1:ee089790cdbb 20 ===================================================================
bcostm 1:ee089790cdbb 21
bcostm 1:ee089790cdbb 22 It implements a high level communication layer for read and write
bcostm 1:ee089790cdbb 23 from/to this memory. The needed STM32F7xx hardware resources (I2C and
bcostm 1:ee089790cdbb 24 GPIO) are defined in stm32746g_discovery.h file, and the initialization is
bcostm 1:ee089790cdbb 25 performed in EEPROM_IO_Init() function declared in stm32746g_discovery.c
bcostm 1:ee089790cdbb 26 file.
bcostm 1:ee089790cdbb 27 You can easily tailor this driver to any other development board,
bcostm 1:ee089790cdbb 28 by just adapting the defines for hardware resources and
bcostm 1:ee089790cdbb 29 EEPROM_IO_Init() function.
bcostm 1:ee089790cdbb 30
bcostm 1:ee089790cdbb 31 @note In this driver, basic read and write functions (BSP_EEPROM_ReadBuffer()
bcostm 1:ee089790cdbb 32 and BSP_EEPROM_WritePage()) use DMA mode to perform the data
bcostm 1:ee089790cdbb 33 transfer to/from EEPROM memory.
bcostm 1:ee089790cdbb 34
bcostm 1:ee089790cdbb 35 @note Regarding BSP_EEPROM_WritePage(), it is an optimized function to perform
bcostm 1:ee089790cdbb 36 small write (less than 1 page) BUT the number of bytes (combined to write start address) must not
bcostm 1:ee089790cdbb 37 cross the EEPROM page boundary. This function can only writes into
bcostm 1:ee089790cdbb 38 the boundaries of an EEPROM page.
bcostm 1:ee089790cdbb 39 This function doesn't check on boundaries condition (in this driver
bcostm 1:ee089790cdbb 40 the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is
bcostm 1:ee089790cdbb 41 responsible of checking on Page boundaries).
bcostm 1:ee089790cdbb 42
bcostm 1:ee089790cdbb 43
bcostm 1:ee089790cdbb 44 +-----------------------------------------------------------------+
bcostm 1:ee089790cdbb 45 | Pin assignment for M24LR64 EEPROM |
bcostm 1:ee089790cdbb 46 +---------------------------------------+-----------+-------------+
bcostm 1:ee089790cdbb 47 | STM32F7xx I2C Pins | EEPROM | Pin |
bcostm 1:ee089790cdbb 48 +---------------------------------------+-----------+-------------+
bcostm 1:ee089790cdbb 49 | . | E0(GND) | 1 (0V) |
bcostm 1:ee089790cdbb 50 | . | AC0 | 2 |
bcostm 1:ee089790cdbb 51 | . | AC1 | 3 |
bcostm 1:ee089790cdbb 52 | . | VSS | 4 (0V) |
bcostm 1:ee089790cdbb 53 | SDA | SDA | 5 |
bcostm 1:ee089790cdbb 54 | SCL | SCL | 6 |
bcostm 1:ee089790cdbb 55 | . | E1(GND) | 7 (0V) |
bcostm 1:ee089790cdbb 56 | . | VDD | 8 (3.3V) |
bcostm 1:ee089790cdbb 57 +---------------------------------------+-----------+-------------+
bcostm 1:ee089790cdbb 58 @endverbatim
bcostm 1:ee089790cdbb 59 ******************************************************************************
bcostm 1:ee089790cdbb 60 * @attention
bcostm 1:ee089790cdbb 61 *
bcostm 1:ee089790cdbb 62 * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
bcostm 1:ee089790cdbb 63 *
bcostm 1:ee089790cdbb 64 * Redistribution and use in source and binary forms, with or without modification,
bcostm 1:ee089790cdbb 65 * are permitted provided that the following conditions are met:
bcostm 1:ee089790cdbb 66 * 1. Redistributions of source code must retain the above copyright notice,
bcostm 1:ee089790cdbb 67 * this list of conditions and the following disclaimer.
bcostm 1:ee089790cdbb 68 * 2. Redistributions in binary form must reproduce the above copyright notice,
bcostm 1:ee089790cdbb 69 * this list of conditions and the following disclaimer in the documentation
bcostm 1:ee089790cdbb 70 * and/or other materials provided with the distribution.
bcostm 1:ee089790cdbb 71 * 3. Neither the name of STMicroelectronics nor the names of its contributors
bcostm 1:ee089790cdbb 72 * may be used to endorse or promote products derived from this software
bcostm 1:ee089790cdbb 73 * without specific prior written permission.
bcostm 1:ee089790cdbb 74 *
bcostm 1:ee089790cdbb 75 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
bcostm 1:ee089790cdbb 76 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
bcostm 1:ee089790cdbb 77 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
bcostm 1:ee089790cdbb 78 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
bcostm 1:ee089790cdbb 79 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
bcostm 1:ee089790cdbb 80 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
bcostm 1:ee089790cdbb 81 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
bcostm 1:ee089790cdbb 82 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
bcostm 1:ee089790cdbb 83 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
bcostm 1:ee089790cdbb 84 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bcostm 1:ee089790cdbb 85 *
bcostm 1:ee089790cdbb 86 ******************************************************************************
bcostm 1:ee089790cdbb 87 */
bcostm 1:ee089790cdbb 88 /* Includes ------------------------------------------------------------------*/
bcostm 1:ee089790cdbb 89 #include "stm32746g_discovery_eeprom.h"
bcostm 1:ee089790cdbb 90
bcostm 1:ee089790cdbb 91 /** @addtogroup BSP
bcostm 1:ee089790cdbb 92 * @{
bcostm 1:ee089790cdbb 93 */
bcostm 1:ee089790cdbb 94
bcostm 1:ee089790cdbb 95 /** @addtogroup STM32746G_DISCOVERY
bcostm 1:ee089790cdbb 96 * @{
bcostm 1:ee089790cdbb 97 */
bcostm 1:ee089790cdbb 98
bcostm 1:ee089790cdbb 99 /** @addtogroup STM32746G_DISCOVERY_EEPROM
bcostm 1:ee089790cdbb 100 * @brief This file includes the I2C EEPROM driver of STM32746G-Discovery board.
bcostm 1:ee089790cdbb 101 * @{
bcostm 1:ee089790cdbb 102 */
bcostm 1:ee089790cdbb 103
bcostm 1:ee089790cdbb 104 /** @defgroup STM32746G_DISCOVERY_EEPROM_Private_Types STM32746G_DISCOVERY_EEPROM Private Types
bcostm 1:ee089790cdbb 105 * @{
bcostm 1:ee089790cdbb 106 */
bcostm 1:ee089790cdbb 107 /**
bcostm 1:ee089790cdbb 108 * @}
bcostm 1:ee089790cdbb 109 */
bcostm 1:ee089790cdbb 110
bcostm 1:ee089790cdbb 111 /** @defgroup STM32746G_DISCOVERY_EEPROM_Private_Defines STM32746G_DISCOVERY_EEPROM Private Defines
bcostm 1:ee089790cdbb 112 * @{
bcostm 1:ee089790cdbb 113 */
bcostm 1:ee089790cdbb 114 /**
bcostm 1:ee089790cdbb 115 * @}
bcostm 1:ee089790cdbb 116 */
bcostm 1:ee089790cdbb 117
bcostm 1:ee089790cdbb 118 /** @defgroup STM32746G_DISCOVERY_EEPROM_Private_Macros STM32746G_DISCOVERY_EEPROM Private Macros
bcostm 1:ee089790cdbb 119 * @{
bcostm 1:ee089790cdbb 120 */
bcostm 1:ee089790cdbb 121 /**
bcostm 1:ee089790cdbb 122 * @}
bcostm 1:ee089790cdbb 123 */
bcostm 1:ee089790cdbb 124
bcostm 1:ee089790cdbb 125 /** @defgroup STM32746G_DISCOVERY_EEPROM_Private_Variables STM32746G_DISCOVERY_EEPROM Private Variables
bcostm 1:ee089790cdbb 126 * @{
bcostm 1:ee089790cdbb 127 */
bcostm 1:ee089790cdbb 128 __IO uint16_t EEPROMAddress = 0;
bcostm 1:ee089790cdbb 129 __IO uint16_t EEPROMDataRead;
bcostm 1:ee089790cdbb 130 __IO uint8_t EEPROMDataWrite;
bcostm 1:ee089790cdbb 131 /**
bcostm 1:ee089790cdbb 132 * @}
bcostm 1:ee089790cdbb 133 */
bcostm 1:ee089790cdbb 134
bcostm 1:ee089790cdbb 135 /** @defgroup STM32746G_DISCOVERY_EEPROM_Private_Function_Prototypes STM32746G_DISCOVERY_EEPROM Private Function Prototypes
bcostm 1:ee089790cdbb 136 * @{
bcostm 1:ee089790cdbb 137 */
bcostm 1:ee089790cdbb 138 /**
bcostm 1:ee089790cdbb 139 * @}
bcostm 1:ee089790cdbb 140 */
bcostm 1:ee089790cdbb 141
bcostm 1:ee089790cdbb 142 /** @defgroup STM32746G_DISCOVERY_EEPROM_Exported_Functions STM32746G_DISCOVERY_EEPROM Exported Functions
bcostm 1:ee089790cdbb 143 * @{
bcostm 1:ee089790cdbb 144 */
bcostm 1:ee089790cdbb 145
bcostm 1:ee089790cdbb 146 /**
bcostm 1:ee089790cdbb 147 * @brief Initializes peripherals used by the I2C EEPROM driver.
bcostm 1:ee089790cdbb 148 *
bcostm 1:ee089790cdbb 149 * @note There are 2 different versions of M24LR64 (A01 & A02).
bcostm 1:ee089790cdbb 150 * Then try to connect on 1st one (EEPROM_I2C_ADDRESS_A01)
bcostm 1:ee089790cdbb 151 * and if problem, check the 2nd one (EEPROM_I2C_ADDRESS_A02)
bcostm 1:ee089790cdbb 152 * @retval EEPROM_OK (0) if operation is correctly performed, else return value
bcostm 1:ee089790cdbb 153 * different from EEPROM_OK (0)
bcostm 1:ee089790cdbb 154 */
bcostm 1:ee089790cdbb 155 uint32_t BSP_EEPROM_Init(void)
bcostm 1:ee089790cdbb 156 {
bcostm 1:ee089790cdbb 157 /* I2C Initialization */
bcostm 1:ee089790cdbb 158 EEPROM_IO_Init();
bcostm 1:ee089790cdbb 159
bcostm 1:ee089790cdbb 160 /* Select the EEPROM address for A01 and check if OK */
bcostm 1:ee089790cdbb 161 EEPROMAddress = EEPROM_I2C_ADDRESS_A01;
bcostm 1:ee089790cdbb 162 if(EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
bcostm 1:ee089790cdbb 163 {
bcostm 1:ee089790cdbb 164 /* Select the EEPROM address for A02 and check if OK */
bcostm 1:ee089790cdbb 165 EEPROMAddress = EEPROM_I2C_ADDRESS_A02;
bcostm 1:ee089790cdbb 166 if(EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
bcostm 1:ee089790cdbb 167 {
bcostm 1:ee089790cdbb 168 return EEPROM_FAIL;
bcostm 1:ee089790cdbb 169 }
bcostm 1:ee089790cdbb 170 }
bcostm 1:ee089790cdbb 171 return EEPROM_OK;
bcostm 1:ee089790cdbb 172 }
bcostm 1:ee089790cdbb 173
bcostm 1:ee089790cdbb 174 /**
bcostm 1:ee089790cdbb 175 * @brief DeInitializes the EEPROM.
bcostm 1:ee089790cdbb 176 * @retval EEPROM state
bcostm 1:ee089790cdbb 177 */
bcostm 1:ee089790cdbb 178 uint8_t BSP_EEPROM_DeInit(void)
bcostm 1:ee089790cdbb 179 {
bcostm 1:ee089790cdbb 180 /* I2C won't be disabled because common to other functionalities */
bcostm 1:ee089790cdbb 181 return EEPROM_OK;
bcostm 1:ee089790cdbb 182 }
bcostm 1:ee089790cdbb 183
bcostm 1:ee089790cdbb 184 /**
bcostm 1:ee089790cdbb 185 * @brief Reads a block of data from the EEPROM.
bcostm 1:ee089790cdbb 186 * @param pBuffer: pointer to the buffer that receives the data read from
bcostm 1:ee089790cdbb 187 * the EEPROM.
bcostm 1:ee089790cdbb 188 * @param ReadAddr: EEPROM's internal address to start reading from.
bcostm 1:ee089790cdbb 189 * @param NumByteToRead: pointer to the variable holding number of bytes to
bcostm 1:ee089790cdbb 190 * be read from the EEPROM.
bcostm 1:ee089790cdbb 191 *
bcostm 1:ee089790cdbb 192 * @note The variable pointed by NumByteToRead is reset to 0 when all the
bcostm 1:ee089790cdbb 193 * data are read from the EEPROM. Application should monitor this
bcostm 1:ee089790cdbb 194 * variable in order know when the transfer is complete.
bcostm 1:ee089790cdbb 195 *
bcostm 1:ee089790cdbb 196 * @retval EEPROM_OK (0) if operation is correctly performed, else return value
bcostm 1:ee089790cdbb 197 * different from EEPROM_OK (0) or the timeout user callback.
bcostm 1:ee089790cdbb 198 */
bcostm 1:ee089790cdbb 199 uint32_t BSP_EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
bcostm 1:ee089790cdbb 200 {
bcostm 1:ee089790cdbb 201 uint32_t buffersize = *NumByteToRead;
bcostm 1:ee089790cdbb 202
bcostm 1:ee089790cdbb 203 /* Set the pointer to the Number of data to be read. This pointer will be used
bcostm 1:ee089790cdbb 204 by the DMA Transfer Completer interrupt Handler in order to reset the
bcostm 1:ee089790cdbb 205 variable to 0. User should check on this variable in order to know if the
bcostm 1:ee089790cdbb 206 DMA transfer has been complete or not. */
bcostm 1:ee089790cdbb 207 EEPROMDataRead = *NumByteToRead;
bcostm 1:ee089790cdbb 208
bcostm 1:ee089790cdbb 209 if(EEPROM_IO_ReadData(EEPROMAddress, ReadAddr, pBuffer, buffersize) != HAL_OK)
bcostm 1:ee089790cdbb 210 {
bcostm 1:ee089790cdbb 211 BSP_EEPROM_TIMEOUT_UserCallback();
bcostm 1:ee089790cdbb 212 return EEPROM_FAIL;
bcostm 1:ee089790cdbb 213 }
bcostm 1:ee089790cdbb 214
bcostm 1:ee089790cdbb 215 /* If all operations OK, return EEPROM_OK (0) */
bcostm 1:ee089790cdbb 216 return EEPROM_OK;
bcostm 1:ee089790cdbb 217 }
bcostm 1:ee089790cdbb 218
bcostm 1:ee089790cdbb 219 /**
bcostm 1:ee089790cdbb 220 * @brief Writes more than one byte to the EEPROM with a single WRITE cycle.
bcostm 1:ee089790cdbb 221 *
bcostm 1:ee089790cdbb 222 * @note The number of bytes (combined to write start address) must not
bcostm 1:ee089790cdbb 223 * cross the EEPROM page boundary. This function can only write into
bcostm 1:ee089790cdbb 224 * the boundaries of an EEPROM page.
bcostm 1:ee089790cdbb 225 * This function doesn't check on boundaries condition (in this driver
bcostm 1:ee089790cdbb 226 * the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is
bcostm 1:ee089790cdbb 227 * responsible of checking on Page boundaries).
bcostm 1:ee089790cdbb 228 *
bcostm 1:ee089790cdbb 229 * @param pBuffer: pointer to the buffer containing the data to be written to
bcostm 1:ee089790cdbb 230 * the EEPROM.
bcostm 1:ee089790cdbb 231 * @param WriteAddr: EEPROM's internal address to write to.
bcostm 1:ee089790cdbb 232 * @param NumByteToWrite: pointer to the variable holding number of bytes to
bcostm 1:ee089790cdbb 233 * be written into the EEPROM.
bcostm 1:ee089790cdbb 234 *
bcostm 1:ee089790cdbb 235 * @note The variable pointed by NumByteToWrite is reset to 0 when all the
bcostm 1:ee089790cdbb 236 * data are written to the EEPROM. Application should monitor this
bcostm 1:ee089790cdbb 237 * variable in order know when the transfer is complete.
bcostm 1:ee089790cdbb 238 *
bcostm 1:ee089790cdbb 239 * @note This function just configure the communication and enable the DMA
bcostm 1:ee089790cdbb 240 * channel to transfer data. Meanwhile, the user application may perform
bcostm 1:ee089790cdbb 241 * other tasks in parallel.
bcostm 1:ee089790cdbb 242 *
bcostm 1:ee089790cdbb 243 * @retval EEPROM_OK (0) if operation is correctly performed, else return value
bcostm 1:ee089790cdbb 244 * different from EEPROM_OK (0) or the timeout user callback.
bcostm 1:ee089790cdbb 245 */
bcostm 1:ee089790cdbb 246 uint32_t BSP_EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
bcostm 1:ee089790cdbb 247 {
bcostm 1:ee089790cdbb 248 uint32_t buffersize = *NumByteToWrite;
bcostm 1:ee089790cdbb 249 uint32_t status = EEPROM_OK;
bcostm 1:ee089790cdbb 250
bcostm 1:ee089790cdbb 251 /* Set the pointer to the Number of data to be written. This pointer will be used
bcostm 1:ee089790cdbb 252 by the DMA Transfer Completer interrupt Handler in order to reset the
bcostm 1:ee089790cdbb 253 variable to 0. User should check on this variable in order to know if the
bcostm 1:ee089790cdbb 254 DMA transfer has been complete or not. */
bcostm 1:ee089790cdbb 255 EEPROMDataWrite = *NumByteToWrite;
bcostm 1:ee089790cdbb 256
bcostm 1:ee089790cdbb 257 if(EEPROM_IO_WriteData(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK)
bcostm 1:ee089790cdbb 258 {
bcostm 1:ee089790cdbb 259 BSP_EEPROM_TIMEOUT_UserCallback();
bcostm 1:ee089790cdbb 260 status = EEPROM_FAIL;
bcostm 1:ee089790cdbb 261 }
bcostm 1:ee089790cdbb 262
bcostm 1:ee089790cdbb 263 if(BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK)
bcostm 1:ee089790cdbb 264 {
bcostm 1:ee089790cdbb 265 return EEPROM_FAIL;
bcostm 1:ee089790cdbb 266 }
bcostm 1:ee089790cdbb 267
bcostm 1:ee089790cdbb 268 /* If all operations OK, return EEPROM_OK (0) */
bcostm 1:ee089790cdbb 269 return status;
bcostm 1:ee089790cdbb 270 }
bcostm 1:ee089790cdbb 271
bcostm 1:ee089790cdbb 272 /**
bcostm 1:ee089790cdbb 273 * @brief Writes buffer of data to the I2C EEPROM.
bcostm 1:ee089790cdbb 274 * @param pBuffer: pointer to the buffer containing the data to be written
bcostm 1:ee089790cdbb 275 * to the EEPROM.
bcostm 1:ee089790cdbb 276 * @param WriteAddr: EEPROM's internal address to write to.
bcostm 1:ee089790cdbb 277 * @param NumByteToWrite: number of bytes to write to the EEPROM.
bcostm 1:ee089790cdbb 278 * @retval EEPROM_OK (0) if operation is correctly performed, else return value
bcostm 1:ee089790cdbb 279 * different from EEPROM_OK (0) or the timeout user callback.
bcostm 1:ee089790cdbb 280 */
bcostm 1:ee089790cdbb 281 uint32_t BSP_EEPROM_WriteBuffer(uint8_t *pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
bcostm 1:ee089790cdbb 282 {
bcostm 1:ee089790cdbb 283 uint16_t numofpage = 0, numofsingle = 0, count = 0;
bcostm 1:ee089790cdbb 284 uint16_t addr = 0;
bcostm 1:ee089790cdbb 285 uint8_t dataindex = 0;
bcostm 1:ee089790cdbb 286 uint32_t status = EEPROM_OK;
bcostm 1:ee089790cdbb 287
bcostm 1:ee089790cdbb 288 addr = WriteAddr % EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 289 count = EEPROM_PAGESIZE - addr;
bcostm 1:ee089790cdbb 290 numofpage = NumByteToWrite / EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 291 numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 292
bcostm 1:ee089790cdbb 293 /* If WriteAddr is EEPROM_PAGESIZE aligned */
bcostm 1:ee089790cdbb 294 if(addr == 0)
bcostm 1:ee089790cdbb 295 {
bcostm 1:ee089790cdbb 296 /* If NumByteToWrite < EEPROM_PAGESIZE */
bcostm 1:ee089790cdbb 297 if(numofpage == 0)
bcostm 1:ee089790cdbb 298 {
bcostm 1:ee089790cdbb 299 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 300 dataindex = numofsingle;
bcostm 1:ee089790cdbb 301 /* Start writing data */
bcostm 1:ee089790cdbb 302 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 303 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 304 {
bcostm 1:ee089790cdbb 305 return status;
bcostm 1:ee089790cdbb 306 }
bcostm 1:ee089790cdbb 307 }
bcostm 1:ee089790cdbb 308 /* If NumByteToWrite > EEPROM_PAGESIZE */
bcostm 1:ee089790cdbb 309 else
bcostm 1:ee089790cdbb 310 {
bcostm 1:ee089790cdbb 311 while(numofpage--)
bcostm 1:ee089790cdbb 312 {
bcostm 1:ee089790cdbb 313 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 314 dataindex = EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 315 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 316 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 317 {
bcostm 1:ee089790cdbb 318 return status;
bcostm 1:ee089790cdbb 319 }
bcostm 1:ee089790cdbb 320
bcostm 1:ee089790cdbb 321 WriteAddr += EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 322 pBuffer += EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 323 }
bcostm 1:ee089790cdbb 324
bcostm 1:ee089790cdbb 325 if(numofsingle!=0)
bcostm 1:ee089790cdbb 326 {
bcostm 1:ee089790cdbb 327 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 328 dataindex = numofsingle;
bcostm 1:ee089790cdbb 329 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 330 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 331 {
bcostm 1:ee089790cdbb 332 return status;
bcostm 1:ee089790cdbb 333 }
bcostm 1:ee089790cdbb 334 }
bcostm 1:ee089790cdbb 335 }
bcostm 1:ee089790cdbb 336 }
bcostm 1:ee089790cdbb 337 /* If WriteAddr is not EEPROM_PAGESIZE aligned */
bcostm 1:ee089790cdbb 338 else
bcostm 1:ee089790cdbb 339 {
bcostm 1:ee089790cdbb 340 /* If NumByteToWrite < EEPROM_PAGESIZE */
bcostm 1:ee089790cdbb 341 if(numofpage== 0)
bcostm 1:ee089790cdbb 342 {
bcostm 1:ee089790cdbb 343 /* If the number of data to be written is more than the remaining space
bcostm 1:ee089790cdbb 344 in the current page: */
bcostm 1:ee089790cdbb 345 if(NumByteToWrite > count)
bcostm 1:ee089790cdbb 346 {
bcostm 1:ee089790cdbb 347 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 348 dataindex = count;
bcostm 1:ee089790cdbb 349 /* Write the data contained in same page */
bcostm 1:ee089790cdbb 350 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 351 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 352 {
bcostm 1:ee089790cdbb 353 return status;
bcostm 1:ee089790cdbb 354 }
bcostm 1:ee089790cdbb 355
bcostm 1:ee089790cdbb 356 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 357 dataindex = (NumByteToWrite - count);
bcostm 1:ee089790cdbb 358 /* Write the remaining data in the following page */
bcostm 1:ee089790cdbb 359 status = BSP_EEPROM_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 360 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 361 {
bcostm 1:ee089790cdbb 362 return status;
bcostm 1:ee089790cdbb 363 }
bcostm 1:ee089790cdbb 364 }
bcostm 1:ee089790cdbb 365 else
bcostm 1:ee089790cdbb 366 {
bcostm 1:ee089790cdbb 367 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 368 dataindex = numofsingle;
bcostm 1:ee089790cdbb 369 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 370 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 371 {
bcostm 1:ee089790cdbb 372 return status;
bcostm 1:ee089790cdbb 373 }
bcostm 1:ee089790cdbb 374 }
bcostm 1:ee089790cdbb 375 }
bcostm 1:ee089790cdbb 376 /* If NumByteToWrite > EEPROM_PAGESIZE */
bcostm 1:ee089790cdbb 377 else
bcostm 1:ee089790cdbb 378 {
bcostm 1:ee089790cdbb 379 NumByteToWrite -= count;
bcostm 1:ee089790cdbb 380 numofpage = NumByteToWrite / EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 381 numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 382
bcostm 1:ee089790cdbb 383 if(count != 0)
bcostm 1:ee089790cdbb 384 {
bcostm 1:ee089790cdbb 385 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 386 dataindex = count;
bcostm 1:ee089790cdbb 387 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 388 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 389 {
bcostm 1:ee089790cdbb 390 return status;
bcostm 1:ee089790cdbb 391 }
bcostm 1:ee089790cdbb 392 WriteAddr += count;
bcostm 1:ee089790cdbb 393 pBuffer += count;
bcostm 1:ee089790cdbb 394 }
bcostm 1:ee089790cdbb 395
bcostm 1:ee089790cdbb 396 while(numofpage--)
bcostm 1:ee089790cdbb 397 {
bcostm 1:ee089790cdbb 398 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 399 dataindex = EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 400 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 401 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 402 {
bcostm 1:ee089790cdbb 403 return status;
bcostm 1:ee089790cdbb 404 }
bcostm 1:ee089790cdbb 405 WriteAddr += EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 406 pBuffer += EEPROM_PAGESIZE;
bcostm 1:ee089790cdbb 407 }
bcostm 1:ee089790cdbb 408 if(numofsingle != 0)
bcostm 1:ee089790cdbb 409 {
bcostm 1:ee089790cdbb 410 /* Store the number of data to be written */
bcostm 1:ee089790cdbb 411 dataindex = numofsingle;
bcostm 1:ee089790cdbb 412 status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
bcostm 1:ee089790cdbb 413 if(status != EEPROM_OK)
bcostm 1:ee089790cdbb 414 {
bcostm 1:ee089790cdbb 415 return status;
bcostm 1:ee089790cdbb 416 }
bcostm 1:ee089790cdbb 417 }
bcostm 1:ee089790cdbb 418 }
bcostm 1:ee089790cdbb 419 }
bcostm 1:ee089790cdbb 420
bcostm 1:ee089790cdbb 421 /* If all operations OK, return EEPROM_OK (0) */
bcostm 1:ee089790cdbb 422 return EEPROM_OK;
bcostm 1:ee089790cdbb 423 }
bcostm 1:ee089790cdbb 424
bcostm 1:ee089790cdbb 425 /**
bcostm 1:ee089790cdbb 426 * @brief Wait for EEPROM Standby state.
bcostm 1:ee089790cdbb 427 *
bcostm 1:ee089790cdbb 428 * @note This function allows to wait and check that EEPROM has finished the
bcostm 1:ee089790cdbb 429 * last operation. It is mostly used after Write operation: after receiving
bcostm 1:ee089790cdbb 430 * the buffer to be written, the EEPROM may need additional time to actually
bcostm 1:ee089790cdbb 431 * perform the write operation. During this time, it doesn't answer to
bcostm 1:ee089790cdbb 432 * I2C packets addressed to it. Once the write operation is complete
bcostm 1:ee089790cdbb 433 * the EEPROM responds to its address.
bcostm 1:ee089790cdbb 434 *
bcostm 1:ee089790cdbb 435 * @retval EEPROM_OK (0) if operation is correctly performed, else return value
bcostm 1:ee089790cdbb 436 * different from EEPROM_OK (0) or the timeout user callback.
bcostm 1:ee089790cdbb 437 */
bcostm 1:ee089790cdbb 438 uint32_t BSP_EEPROM_WaitEepromStandbyState(void)
bcostm 1:ee089790cdbb 439 {
bcostm 1:ee089790cdbb 440 /* Check if the maximum allowed number of trials has bee reached */
bcostm 1:ee089790cdbb 441 if(EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
bcostm 1:ee089790cdbb 442 {
bcostm 1:ee089790cdbb 443 /* If the maximum number of trials has been reached, exit the function */
bcostm 1:ee089790cdbb 444 BSP_EEPROM_TIMEOUT_UserCallback();
bcostm 1:ee089790cdbb 445 return EEPROM_TIMEOUT;
bcostm 1:ee089790cdbb 446 }
bcostm 1:ee089790cdbb 447 return EEPROM_OK;
bcostm 1:ee089790cdbb 448 }
bcostm 1:ee089790cdbb 449
bcostm 1:ee089790cdbb 450 /**
bcostm 1:ee089790cdbb 451 * @brief Basic management of the timeout situation.
bcostm 1:ee089790cdbb 452 * @retval None
bcostm 1:ee089790cdbb 453 */
bcostm 1:ee089790cdbb 454 __weak void BSP_EEPROM_TIMEOUT_UserCallback(void)
bcostm 1:ee089790cdbb 455 {
bcostm 1:ee089790cdbb 456 }
bcostm 1:ee089790cdbb 457
bcostm 1:ee089790cdbb 458 /**
bcostm 1:ee089790cdbb 459 * @}
bcostm 1:ee089790cdbb 460 */
bcostm 1:ee089790cdbb 461
bcostm 1:ee089790cdbb 462 /**
bcostm 1:ee089790cdbb 463 * @}
bcostm 1:ee089790cdbb 464 */
bcostm 1:ee089790cdbb 465
bcostm 1:ee089790cdbb 466 /**
bcostm 1:ee089790cdbb 467 * @}
bcostm 1:ee089790cdbb 468 */
bcostm 1:ee089790cdbb 469
bcostm 1:ee089790cdbb 470 /**
bcostm 1:ee089790cdbb 471 * @}
bcostm 1:ee089790cdbb 472 */
bcostm 1:ee089790cdbb 473
bcostm 1:ee089790cdbb 474 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/