有没有可能把STM32的flash里面的某个函数擦除?
时间:10-02
整理:3721RD
点击:
比如说:
void fun1(void)
{
//函数内容
}
void fun2(void)
{
//函数内容
}
void fun3(void)
{
//函数内容
}
main()
{
fun1();
fun2();
fun3();
}
此程序编译完之后,下载到STM32的FLASH中去,
fun2的作用是把fun1所在page擦除,并且把fun1();这句话替换成NOP
这样可能实现吗?
我试了很久,都不行
擦除只能擦除程序没用到的page,一旦程序写到flash中,比如说这个程序占用了0x08000000~0x08002000,那么这一段是无论如何都擦写不掉的
只能擦写这后面的
可以擦写的,看看官方的IAP手册,把你要擦除的页读出来,将要改的地方改好再将FLASH该页擦除,然后再讲修改好的写进去就好了。
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=1455510]@file[/url] STM32F0xx_EEPROM_Emulation/inc/eeprom.h
- * [url=home.php?mod=space&uid=40524]@author[/url] MCD Application Team
- * [url=home.php?mod=space&uid=644434]@version[/url] V1.0.0
- * @date 29-May-2012
- * @brief This file contains all the functions prototypes for the EEPROM
- * emulation firmware library.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Define to prevent recursive inclusion -------------------------------------*/
- #ifndef __EEPROM_H
- #define __EEPROM_H
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f0xx.h"
- /* Exported constants --------------------------------------------------------*/
- /* Define the size of the sectors to be used */
- #define PAGE_SIZE ((uint32_t)0x0040) /* Page size = 1KByte */
- /* EEPROM start address in Flash */
- #define EEPROM_START_ADDRESS ((uint32_t)0x0800FC00) /* EEPROM emulation start address:
- from sector2, after 8KByte of used
- Flash memory */
- /* Exported types ------------------------------------------------------------*/
- /* Exported macro ------------------------------------------------------------*/
- /* Exported functions ------------------------------------------------------- */
- void read_eeprom(__IO word addr, hword len, __IO byte* _Dst);
- bool write_eeprom(__IO word addr, hword len, __IO byte* _Dst);
- #endif /* __EEPROM_H */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
- /**
- ******************************************************************************
- * @file STM32F0xx_EEPROM_Emulation/src/eeprom.c
- * @author MCD Application Team
- * @version V1.0.0
- * @date 29-May-2012
- * @brief This file provides all the EEPROM emulation firmware functions.
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2012 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /** @addtogroup STM32F0xx_EEPROM_Emulation
- * @{
- */
- /* Includes ------------------------------------------------------------------*/
- #include "eeprom.h"
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- /* Private functions ---------------------------------------------------------*/
- //void read_eeprom (__IO word addr, hword len, __IO byte* _Dst) {
- // hword i = 0;
- // word _addr = addr;
- // //hword _len = len;
- // _addr += EEPROM_START_ADDRESS;
- // _addr &= 0xFE;
- // //_len = (addr & 0x01) + (len | 0x01);
- // //_len >>= 1;
- // for (i = 0; i < len; i++) {
- // *_Dst = (*(__IO uint16_t *)_addr) >> 0;
- // _addr ++;
- // }
- //}
- void read_eeprom(__IO word addr, hword len, __IO byte* _Dst) {
- hword i = 0;
- byte buf[PAGE_SIZE] = { 0 };
- for (i = 0; i < (PAGE_SIZE >> 1); i++) {
- ((uint16_t*)buf)[i] = ((__IO uint16_t *)EEPROM_START_ADDRESS)[i];
- }
- for (i = 0; i < len; i++) {
- _Dst[i] = buf[i + addr];
- }
- }
- bool write_eeprom(__IO word addr, hword len, __IO byte* _Dst) {
- byte buf[PAGE_SIZE] = { 0 };
- hword i = 0;
- bool stat = false;
- for (i = 0; i < (PAGE_SIZE >> 1); i++) {
- ((uint16_t*)buf)[i] = ((__IO uint16_t *)EEPROM_START_ADDRESS)[i];
- }
- for (i = 0; i < len; i++) {
- buf[addr + i] = _Dst[i];
- }
- /* Unlock the Flash to enable the flash control register access *************/
- FLASH_Unlock();
- /* Erase the user Flash area
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
- /* Clear pending flags (if any) */
- FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);
- Lable_eeprom_erase:
- /* Erase the FLASH pages */
- while (FLASH_ErasePage(EEPROM_START_ADDRESS) != FLASH_COMPLETE)
- {
- /* Error occurred while sector erase.
- User can add here some code to deal with this error */
- if(stat == false)return false;
- }
- /* Program the user Flash area word by word
- (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
- addr = EEPROM_START_ADDRESS;
- while (addr < EEPROM_START_ADDRESS + PAGE_SIZE)
- {
- if (FLASH_ProgramWord(addr, *((word*)(&buf[addr - EEPROM_START_ADDRESS]))) == FLASH_COMPLETE)
- {
- addr = addr + 4;
- }
- else
- {
- /* Error occurred while writing data in Flash memory.
- User can add here some code to deal with this error */
- stat = true;
- goto Lable_eeprom_erase;
- }
- }
- FLASH_Lock();
- return true;
- }
- /**
- * @}
- */
- /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
这个代码给你参考一下,读写FLASH的
kan kan xian,gan xie gan xie
安富莱的开发板好像有这方面的,你到他们网站看下