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