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