STM32L4R9I_EVAL BSP User Manual: stm32l4r9i_eval_eeprom.c Source File

STM32L4R9I_EVAL BSP

stm32l4r9i_eval_eeprom.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32l4r9i_eval_eeprom.c
00004   * @author  MCD Application Team
00005   * @brief   This file provides a set of functions needed to manage an I2C M24LR64
00006   *          EEPROM memory.
00007   @verbatim
00008 
00009             ===================================================================
00010             Notes:
00011              - The I2C EEPROM memory (M24LR64) is available on separate daughter
00012                board ANT7-M24LR-A, which is not provided with the STM32L4R9I_EVAL
00013                board.
00014                To use this driver you have to connect the ANT7-M24LR-A to CN5
00015                connector of STM32L4R9I_EVAL board.
00016             ===================================================================
00017 
00018             It implements a high level communication layer for read and write
00019             from/to this memory. The needed STM32L4xx hardware resources (I2C and
00020             GPIO) are defined in stm32l4r9i_eval.h file, and the initialization is
00021             performed in EEPROM_IO_Init() function declared in stm32l4r9i_eval.c
00022             file.
00023             You can easily tailor this driver to any other development board,
00024             by just adapting the defines for hardware resources and
00025             EEPROM_IO_Init() function.
00026 
00027             @note In this driver, basic read and write functions (BSP_EEPROM_ReadBuffer()
00028                   and BSP_EEPROM_WritePage()) use DMA mode to perform the data
00029                   transfer to/from EEPROM memory.
00030 
00031            @note   Regarding BSP_EEPROM_WritePage(), it is an optimized function to perform
00032                   small write (less than 1 page) BUT the number of bytes (combined to write start address) must not
00033                   cross the EEPROM page boundary. This function can only writes into
00034                   the boundaries of an EEPROM page.
00035                   This function doesn't check on boundaries condition (in this driver
00036                   the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is
00037                   responsible of checking on Page boundaries).
00038 
00039 
00040        +-----------------------------------------------------------------+
00041        |               Pin assignment for M24LR64 EEPROM                 |
00042        +---------------------------------------+-----------+-------------+
00043        |  STM32L4xx I2C Pins                   |   EEPROM  |   Pin       |
00044        +---------------------------------------+-----------+-------------+
00045        | .                                     |   E0(GND) |    1  (0V)  |
00046        | .                                     |   AC0     |    2        |
00047        | .                                     |   AC1     |    3        |
00048        | .                                     |   VSS     |    4  (0V)  |
00049        | SDA                                   |   SDA     |    5        |
00050        | SCL                                   |   SCL     |    6        |
00051        | .                                     |   E1(GND) |    7  (0V)  |
00052        | .                                     |   VDD     |    8 (3.3V) |
00053        +---------------------------------------+-----------+-------------+
00054   @endverbatim
00055   ******************************************************************************
00056   * @attention
00057   *
00058   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00059   *
00060   * Redistribution and use in source and binary forms, with or without modification,
00061   * are permitted provided that the following conditions are met:
00062   *   1. Redistributions of source code must retain the above copyright notice,
00063   *      this list of conditions and the following disclaimer.
00064   *   2. Redistributions in binary form must reproduce the above copyright notice,
00065   *      this list of conditions and the following disclaimer in the documentation
00066   *      and/or other materials provided with the distribution.
00067   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00068   *      may be used to endorse or promote products derived from this software
00069   *      without specific prior written permission.
00070   *
00071   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00072   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00073   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00074   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00075   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00076   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00077   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00078   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00079   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00080   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00081   *
00082   ******************************************************************************
00083   */
00084 /* Includes ------------------------------------------------------------------*/
00085 #include "stm32l4r9i_eval_eeprom.h"
00086 
00087 /** @addtogroup BSP
00088   * @{
00089   */
00090 
00091 /** @addtogroup STM32L4R9I_EVAL
00092   * @{
00093   */
00094 
00095 /** @defgroup STM32L4R9I_EVAL_EEPROM STM32L4R9I_EVAL EEPROM
00096   * @brief This file includes the I2C EEPROM driver of STM32L4R9I-EVAL evaluation board.
00097   * @{
00098   */
00099 
00100 /** @defgroup STM32L4R9I_EVAL_EEPROM_Private_Variables Private Variables
00101   * @{
00102   */
00103 __IO uint16_t EEPROMAddress = 0;
00104 __IO uint16_t EEPROMDataRead;
00105 __IO uint8_t  EEPROMDataWrite;
00106 /**
00107   * @}
00108   */
00109 
00110 /** @defgroup STM32L4R9I_EVAL_EEPROM_Exported_Functions Exported Functions
00111   * @{
00112   */
00113 
00114 /**
00115   * @brief  Initializes peripherals used by the I2C EEPROM driver.
00116   * @retval EEPROM_OK (0) if operation is correctly performed, else return value
00117   *         different from EEPROM_OK (0)
00118   */
00119 uint32_t BSP_EEPROM_Init(void)
00120 {
00121   /* I2C Initialization */
00122   EEPROM_IO_Init();
00123 
00124   /* Select the EEPROM address check if OK */
00125   EEPROMAddress = EEPROM_I2C_ADDRESS;
00126   if(EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
00127   {
00128     return EEPROM_FAIL;
00129   }
00130   return EEPROM_OK;
00131 }
00132 
00133 /**
00134   * @brief  DeInitializes the EEPROM.
00135   * @retval EEPROM state
00136   */
00137 uint8_t BSP_EEPROM_DeInit(void)
00138 {
00139   /* I2C is not disabled because common to other functionalities */
00140   return EEPROM_OK;
00141 }
00142 
00143 /**
00144   * @brief  Reads a block of data from the EEPROM.
00145   * @param  pBuffer: pointer to the buffer that receives the data read from
00146   *         the EEPROM.
00147   * @param  ReadAddr: EEPROM's internal address to start reading from.
00148   * @param  NumByteToRead: pointer to the variable holding number of bytes to
00149   *         be read from the EEPROM.
00150   *
00151   *        @note The variable pointed by NumByteToRead is reset to 0 when all the
00152   *              data are read from the EEPROM. Application should monitor this
00153   *              variable in order know when the transfer is complete.
00154   *
00155   * @retval EEPROM_OK (0) if operation is correctly performed, else return value
00156   *         different from EEPROM_OK (0) or the timeout user callback.
00157   */
00158 uint32_t BSP_EEPROM_ReadBuffer(uint8_t* pBuffer, uint16_t ReadAddr, uint16_t* NumByteToRead)
00159 {
00160   uint32_t buffersize = *NumByteToRead;
00161 
00162   /* Set the pointer to the Number of data to be read. This pointer will be used
00163      by the DMA Transfer Completer interrupt Handler in order to reset the
00164      variable to 0. User should check on this variable in order to know if the
00165      DMA transfer has been complete or not. */
00166   EEPROMDataRead = *NumByteToRead;
00167 
00168   if(EEPROM_IO_ReadData(EEPROMAddress, ReadAddr, pBuffer, buffersize) != HAL_OK)
00169   {
00170     BSP_EEPROM_TIMEOUT_UserCallback();
00171     return EEPROM_FAIL;
00172   }
00173 
00174   /* If all operations OK, return EEPROM_OK (0) */
00175   return EEPROM_OK;
00176 }
00177 
00178 /**
00179   * @brief  Writes more than one byte to the EEPROM with a single WRITE cycle.
00180   *
00181   * @note   The number of bytes (combined to write start address) must not
00182   *         cross the EEPROM page boundary. This function can only write into
00183   *         the boundaries of an EEPROM page.
00184   *         This function doesn't check on boundaries condition (in this driver
00185   *         the function BSP_EEPROM_WriteBuffer() which calls BSP_EEPROM_WritePage() is
00186   *         responsible of checking on Page boundaries).
00187   *
00188   * @param  pBuffer: pointer to the buffer containing the data to be written to
00189   *         the EEPROM.
00190   * @param  WriteAddr: EEPROM's internal address to write to.
00191   * @param  NumByteToWrite: pointer to the variable holding number of bytes to
00192   *         be written into the EEPROM.
00193   *
00194   *        @note The variable pointed by NumByteToWrite is reset to 0 when all the
00195   *              data are written to the EEPROM. Application should monitor this
00196   *              variable in order know when the transfer is complete.
00197   *
00198   *        @note This function just configure the communication and enable the DMA
00199   *              channel to transfer data. Meanwhile, the user application may perform
00200   *              other tasks in parallel.
00201   *
00202   * @retval EEPROM_OK (0) if operation is correctly performed, else return value
00203   *         different from EEPROM_OK (0) or the timeout user callback.
00204   */
00205 uint32_t BSP_EEPROM_WritePage(uint8_t* pBuffer, uint16_t WriteAddr, uint8_t* NumByteToWrite)
00206 {
00207   uint32_t buffersize = *NumByteToWrite;
00208   uint32_t status = EEPROM_OK;
00209 
00210   /* Set the pointer to the Number of data to be written. This pointer will be used
00211       by the DMA Transfer Completer interrupt Handler in order to reset the
00212       variable to 0. User should check on this variable in order to know if the
00213       DMA transfer has been complete or not. */
00214   EEPROMDataWrite = *NumByteToWrite;
00215 
00216   if(EEPROM_IO_WriteData(EEPROMAddress, WriteAddr, pBuffer, buffersize) != HAL_OK)
00217   {
00218     BSP_EEPROM_TIMEOUT_UserCallback();
00219     status = EEPROM_FAIL;
00220   }
00221 
00222   if(BSP_EEPROM_WaitEepromStandbyState() != EEPROM_OK)
00223   {
00224     return EEPROM_FAIL;
00225   }
00226 
00227   /* If all operations OK, return EEPROM_OK (0) */
00228   return status;
00229 }
00230 
00231 /**
00232   * @brief  Writes buffer of data to the I2C EEPROM.
00233   * @param  pBuffer: pointer to the buffer  containing the data to be written
00234   *         to the EEPROM.
00235   * @param  WriteAddr: EEPROM's internal address to write to.
00236   * @param  NumByteToWrite: number of bytes to write to the EEPROM.
00237   * @retval EEPROM_OK (0) if operation is correctly performed, else return value
00238   *         different from EEPROM_OK (0) or the timeout user callback.
00239   */
00240 uint32_t BSP_EEPROM_WriteBuffer(uint8_t *pBuffer, uint16_t WriteAddr, uint16_t NumByteToWrite)
00241 {
00242   uint16_t numofpage = 0, numofsingle = 0, count = 0;
00243   uint16_t addr = 0;
00244   uint8_t  dataindex = 0;
00245   uint32_t status = EEPROM_OK;
00246 
00247   addr = WriteAddr % EEPROM_PAGESIZE;
00248   count = EEPROM_PAGESIZE - addr;
00249   numofpage =  NumByteToWrite / EEPROM_PAGESIZE;
00250   numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
00251 
00252   /* If WriteAddr is EEPROM_PAGESIZE aligned */
00253   if(addr == 0)
00254   {
00255     /* If NumByteToWrite < EEPROM_PAGESIZE */
00256     if(numofpage == 0)
00257     {
00258       /* Store the number of data to be written */
00259       dataindex = numofsingle;
00260       /* Start writing data */
00261       status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00262       if(status != EEPROM_OK)
00263       {
00264         return status;
00265       }
00266     }
00267     /* If NumByteToWrite > EEPROM_PAGESIZE */
00268     else
00269     {
00270       while(numofpage--)
00271       {
00272         /* Store the number of data to be written */
00273         dataindex = EEPROM_PAGESIZE;
00274         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00275         if(status != EEPROM_OK)
00276         {
00277           return status;
00278         }
00279 
00280         WriteAddr +=  EEPROM_PAGESIZE;
00281         pBuffer += EEPROM_PAGESIZE;
00282       }
00283 
00284       if(numofsingle!=0)
00285       {
00286         /* Store the number of data to be written */
00287         dataindex = numofsingle;
00288         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00289         if(status != EEPROM_OK)
00290         {
00291           return status;
00292         }
00293       }
00294     }
00295   }
00296   /* If WriteAddr is not EEPROM_PAGESIZE aligned */
00297   else
00298   {
00299     /* If NumByteToWrite < EEPROM_PAGESIZE */
00300     if(numofpage== 0)
00301     {
00302       /* If the number of data to be written is more than the remaining space
00303       in the current page: */
00304       if(NumByteToWrite > count)
00305       {
00306         /* Store the number of data to be written */
00307         dataindex = count;
00308         /* Write the data contained in same page */
00309         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00310         if(status != EEPROM_OK)
00311         {
00312           return status;
00313         }
00314 
00315         /* Store the number of data to be written */
00316         dataindex = (NumByteToWrite - count);
00317         /* Write the remaining data in the following page */
00318         status = BSP_EEPROM_WritePage((uint8_t*)(pBuffer + count), (WriteAddr + count), (uint8_t*)(&dataindex));
00319         if(status != EEPROM_OK)
00320         {
00321           return status;
00322         }
00323       }
00324       else
00325       {
00326         /* Store the number of data to be written */
00327         dataindex = numofsingle;
00328         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00329         if(status != EEPROM_OK)
00330         {
00331           return status;
00332         }
00333       }
00334     }
00335     /* If NumByteToWrite > EEPROM_PAGESIZE */
00336     else
00337     {
00338       NumByteToWrite -= count;
00339       numofpage =  NumByteToWrite / EEPROM_PAGESIZE;
00340       numofsingle = NumByteToWrite % EEPROM_PAGESIZE;
00341 
00342       if(count != 0)
00343       {
00344         /* Store the number of data to be written */
00345         dataindex = count;
00346         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00347         if(status != EEPROM_OK)
00348         {
00349           return status;
00350         }
00351         WriteAddr += count;
00352         pBuffer += count;
00353       }
00354 
00355       while(numofpage--)
00356       {
00357         /* Store the number of data to be written */
00358         dataindex = EEPROM_PAGESIZE;
00359         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00360         if(status != EEPROM_OK)
00361         {
00362           return status;
00363         }
00364         WriteAddr +=  EEPROM_PAGESIZE;
00365         pBuffer += EEPROM_PAGESIZE;
00366       }
00367       if(numofsingle != 0)
00368       {
00369         /* Store the number of data to be written */
00370         dataindex = numofsingle;
00371         status = BSP_EEPROM_WritePage(pBuffer, WriteAddr, (uint8_t*)(&dataindex));
00372         if(status != EEPROM_OK)
00373         {
00374           return status;
00375         }
00376       }
00377     }
00378   }
00379 
00380   /* If all operations OK, return EEPROM_OK (0) */
00381   return EEPROM_OK;
00382 }
00383 
00384 /**
00385   * @brief  Wait for EEPROM Standby state.
00386   *
00387   * @note  This function allows to wait and check that EEPROM has finished the
00388   *        last operation. It is mostly used after Write operation: after receiving
00389   *        the buffer to be written, the EEPROM may need additional time to actually
00390   *        perform the write operation. During this time, it doesn't answer to
00391   *        I2C packets addressed to it. Once the write operation is complete
00392   *        the EEPROM responds to its address.
00393   *
00394   * @retval EEPROM_OK (0) if operation is correctly performed, else return value
00395   *         different from EEPROM_OK (0) or the timeout user callback.
00396   */
00397 uint32_t BSP_EEPROM_WaitEepromStandbyState(void)
00398 {
00399   /* Check if the maximum allowed number of trials has bee reached */
00400   if(EEPROM_IO_IsDeviceReady(EEPROMAddress, EEPROM_MAX_TRIALS) != HAL_OK)
00401   {
00402     /* If the maximum number of trials has been reached, exit the function */
00403     BSP_EEPROM_TIMEOUT_UserCallback();
00404     return EEPROM_TIMEOUT;
00405   }
00406   return EEPROM_OK;
00407 }
00408 
00409 /**
00410   * @brief  Basic management of the timeout situation.
00411   * @retval None
00412   */
00413 __weak void BSP_EEPROM_TIMEOUT_UserCallback(void)
00414 {
00415 }
00416 
00417 /**
00418   * @}
00419   */
00420 
00421 /**
00422   * @}
00423   */
00424 
00425 /**
00426   * @}
00427   */
00428 
00429 /**
00430   * @}
00431   */
00432 
00433 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Generated on Thu Oct 12 2017 10:53:59 for STM32L4R9I_EVAL BSP User Manual by   doxygen 1.7.6.1