Library to control Silicon Labs SI570 10 MHZ TO 1.4 GHZ I2C PROGRAMMABLE XO/VCXO.

Dependencies:   mbed

Fork of SI570 by Gerrit Polder

Committer:
DL3LD
Date:
Sun Mar 27 06:55:59 2016 +0000
Revision:
1:1556bcaaf759
STM32F746NG SI570 VFO Test

Who changed what in which revision?

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