stm8s_flash.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. /**
  2. ******************************************************************************
  3. * @file stm8s_flash.c
  4. * @author MCD Application Team
  5. * @version V2.2.0
  6. * @date 30-September-2014
  7. * @brief This file contains all the functions for the FLASH peripheral.
  8. ******************************************************************************
  9. * @attention
  10. *
  11. * <h2><center>&copy; COPYRIGHT 2014 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. /* Includes ------------------------------------------------------------------*/
  28. #include "stm8s_flash.h"
  29. /** @addtogroup STM8S_StdPeriph_Driver
  30. * @{
  31. */
  32. /**
  33. @code
  34. This driver provides functions to configure and program the Flash memory of all
  35. STM8S devices.
  36. It includes as well functions that can be either executed from RAM or not, and
  37. other functions that must be executed from RAM otherwise useless.
  38. The table below lists the functions that can be executed from RAM.
  39. +--------------------------------------------------------------------------------|
  40. | Functions prototypes | RAM execution | Comments |
  41. ---------------------------------------------------------------------------------|
  42. | | Mandatory in case of block | Can be executed |
  43. | FLASH_WaitForLastOperation | Operation: | from Flash in case |
  44. | | - Block programming | of byte and word |
  45. | | - Block erase | Operations |
  46. |--------------------------------------------------------------------------------|
  47. | FLASH_ProgramBlock | Exclusively | useless from Flash |
  48. |--------------------------------------------------------------------------------|
  49. | FLASH_EraseBlock | Exclusively | useless from Flash |
  50. |--------------------------------------------------------------------------------|
  51. To be able to execute functions from RAM several steps have to be followed.
  52. These steps may differ from one toolchain to another.
  53. A detailed description is available below within this driver.
  54. You can also refer to the FLASH examples provided within the
  55. STM8S_StdPeriph_Lib package.
  56. @endcode
  57. */
  58. /* Private typedef -----------------------------------------------------------*/
  59. /* Private define ------------------------------------------------------------*/
  60. #define FLASH_CLEAR_BYTE ((uint8_t)0x00)
  61. #define FLASH_SET_BYTE ((uint8_t)0xFF)
  62. #define OPERATION_TIMEOUT ((uint16_t)0xFFFF)
  63. /* Private macro -------------------------------------------------------------*/
  64. /* Private variables ---------------------------------------------------------*/
  65. /* Private function prototypes -----------------------------------------------*/
  66. /* Private Constants ---------------------------------------------------------*/
  67. /** @addtogroup FLASH_Public_functions
  68. * @{
  69. */
  70. /**
  71. * @brief Unlocks the program or data EEPROM memory
  72. * @param FLASH_MemType : Memory type to unlock
  73. * This parameter can be a value of @ref FLASH_MemType_TypeDef
  74. * @retval None
  75. */
  76. void FLASH_Unlock(FLASH_MemType_TypeDef FLASH_MemType)
  77. {
  78. /* Check parameter */
  79. assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
  80. /* Unlock program memory */
  81. if(FLASH_MemType == FLASH_MEMTYPE_PROG)
  82. {
  83. FLASH->PUKR = FLASH_RASS_KEY1;
  84. FLASH->PUKR = FLASH_RASS_KEY2;
  85. }
  86. /* Unlock data memory */
  87. else
  88. {
  89. FLASH->DUKR = FLASH_RASS_KEY2; /* Warning: keys are reversed on data memory !!! */
  90. FLASH->DUKR = FLASH_RASS_KEY1;
  91. }
  92. }
  93. /**
  94. * @brief Locks the program or data EEPROM memory
  95. * @param FLASH_MemType : Memory type
  96. * This parameter can be a value of @ref FLASH_MemType_TypeDef
  97. * @retval None
  98. */
  99. void FLASH_Lock(FLASH_MemType_TypeDef FLASH_MemType)
  100. {
  101. /* Check parameter */
  102. assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
  103. /* Lock memory */
  104. FLASH->IAPSR &= (uint8_t)FLASH_MemType;
  105. }
  106. /**
  107. * @brief DeInitializes the FLASH registers to their default reset values.
  108. * @param None
  109. * @retval None
  110. */
  111. void FLASH_DeInit(void)
  112. {
  113. FLASH->CR1 = FLASH_CR1_RESET_VALUE;
  114. FLASH->CR2 = FLASH_CR2_RESET_VALUE;
  115. FLASH->NCR2 = FLASH_NCR2_RESET_VALUE;
  116. FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_DUL);
  117. FLASH->IAPSR &= (uint8_t)(~FLASH_IAPSR_PUL);
  118. (void) FLASH->IAPSR; /* Reading of this register causes the clearing of status flags */
  119. }
  120. /**
  121. * @brief Enables or Disables the Flash interrupt mode
  122. * @param NewState : The new state of the flash interrupt mode
  123. * This parameter can be a value of @ref FunctionalState enumeration.
  124. * @retval None
  125. */
  126. void FLASH_ITConfig(FunctionalState NewState)
  127. {
  128. /* Check parameter */
  129. assert_param(IS_FUNCTIONALSTATE_OK(NewState));
  130. if(NewState != DISABLE)
  131. {
  132. FLASH->CR1 |= FLASH_CR1_IE; /* Enables the interrupt sources */
  133. }
  134. else
  135. {
  136. FLASH->CR1 &= (uint8_t)(~FLASH_CR1_IE); /* Disables the interrupt sources */
  137. }
  138. }
  139. /**
  140. * @brief Erases one byte in the program or data EEPROM memory
  141. * @note PointerAttr define is declared in the stm8s.h file to select if
  142. * the pointer will be declared as near (2 bytes) or far (3 bytes).
  143. * @param Address : Address of the byte to erase
  144. * @retval None
  145. */
  146. void FLASH_EraseByte(uint32_t Address)
  147. {
  148. /* Check parameter */
  149. assert_param(IS_FLASH_ADDRESS_OK(Address));
  150. /* Erase byte */
  151. *(PointerAttr uint8_t*) (MemoryAddressCast)Address = FLASH_CLEAR_BYTE;
  152. }
  153. /**
  154. * @brief Programs one byte in program or data EEPROM memory
  155. * @note PointerAttr define is declared in the stm8s.h file to select if
  156. * the pointer will be declared as near (2 bytes) or far (3 bytes).
  157. * @param Address : Address where the byte will be programmed
  158. * @param Data : Value to be programmed
  159. * @retval None
  160. */
  161. void FLASH_ProgramByte(uint32_t Address, uint8_t Data)
  162. {
  163. /* Check parameters */
  164. assert_param(IS_FLASH_ADDRESS_OK(Address));
  165. *(PointerAttr uint8_t*) (MemoryAddressCast)Address = Data;
  166. }
  167. /**
  168. * @brief Reads any byte from flash memory
  169. * @note PointerAttr define is declared in the stm8s.h file to select if
  170. * the pointer will be declared as near (2 bytes) or far (3 bytes).
  171. * @param Address : Address to read
  172. * @retval Value of the byte
  173. */
  174. uint8_t FLASH_ReadByte(uint32_t Address)
  175. {
  176. /* Check parameter */
  177. assert_param(IS_FLASH_ADDRESS_OK(Address));
  178. /* Read byte */
  179. return(*(PointerAttr uint8_t *) (MemoryAddressCast)Address);
  180. }
  181. /**
  182. * @brief Programs one word (4 bytes) in program or data EEPROM memory
  183. * @note PointerAttr define is declared in the stm8s.h file to select if
  184. * the pointer will be declared as near (2 bytes) or far (3 bytes).
  185. * @param Address : The address where the data will be programmed
  186. * @param Data : Value to be programmed
  187. * @retval None
  188. */
  189. void FLASH_ProgramWord(uint32_t Address, uint32_t Data)
  190. {
  191. /* Check parameters */
  192. assert_param(IS_FLASH_ADDRESS_OK(Address));
  193. /* Enable Word Write Once */
  194. FLASH->CR2 |= FLASH_CR2_WPRG;
  195. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NWPRG);
  196. /* Write one byte - from lowest address*/
  197. *((PointerAttr uint8_t*)(MemoryAddressCast)Address) = *((uint8_t*)(&Data));
  198. /* Write one byte*/
  199. *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 1) = *((uint8_t*)(&Data)+1);
  200. /* Write one byte*/
  201. *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 2) = *((uint8_t*)(&Data)+2);
  202. /* Write one byte - from higher address*/
  203. *(((PointerAttr uint8_t*)(MemoryAddressCast)Address) + 3) = *((uint8_t*)(&Data)+3);
  204. }
  205. /**
  206. * @brief Programs option byte
  207. * @param Address : option byte address to program
  208. * @param Data : Value to write
  209. * @retval None
  210. */
  211. void FLASH_ProgramOptionByte(uint16_t Address, uint8_t Data)
  212. {
  213. /* Check parameter */
  214. assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
  215. /* Enable write access to option bytes */
  216. FLASH->CR2 |= FLASH_CR2_OPT;
  217. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
  218. /* check if the option byte to program is ROP*/
  219. if(Address == 0x4800)
  220. {
  221. /* Program option byte*/
  222. *((NEAR uint8_t*)Address) = Data;
  223. }
  224. else
  225. {
  226. /* Program option byte and his complement */
  227. *((NEAR uint8_t*)Address) = Data;
  228. *((NEAR uint8_t*)((uint16_t)(Address + 1))) = (uint8_t)(~Data);
  229. }
  230. FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
  231. /* Disable write access to option bytes */
  232. FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
  233. FLASH->NCR2 |= FLASH_NCR2_NOPT;
  234. }
  235. /**
  236. * @brief Erases option byte
  237. * @param Address : Option byte address to erase
  238. * @retval None
  239. */
  240. void FLASH_EraseOptionByte(uint16_t Address)
  241. {
  242. /* Check parameter */
  243. assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
  244. /* Enable write access to option bytes */
  245. FLASH->CR2 |= FLASH_CR2_OPT;
  246. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NOPT);
  247. /* check if the option byte to erase is ROP */
  248. if(Address == 0x4800)
  249. {
  250. /* Erase option byte */
  251. *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
  252. }
  253. else
  254. {
  255. /* Erase option byte and his complement */
  256. *((NEAR uint8_t*)Address) = FLASH_CLEAR_BYTE;
  257. *((NEAR uint8_t*)((uint16_t)(Address + (uint16_t)1 ))) = FLASH_SET_BYTE;
  258. }
  259. FLASH_WaitForLastOperation(FLASH_MEMTYPE_PROG);
  260. /* Disable write access to option bytes */
  261. FLASH->CR2 &= (uint8_t)(~FLASH_CR2_OPT);
  262. FLASH->NCR2 |= FLASH_NCR2_NOPT;
  263. }
  264. /**
  265. * @brief Reads one option byte
  266. * @param Address option byte address to read.
  267. * @retval Option byte read value + its complement
  268. */
  269. uint16_t FLASH_ReadOptionByte(uint16_t Address)
  270. {
  271. uint8_t value_optbyte, value_optbyte_complement = 0;
  272. uint16_t res_value = 0;
  273. /* Check parameter */
  274. assert_param(IS_OPTION_BYTE_ADDRESS_OK(Address));
  275. value_optbyte = *((NEAR uint8_t*)Address); /* Read option byte */
  276. value_optbyte_complement = *(((NEAR uint8_t*)Address) + 1); /* Read option byte complement */
  277. /* Read-out protection option byte */
  278. if(Address == 0x4800)
  279. {
  280. res_value = value_optbyte;
  281. }
  282. else
  283. {
  284. if(value_optbyte == (uint8_t)(~value_optbyte_complement))
  285. {
  286. res_value = (uint16_t)((uint16_t)value_optbyte << 8);
  287. res_value = res_value | (uint16_t)value_optbyte_complement;
  288. }
  289. else
  290. {
  291. res_value = FLASH_OPTIONBYTE_ERROR;
  292. }
  293. }
  294. return(res_value);
  295. }
  296. /**
  297. * @brief Select the Flash behaviour in low power mode
  298. * @param FLASH_LPMode Low power mode selection
  299. * This parameter can be any of the @ref FLASH_LPMode_TypeDef values.
  300. * @retval None
  301. */
  302. void FLASH_SetLowPowerMode(FLASH_LPMode_TypeDef FLASH_LPMode)
  303. {
  304. /* Check parameter */
  305. assert_param(IS_FLASH_LOW_POWER_MODE_OK(FLASH_LPMode));
  306. /* Clears the two bits */
  307. FLASH->CR1 &= (uint8_t)(~(FLASH_CR1_HALT | FLASH_CR1_AHALT));
  308. /* Sets the new mode */
  309. FLASH->CR1 |= (uint8_t)FLASH_LPMode;
  310. }
  311. /**
  312. * @brief Sets the fixed programming time
  313. * @param FLASH_ProgTime Indicates the programming time to be fixed
  314. * This parameter can be any of the @ref FLASH_ProgramTime_TypeDef values.
  315. * @retval None
  316. */
  317. void FLASH_SetProgrammingTime(FLASH_ProgramTime_TypeDef FLASH_ProgTime)
  318. {
  319. /* Check parameter */
  320. assert_param(IS_FLASH_PROGRAM_TIME_OK(FLASH_ProgTime));
  321. FLASH->CR1 &= (uint8_t)(~FLASH_CR1_FIX);
  322. FLASH->CR1 |= (uint8_t)FLASH_ProgTime;
  323. }
  324. /**
  325. * @brief Returns the Flash behaviour type in low power mode
  326. * @param None
  327. * @retval FLASH_LPMode_TypeDef Flash behaviour type in low power mode
  328. */
  329. FLASH_LPMode_TypeDef FLASH_GetLowPowerMode(void)
  330. {
  331. return((FLASH_LPMode_TypeDef)(FLASH->CR1 & (uint8_t)(FLASH_CR1_HALT | FLASH_CR1_AHALT)));
  332. }
  333. /**
  334. * @brief Returns the fixed programming time
  335. * @param None
  336. * @retval FLASH_ProgramTime_TypeDef Fixed programming time value
  337. */
  338. FLASH_ProgramTime_TypeDef FLASH_GetProgrammingTime(void)
  339. {
  340. return((FLASH_ProgramTime_TypeDef)(FLASH->CR1 & FLASH_CR1_FIX));
  341. }
  342. /**
  343. * @brief Returns the Boot memory size in bytes
  344. * @param None
  345. * @retval Boot memory size in bytes
  346. */
  347. uint32_t FLASH_GetBootSize(void)
  348. {
  349. uint32_t temp = 0;
  350. /* Calculates the number of bytes */
  351. temp = (uint32_t)((uint32_t)FLASH->FPR * (uint32_t)512);
  352. /* Correction because size of 127.5 kb doesn't exist */
  353. if(FLASH->FPR == 0xFF)
  354. {
  355. temp += 512;
  356. }
  357. /* Return value */
  358. return(temp);
  359. }
  360. /**
  361. * @brief Checks whether the specified SPI flag is set or not.
  362. * @param FLASH_FLAG : Specifies the flag to check.
  363. * This parameter can be any of the @ref FLASH_Flag_TypeDef enumeration.
  364. * @retval FlagStatus : Indicates the state of FLASH_FLAG.
  365. * This parameter can be any of the @ref FlagStatus enumeration.
  366. * @note This function can clear the EOP, WR_PG_DIS flags in the IAPSR register.
  367. */
  368. FlagStatus FLASH_GetFlagStatus(FLASH_Flag_TypeDef FLASH_FLAG)
  369. {
  370. FlagStatus status = RESET;
  371. /* Check parameters */
  372. assert_param(IS_FLASH_FLAGS_OK(FLASH_FLAG));
  373. /* Check the status of the specified FLASH flag */
  374. if((FLASH->IAPSR & (uint8_t)FLASH_FLAG) != (uint8_t)RESET)
  375. {
  376. status = SET; /* FLASH_FLAG is set */
  377. }
  378. else
  379. {
  380. status = RESET; /* FLASH_FLAG is reset*/
  381. }
  382. /* Return the FLASH_FLAG status */
  383. return status;
  384. }
  385. /**
  386. @code
  387. All the functions defined below must be executed from RAM exclusively, except
  388. for the FLASH_WaitForLastOperation function which can be executed from Flash.
  389. Steps of the execution from RAM differs from one toolchain to another:
  390. - For Cosmic Compiler:
  391. 1- Define a segment FLASH_CODE by the mean of " #pragma section (FLASH_CODE)".
  392. This segment is defined in the stm8s_flash.c file.
  393. 2- Uncomment the "#define RAM_EXECUTION (1)" line in the stm8s.h file,
  394. or define it in Cosmic compiler preprocessor to enable the FLASH_CODE segment
  395. definition.
  396. 3- In STVD Select Project\Settings\Linker\Category "input" and in the RAM section
  397. add the FLASH_CODE segment with "-ic" options.
  398. 4- In main.c file call the _fctcpy() function with first segment character as
  399. parameter "_fctcpy('F');" to load the declared moveable code segment
  400. (FLASH_CODE) in RAM before execution.
  401. 5- By default the _fctcpy function is packaged in the Cosmic machine library,
  402. so the function prototype "int _fctcopy(char name);" must be added in main.c
  403. file.
  404. - For Raisonance Compiler
  405. 1- Use the inram keyword in the function declaration to specify that it can be
  406. executed from RAM.
  407. This is done within the stm8s_flash.c file, and it's conditioned by
  408. RAM_EXECUTION definition.
  409. 2- Uncomment the "#define RAM_EXECUTION (1)" line in the stm8s.h file, or
  410. define it in Raisonance compiler preprocessor to enable the access for the
  411. inram functions.
  412. 3- An inram function code is copied from Flash to RAM by the C startup code.
  413. In some applications, the RAM area where the code was initially stored may be
  414. erased or corrupted, so it may be desirable to perform the copy again.
  415. Depending on the application memory model, the memcpy() or fmemcpy() functions
  416. should be used to perform the copy.
  417. • In case your project uses the SMALL memory model (code smaller than 64K),
  418. memcpy()function is recommended to perform the copy
  419. • In case your project uses the LARGE memory model, functions can be
  420. everywhere in the 24-bits address space (not limited to the first 64KB of
  421. code), In this case, the use of memcpy() function will not be appropriate,
  422. you need to use the specific fmemcpy() function (which copies objects with
  423. 24-bit addresses).
  424. - The linker automatically defines 2 symbols for each inram function:
  425. • __address__functionname is a symbol that holds the Flash address
  426. where the given function code is stored.
  427. • __size__functionname is a symbol that holds the function size in bytes.
  428. And we already have the function address (which is itself a pointer)
  429. 4- In main.c file these two steps should be performed for each inram function:
  430. • Import the "__address__functionname" and "__size__functionname" symbols
  431. as global variables:
  432. extern int __address__functionname; // Symbol holding the flash address
  433. extern int __size__functionname; // Symbol holding the function size
  434. • In case of SMALL memory model use, Call the memcpy() function to copy the
  435. inram function to the RAM destination address:
  436. memcpy(functionname, // RAM destination address
  437. (void*)&__address__functionname, // Flash source address
  438. (int)&__size__functionname); // Code size of the function
  439. • In case of LARGE memory model use, call the fmemcpy() function to copy
  440. the inram function to the RAM destination address:
  441. memcpy(functionname, // RAM destination address
  442. (void @far*)&__address__functionname, // Flash source address
  443. (int)&__size__functionname); // Code size of the function
  444. - For IAR Compiler:
  445. 1- Use the __ramfunc keyword in the function declaration to specify that it
  446. can be executed from RAM.
  447. This is done within the stm8s_flash.c file, and it's conditioned by
  448. RAM_EXECUTION definition.
  449. 2- Uncomment the "#define RAM_EXECUTION (1)" line in the stm8s.h file, or
  450. define it in IAR compiler preprocessor to enable the access for the
  451. __ramfunc functions.
  452. - Note:
  453. 1- Ignore the IAR compiler warnings, these warnings don't impact the FLASH Program/Erase
  454. operations.
  455. The code performing the Flash Program/erase must be executed from RAM; the variables
  456. initializations don't necessary require the execution from RAM, only CR2/NCR2 registers
  457. configuration and data programing must be executed from RAM.
  458. 2- These warnings depends on IAR compiler: as the code generation is made using many
  459. runtime library functions to keep code size to a minimum.
  460. 3- It is recommended to use High Speed Optimization with IAR (-Ohs), in order
  461. to reduce the runtime library calls in the generated code.
  462. The FLASH examples given within the STM8S_StdPeriph_Lib package, details all
  463. the steps described above.
  464. @endcode
  465. */
  466. /**
  467. * @brief
  468. *******************************************************************************
  469. * Execution from RAM enable
  470. *******************************************************************************
  471. *
  472. * To enable execution from RAM you can either uncomment the following define
  473. * in the stm8s.h file or define it in your toolchain compiler preprocessor
  474. * - #define RAM_EXECUTION (1)
  475. */
  476. #if defined (_COSMIC_) && defined (RAM_EXECUTION)
  477. #pragma section (FLASH_CODE)
  478. #endif /* _COSMIC_ && RAM_EXECUTION */
  479. /**
  480. * @brief Wait for a Flash operation to complete.
  481. * @note The call and execution of this function must be done from RAM in case
  482. * of Block operation.
  483. * @param FLASH_MemType : Memory type
  484. * This parameter can be a value of @ref FLASH_MemType_TypeDef
  485. * @retval FLASH status
  486. */
  487. IN_RAM(FLASH_Status_TypeDef FLASH_WaitForLastOperation(FLASH_MemType_TypeDef FLASH_MemType))
  488. {
  489. uint8_t flagstatus = 0x00;
  490. uint16_t timeout = OPERATION_TIMEOUT;
  491. /* Wait until operation completion or write protection page occurred */
  492. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined(STM8S105) || \
  493. defined(STM8S005) || defined(STM8AF52Ax) || defined(STM8AF62Ax) || defined(STM8AF626x)
  494. if(FLASH_MemType == FLASH_MEMTYPE_PROG)
  495. {
  496. while((flagstatus == 0x00) && (timeout != 0x00))
  497. {
  498. flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_EOP |
  499. FLASH_IAPSR_WR_PG_DIS));
  500. timeout--;
  501. }
  502. }
  503. else
  504. {
  505. while((flagstatus == 0x00) && (timeout != 0x00))
  506. {
  507. flagstatus = (uint8_t)(FLASH->IAPSR & (uint8_t)(FLASH_IAPSR_HVOFF |
  508. FLASH_IAPSR_WR_PG_DIS));
  509. timeout--;
  510. }
  511. }
  512. #else /*STM8S103, STM8S903, STM8AF622x */
  513. while((flagstatus == 0x00) && (timeout != 0x00))
  514. {
  515. flagstatus = (uint8_t)(FLASH->IAPSR & (FLASH_IAPSR_EOP | FLASH_IAPSR_WR_PG_DIS));
  516. timeout--;
  517. }
  518. #endif /* STM8S208, STM8S207, STM8S105, STM8AF52Ax, STM8AF62Ax, STM8AF262x */
  519. if(timeout == 0x00 )
  520. {
  521. flagstatus = FLASH_STATUS_TIMEOUT;
  522. }
  523. return((FLASH_Status_TypeDef)flagstatus);
  524. }
  525. /**
  526. * @brief Erases a block in the program or data memory.
  527. * @note This function should be executed from RAM.
  528. * @param FLASH_MemType : The type of memory to erase
  529. * @param BlockNum : Indicates the block number to erase
  530. * @retval None.
  531. */
  532. IN_RAM(void FLASH_EraseBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType))
  533. {
  534. uint32_t startaddress = 0;
  535. #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
  536. defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
  537. uint32_t PointerAttr *pwFlash;
  538. #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
  539. uint8_t PointerAttr *pwFlash;
  540. #endif
  541. /* Check parameters */
  542. assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
  543. if(FLASH_MemType == FLASH_MEMTYPE_PROG)
  544. {
  545. assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
  546. startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
  547. }
  548. else
  549. {
  550. assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
  551. startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
  552. }
  553. /* Point to the first block address */
  554. #if defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || defined (STM8AF52Ax)
  555. pwFlash = (PointerAttr uint8_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
  556. #elif defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
  557. defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
  558. pwFlash = (PointerAttr uint32_t *)(MemoryAddressCast)(startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE));
  559. #endif /* STM8S208, STM8S207 */
  560. /* Enable erase block mode */
  561. FLASH->CR2 |= FLASH_CR2_ERASE;
  562. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NERASE);
  563. #if defined(STM8S105) || defined(STM8S005) || defined(STM8S103) || defined(STM8S003) || \
  564. defined (STM8S903) || defined (STM8AF626x) || defined (STM8AF622x)
  565. *pwFlash = (uint32_t)0;
  566. #elif defined (STM8S208) || defined(STM8S207) || defined(STM8S007) || defined (STM8AF62Ax) || \
  567. defined (STM8AF52Ax)
  568. *pwFlash = (uint8_t)0;
  569. *(pwFlash + 1) = (uint8_t)0;
  570. *(pwFlash + 2) = (uint8_t)0;
  571. *(pwFlash + 3) = (uint8_t)0;
  572. #endif
  573. }
  574. /**
  575. * @brief Programs a memory block
  576. * @note This function should be executed from RAM.
  577. * @param FLASH_MemType : The type of memory to program
  578. * @param BlockNum : The block number
  579. * @param FLASH_ProgMode : The programming mode.
  580. * @param Buffer : Pointer to buffer containing source data.
  581. * @retval None.
  582. */
  583. IN_RAM(void FLASH_ProgramBlock(uint16_t BlockNum, FLASH_MemType_TypeDef FLASH_MemType,
  584. FLASH_ProgramMode_TypeDef FLASH_ProgMode, uint8_t *Buffer))
  585. {
  586. uint16_t Count = 0;
  587. uint32_t startaddress = 0;
  588. /* Check parameters */
  589. assert_param(IS_MEMORY_TYPE_OK(FLASH_MemType));
  590. assert_param(IS_FLASH_PROGRAM_MODE_OK(FLASH_ProgMode));
  591. if(FLASH_MemType == FLASH_MEMTYPE_PROG)
  592. {
  593. assert_param(IS_FLASH_PROG_BLOCK_NUMBER_OK(BlockNum));
  594. startaddress = FLASH_PROG_START_PHYSICAL_ADDRESS;
  595. }
  596. else
  597. {
  598. assert_param(IS_FLASH_DATA_BLOCK_NUMBER_OK(BlockNum));
  599. startaddress = FLASH_DATA_START_PHYSICAL_ADDRESS;
  600. }
  601. /* Point to the first block address */
  602. startaddress = startaddress + ((uint32_t)BlockNum * FLASH_BLOCK_SIZE);
  603. /* Selection of Standard or Fast programming mode */
  604. if(FLASH_ProgMode == FLASH_PROGRAMMODE_STANDARD)
  605. {
  606. /* Standard programming mode */ /*No need in standard mode */
  607. FLASH->CR2 |= FLASH_CR2_PRG;
  608. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NPRG);
  609. }
  610. else
  611. {
  612. /* Fast programming mode */
  613. FLASH->CR2 |= FLASH_CR2_FPRG;
  614. FLASH->NCR2 &= (uint8_t)(~FLASH_NCR2_NFPRG);
  615. }
  616. /* Copy data bytes from RAM to FLASH memory */
  617. for(Count = 0; Count < FLASH_BLOCK_SIZE; Count++)
  618. {
  619. *((PointerAttr uint8_t*) (MemoryAddressCast)startaddress + Count) = ((uint8_t)(Buffer[Count]));
  620. }
  621. }
  622. #if defined (_COSMIC_) && defined (RAM_EXECUTION)
  623. /* End of FLASH_CODE section */
  624. #pragma section ()
  625. #endif /* _COSMIC_ && RAM_EXECUTION */
  626. /**
  627. * @}
  628. */
  629. /**
  630. * @}
  631. */
  632. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/