STM32F469NI Discovery board drivers

Dependents:   DISCO-F469NI_LCDTS_GUI_demo Configurable_Robots DISCO-F469NI_LCD_demo DISCO-F469NI_SD_demo ... more

Committer:
Jerome Coutant
Date:
Tue May 16 10:50:26 2017 +0200
Revision:
2:123b894b49dd
Child:
3:3cdfcc4f7c9d
STM32Cube_FW_BSP_DISCO_F469NI

Who changed what in which revision?

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