STM8S/A Standard Peripherals Drivers: stm8s_flash.c Source File

STM8S/A Standard Peripherals Library

stm8s_flash.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm8s_flash.c
00004   * @author  MCD Application Team
00005   * @version V2.2.0
00006   * @date    30-September-2014
00007   * @brief   This file contains all the functions for the FLASH peripheral.
00008    ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include "stm8s_flash.h"
00030 
00031 /** @addtogroup STM8S_StdPeriph_Driver
00032   * @{
00033   */
00034 /**
00035 @code
00036  This driver provides functions to configure and program the Flash memory of all
00037  STM8S devices.
00038 
00039  It includes as well functions that can be either executed from RAM or not, and
00040  other functions that must be executed from RAM otherwise useless.
00041 
00042  The table below lists the functions that can be executed from RAM.
00043 
00044  +--------------------------------------------------------------------------------|
00045  |   Functions prototypes      |    RAM execution            |     Comments       |
00046  ---------------------------------------------------------------------------------|
00047  |                             | Mandatory in case of block  | Can be executed    |
00048  | FLASH_WaitForLastOperation  | Operation:                  | from Flash in case |
00049  |                             | - Block programming         | of byte and word   |
00050  |                             | - Block erase               | Operations         |
00051  |--------------------------------------------------------------------------------|
00052  | FLASH_ProgramBlock          |       Exclusively           | useless from Flash |
00053  |--------------------------------------------------------------------------------|
00054  | FLASH_EraseBlock            |       Exclusively           | useless from Flash |
00055  |--------------------------------------------------------------------------------|
00056 
00057  To be able to execute functions from RAM several steps have to be followed.
00058  These steps may differ from one toolchain to another.
00059  A detailed description is available below within this driver.
00060  You can also refer to the FLASH examples provided within the
00061  STM8S_StdPeriph_Lib package.
00062 
00063 @endcode
00064 */
00065 
00066 
00067 /* Private typedef -----------------------------------------------------------*/
00068 /* Private define ------------------------------------------------------------*/
00069 #define FLASH_CLEAR_BYTE    ((uint8_t)0x00)
00070 #define FLASH_SET_BYTE      ((uint8_t)0xFF)
00071 #define OPERATION_TIMEOUT   ((uint16_t)0xFFFF)
00072 /* Private macro -------------------------------------------------------------*/
00073 /* Private variables ---------------------------------------------------------*/
00074 /* Private function prototypes -----------------------------------------------*/
00075 /* Private Constants ---------------------------------------------------------*/
00076 
00077 /** @addtogroup FLASH_Public_functions
00078   * @{
00079   */
00080 
00081 /**
00082   * @brief  Unlocks the program or data EEPROM memory
00083   * @param  FLASH_MemType : Memory type to unlock
00084   *         This parameter can be a value of @ref FLASH_MemType_TypeDef
00085   * @retval None
00086   */
00087 void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
00088 {
00089   /* Check parameter */
00090   assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
00091   
00092   /* Unlock program memory */
00093   if(FLASH_MemType == FLASH_MEMTYPE_PROG)
00094   {
00095     FLASH->PUKR = FLASH_RASS_KEY1;
00096     FLASH->PUKR = FLASH_RASS_KEY2;
00097   }
00098   /* Unlock data memory */
00099   else
00100   {
00101     FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on data memory !!! */
00102     FLASH->DUKR = FLASH_RASS_KEY1;
00103   }
00104 }
00105 
00106 /**
00107   * @brief  Locks the program or data EEPROM memory
00108   * @param  FLASH_MemType : Memory type
00109   *         This parameter can be a value of @ref FLASH_MemType_TypeDef
00110   * @retval None
00111   */
00112 void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
00113 {
00114   /* Check parameter */
00115   assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
00116   
00117   /* Lock memory */
00118   FLASH->IAPSR &= (uint8_t)FLASH_MemType;
00119 }
00120 
00121 /**
00122   * @brief  DeInitializes the FLASH registers to their default reset values.
00123   * @param  None
00124   * @retval None
00125   */
00126 void FLASH_DeInit(void)
00127 {
00128   FLASH->CR1 = FLASH_CR1_RESET_VALUE;
00129   FLASH->CR2 = FLASH_CR2_RESET_VALUE;
00130   FLASH->NCR2 = FLASH_NCR2_RESET_VALUE;
00131   FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_DUL);
00132   FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_PUL);
00133   (void) FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
00134 }
00135 
00136 /**
00137   * @brief  Enables or Disables the Flash interrupt mode
00138   * @param  NewState : The new state of the flash interrupt mode
00139   *         This parameter can be a value of @ref FunctionalState enumeration.
00140   * @retval None
00141   */
00142 void FLASH_ITConfig(FunctionalState NewState)
00143 {
00144   /* Check parameter */
00145   assert_param(IS_FUNCTIONALSTATE_OK(NewState));
00146   
00147   if(NewState != DISABLE)
00148   {
00149     FLASH->CR1 |= FLASH_CR1_IE; /* Enables the interrupt sources */
00150   }
00151   else
00152   {
00153     FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE); /* Disables the interrupt sources */
00154   }
00155 }
00156 
00157 /**
00158   * @brief  Erases one byte in the program or data EEPROM memory
00159   * @note   PointerAttr define is declared in the stm8s.h file to select if 
00160   *         the pointer will be declared as near (2 bytes) or far (3 bytes).
00161   * @param  Address : Address of the byte to erase
00162   * @retval None
00163   */
00164 void FLASH_EraseByte(uint32_t Address)
00165 {
00166   /* Check parameter */
00167   assert_param(IS_FLASH_ADDRESS_OK(Address));
00168   
00169   /* Erase byte */
00170   *(PointerAttr uint8_t*) (MemoryAddressCast)Address = FLASH_CLEAR_BYTE; 
00171 }
00172 
00173 /**
00174   * @brief  Programs one byte in program or data EEPROM memory
00175   * @note   PointerAttr define is declared in the stm8s.h file to select if 
00176   *         the pointer will be declared as near (2 bytes) or far (3 bytes).
00177   * @param  Address : Address where the byte will be programmed
00178   * @param  Data : Value to be programmed
00179   * @retval None
00180   */
00181 void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
00182 {
00183   /* Check parameters */
00184   assert_param(IS_FLASH_ADDRESS_OK(Address));
00185   *(PointerAttr uint8_t*) (MemoryAddressCast)Address = Data;
00186 }
00187 
00188 /**
00189   * @brief  Reads any byte from flash memory
00190   * @note   PointerAttr define is declared in the stm8s.h file to select if 
00191   *         the pointer will be declared as near (2 bytes) or far (3 bytes).
00192   * @param  Address : Address to read
00193   * @retval Value of the byte
00194   */
00195 uint8_t FLASH_ReadByte(uint32_t Address)
00196 {
00197   /* Check parameter */
00198   assert_param(IS_FLASH_ADDRESS_OK(Address));
00199   
00200   /* Read byte */
00201   return(*(PointerAttr uint8_t *) (MemoryAddressCast)Address); 
00202 }
00203 
00204 /**
00205   * @brief  Programs one word (4 bytes) in program or data EEPROM memory
00206   * @note   PointerAttr define is declared in the stm8s.h file to select if 
00207   *         the pointer will be declared as near (2 bytes) or far (3 bytes).
00208   * @param  Address : The address where the data will be programmed
00209   * @param  Data : Value to be programmed
00210   * @retval None
00211   */
00212 void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
00213 {
00214   /* Check parameters */
00215   assert_param(IS_FLASH_ADDRESS_OK(Address));
00216   
00217   /* Enable Word Write Once */
00218   FLASH->CR2 |= FLASH_CR2_WPRG;
00219   FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NWPRG);
00220   
00221   /* Write one byte - from lowest address*/
00222   *((PointerAttr uint8_t*)(MemoryAddressCast)Address)       = *((uint8_t*)(&Data));
00223   /* Write one byte*/
00224   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 1) = *((uint8_t*)(&Data)+1); 
00225   /* Write one byte*/    
00226   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 2) = *((uint8_t*)(&Data)+2); 
00227   /* Write one byte - from higher address*/
00228   *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 3) = *((uint8_t*)(&Data)+3); 
00229 }
00230 
00231 /**
00232   * @brief  Programs option byte
00233   * @param  Address : option byte address to program
00234   * @param  Data : Value to write
00235   * @retval None
00236   */
00237 void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
00238 {
00239   /* Check parameter */
00240   assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
00241   
00242   /* Enable write access to option bytes */
00243   FLASH->CR2 |= FLASH_CR2_OPT;
00244   FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
00245   
00246   /* check if the option byte to program is ROP*/
00247   if(Address == 0x4800)
00248   {
00249     /* Program option byte*/
00250     *((NEAR uint8_t*)Address) = Data;
00251   }
00252   else
00253   {
00254     /* Program option byte and his complement */
00255     *((NEAR uint8_t*)Address) = Data;
00256     *((NEAR uint8_t*)((uint16_t)(Address + 1))) = (uint8_t)(~Data);
00257   }
00258   FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
00259   
00260   /* Disable write access to option bytes */
00261   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
00262   FLASH->NCR2 |= FLASH_NCR2_NOPT;
00263 }
00264 
00265 /**
00266   * @brief  Erases option byte
00267   * @param  Address : Option byte address to erase
00268   * @retval None
00269   */
00270 void FLASH_EraseOptionByte(uint16_t Address)
00271 {
00272   /* Check parameter */
00273   assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
00274   
00275   /* Enable write access to option bytes */
00276   FLASH->CR2 |= FLASH_CR2_OPT;
00277   FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
00278   
00279   /* check if the option byte to erase is ROP */
00280   if(Address == 0x4800)
00281   {
00282     /* Erase option byte */
00283     *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
00284   }
00285   else
00286   {
00287     /* Erase option byte and his complement */
00288     *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
00289     *((NEAR uint8_t*)((uint16_t)(Address + (uint16_t)1 ))) = FLASH_SET_BYTE;
00290   }
00291   FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
00292   
00293   /* Disable write access to option bytes */
00294   FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
00295   FLASH->NCR2 |= FLASH_NCR2_NOPT;
00296 }
00297 
00298 /**
00299   * @brief  Reads one option byte
00300   * @param  Address  option byte address to read.
00301   * @retval Option byte read value + its complement
00302   */
00303 uint16_t FLASH_ReadOptionByte(uint16_t Address)
00304 {
00305   uint8_t value_optbyte, value_optbyte_complement = 0;
00306   uint16_t res_value = 0;
00307   
00308   /* Check parameter */
00309   assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
00310     
00311   value_optbyte = *((NEAR uint8_t*)Address); /* Read option byte */
00312   value_optbyte_complement = *(((NEAR uint8_t*)Address) + 1); /* Read option byte complement */
00313   
00314   /* Read-out protection option byte */
00315   if(Address == 0x4800)  
00316   {
00317     res_value =  value_optbyte;
00318   }
00319   else
00320   {
00321     if(value_optbyte == (uint8_t)(~value_optbyte_complement))
00322     {
00323       res_value = (uint16_t)((uint16_t)value_optbyte << 8);
00324       res_value = res_value | (uint16_t)value_optbyte_complement;
00325     }
00326     else
00327     {
00328       res_value = FLASH_OPTIONBYTE_ERROR;
00329     }
00330   }
00331   return(res_value);
00332 }
00333 
00334 /**
00335   * @brief  Select the Flash behaviour in low power mode
00336   * @param  FLASH_LPMode Low power mode selection
00337   *         This parameter can be any of the @ref FLASH_LPMode_TypeDef values.
00338   * @retval None
00339   */
00340 void FLASH_SetLowPowerMode(FLASH_LPMode_TypeDef FLASH_LPMode)
00341 {
00342   /* Check parameter */
00343   assert_param(IS_FLASH_LOW_POWER_MODE_OK(FLASH_LPMode));
00344   
00345   /* Clears the two bits */
00346   FLASH->CR1 &= (uint8_t)(~(FLASH_CR1_HALT | FLASH_CR1_AHALT)); 
00347   
00348   /* Sets the new mode */
00349   FLASH->CR1 |= (uint8_t)FLASH_LPMode; 
00350 }
00351 
00352 /**
00353   * @brief  Sets the fixed programming time
00354   * @param  FLASH_ProgTime Indicates the programming time to be fixed
00355   *         This parameter can be any of the @ref FLASH_ProgramTime_TypeDef values.
00356   * @retval None
00357   */
00358 void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
00359 {
00360   /* Check parameter */
00361   assert_param(IS_FLASH_PROGRAM_TIME_OK(FLASH_ProgTime));
00362   
00363   FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
00364   FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
00365 }
00366 
00367 /**
00368   * @brief  Returns the Flash behaviour type in low power mode
00369   * @param  None
00370   * @retval FLASH_LPMode_TypeDef Flash behaviour type in low power mode
00371   */
00372 FLASH_LPMode_TypeDef FLASH_GetLowPowerMode(void)
00373 {
00374   return((FLASH_LPMode_TypeDef)(FLASH->CR1 & (uint8_t)(FLASH_CR1_HALT | FLASH_CR1_AHALT)));
00375 }
00376 
00377 /**
00378   * @brief  Returns the fixed programming time
00379   * @param  None
00380   * @retval FLASH_ProgramTime_TypeDef Fixed programming time value
00381   */
00382 FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
00383 {
00384   return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
00385 }
00386 
00387 /**
00388   * @brief  Returns the Boot memory size in bytes
00389   * @param  None
00390   * @retval Boot memory size in bytes
00391   */
00392 uint32_t FLASH_GetBootSize(void)
00393 {
00394   uint32_t temp = 0;
00395   
00396   /* Calculates the number of bytes */
00397   temp = (uint32_t)((uint32_t)FLASH->FPR * (uint32_t)512);
00398   
00399   /* Correction because size of 127.5 kb doesn't exist */
00400   if(FLASH->FPR == 0xFF)
00401   {
00402     temp += 512;
00403   }
00404   
00405   /* Return value */
00406   return(temp);
00407 }
00408 
00409 /**
00410   * @brief  Checks whether the specified SPI flag is set or not.
00411   * @param  FLASH_FLAG : Specifies the flag to check.
00412   *         This parameter can be any of the @ref FLASH_Flag_TypeDef enumeration.
00413   * @retval FlagStatus : Indicates the state of FLASH_FLAG.
00414   *         This parameter can be any of the @ref FlagStatus enumeration.
00415   * @note   This function can clear the EOP, WR_PG_DIS flags in the IAPSR register.
00416   */
00417 FlagStatus FLASH_GetFlagStatus(FLASH_Flag_TypeDef FLASH_FLAG)
00418 {
00419   FlagStatus status = RESET;
00420   /* Check parameters */
00421   assert_param(IS_FLASH_FLAGS_OK(FLASH_FLAG));
00422   
00423   /* Check the status of the specified FLASH flag */
00424   if((FLASH->IAPSR & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
00425   {
00426     status = SET; /* FLASH_FLAG is set */
00427   }
00428   else
00429   {
00430     status = RESET; /* FLASH_FLAG is reset*/
00431   }
00432   
00433   /* Return the FLASH_FLAG status */
00434   return status;
00435 }
00436 
00437 /**
00438 @code
00439  All the functions defined below must be executed from RAM exclusively, except
00440  for the FLASH_WaitForLastOperation function which can be executed from Flash.
00441 
00442  Steps of the execution from RAM differs from one toolchain to another:
00443  - For Cosmic Compiler:
00444     1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
00445     This segment is defined in the stm8s_flash.c file.
00446   2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8s.h file,
00447     or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
00448    definition.
00449   3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
00450     add the FLASH_CODE segment with "-ic" options.
00451   4- In main.c file call the _fctcpy() function with first segment character as 
00452     parameter "_fctcpy('F');" to load the declared moveable code segment
00453     (FLASH_CODE) in RAM before execution.
00454   5- By default the _fctcpy function is packaged in the Cosmic machine library,
00455     so the function prototype "int _fctcopy(char name);" must be added in main.c
00456     file.
00457 
00458   - For Raisonance Compiler
00459    1- Use the inram keyword in the function declaration to specify that it can be
00460     executed from RAM.
00461     This is done within the stm8s_flash.c file, and it's conditioned by 
00462     RAM_EXECUTION definition.
00463    2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8s.h file, or 
00464    define it in Raisonance compiler preprocessor to enable the access for the 
00465    inram functions.
00466    3- An inram function code is copied from Flash to RAM by the C startup code. 
00467    In some applications, the RAM area where the code was initially stored may be
00468    erased or corrupted, so it may be desirable to perform the copy again. 
00469    Depending on the application memory model, the memcpy() or fmemcpy() functions
00470    should be used to perform the copy.
00471       � In case your project uses the SMALL memory model (code smaller than 64K),
00472        memcpy()function is recommended to perform the copy
00473       � In case your project uses the LARGE memory model, functions can be 
00474       everywhenre in the 24-bits address space (not limited to the first 64KB of
00475       code), In this case, the use of memcpy() function will not be appropriate,
00476       you need to use the specific fmemcpy() function (which copies objects with
00477       24-bit addresses).
00478       - The linker automatically defines 2 symbols for each inram function:
00479            � __address__functionname is a symbol that holds the Flash address 
00480            where the given function code is stored.
00481            � __size__functionname is a symbol that holds the function size in bytes.
00482      And we already have the function address (which is itself a pointer)
00483   4- In main.c file these two steps should be performed for each inram function:
00484      � Import the "__address__functionname" and "__size__functionname" symbols
00485        as global variables:
00486          extern int __address__functionname; // Symbol holding the flash address
00487          extern int __size__functionname;    // Symbol holding the function size
00488      � In case of SMALL memory model use, Call the memcpy() function to copy the
00489       inram function to the RAM destination address:
00490                 memcpy(functionname, // RAM destination address
00491                       (void*)&__address__functionname, // Flash source address
00492                       (int)&__size__functionname); // Code size of the function
00493      � In case of LARGE memory model use, call the fmemcpy() function to copy 
00494      the inram function to the RAM destination address:
00495                  memcpy(functionname, // RAM destination address
00496                       (void @far*)&__address__functionname, // Flash source address
00497                       (int)&__size__functionname); // Code size of the function
00498 
00499  - For IAR Compiler:
00500     1- Use the __ramfunc keyword in the function declaration to specify that it 
00501     can be executed from RAM.
00502     This is done within the stm8s_flash.c file, and it's conditioned by 
00503     RAM_EXECUTION definition.
00504     2- Uncomment the "#define RAM_EXECUTION  (1)" line in the stm8s.h file, or 
00505    define it in IAR compiler preprocessor to enable the access for the 
00506    __ramfunc functions.
00507 
00508  - Note: 
00509     1- Ignore the IAR compiler warnings, these warnings don't impact the FLASH Program/Erase
00510     operations.
00511     The code performing the Flash Program/erase must be executed from RAM; the variables
00512     initializations don't necessary require the execution from RAM, only CR2/NCR2 registers 
00513     configuration and data programing must be executed from RAM.
00514     2- These warnings depends on IAR compiler: as the code generation is made using many
00515     runtime library functions to keep code size to a minimum.
00516     3- It is recommended to use High Speed Optimization with IAR (-Ohs), in order 
00517     to reduce the runtime library calls in the generated code.
00518 
00519 
00520 
00521  The FLASH examples given within the STM8S_StdPeriph_Lib package, details all 
00522  the steps described above.
00523 
00524 @endcode
00525 */
00526 
00527 /**
00528   * @brief
00529   *******************************************************************************
00530   *                         Execution from RAM enable
00531   *******************************************************************************
00532   *
00533   * To enable execution from RAM you can either uncomment the following define 
00534   * in the stm8s.h file or define it in your toolchain compiler preprocessor
00535   * - #define RAM_EXECUTION  (1) 
00536   */
00537   
00538 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
00539  #pragma section (FLASH_CODE)
00540 #endif  /* _COSMIC_ && RAM_EXECUTION */
00541 /**
00542   * @brief  Wait for a Flash operation to complete.
00543   * @note   The call and execution of this function must be done from RAM in case
00544   *         of Block operation.
00545   * @param  FLASH_MemType : Memory type
00546   *         This parameter can be a value of @ref FLASH_MemType_TypeDef
00547   * @retval FLASH status
00548   */
00549 IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType)) 
00550 {
00551   uint8_t flagstatus = 0x00;
00552   uint16_t timeout = OPERATION_TIMEOUT;
00553   
00554   /* Wait until operation completion or write protection page occurred */
00555 #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
00556   defined(STM8S005) || defined(STM8AF52Ax) || defined(STM8AF62Ax) || defined(STM8AF626x)  
00557     if(FLASH_MemType == FLASH_MEMTYPE_PROG)
00558     {
00559       while((flagstatus == 0x00) && (timeout != 0x00))
00560       {
00561         flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_EOP |
00562                                                         FLASH_IAPSR_WR_PG_DIS));
00563         timeout--;
00564       }
00565     }
00566     else
00567     {
00568       while((flagstatus == 0x00) && (timeout != 0x00))
00569       {
00570         flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_HVOFF |
00571                                                         FLASH_IAPSR_WR_PG_DIS));
00572         timeout--;
00573       }
00574     }
00575 #else /*STM8S103, STM8S903, STM8AF622x */
00576   while((flagstatus == 0x00) && (timeout != 0x00))
00577   {
00578     flagstatus = (uint8_t)(FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS));
00579     timeout--;
00580   }
00581 #endif /* STM8S208, STM8S207, STM8S105, STM8AF52Ax, STM8AF62Ax, STM8AF262x */
00582   
00583   if(timeout == 0x00 )
00584   {
00585     flagstatus = FLASH_STATUS_TIMEOUT;
00586   }
00587   
00588   return((FLASH_Status_TypeDef)flagstatus);
00589 }
00590 
00591 /**
00592   * @brief  Erases a block in the program or data memory.
00593   * @note   This function should be executed from RAM.
00594   * @param  FLASH_MemType :  The type of memory to erase
00595   * @param  BlockNum : Indicates the block number to erase
00596   * @retval None.
00597   */
00598 IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
00599 {
00600   uint32_t startaddress = 0;
00601   
00602 #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
00603   defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
00604     uint32_t PointerAttr  *pwFlash;
00605 #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax) 
00606   uint8_t PointerAttr  *pwFlash;
00607 #endif
00608   
00609   /* Check parameters */
00610   assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
00611   if(FLASH_MemType == FLASH_MEMTYPE_PROG)
00612   {
00613     assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
00614     startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
00615   }
00616   else
00617   {
00618     assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
00619     startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
00620   }
00621   
00622   /* Point to the first block address */
00623 #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
00624   pwFlash = (PointerAttr uint8_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
00625 #elif defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
00626   defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
00627     pwFlash = (PointerAttr uint32_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
00628 #endif  /* STM8S208, STM8S207 */
00629   
00630   /* Enable erase block mode */
00631   FLASH->CR2 |= FLASH_CR2_ERASE;
00632   FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NERASE);
00633   
00634 #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) ||  \
00635   defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
00636     *pwFlash = (uint32_t)0;
00637 #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || \
00638   defined (STM8AF52Ax)
00639     *pwFlash = (uint8_t)0;
00640   *(pwFlash + 1) = (uint8_t)0;
00641   *(pwFlash + 2) = (uint8_t)0;
00642   *(pwFlash + 3) = (uint8_t)0;    
00643 #endif
00644 }
00645 
00646 /**
00647   * @brief  Programs a memory block
00648   * @note   This function should be executed from RAM.
00649   * @param  FLASH_MemType : The type of memory to program
00650   * @param  BlockNum : The block number
00651   * @param  FLASH_ProgMode : The programming mode.
00652   * @param  Buffer : Pointer to buffer containing source data.
00653   * @retval None.
00654   */
00655 IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType, 
00656                         FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer))
00657 {
00658   uint16_t Count = 0;
00659   uint32_t startaddress = 0;
00660   
00661   /* Check parameters */
00662   assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
00663   assert_param(IS_FLASH_PROGRAM_MODE_OK(FLASH_ProgMode));
00664   if(FLASH_MemType == FLASH_MEMTYPE_PROG)
00665   {
00666     assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
00667     startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
00668   }
00669   else
00670   {
00671     assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
00672     startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
00673   }
00674   
00675   /* Point to the first block address */
00676   startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE);
00677   
00678   /* Selection of Standard or Fast programming mode */
00679   if(FLASH_ProgMode == FLASH_PROGRAMMODE_STANDARD)
00680   {
00681     /* Standard programming mode */ /*No need in standard mode */
00682     FLASH->CR2 |= FLASH_CR2_PRG;
00683     FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NPRG);
00684   }
00685   else
00686   {
00687     /* Fast programming mode */
00688     FLASH->CR2 |= FLASH_CR2_FPRG;
00689     FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NFPRG);
00690   }
00691   
00692   /* Copy data bytes from RAM to FLASH memory */
00693   for(Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
00694   {
00695     *((PointerAttr uint8_t*) (MemoryAddressCast)startaddress + Count) = ((uint8_t)(Buffer[Count]));
00696   }
00697 }
00698 
00699 #if defined (_COSMIC_) && defined (RAM_EXECUTION)
00700  /* End of FLASH_CODE section */
00701  #pragma section ()
00702 #endif /* _COSMIC_ && RAM_EXECUTION */
00703 
00704 
00705 /**
00706   * @}
00707   */
00708   
00709 /**
00710   * @}
00711   */
00712   
00713 
00714 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
STM8 Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM8 8-bit Microcontrollers platform visit www.st.com