Strona 1 z 1

Emulacja EEPROM w STM32F0xx

: 04 kwie 2018, 16:46
autor: SunRiver
Na prośbę kolegi Daro ,

STM udostępnia bardzo wygodne biblioteki do emulacji eeprom na pamięci Flash
Tu przedstawiam dla F0xx ...

eeprom.c
  1. /**
  2.   ******************************************************************************
  3.   * @file    STM32F0xx_EEPROM_Emulation/src/eeprom.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    29-May-2012
  7.   * @brief   This file provides all the EEPROM emulation firmware functions.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */
  27.  
  28. /** @addtogroup STM32F0xx_EEPROM_Emulation
  29.   * @{
  30.   */
  31.  
  32. /* Includes ------------------------------------------------------------------*/
  33. #include "eeprom.h"
  34.  
  35. /* Private typedef -----------------------------------------------------------*/
  36. /* Private define ------------------------------------------------------------*/
  37. /* Private macro -------------------------------------------------------------*/
  38. /* Private variables ---------------------------------------------------------*/
  39.  
  40. /* Global variable used to store variable value in read sequence */
  41. uint16_t DataVar = 0;
  42.  
  43. /* Virtual address defined by the user: 0xFFFF value is prohibited */
  44. extern uint16_t VirtAddVarTab[NB_OF_VAR];
  45.  
  46. /* Private function prototypes -----------------------------------------------*/
  47. /* Private functions ---------------------------------------------------------*/
  48. static FLASH_Status EE_Format(void);
  49. static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data);
  50. static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data);
  51. static uint16_t EE_FindValidPage(uint8_t Operation);
  52.  
  53. /**
  54.   * @brief  Restore the pages to a known good state in case of page's status
  55.   *   corruption after a power loss.
  56.   * @param  None.
  57.   * @retval - Flash error code: on write Flash error
  58.   *         - FLASH_COMPLETE: on success
  59.   */
  60. uint16_t EE_Init(void)
  61. {
  62.   uint16_t PageStatus0 = 6, PageStatus1 = 6;
  63.   uint16_t VarIdx = 0;
  64.   uint16_t EepromStatus = 0, ReadStatus = 0;
  65.   int16_t x = -1;
  66.   uint16_t  FlashStatus;
  67.  
  68.   /* Get Page0 status */
  69.   PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
  70.   /* Get Page1 status */
  71.   PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
  72.  
  73.   /* Check for invalid header states and repair if necessary */
  74.   switch (PageStatus0)
  75.   {
  76.     case ERASED:
  77.       if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */
  78.       {
  79.         /* Erase Page0 */
  80.         FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
  81.         /* If erase operation was failed, a Flash error code is returned */
  82.         if (FlashStatus != FLASH_COMPLETE)
  83.         {
  84.           return FlashStatus;
  85.         }
  86.       }
  87.       else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */
  88.       {
  89.         /* Erase Page0 */
  90.         FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
  91.         /* If erase operation was failed, a Flash error code is returned */
  92.         if (FlashStatus != FLASH_COMPLETE)
  93.         {
  94.           return FlashStatus;
  95.         }
  96.         /* Mark Page1 as valid */
  97.         FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
  98.         /* If program operation was failed, a Flash error code is returned */
  99.         if (FlashStatus != FLASH_COMPLETE)
  100.         {
  101.           return FlashStatus;
  102.         }
  103.       }
  104.       else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */
  105.       {
  106.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  107.         FlashStatus = EE_Format();
  108.         /* If erase/program operation was failed, a Flash error code is returned */
  109.         if (FlashStatus != FLASH_COMPLETE)
  110.         {
  111.           return FlashStatus;
  112.         }
  113.       }
  114.       break;
  115.  
  116.     case RECEIVE_DATA:
  117.       if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */
  118.       {
  119.         /* Transfer data from Page1 to Page0 */
  120.         for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
  121.         {
  122.           if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
  123.           {
  124.             x = VarIdx;
  125.           }
  126.           if (VarIdx != x)
  127.           {
  128.             /* Read the last variables' updates */
  129.             ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  130.             /* In case variable corresponding to the virtual address was found */
  131.             if (ReadStatus != 0x1)
  132.             {
  133.               /* Transfer the variable to the Page0 */
  134.               EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  135.               /* If program operation was failed, a Flash error code is returned */
  136.               if (EepromStatus != FLASH_COMPLETE)
  137.               {
  138.                 return EepromStatus;
  139.               }
  140.             }
  141.           }
  142.         }
  143.         /* Mark Page0 as valid */
  144.         FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
  145.         /* If program operation was failed, a Flash error code is returned */
  146.         if (FlashStatus != FLASH_COMPLETE)
  147.         {
  148.           return FlashStatus;
  149.         }
  150.         /* Erase Page1 */
  151.         FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
  152.         /* If erase operation was failed, a Flash error code is returned */
  153.         if (FlashStatus != FLASH_COMPLETE)
  154.         {
  155.           return FlashStatus;
  156.         }
  157.       }
  158.       else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */
  159.       {
  160.         /* Erase Page1 */
  161.         FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
  162.         /* If erase operation was failed, a Flash error code is returned */
  163.         if (FlashStatus != FLASH_COMPLETE)
  164.         {
  165.           return FlashStatus;
  166.         }
  167.         /* Mark Page0 as valid */
  168.         FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
  169.         /* If program operation was failed, a Flash error code is returned */
  170.         if (FlashStatus != FLASH_COMPLETE)
  171.         {
  172.           return FlashStatus;
  173.         }
  174.       }
  175.       else /* Invalid state -> format eeprom */
  176.       {
  177.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  178.         FlashStatus = EE_Format();
  179.         /* If erase/program operation was failed, a Flash error code is returned */
  180.         if (FlashStatus != FLASH_COMPLETE)
  181.         {
  182.           return FlashStatus;
  183.         }
  184.       }
  185.       break;
  186.  
  187.     case VALID_PAGE:
  188.       if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */
  189.       {
  190.         /* Erase both Page0 and Page1 and set Page0 as valid page */
  191.         FlashStatus = EE_Format();
  192.         /* If erase/program operation was failed, a Flash error code is returned */
  193.         if (FlashStatus != FLASH_COMPLETE)
  194.         {
  195.           return FlashStatus;
  196.         }
  197.       }
  198.       else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */
  199.       {
  200.         /* Erase Page1 */
  201.         FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
  202.         /* If erase operation was failed, a Flash error code is returned */
  203.         if (FlashStatus != FLASH_COMPLETE)
  204.         {
  205.           return FlashStatus;
  206.         }
  207.       }
  208.       else /* Page0 valid, Page1 receive */
  209.       {
  210.         /* Transfer data from Page0 to Page1 */
  211.         for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
  212.         {
  213.           if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx])
  214.           {
  215.             x = VarIdx;
  216.           }
  217.           if (VarIdx != x)
  218.           {
  219.             /* Read the last variables' updates */
  220.             ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  221.             /* In case variable corresponding to the virtual address was found */
  222.             if (ReadStatus != 0x1)
  223.             {
  224.               /* Transfer the variable to the Page1 */
  225.               EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  226.               /* If program operation was failed, a Flash error code is returned */
  227.               if (EepromStatus != FLASH_COMPLETE)
  228.               {
  229.                 return EepromStatus;
  230.               }
  231.             }
  232.           }
  233.         }
  234.         /* Mark Page1 as valid */
  235.         FlashStatus = FLASH_ProgramHalfWord(PAGE1_BASE_ADDRESS, VALID_PAGE);
  236.         /* If program operation was failed, a Flash error code is returned */
  237.         if (FlashStatus != FLASH_COMPLETE)
  238.         {
  239.           return FlashStatus;
  240.         }
  241.         /* Erase Page0 */
  242.         FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
  243.         /* If erase operation was failed, a Flash error code is returned */
  244.         if (FlashStatus != FLASH_COMPLETE)
  245.         {
  246.           return FlashStatus;
  247.         }
  248.       }
  249.       break;
  250.  
  251.     default:  /* Any other state -> format eeprom */
  252.       /* Erase both Page0 and Page1 and set Page0 as valid page */
  253.       FlashStatus = EE_Format();
  254.       /* If erase/program operation was failed, a Flash error code is returned */
  255.       if (FlashStatus != FLASH_COMPLETE)
  256.       {
  257.         return FlashStatus;
  258.       }
  259.       break;
  260.   }
  261.  
  262.   return FLASH_COMPLETE;
  263. }
  264.  
  265. /**
  266.   * @brief  Returns the last stored variable data, if found, which correspond to
  267.   *   the passed virtual address
  268.   * @param  VirtAddress: Variable virtual address
  269.   * @param  Data: Global variable contains the read variable value
  270.   * @retval Success or error status:
  271.   *           - 0: if variable was found
  272.   *           - 1: if the variable was not found
  273.   *           - NO_VALID_PAGE: if no valid page was found.
  274.   */
  275. uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data)
  276. {
  277.   uint16_t ValidPage = PAGE0;
  278.   uint16_t AddressValue = 0x5555, ReadStatus = 1;
  279.   uint32_t Address = 0x08010000, PageStartAddress = 0x08010000;
  280.  
  281.   /* Get active Page for read operation */
  282.   ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
  283.  
  284.   /* Check if there is no valid page */
  285.   if (ValidPage == NO_VALID_PAGE)
  286.   {
  287.     return  NO_VALID_PAGE;
  288.   }
  289.  
  290.   /* Get the valid Page start Address */
  291.   PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
  292.  
  293.   /* Get the valid Page end Address */
  294.   Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
  295.  
  296.   /* Check each active page address starting from end */
  297.   while (Address > (PageStartAddress + 2))
  298.   {
  299.     /* Get the current location content to be compared with virtual address */
  300.     AddressValue = (*(__IO uint16_t*)Address);
  301.  
  302.     /* Compare the read address with the virtual address */
  303.     if (AddressValue == VirtAddress)
  304.     {
  305.       /* Get content of Address-2 which is variable value */
  306.       *Data = (*(__IO uint16_t*)(Address - 2));
  307.  
  308.       /* In case variable value is read, reset ReadStatus flag */
  309.       ReadStatus = 0;
  310.  
  311.       break;
  312.     }
  313.     else
  314.     {
  315.       /* Next address location */
  316.       Address = Address - 4;
  317.     }
  318.   }
  319.  
  320.   /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */
  321.   return ReadStatus;
  322. }
  323.  
  324. /**
  325.   * @brief  Writes/upadtes variable data in EEPROM.
  326.   * @param  VirtAddress: Variable virtual address
  327.   * @param  Data: 16 bit data to be written
  328.   * @retval Success or error status:
  329.   *           - FLASH_COMPLETE: on success
  330.   *           - PAGE_FULL: if valid page is full
  331.   *           - NO_VALID_PAGE: if no valid page was found
  332.   *           - Flash error code: on write Flash error
  333.   */
  334. uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data)
  335. {
  336.   uint16_t Status = 0;
  337.  
  338.   /* Write the variable virtual address and value in the EEPROM */
  339.   Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  340.  
  341.   /* In case the EEPROM active page is full */
  342.   if (Status == PAGE_FULL)
  343.   {
  344.     /* Perform Page transfer */
  345.     Status = EE_PageTransfer(VirtAddress, Data);
  346.   }
  347.  
  348.   /* Return last operation status */
  349.   return Status;
  350. }
  351.  
  352. /**
  353.   * @brief  Erases PAGE0 and PAGE1 and writes VALID_PAGE header to PAGE0
  354.   * @param  None
  355.   * @retval Status of the last operation (Flash write or erase) done during
  356.   *         EEPROM formating
  357.   */
  358. static FLASH_Status EE_Format(void)
  359. {
  360.   FLASH_Status FlashStatus = FLASH_COMPLETE;
  361.  
  362.   /* Erase Page0 */
  363.   FlashStatus = FLASH_ErasePage(PAGE0_BASE_ADDRESS);
  364.  
  365.   /* If erase operation was failed, a Flash error code is returned */
  366.   if (FlashStatus != FLASH_COMPLETE)
  367.   {
  368.     return FlashStatus;
  369.   }
  370.  
  371.   /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
  372.   FlashStatus = FLASH_ProgramHalfWord(PAGE0_BASE_ADDRESS, VALID_PAGE);
  373.  
  374.   /* If program operation was failed, a Flash error code is returned */
  375.   if (FlashStatus != FLASH_COMPLETE)
  376.   {
  377.     return FlashStatus;
  378.   }
  379.  
  380.   /* Erase Page1 */
  381.   FlashStatus = FLASH_ErasePage(PAGE1_BASE_ADDRESS);
  382.  
  383.   /* Return Page1 erase operation status */
  384.   return FlashStatus;
  385. }
  386.  
  387. /**
  388.   * @brief  Find valid Page for write or read operation
  389.   * @param  Operation: operation to achieve on the valid page.
  390.   *   This parameter can be one of the following values:
  391.   *     @arg READ_FROM_VALID_PAGE: read operation from valid page
  392.   *     @arg WRITE_IN_VALID_PAGE: write operation from valid page
  393.   * @retval Valid page number (PAGE0 or PAGE1) or NO_VALID_PAGE in case
  394.   *   of no valid page was found
  395.   */
  396. static uint16_t EE_FindValidPage(uint8_t Operation)
  397. {
  398.   uint16_t PageStatus0 = 6, PageStatus1 = 6;
  399.  
  400.   /* Get Page0 actual status */
  401.   PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS);
  402.  
  403.   /* Get Page1 actual status */
  404.   PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS);
  405.  
  406.   /* Write or read operation */
  407.   switch (Operation)
  408.   {
  409.     case WRITE_IN_VALID_PAGE:   /* ---- Write operation ---- */
  410.       if (PageStatus1 == VALID_PAGE)
  411.       {
  412.         /* Page0 receiving data */
  413.         if (PageStatus0 == RECEIVE_DATA)
  414.         {
  415.           return PAGE0;         /* Page0 valid */
  416.         }
  417.         else
  418.         {
  419.           return PAGE1;         /* Page1 valid */
  420.         }
  421.       }
  422.       else if (PageStatus0 == VALID_PAGE)
  423.       {
  424.         /* Page1 receiving data */
  425.         if (PageStatus1 == RECEIVE_DATA)
  426.         {
  427.           return PAGE1;         /* Page1 valid */
  428.         }
  429.         else
  430.         {
  431.           return PAGE0;         /* Page0 valid */
  432.         }
  433.       }
  434.       else
  435.       {
  436.         return NO_VALID_PAGE;   /* No valid Page */
  437.       }
  438.  
  439.     case READ_FROM_VALID_PAGE:  /* ---- Read operation ---- */
  440.       if (PageStatus0 == VALID_PAGE)
  441.       {
  442.         return PAGE0;           /* Page0 valid */
  443.       }
  444.       else if (PageStatus1 == VALID_PAGE)
  445.       {
  446.         return PAGE1;           /* Page1 valid */
  447.       }
  448.       else
  449.       {
  450.         return NO_VALID_PAGE ;  /* No valid Page */
  451.       }
  452.  
  453.     default:
  454.       return PAGE0;             /* Page0 valid */
  455.   }
  456. }
  457.  
  458. /**
  459.   * @brief  Verify if active page is full and Writes variable in EEPROM.
  460.   * @param  VirtAddress: 16 bit virtual address of the variable
  461.   * @param  Data: 16 bit data to be written as variable value
  462.   * @retval Success or error status:
  463.   *           - FLASH_COMPLETE: on success
  464.   *           - PAGE_FULL: if valid page is full
  465.   *           - NO_VALID_PAGE: if no valid page was found
  466.   *           - Flash error code: on write Flash error
  467.   */
  468. static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data)
  469. {
  470.   FLASH_Status FlashStatus = FLASH_COMPLETE;
  471.   uint16_t ValidPage = PAGE0;
  472.   uint32_t Address = 0x08010000, PageEndAddress = 0x080107FF;
  473.  
  474.   /* Get valid Page for write operation */
  475.   ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
  476.  
  477.   /* Check if there is no valid page */
  478.   if (ValidPage == NO_VALID_PAGE)
  479.   {
  480.     return  NO_VALID_PAGE;
  481.   }
  482.  
  483.   /* Get the valid Page start Address */
  484.   Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE));
  485.  
  486.   /* Get the valid Page end Address */
  487.   PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE));
  488.  
  489.   /* Check each active page address starting from begining */
  490.   while (Address < PageEndAddress)
  491.   {
  492.     /* Verify if Address and Address+2 contents are 0xFFFFFFFF */
  493.     if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF)
  494.     {
  495.       /* Set variable data */
  496.       FlashStatus = FLASH_ProgramHalfWord(Address, Data);
  497.       /* If program operation was failed, a Flash error code is returned */
  498.       if (FlashStatus != FLASH_COMPLETE)
  499.       {
  500.         return FlashStatus;
  501.       }
  502.       /* Set variable virtual address */
  503.       FlashStatus = FLASH_ProgramHalfWord(Address + 2, VirtAddress);
  504.       /* Return program operation status */
  505.       return FlashStatus;
  506.     }
  507.     else
  508.     {
  509.       /* Next address location */
  510.       Address = Address + 4;
  511.     }
  512.   }
  513.  
  514.   /* Return PAGE_FULL in case the valid page is full */
  515.   return PAGE_FULL;
  516. }
  517.  
  518. /**
  519.   * @brief  Transfers last updated variables data from the full Page to
  520.   *   an empty one.
  521.   * @param  VirtAddress: 16 bit virtual address of the variable
  522.   * @param  Data: 16 bit data to be written as variable value
  523.   * @retval Success or error status:
  524.   *           - FLASH_COMPLETE: on success
  525.   *           - PAGE_FULL: if valid page is full
  526.   *           - NO_VALID_PAGE: if no valid page was found
  527.   *           - Flash error code: on write Flash error
  528.   */
  529. static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
  530. {
  531.   FLASH_Status FlashStatus = FLASH_COMPLETE;
  532.   uint32_t NewPageAddress = 0x080103FF, OldPageAddress = 0x08010000;
  533.   uint16_t ValidPage = PAGE0, VarIdx = 0;
  534.   uint16_t EepromStatus = 0, ReadStatus = 0;
  535.  
  536.   /* Get active Page for read operation */
  537.   ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE);
  538.  
  539.   if (ValidPage == PAGE1)       /* Page1 valid */
  540.   {
  541.     /* New page address where variable will be moved to */
  542.     NewPageAddress = PAGE0_BASE_ADDRESS;
  543.  
  544.     /* Old page address where variable will be taken from */
  545.     OldPageAddress = PAGE1_BASE_ADDRESS;
  546.   }
  547.   else if (ValidPage == PAGE0)  /* Page0 valid */
  548.   {
  549.     /* New page address where variable will be moved to */
  550.     NewPageAddress = PAGE1_BASE_ADDRESS;
  551.  
  552.     /* Old page address where variable will be taken from */
  553.     OldPageAddress = PAGE0_BASE_ADDRESS;
  554.   }
  555.   else
  556.   {
  557.     return NO_VALID_PAGE;       /* No valid Page */
  558.   }
  559.  
  560.   /* Set the new Page status to RECEIVE_DATA status */
  561.   FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, RECEIVE_DATA);
  562.   /* If program operation was failed, a Flash error code is returned */
  563.   if (FlashStatus != FLASH_COMPLETE)
  564.   {
  565.     return FlashStatus;
  566.   }
  567.  
  568.   /* Write the variable passed as parameter in the new active page */
  569.   EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
  570.   /* If program operation was failed, a Flash error code is returned */
  571.   if (EepromStatus != FLASH_COMPLETE)
  572.   {
  573.     return EepromStatus;
  574.   }
  575.  
  576.   /* Transfer process: transfer variables from old to the new active page */
  577.   for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++)
  578.   {
  579.     if (VirtAddVarTab[VarIdx] != VirtAddress)  /* Check each variable except the one passed as parameter */
  580.     {
  581.       /* Read the other last variable updates */
  582.       ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar);
  583.       /* In case variable corresponding to the virtual address was found */
  584.       if (ReadStatus != 0x1)
  585.       {
  586.         /* Transfer the variable to the new active page */
  587.         EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar);
  588.         /* If program operation was failed, a Flash error code is returned */
  589.         if (EepromStatus != FLASH_COMPLETE)
  590.         {
  591.           return EepromStatus;
  592.         }
  593.       }
  594.     }
  595.   }
  596.  
  597.   /* Erase the old Page: Set old Page status to ERASED status */
  598.   FlashStatus = FLASH_ErasePage(OldPageAddress);
  599.   /* If erase operation was failed, a Flash error code is returned */
  600.   if (FlashStatus != FLASH_COMPLETE)
  601.   {
  602.     return FlashStatus;
  603.   }
  604.  
  605.   /* Set new Page status to VALID_PAGE status */
  606.   FlashStatus = FLASH_ProgramHalfWord(NewPageAddress, VALID_PAGE);
  607.   /* If program operation was failed, a Flash error code is returned */
  608.   if (FlashStatus != FLASH_COMPLETE)
  609.   {
  610.     return FlashStatus;
  611.   }
  612.  
  613.   /* Return last operation flash status */
  614.   return FlashStatus;
  615. }
  616.  
  617. /**
  618.   * @}
  619.   */
  620.  
  621. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  622.  
  623.  
  624.  
oraz eeprom.h

  1.  
  2. /**
  3.   ******************************************************************************
  4.   * @file    STM32F0xx_EEPROM_Emulation/inc/eeprom.h
  5.   * @author  MCD Application Team
  6.   * @version V1.0.0
  7.   * @date    29-May-2012
  8.   * @brief   This file contains all the functions prototypes for the EEPROM
  9.   *          emulation firmware library.
  10.   ******************************************************************************
  11.   * @attention
  12.   *
  13.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  14.   *
  15.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  16.   * You may not use this file except in compliance with the License.
  17.   * You may obtain a copy of the License at:
  18.   *
  19.   *        http://www.st.com/software_license_agreement_liberty_v2
  20.   *
  21.   * Unless required by applicable law or agreed to in writing, software
  22.   * distributed under the License is distributed on an "AS IS" BASIS,
  23.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24.   * See the License for the specific language governing permissions and
  25.   * limitations under the License.
  26.   *
  27.   ******************************************************************************
  28.   */
  29.  
  30. /* Define to prevent recursive inclusion -------------------------------------*/
  31. #ifndef __EEPROM_H
  32. #define __EEPROM_H
  33.  
  34. /* Includes ------------------------------------------------------------------*/
  35. #include "stm32f0xx.h"
  36.  
  37. /* Exported constants --------------------------------------------------------*/
  38. /* Define the size of the sectors to be used */
  39. #define PAGE_SIZE             ((uint32_t)0x0400)  /* Page size = 1KByte */
  40.  
  41. /* EEPROM start address in Flash */
  42. #define EEPROM_START_ADDRESS  ((uint32_t)0x0800F800) /* EEPROM emulation start address:
  43.                                                         from sector2, after 8KByte of used
  44.                                                         Flash memory */
  45.  
  46. /* Pages 0 and 1 base and end addresses */
  47. #define PAGE0_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x0000))
  48. #define PAGE0_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (PAGE_SIZE - 1)))
  49.  
  50. #define PAGE1_BASE_ADDRESS    ((uint32_t)(EEPROM_START_ADDRESS + 0x0400))
  51. #define PAGE1_END_ADDRESS     ((uint32_t)(EEPROM_START_ADDRESS + (2 * PAGE_SIZE - 1)))
  52.  
  53. /* Used Flash pages for EEPROM emulation */
  54. #define PAGE0                 ((uint16_t)0x0000)
  55. #define PAGE1                 ((uint16_t)0x0001)
  56.  
  57. /* No valid page define */
  58. #define NO_VALID_PAGE         ((uint16_t)0x00AB)
  59.  
  60. /* Page status definitions */
  61. #define ERASED                ((uint16_t)0xFFFF)     /* Page is empty */
  62. #define RECEIVE_DATA          ((uint16_t)0xEEEE)     /* Page is marked to receive data */
  63. #define VALID_PAGE            ((uint16_t)0x0000)     /* Page containing valid data */
  64.  
  65. /* Valid pages in read and write defines */
  66. #define READ_FROM_VALID_PAGE  ((uint8_t)0x00)
  67. #define WRITE_IN_VALID_PAGE   ((uint8_t)0x01)
  68.  
  69. /* Page full define */
  70. #define PAGE_FULL             ((uint8_t)0x80)
  71.  
  72. /* Variables' number */
  73. #define NB_OF_VAR             ((uint8_t)0x06)
  74.  
  75. /* Exported types ------------------------------------------------------------*/
  76. /* Exported macro ------------------------------------------------------------*/
  77. /* Exported functions ------------------------------------------------------- */
  78. uint16_t EE_Init(void);
  79. uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data);
  80. uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data);
  81.  
  82. #endif /* __EEPROM_H */
  83.  
  84. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
  85.  
Używanie jest bardzo przyjemnie i wygodne .....
  1.  
  2. // main.c
  3. .
  4. .
  5. .
  6. .
  7. .
  8.  
  9. int main()
  10. {
  11.        .
  12.        .
  13.        .
  14.  
  15. /*********************************************************************************
  16.  * Przygotowanie Emulacji EEPROM na FLASH
  17.  *
  18.  *********************************************************************************/
  19.     FLASH_Unlock();     // Odblokowanie zapisu do FLASH
  20.     EE_Init();          //Inicjalizacja emulacji EEPROM
  21.  
  22.     //****************** Odczyt wartości z EEPROM .. Adresy względne *************
  23.     EE_ReadVariable(0x0, &zmienna1);
  24.     EE_ReadVariable(0x1, &zmienna2);
  25.     EE_ReadVariable(0x2, &zmienna3);
  26.     EE_ReadVariable(0x3, &zmienna4);
  27.     EE_ReadVariable(0x4, &zmienna5);
  28.  
  29.  
  30. /******************************************************************************
  31.  * KONIEC EEPROM
  32.  ******************************************************************************/
  33. .
  34. .
  35. .
  36. .
  37.  
  38. }
  39.  
  40.  

Jak widać prosto łatwo i przyjemnie ... są dla wszystkich rodzin , ale jak coś mozna też zawsze w oparciu , stworzyć swoją ...

Ponadto ... jak chcesz umieszczać dane we Flash to definiujesz je jako const np:

Kod: Zaznacz cały


const char jakas_tablica[15] = {0x13, 0xff, 0xee, 0x74, 0x65, 0x5e, 0x34, 0xad, 0x76, 0xb9, 0x50, 0xc5, 0xba, 0x10, };

natomiast to co chcesz ładować do ram definiujesz:

Kod: Zaznacz cały


uint8_t ram_set[8]={11, 22, 33, 8, 7 ,75, 9, 12}; 

I to takie wszystko jest proste dlatego warto czasem poświecić chwilkę na przemyślenia ....