微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 有没有可能把STM32的flash里面的某个函数擦除?

有没有可能把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该页擦除,然后再讲修改好的写进去就好了。

  1. /**
  2.   ******************************************************************************
  3.   * [url=home.php?mod=space&uid=1455510]@file[/url]    STM32F0xx_EEPROM_Emulation/inc/eeprom.h
  4.   * [url=home.php?mod=space&uid=40524]@author[/url]  MCD Application Team
  5.   * [url=home.php?mod=space&uid=644434]@version[/url] V1.0.0
  6.   * @date    29-May-2012
  7.   * @brief   This file contains all the functions prototypes for the EEPROM
  8.   *          emulation firmware library.
  9.   ******************************************************************************
  10.   * @attention
  11.   *
  12.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  13.   *
  14.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  15.   * You may not use this file except in compliance with the License.
  16.   * You may obtain a copy of the License at:
  17.   *
  18.   *        http://www.st.com/software_license_agreement_liberty_v2
  19.   *
  20.   * Unless required by applicable law or agreed to in writing, software
  21.   * distributed under the License is distributed on an "AS IS" BASIS,
  22.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  23.   * See the License for the specific language governing permissions and
  24.   * limitations under the License.
  25.   *
  26.   ******************************************************************************
  27.   */

  28. /* Define to prevent recursive inclusion -------------------------------------*/
  29. #ifndef __EEPROM_H
  30. #define __EEPROM_H

  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm32f0xx.h"
  33. /* Exported constants --------------------------------------------------------*/
  34. /* Define the size of the sectors to be used */
  35. #define PAGE_SIZE             ((uint32_t)0x0040)  /* Page size = 1KByte */

  36. /* EEPROM start address in Flash */
  37. #define EEPROM_START_ADDRESS  ((uint32_t)0x0800FC00) /* EEPROM emulation start address:
  38.                                                         from sector2, after 8KByte of used
  39.                                                         Flash memory */

  40. /* Exported types ------------------------------------------------------------*/
  41. /* Exported macro ------------------------------------------------------------*/

  42. /* Exported functions ------------------------------------------------------- */
  43. void read_eeprom(__IO word addr, hword len, __IO byte* _Dst);
  44. bool write_eeprom(__IO word addr, hword len, __IO byte* _Dst);

  45. #endif /* __EEPROM_H */

  46. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码


  1. /**
  2.   ******************************************************************************
  3.   * @file    STM32F0xx_EEPROM_Emulation/src/eeprom.c
  4.   * @author  MCD Application Team
  5.   * @version V1.0.0
  6.   * @date    29-May-2012
  7.   * @brief   This file provides all the EEPROM emulation firmware functions.
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; COPYRIGHT 2012 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */

  27. /** @addtogroup STM32F0xx_EEPROM_Emulation
  28.   * @{
  29.   */

  30. /* Includes ------------------------------------------------------------------*/
  31. #include "eeprom.h"

  32. /* Private typedef -----------------------------------------------------------*/
  33. /* Private define ------------------------------------------------------------*/
  34. /* Private macro -------------------------------------------------------------*/
  35. /* Private variables ---------------------------------------------------------*/
  36. /* Private function prototypes -----------------------------------------------*/
  37. /* Private functions ---------------------------------------------------------*/


  38. //void read_eeprom (__IO word addr, hword len, __IO byte* _Dst) {
  39. //        hword i = 0;
  40. //        word _addr = addr;
  41. //        //hword _len = len;
  42. //        _addr += EEPROM_START_ADDRESS;
  43. //        _addr &= 0xFE;
  44. //        //_len = (addr & 0x01) + (len | 0x01);
  45. //        //_len >>= 1;
  46. //        for (i = 0; i < len; i++) {
  47. //                *_Dst = (*(__IO uint16_t *)_addr) >> 0;
  48. //                _addr ++;
  49. //        }
  50. //}


  51. void read_eeprom(__IO word addr, hword len, __IO byte* _Dst) {
  52.         hword i = 0;
  53.         byte buf[PAGE_SIZE] = { 0 };
  54.         for (i = 0; i < (PAGE_SIZE >> 1); i++) {
  55.                 ((uint16_t*)buf)[i] = ((__IO uint16_t *)EEPROM_START_ADDRESS)[i];
  56.         }
  57.         for (i = 0; i < len; i++) {
  58.                 _Dst[i] = buf[i + addr];
  59.         }
  60. }


  61. bool write_eeprom(__IO word addr, hword len, __IO byte* _Dst) {
  62.         byte buf[PAGE_SIZE] = { 0 };
  63.         hword i = 0;
  64.         bool stat = false;
  65.         for (i = 0; i < (PAGE_SIZE >> 1); i++) {
  66.                 ((uint16_t*)buf)[i] = ((__IO uint16_t *)EEPROM_START_ADDRESS)[i];
  67.         }
  68.         for (i = 0; i < len; i++) {
  69.                 buf[addr + i] = _Dst[i];
  70.         }

  71.         /* Unlock the Flash to enable the flash control register access *************/
  72.         FLASH_Unlock();

  73.         /* Erase the user Flash area
  74.         (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  75.         /* Clear pending flags (if any) */
  76.         FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_PGERR | FLASH_FLAG_WRPERR);

  77. Lable_eeprom_erase:
  78.         /* Erase the FLASH pages */
  79.         while (FLASH_ErasePage(EEPROM_START_ADDRESS) != FLASH_COMPLETE)
  80.         {
  81.                 /* Error occurred while sector erase.
  82.                 User can add here some code to deal with this error  */
  83.                 if(stat == false)return false;
  84.         }
  85.         /* Program the user Flash area word by word
  86.         (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/

  87.         addr = EEPROM_START_ADDRESS;

  88.         while (addr < EEPROM_START_ADDRESS + PAGE_SIZE)
  89.         {
  90.                 if (FLASH_ProgramWord(addr, *((word*)(&buf[addr - EEPROM_START_ADDRESS]))) == FLASH_COMPLETE)
  91.                 {
  92.                         addr = addr + 4;
  93.                 }
  94.                 else
  95.                 {
  96.                         /* Error occurred while writing data in Flash memory.
  97.                         User can add here some code to deal with this error */
  98.                         stat = true;
  99.                         goto Lable_eeprom_erase;
  100.                 }
  101.         }
  102.         FLASH_Lock();
  103.         return true;
  104. }




  105. /**
  106.   * @}
  107.   */

  108. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

复制代码



这个代码给你参考一下,读写FLASH的

kan kan xian,gan xie gan xie

安富莱的开发板好像有这方面的,你到他们网站看下

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top