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