STM8L15x Standard Peripherals Drivers: stm8l15x_flash.c Source File

STM8L15x/16x Standard Peripherals Drivers

STM8L15x Standard Peripherals Drivers

stm8l15x_flash.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8l15x_flash.c
00004   * @author  MCD Application Team
00005   * @version V1.5.0
00006   * @date    13-May-2011
00007   * @brief   This file provides all the Flash firmware functions. These functions 
00008   *          can be executed from Internal FLASH or Internal RAM memories.
00009   *            - FLASH program and Data EEPROM memories interface configuration
00010   *            - FLASH program and Data EEPROM memories Programming
00011   *            - Option Bytes Programming
00012   *            - Interrupts and flags management
00013   *            - Functions to be executed from RAM
00014   *               
00015   *  @verbatim
00016   *  
00017   *          ===================================================================
00018   *                                 How to use this driver
00019   *          ===================================================================
00020   *                           
00021   *          This driver provides functions to configure and program the Flash 
00022   *          memory of all STM8L15x devices
00023   *          These functions are split in 4 groups
00024   * 
00025   *           1. FLASH program and Data EEPROM memories interface configuration 
00026   *              functions: this group includes the management of the following 
00027   *              features:
00028   *                    - Set the fixed programming time
00029   *                    - Configure the power state during WFI mode
00030   *                    - Configure the power state during run, low power run and
00031   *                      WFE modes (should be executed from internal RAM) 
00032   *  
00033   *           2. FLASH program and Data EEPROM memories Programming functions: 
00034   *              this group includes all needed functions to erase and program 
00035   *              the FLASH program or the Data EEPROM memory.
00036   *                    - Lock and Unlock the Flash program interface.
00037   *                    - Lock and Unlock the Data EEPROM interface.  
00038   *                    - Erase function: Erase Byte, Erase Word and Erase Block 
00039   *                      (should be executed from internal RAM). 
00040   *                    - Program functions: Program Byte, Program Word, 
00041   *                      Program Block (should be executed from internal RAM) 
00042   *                      and Fast Program Block (should be executed from internal
00043   *                      RAM).
00044   *    
00045   *           3. FLASH Option Bytes Programming functions: this group includes 
00046   *              all needed functions to:
00047   *                    - Program/erase the user option Bytes 
00048   *                    - Get the Read Out Protection status (ROP option byte)
00049   *                    - Get the User Boot Code size (UBC option byte)
00050   *                    - Get the Protected Code size (PCODE option byte) 
00051   *                    
00052   *            Note: Option byte programming is very similar to data EEPROM byte
00053   *                  programming.          
00054   *  
00055   *           4. FLASH Interrupts and flag management functions: this group 
00056   *              includes all needed functions to:
00057   *                    - Enable/Disable the flash interrupt sources
00058   *                    - Get flags status
00059   *                    - Wait for last flash operation(can be executed from 
00060   *                      internal RAM)
00061   *                        
00062   *           5. Functions to be executed from RAM: this group includes the functions
00063   *              that should be executed from RAM and provides description on how 
00064   *              to handle this with the different supported toolchains
00065   *   
00066   *  The table below lists the functions that can be executed from RAM.
00067   *  
00068   *   +--------------------------------------------------------------------------------|
00069   *   |   Functions prototypes      |    RAM execution            |     Comments       |
00070   *   ---------------------------------------------------------------------------------|
00071   *   |                             | Mandatory in case of block  | Can be executed    |
00072   *   | FLASH_WaitForLastOperation  | Operation:                  | from Flash in case |
00073   *   |                             | - Block programming         | of byte and word   |
00074   *   |                             | - Block erase               | Operations         |
00075   *   |--------------------------------------------------------------------------------|
00076   *   | FLASH_PowerRunModeConfig    |       Exclusively           | useless from Flash |
00077   *   |--------------------------------------------------------------------------------|
00078   *   | FLASH_GetPowerStatus        |       Exclusively           | useless from Flash |
00079   *   |--------------------------------------------------------------------------------|
00080   *   | FLASH_ProgramBlock          |       Exclusively           | useless from Flash |
00081   *   |--------------------------------------------------------------------------------|
00082   *   | FLASH_EraseBlock            |       Exclusively           | useless from Flash |
00083   *   |--------------------------------------------------------------------------------|
00084   *  
00085   *  To be able to execute functions from RAM several steps have to be followed.
00086   *   These steps may differ from one toolchain to another.
00087   *   A detailed description is available below within this driver.
00088   *   You can also refer to the Flash_DataProgram example provided within the
00089   *   STM8L15x_StdPeriph_Lib package.
00090   * 
00091   *  @endverbatim
00092   *                      
00093   ******************************************************************************
00094   * @attention
00095   *
00096   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00097   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00098   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00099   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00100   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00101   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00102   *
00103   * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
00104   ******************************************************************************  
00105   */
00106 /* Includes ------------------------------------------------------------------*/
00107 #include "stm8l15x_flash.h"
00108 
00109 /** @addtogroup STM8L15x_StdPeriph_Driver
00110   * @{
00111   */
00112 
00113 /** @defgroup FLASH 
00114   * @brief FLASH driver modules
00115   * @{
00116   */
00117   
00118 /* Private typedef -----------------------------------------------------------*/
00119 /* Private define ------------------------------------------------------------*/
00120 /** @defgroup FLASH_Private_Define
00121   * @{
00122   */ 
00123 #define FLASH_CLEAR_BYTE   ((uint8_t)0x00)
00124 #define FLASH_SET_BYTE     ((uint8_t)0xFF)
00125 #define OPERATION_TIMEOUT  ((uint16_t)0xFFFF)
00126 /**
00127   * @}
00128   */
00129 /* Private macro -------------------------------------------------------------*/
00130 /* Private variables ---------------------------------------------------------*/
00131 /* Private function prototypes -----------------------------------------------*/
00132 /* Private functions ---------------------------------------------------------*/
00133  
00134 /** @defgroup FLASH_Private_Functions
00135   * @{
00136   */ 
00137 
00138 /** @defgroup FLASH_Group1 FLASH program and Data EEPROM memories Interface 
00139   *                        configuration functions
00140   *  @brief   FLASH Interface configuration functions 
00141  *
00142 @verbatim   
00143  ===============================================================================
00144       FLASH program and Data EEPROM memories interface configuration functions
00145  ===============================================================================  
00146 
00147    The FLASH program and Data EEPROM memories interface configuration functions,
00148     includes the following functions:
00149                 
00150    - FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void);
00151    - void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime);
00152          - void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power);
00153 
00154    These functions don't need the unlock sequence.
00155 
00156 @endverbatim
00157   * @{
00158   */
00159         
00160 /**
00161   * @brief  Sets the fixed programming time
00162   * @param  FLASH_ProgTime : Indicates the programming time to be fixed
00163   *          This parameter can be one of the following values:
00164   *            @arg FLASH_ProgramTime_Standard: Standard programming time fixed at 1/2 tprog
00165   *            @arg FLASH_ProgramTime_TProg: Programming time fixed at tprog 
00166   * @retval None
00167   */
00168 void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
00169 {
00170   /* Check parameter */
00171   assert_param(IS_FLASH_PROGRAM_TIME(FLASH_ProgTime));
00172 
00173   FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
00174   FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
00175 }
00176 
00177 /**
00178   * @brief  Returns the fixed programming time
00179   * @param  None
00180   * @retval Fixed programming time value
00181   */
00182 FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
00183 {
00184   return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
00185 }
00186 
00187 /**
00188   * @brief  Configures the power state for Flash program and data EEPROM during
00189   *          wait for interrupt mode
00190   * @param  FLASH_Power: The power state for Flash program and data EEPROM during
00191   *         wait for interrupt mode
00192   *          This parameter can be one of the following values:
00193   *            @arg FLASH_Power_IDDQ: Flash program and data EEPROM in IDDQ
00194   *            @arg FLASH_Power_On: Flash program and data EEPROM not in IDDQ 
00195   * @retval None
00196   */
00197 void FLASH_PowerWaitModeConfig(FLASH_Power_TypeDef FLASH_Power)
00198 {
00199   /* Check parameter */
00200   assert_param(IS_FLASH_POWER(FLASH_Power));
00201 
00202   /* Flash program and data EEPROM in IDDQ during wait for interrupt mode*/
00203   if (FLASH_Power != FLASH_Power_On)
00204   {
00205     FLASH->CR1 |= (uint8_t)FLASH_CR1_WAITM;
00206   }
00207   /* Flash program and data EEPROM not in IDDQ during wait for interrupt mode*/
00208   else
00209   {
00210     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_WAITM);
00211   }
00212 }
00213 
00214 /**
00215   * @}
00216   */
00217 
00218 /** @defgroup FLASH_Group2 FLASH Memory Programming functions
00219  *  @brief   FLASH Memory Programming functions
00220  *
00221 @verbatim   
00222  ===============================================================================
00223                       FLASH Memory Programming functions
00224  ===============================================================================  
00225 
00226    The FLASH Memory Programming functions, includes the following functions:
00227     - void FLASH_DeInit(void);
00228     - void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType);
00229     - void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType);
00230     - void FLASH_ProgramByte(uint32_t Address, uint8_t Data);
00231     - void FLASH_EraseByte(uint32_t Address);
00232     - void FLASH_ProgramWord(uint32_t Address, uint32_t Data);
00233     - uint8_t FLASH_ReadByte(uint32_t Address);
00234    
00235    Any operation of erase or program should follow these steps:
00236 
00237    1. Call the FLASH_Unlock(FLASH_MemType) function to enable the memory access
00238 
00239    2. Call the desired function to erase or program data
00240 
00241    3. Call the FLASH_Lock() function to disable the memory access 
00242      (it is recommended to protect the FLASH memory against possible unwanted operation)
00243 
00244 @endverbatim
00245   * @{
00246   */
00247   
00248 /**
00249   * @brief  Deinitializes the FLASH registers to their default reset values.
00250   * @param  None
00251   * @retval None
00252   */
00253 void FLASH_DeInit(void)
00254 {
00255   FLASH->CR1 = FLASH_CR1_RESET_VALUE;
00256   FLASH->CR2 = FLASH_CR2_RESET_VALUE;
00257   FLASH->IAPSR = FLASH_IAPSR_RESET_VALUE;
00258   (void) FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
00259 }
00260   
00261 /**
00262   * @brief  Unlocks the program or data EEPROM memory
00263   * @param  FLASH_MemType : Memory type to unlock
00264   *          This parameter can be one of the following values:
00265   *            @arg FLASH_MemType_Program: Program memory
00266   *            @arg FLASH_MemType_Data: Data EEPROM memory 
00267   * @retval None
00268   */
00269 void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
00270 {
00271   /* Check parameter */
00272   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
00273 
00274   /* Unlock program memory */
00275   if (FLASH_MemType == FLASH_MemType_Program)
00276   {
00277     FLASH->PUKR = FLASH_RASS_KEY1;
00278     FLASH->PUKR = FLASH_RASS_KEY2;
00279   }
00280 
00281   /* Unlock data memory */
00282   if (FLASH_MemType == FLASH_MemType_Data)
00283   {
00284     FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on data memory !!! */
00285     FLASH->DUKR = FLASH_RASS_KEY1;
00286   }
00287 }
00288 
00289 /**
00290   * @brief  Locks the program or data EEPROM memory
00291   * @param  FLASH_MemType : Memory type
00292   *          This parameter can be one of the following values:
00293   *            @arg FLASH_MemType_Program: Program memory
00294   *            @arg FLASH_MemType_Data: Data EEPROM memory 
00295   * @retval None
00296   */
00297 void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
00298 {
00299   /* Check parameter */
00300   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
00301   /* Lock memory */
00302   FLASH->IAPSR &= (uint8_t)FLASH_MemType;
00303 }
00304 
00305 /**
00306   * @brief  Programs one byte in program or data EEPROM memory
00307   * @param  Address : Address where the byte will be programmed
00308   * @param  Data : Value to be programmed
00309   * @retval None
00310   */
00311 void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
00312 {
00313   /* Check parameters */
00314   assert_param(IS_FLASH_ADDRESS(Address));
00315 
00316   *(PointerAttr uint8_t*) (uint16_t)Address = Data;
00317 }
00318 
00319 /**
00320   * @brief  Erases one byte in the program or data EEPROM memory
00321   * @param  Address : Address of the byte to erase
00322   * @retval None
00323   */
00324 void FLASH_EraseByte(uint32_t Address)
00325 {
00326   /* Check parameter */
00327   assert_param(IS_FLASH_ADDRESS(Address));
00328 
00329   *(PointerAttr uint8_t*) (uint16_t)Address = FLASH_CLEAR_BYTE; /* Erase byte */
00330 }
00331 
00332 /**
00333   * @brief  Programs one word (4 bytes) in program or data EEPROM memory
00334   * @param  Address : The address where the data will be programmed
00335   * @param  Data : Value to be programmed
00336   * @retval None
00337   */
00338 void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
00339 {
00340   /* Check parameters */
00341   assert_param(IS_FLASH_ADDRESS(Address));
00342   /* Enable Word Write Once */
00343   FLASH->CR2 |= FLASH_CR2_WPRG;
00344 
00345   /* Write one byte - from lowest address*/
00346   *((PointerAttr uint8_t*)(uint16_t)Address)       = *((uint8_t*)(&Data));   
00347   /* Write one byte*/
00348   *(((PointerAttr uint8_t*)(uint16_t)Address) + 1) = *((uint8_t*)(&Data) + 1);
00349   /* Write one byte*/
00350   *(((PointerAttr uint8_t*)(uint16_t)Address) + 2) = *((uint8_t*)(&Data) + 2); 
00351   /* Write one byte - from higher address*/
00352   *(((PointerAttr uint8_t*)(uint16_t)Address) + 3) = *((uint8_t*)(&Data) + 3); 
00353 }
00354 
00355 /**
00356   * @brief  Reads one byte from flash memory
00357   * @param  Address : Address to read
00358   * @retval Value of the byte
00359   */
00360 uint8_t FLASH_ReadByte(uint32_t Address)
00361 {
00362   /* Read byte */
00363   return(*(PointerAttr uint8_t *) (uint16_t)Address);
00364 }
00365 /**
00366   * @}
00367   */
00368 
00369 /** @defgroup FLASH_Group3 Option Bytes Programming functions
00370  *  @brief   Option Bytes Programming functions 
00371  *
00372 @verbatim   
00373  ===============================================================================
00374                         Option Bytes Programming functions
00375  ===============================================================================  
00376 
00377    The FLASH_Option Bytes Programming_functions, includes the following functions:
00378 
00379    - void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data);
00380    - void FLASH_EraseOptionByte(uint16_t Address);
00381    - FunctionalState FLASH_GetReadOutProtectionStatus(void);
00382    - uint16_t FLASH_GetBootSize(void);
00383    - uint16_t FLASH_GetCodeSize(void);
00384    
00385    Any operation of erase or program should follow these steps:
00386    
00387    1. Call the FLASH_Unlock(FLASH_MemType_Data); function to enable the Flash 
00388       option control register access
00389    
00390    2. Call the desired function to erase or program data
00391       - void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data); => to program
00392         the option byte Address with the desired Data value.  
00393       - void FLASH_EraseOptionByte(uint16_t Address); => to erase the option byte
00394         Address.                         
00395    
00396    3. Once all needed option bytes to be programmed are correctly written, call the
00397       FLASH_Lock(FLASH_MemType_Data) to disable the memory access ( It is recommended to
00398       protect the FLASH memory against possible unwanted operation)
00399 
00400 @endverbatim
00401   * @{
00402   */
00403   
00404 /**
00405   * @brief  Programs option byte
00406   * @param  Address : option byte address to program
00407   * @param  Data : Value to write
00408   * @retval None
00409   */
00410 void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
00411 {
00412   /* Check parameter */
00413   assert_param(IS_OPTION_BYTE_ADDRESS(Address));
00414 
00415   /* Enable write access to option bytes */
00416   FLASH->CR2 |= FLASH_CR2_OPT;
00417 
00418   /* Program option byte and his complement */
00419   *((PointerAttr uint8_t*)Address) = Data;
00420 
00421   FLASH_WaitForLastOperation(FLASH_MemType_Program);
00422 
00423   /* Disable write access to option bytes */
00424   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
00425 }
00426 
00427 /**
00428   * @brief  Erases option byte
00429   * @param  Address : Option byte address to erase
00430   * @retval None
00431   */
00432 void FLASH_EraseOptionByte(uint16_t Address)
00433 {
00434   /* Check parameter */
00435   assert_param(IS_OPTION_BYTE_ADDRESS(Address));
00436 
00437   /* Enable write access to option bytes */
00438   FLASH->CR2 |= FLASH_CR2_OPT;
00439 
00440   /* Erase option byte and his complement */
00441   *((PointerAttr uint8_t*)Address) = FLASH_CLEAR_BYTE;
00442 
00443   FLASH_WaitForLastOperation(FLASH_MemType_Program);
00444 
00445   /* Disable write access to option bytes */
00446   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
00447 }
00448 
00449 /**
00450   * @brief  Returns the FLASH Read Out Protection Status.
00451   * @param  None
00452   * @retval FLASH Read Out Protection Status.
00453   *         This parameter can be a ENABLE or DISABLE
00454   */
00455 FunctionalState FLASH_GetReadOutProtectionStatus(void)
00456 {
00457   FunctionalState state = DISABLE;
00458 
00459   if (OPT->ROP == FLASH_READOUTPROTECTION_KEY)
00460   {
00461     /* The status of the Flash read out protection is enabled*/
00462     state =  ENABLE;
00463   }
00464   else
00465   {
00466     /* The status of the Flash read out protection is disabled*/
00467     state =  DISABLE;
00468   }
00469 
00470   return state;
00471 }
00472 
00473 /**
00474   * @brief  Returns the Boot memory size in bytes
00475   * @param  None
00476   * @retval Boot memory size in bytes
00477   */
00478 uint16_t FLASH_GetBootSize(void)
00479 {
00480   uint16_t temp = 0;
00481 
00482   /* Calculates the number of bytes */
00483   temp = (uint16_t)((uint16_t)OPT->UBC * (uint16_t)128);
00484 
00485   /* Correction because size upper 8kb doesn't exist */
00486   if (OPT->UBC > 0x7F)
00487   {
00488     temp = 8192;
00489   }
00490 
00491   /* Return value */
00492   return(temp);
00493 
00494 }
00495 
00496 /**
00497  *
00498   * @brief  Returns the Code Area size in bytes
00499   * @param  None
00500   * @retval Code Area size in bytes
00501   */
00502 uint16_t FLASH_GetCodeSize(void)
00503 {
00504   uint16_t temp = 0;
00505 
00506   /* Calculates the number of bytes */
00507   temp = (uint16_t)((uint16_t)OPT->PCODESIZE * (uint16_t)128);
00508 
00509   /* Correction because size upper of 8kb doesn't exist */
00510   if (OPT->PCODESIZE > 0x7F)
00511   {
00512     temp = 8192;
00513   }
00514 
00515   /* Return value */
00516   return(temp);
00517 }
00518 
00519 /**
00520   * @}
00521   */
00522 
00523 /** @defgroup FLASH_Group4 Interrupts and flags management functions
00524  *  @brief   Interrupts and flags management functions
00525  *
00526 @verbatim   
00527  ===============================================================================
00528                   Interrupts and flags management functions
00529  ===============================================================================  
00530 
00531 @endverbatim
00532   * @{
00533   */
00534   
00535 /**
00536   * @brief  Enables or Disables the Flash interrupt mode
00537   * @param  NewState : The new state of the flash interrupt mode
00538     *         This parameter can be: ENABLE or DISABLE.
00539   * @retval None
00540   */
00541 void FLASH_ITConfig(FunctionalState NewState)
00542 {
00543 
00544   /* Check parameter */
00545   assert_param(IS_FUNCTIONAL_STATE(NewState));
00546 
00547   if (NewState != DISABLE)
00548   {
00549     /* Enables the interrupt sources */
00550     FLASH->CR1 |= FLASH_CR1_IE;
00551   }
00552   else
00553   {
00554     /* Disables the interrupt sources */
00555     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE);
00556   }
00557 }
00558 
00559 /**
00560   * @brief  Checks whether the specified FLASH flag is set or not.
00561   * @param  FLASH_FLAG : specifies the Flash Flag to check.
00562   *          This parameter can be one of the following values:
00563   *            @arg FLASH_FLAG_HVOFF: End of high voltage
00564   *            @arg FLASH_FLAG_DUL: Data EEPROM unlocked
00565   *            @arg FLASH_FLAG_EOP: End of programming (write or erase operation)
00566   *            @arg FLASH_FLAG_PUL: Flash Program memory unlocked
00567   *            @arg FLASH_FLAG_WR_PG_DIS: Write attempted to protected page       
00568   * @retval Indicates the state of the Flash_FLAG.
00569   *         This parameter can be SET or RESET
00570   */
00571 FlagStatus FLASH_GetFlagStatus(FLASH_FLAG_TypeDef FLASH_FLAG)
00572 {
00573   FlagStatus status = RESET;
00574   assert_param(IS_FLASH_FLAGS(FLASH_FLAG));
00575 
00576   /* Check the status of the specified flash flag*/
00577   if ((FLASH->IAPSR  & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
00578   {
00579     status = SET; /* Flash_FLAG is set*/
00580   }
00581   else
00582   {
00583     status = RESET; /* Flash_FLAG is reset*/
00584   }
00585 
00586   /* Return the Flash_FLAG status*/
00587   return status;
00588 }
00589 
00590 /**
00591   * @}
00592   */ 
00593   
00594 /** @defgroup FLASH_Group5 Functions to be executed from RAM
00595  *  @brief  Functions to be executed from RAM
00596  *
00597 @verbatim   
00598  ===============================================================================
00599                          Functions to be executed from RAM
00600  ===============================================================================  
00601  
00602  All the functions defined below must be executed from RAM exclusively, except
00603  for the FLASH_WaitForLastOperation function which can be executed from Flash.
00604 
00605  Steps of the execution from RAM differs from one toolchain to another:
00606  - For Cosmic Compiler:
00607     1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
00608     This segment is defined in the stm8l15x_flash.c file.
00609   2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file,
00610     or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
00611    definition.
00612   3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
00613     add the FLASH_CODE segment with "-ic" options.
00614   4- In main.c file call the _fctcpy() function with first segment character as 
00615     parameter "_fctcpy('F');" to load the declared moveable code segment
00616     (FLASH_CODE) in RAM before execution.
00617   5- By default the _fctcpy function is packaged in the Cosmic machine library,
00618     so the function prototype "int _fctcopy(char name);" must be added in main.c
00619     file.
00620 
00621   - For Raisonance Compiler
00622    1- Use the inram keyword in the function declaration to specify that it can be
00623     executed from RAM.
00624     This is done within the stm8l15x_flash.c file, and it's conditioned by 
00625     RAM_EXECUTION definition.
00626    2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file, or 
00627    define it in Raisonance compiler preprocessor to enable the access for the 
00628    inram functions.
00629    3- An inram function code is copied from Flash to RAM by the C startup code. 
00630    In some applications, the RAM area where the code was initially stored may be
00631    erased or corrupted, so it may be desirable to perform the copy again. 
00632    Depending on the application memory model, the memcpy() or fmemcpy() functions
00633    should be used to perform the copy.
00634       � In case your project uses the SMALL memory model (code smaller than 64K),
00635        memcpy()function is recommended to perform the copy
00636       � In case your project uses the LARGE memory model, functions can be 
00637       everywhere in the 24-bits address space (not limited to the first 64KB of
00638       code), In this case, the use of memcpy() function will not be appropriate,
00639       you need to use the specific fmemcpy() function (which copies objects with
00640       24-bit addresses).
00641       - The linker automatically defines 2 symbols for each inram function:
00642            � __address__functionname is a symbol that holds the Flash address 
00643            where the given function code is stored.
00644            � __size__functionname is a symbol that holds the function size in bytes.
00645      And we already have the function address (which is itself a pointer)
00646   4- In main.c file these two steps should be performed for each inram function:
00647      � Import the "__address__functionname" and "__size__functionname" symbols
00648        as global variables:
00649          extern int __address__functionname; // Symbol holding the flash address
00650          extern int __size__functionname;    // Symbol holding the function size
00651      � In case of SMALL memory model use, Call the memcpy() function to copy the
00652       inram function to the RAM destination address:
00653                 memcpy(functionname, // RAM destination address
00654                       (void*)&__address__functionname, // Flash source address
00655                       (int)&__size__functionname); // Code size of the function
00656      � In case of LARGE memory model use, call the fmemcpy() function to copy 
00657      the inram function to the RAM destination address:
00658                  memcpy(functionname, // RAM destination address
00659                       (void @far*)&__address__functionname, // Flash source address
00660                       (int)&__size__functionname); // Code size of the function
00661 
00662  - For IAR Compiler:
00663     1- Use the __ramfunc keyword in the function declaration to specify that it 
00664     can be executed from RAM.
00665     This is done within the stm8l15x_flash.c file, and it's conditioned by 
00666     RAM_EXECUTION definition.
00667     2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8l15x.h file, or 
00668    define it in IAR compiler preprocessor to enable the access for the 
00669    __ramfunc functions.
00670  
00671  The Flash_DataProgram example provided within the STM8L15x_StdPeriph_Lib package
00672  details all the steps described above.
00673 
00674 @endverbatim
00675   * @{
00676   */
00677    
00678 /**
00679   * @brief
00680   *******************************************************************************
00681   *                         Execution from RAM enable
00682   *******************************************************************************
00683   *
00684   * To enable execution from RAM you can either uncomment the following define 
00685   * in the stm8l15x.h file or define it in your toolchain compiler preprocessor
00686   * - #define RAM_EXECUTION  (1) 
00687   */
00688 
00689 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
00690  #pragma section (FLASH_CODE)
00691 #endif  /* _COSMIC_ && RAM_EXECUTION */
00692 
00693 /**
00694   * @brief  Configures the power state for Flash program and data EEPROM during
00695   *         run, low power run and low power wait modes
00696   * @note   This function must be called and executed from RAM.
00697   * @param  FLASH_Power: power state of the Flash program and data EEPROM
00698   *          This parameter can be one of the following values:
00699   *            @arg FLASH_Power_IDDQ: Flash program and data EEPROM in IDDQ
00700   *            @arg FLASH_Power_On: Flash program and data EEPROM not in IDDQ 
00701   * @retval None
00702   */
00703 IN_RAM(void FLASH_PowerRunModeConfig(FLASH_Power_TypeDef FLASH_Power))
00704 {
00705   /* Check parameter */
00706   assert_param(IS_FLASH_POWER(FLASH_Power));
00707 
00708   if (FLASH_Power != FLASH_Power_On)
00709   {
00710   FLASH->CR1 |= (uint8_t)FLASH_CR1_EEPM;
00711   }
00712   else
00713   {
00714     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_EEPM);
00715   }
00716 }
00717 
00718 /**
00719   * @brief  Checks the power status for Flash program and data EEPROM
00720   * @note   This function should be called and executed from RAM.
00721   * @param  None
00722   * @retval Flash program and data EEPROM power status
00723   *         The returned value can be one of the following:
00724   *         - FLASH_PowerStatus_IDDQDuringWaitMode: IDDQ during Wait mode
00725   *         - FLASH_PowerStatus_IDDQDuringRunMode: IDDQ during Run mode
00726   *         - FLASH_PowerStatus_IDDQDuringWaitAndRunModes: IDDQ during Wait/Run mode
00727   *         - FLASH_PowerStatus_On: Powered on during Wait and Run modes
00728   */
00729 IN_RAM(FLASH_PowerStatus_TypeDef FLASH_GetPowerStatus(void))
00730 {
00731   return((FLASH_PowerStatus_TypeDef)(FLASH->CR1 & (uint8_t)0x0C));
00732 }
00733 
00734 /**
00735   * @brief  Programs a memory block
00736   * @note   This function should be called and executed from RAM.
00737   * @param  FLASH_MemType : The type of memory to program
00738   *          This parameter can be one of the following values:
00739   *            @arg FLASH_MemType_Program: Program memory
00740   *            @arg FLASH_MemType_Data: Data EEPROM memory 
00741   * @param  BlockNum : The block number
00742   * @param  FLASH_ProgMode : The programming mode.
00743   *          This parameter can be one of the following values:
00744   *            @arg FLASH_ProgramMode_Standard: Standard programming mode
00745   *            @arg FLASH_ProgramMode_Fast: Fast programming mode
00746   * @param  Buffer : Pointer to buffer containing source data.
00747   * @retval None.
00748   */
00749 IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType,
00750                         FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer))
00751 {
00752   uint16_t Count = 0;
00753   uint32_t startaddress = 0;
00754 
00755   /* Check parameters */
00756   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
00757   assert_param(IS_FLASH_PROGRAM_MODE(FLASH_ProgMode));
00758   if (FLASH_MemType == FLASH_MemType_Program)
00759   {
00760   assert_param(IS_FLASH_PROGRAM_BLOCK_NUMBER(BlockNum));
00761     startaddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS;
00762   }
00763   else
00764   {
00765     assert_param(IS_FLASH_DATA_EEPROM_BLOCK_NUMBER(BlockNum));
00766     startaddress = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS;
00767   }
00768 
00769   /* Point to the first block address */
00770   startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE);
00771 
00772   /* Selection of Standard or Fast programming mode */
00773   if (FLASH_ProgMode == FLASH_ProgramMode_Standard)
00774   {
00775   /* Standard programming mode */
00776   FLASH->CR2 |= FLASH_CR2_PRG;
00777   }
00778   else
00779   {
00780   /* Fast programming mode */
00781   FLASH->CR2 |= FLASH_CR2_FPRG;
00782   }
00783 
00784   /* Copy data bytes from RAM to FLASH memory */
00785   for (Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
00786   {
00787 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD)
00788   *((PointerAttr uint8_t*) (uint16_t)startaddress + Count) = ((uint8_t)(Buffer[Count]));
00789 #elif defined (STM8L15X_HD)
00790   *((PointerAttr uint8_t*) (uint32_t)startaddress + Count) = ((uint8_t)(Buffer[Count]));
00791 #endif
00792   }
00793 }
00794 
00795 /**
00796   * @brief  Erases a block in the program or data memory.
00797   * @note   This function should be called and executed from RAM.
00798   * @param  BlockNum : Indicates the block number to erase
00799   * @param  FLASH_MemType :  The type of memory to erase
00800   *          This parameter can be one of the following values:
00801   *            @arg FLASH_MemType_Program: Program memory
00802   *            @arg FLASH_MemType_Data: Data EEPROM memory 
00803   * @retval None.
00804   */
00805 IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
00806 {
00807   uint32_t startaddress = 0;
00808 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD)
00809   uint32_t PointerAttr  *pwFlash;
00810 #elif defined (STM8L15X_HD)
00811   uint8_t PointerAttr  *pwFlash;
00812 #endif
00813 
00814   /* Check parameters */
00815   assert_param(IS_FLASH_MEMORY_TYPE(FLASH_MemType));
00816   if (FLASH_MemType == FLASH_MemType_Program)
00817   {
00818   assert_param(IS_FLASH_PROGRAM_BLOCK_NUMBER(BlockNum));
00819     startaddress = FLASH_PROGRAM_START_PHYSICAL_ADDRESS;
00820   }
00821   else
00822   {
00823     assert_param(IS_FLASH_DATA_EEPROM_BLOCK_NUMBER(BlockNum));
00824     startaddress = FLASH_DATA_EEPROM_START_PHYSICAL_ADDRESS;
00825   }
00826 
00827   /* Point to the first block address */
00828 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP)|| defined (STM8L15X_LD)
00829   pwFlash = (PointerAttr uint32_t *)(uint16_t)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
00830 #elif defined (STM8L15X_HD)
00831   pwFlash = (PointerAttr uint8_t *)(uint32_t)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
00832 #endif
00833 
00834   /* Enable erase block mode */
00835   FLASH->CR2 |= FLASH_CR2_ERASE;
00836 
00837 #if defined (STM8L15X_MD) || defined (STM8L15X_MDP) || defined (STM8L15X_LD)
00838   *pwFlash = (uint32_t)0;
00839 #elif defined (STM8L15X_HD)
00840   *pwFlash = (uint8_t)0;
00841   *(pwFlash + 1) = (uint8_t)0;
00842   *(pwFlash + 2) = (uint8_t)0;
00843   *(pwFlash + 3) = (uint8_t)0;
00844 #endif
00845 }
00846 
00847 /**
00848   * @brief  Waits for a Flash operation to complete.
00849   * @note   The call and execution of this function must be done from RAM in case
00850   *         of Block operation, otherwise it can be executed from Flash
00851   * @param  FLASH_MemType : Memory type
00852   *          This parameter can be one of the following values:
00853   *            @arg FLASH_MemType_Program: Program memory
00854   *            @arg FLASH_MemType_Data: Data EEPROM memory 
00855   * @retval FLASH status
00856   */
00857 IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType))
00858 {
00859   uint32_t timeout = OPERATION_TIMEOUT;
00860   uint8_t flagstatus = 0x00;
00861   /* Wait until operation completion or write protected page occurred */
00862   if (FLASH_MemType == FLASH_MemType_Program)
00863   {
00864   while ((flagstatus == 0x00) && (timeout != 0x00))
00865     {
00866       flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_EOP |
00867                              FLASH_IAPSR_WR_PG_DIS));
00868       timeout--;
00869     }
00870   }
00871   else
00872   {
00873     while ((flagstatus == 0x00) && (timeout != 0x00))
00874     {
00875       flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_HVOFF |
00876                              FLASH_IAPSR_WR_PG_DIS));
00877       timeout--;
00878     }
00879   }
00880   if (timeout == 0x00 )
00881   {
00882   flagstatus = FLASH_Status_TimeOut;
00883   }
00884 
00885   return((FLASH_Status_TypeDef)flagstatus);
00886 }
00887 
00888 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
00889  /* End of FLASH_CODE section */
00890  #pragma section ()
00891 #endif /* _COSMIC_ && RAM_EXECUTION */
00892 
00893 /**
00894   * @}
00895   */
00896 
00897 /**
00898   * @}
00899   */
00900    
00901   /**
00902   * @}
00903   */ 
00904   
00905   /**
00906   * @}
00907   */ 
00908   
00909 /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
STM8S Firmware Library: Overview

 

 

 

For complete documentation on STM8L15x 8-bit microcontrollers platform visit www.st.com