STM32Cube BSP FW for STM32F769I-Discovery

Dependents:   mbed-os-example-blinky-5 DISCO-F769NI_TOUCHSCREEN_demo_custom_1 Datarecorder2 DISCO-F769NI_TOUCHSCREEN_demo ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32f769i_discovery_eeprom.c Source File

stm32f769i_discovery_eeprom.c

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