ST / BSP_DISCO_F746NG

Dependents:   DISCO-F746NG_LCDTS_CC3000_NTP DISCO-F746NG_ROPE_WIFI F746_SpectralAnalysis_NoPhoto ecte433 ... more

Committer:
jeromecoutant
Date:
Wed Nov 27 08:29:42 2019 +0000
Revision:
13:85dbcff443aa
Parent:
10:1050c589b2ad
Fix OV9655 Camera Driver

Who changed what in which revision?

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