发 帖  
原厂入驻New
[问答] STM32F103如何实现在开机运行程序之前将片内SRAM区域清空?
44 STM32F103
分享
如题,要在程序中定义的变量初始化之前将SRAM区域清空。
我首先想到的就是在bootloader里写代码,但是在网上查询后发现bootloader是厂家固化的,用户不能自行修改。
其实我想到在STM32f10x.s文件里写代码(汇编门外汉),在ResetHandler这里折腾了一天没有写出来,而且试了几次发现在这里写了代码,复位芯片时连main的入口地址都到不了。程序根本没法运行。
现在实在没折了,恳请哪位高人指点一下,不胜感激。
0
2020-8-5 08:00:19   评论 分享淘帖 邀请回答
9个回答
在main函数的开始,加入一个清零内存的函数,不就OK了么?
2020-8-5 06:32:58 评论

举报

2020-8-5 06:39:39 评论

举报

我写过了,在main程序的第一行就是清空RAM的程序;
main()
{
  RAM_Erase();//自己写的RAM_Erase()程序
 ...
 ...
  变量赋值与其它子程序
 ...
 ...
}
这个程序能实现将RAM区域清空,但是由于main程序中要调用其它文件里定义的全局变量,而且其中有些变量是赋了初值的。执行RAM_Erase()之后,这些变量赋的初值也没有了,导致程序不能正常运行。
我观察过RAM区域,在编译器(keil uVersion4)编译完成后就自动对一些没有赋初值的全局变量赋0值。而对程序中赋了初值的全局变量在相应的RAM空间就是赋的初值。
所以在main函数中清零的操作是不可行的,我想大概要在上电启动后就先清空整个RAM区域,然后再编译才能保证所有的变量与被赋的值的准确性。
我的这个工程里有个很老的*.s文件,请各位大侠帮忙看看能不能在这里作点文章,清空RAM区域,不胜感激!
;//   <o> Stack Size (in Bytes) <0x0-0xFFFFFFFF:8>
;// </h>
Stack_Size      EQU     0x00004200
;                AREA    STACK, NOINIT, READWRITE, ALIGN=3
               AREA    STACK, NOINIT, READWRITE, ALIGN=3
Stack_Mem       SPACE   Stack_Size
__initial_sp
;// <h> Heap Configuration
;//   <o>  Heap Size (in Bytes) <0x0-0xFFFFFFFF:8>
;// </h>
Heap_Size       EQU     0x00000000
                AREA    HEAP, NOINIT, READWRITE, ALIGN=3
__heap_base
Heap_Mem        SPACE   Heap_Size
__heap_limit
               &nbspRESERVE8
                THUMB
; Vector Table Mapped to Address 0 at Reset
                AREA    RESET, DATA, READONLY
                
                EXTERN  NMIException
                EXTERN  HardFaultException
                EXTERN  MemManageException
                EXTERN  BusFaultException
                EXTERN  UsageFaultException
                EXTERN  SVCHandler
                EXTERN  DebugMonitor
                EXTERN &nbspendSVC
                EXTERN  SysTickHandler
                EXPORT  __Vectors
__Vectors       DCD     __initial_sp              ; Top of Stack
                DCD     Reset_Handler             ; Reset Handler
                DCD     NMIException              ; NMI Handler
                DCD     HardFaultException        ; Hard Fault Handler
                DCD     MemManageException        ; MPU Fault Handler
                DCD     BusFaultException         ; Bus Fault Handler
                DCD     UsageFaultException       ; Usage Fault Handler
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     0                         ; Reserved
                DCD     SVCHandler                ; SVCall Handler
                DCD     DebugMonitor              ; Debug Monitor Handler
                DCD     0                         ; Reserved
                DCD    &nbspendSVC                   ;&nbspendSV Handler
                DCD     SysTickHandler            ; SysTick Handler
                ; External Interrupts
                EXTERN  WWDG_IRQHandler
                EXTERN &nbspVD_IRQHandler
                EXTERN  TAMPER_IRQHandler
                EXTERN  RTC_IRQHandler
                EXTERN  FLASH_IRQHandler
                EXTERN  RCC_IRQHandler
                EXTERN  EXTI0_IRQHandler
                EXTERN  EXTI1_IRQHandler
                EXTERN  EXTI2_IRQHandler
                EXTERN  EXTI3_IRQHandler
                EXTERN  EXTI4_IRQHandler
                EXTERN  DMAChannel1_IRQHandler
                EXTERN  DMAChannel2_IRQHandler
                EXTERN  DMAChannel3_IRQHandler
                EXTERN  DMAChannel4_IRQHandler
                EXTERN  DMAChannel5_IRQHandler
                EXTERN  DMAChannel6_IRQHandler
                EXTERN  DMAChannel7_IRQHandler
                EXTERN  ADC_IRQHandler
                EXTERN  USB_HP_CAN_TX_IRQHandler
                EXTERN  USB_LP_CAN_RX0_IRQHandler
                EXTERN  CAN_RX1_IRQHandler
                EXTERN  CAN_SCE_IRQHandler
                EXTERN  EXTI9_5_IRQHandler
                EXTERN  TIM1_BRK_IRQHandler
                EXTERN  TIM1_UP_IRQHandler
                EXTERN  TIM1_TRG_COM_IRQHandler
                EXTERN  TIM1_CC_IRQHandler
                EXTERN  TIM2_IRQHandler
                EXTERN  TIM3_IRQHandler
                EXTERN  TIM4_IRQHandler
                EXTERN  I2C1_EV_IRQHandler
                EXTERN  I2C1_ER_IRQHandler
                EXTERN  I2C2_EV_IRQHandler
                EXTERN  I2C2_ER_IRQHandler
                EXTERN  SPI1_IRQHandler
                EXTERN  SPI2_IRQHandler
                EXTERN  USART1_IRQHandler
                EXTERN  USART2_IRQHandler
                EXTERN  USART3_IRQHandler
                EXTERN  EXTI15_10_IRQHandler
                EXTERN  RTCAlarm_IRQHandler
                EXTERN  USBWakeUp_IRQHandler
                
                DCD     WWDG_IRQHandler           ; Window Watchdog
                DCD    &nbspVD_IRQHandler            ;&nbspVD through EXTI Line detect
                DCD     TAMPER_IRQHandler         ; Tamper
                DCD     RTC_IRQHandler            ; RTC
                DCD     FLASH_IRQHandler          ; Flash
                DCD     RCC_IRQHandler            ; RCC
                DCD     EXTI0_IRQHandler          ; EXTI Line 0
                DCD     EXTI1_IRQHandler          ; EXTI Line 1
                DCD     EXTI2_IRQHandler          ; EXTI Line 2
                DCD     EXTI3_IRQHandler          ; EXTI Line 3
                DCD     EXTI4_IRQHandler          ; EXTI Line 4
                DCD     DMAChannel1_IRQHandler    ; DMA Channel 1
                DCD     DMAChannel2_IRQHandler    ; DMA Channel 2
                DCD     DMAChannel3_IRQHandler    ; DMA Channel 3
                DCD     DMAChannel4_IRQHandler    ; DMA Channel 4
                DCD     DMAChannel5_IRQHandler    ; DMA Channel 5
                DCD     DMAChannel6_IRQHandler    ; DMA Channel 6
                DCD     DMAChannel7_IRQHandler    ; DMA Channel 7
                DCD     ADC_IRQHandler            ; ADC
                DCD     USB_HP_CAN_TX_IRQHandler  ; USB High&nbspriority or CAN TX
                DCD     USB_LP_CAN_RX0_IRQHandler ; USB Low &nbspriority or CAN RX0
                DCD     CAN_RX1_IRQHandler        ; CAN RX1
                DCD     CAN_SCE_IRQHandler        ; CAN SCE
                DCD     EXTI9_5_IRQHandler        ; EXTI Line 9..5
                DCD     TIM1_BRK_IRQHandler       ; TIM1 Break
                DCD     TIM1_UP_IRQHandler        ; TIM1 Update
                DCD     TIM1_TRG_COM_IRQHandler   ; TIM1 Trigger and Commutation
                DCD     TIM1_CC_IRQHandler        ; TIM1 Capture Compare
                DCD     TIM2_IRQHandler           ; TIM2
                DCD     TIM3_IRQHandler           ; TIM3
                DCD     TIM4_IRQHandler           ; TIM4
                DCD     I2C1_EV_IRQHandler        ; I2C1 Event
                DCD     I2C1_ER_IRQHandler        ; I2C1 Error
                DCD     I2C2_EV_IRQHandler        ; I2C2 Event
                DCD     I2C2_ER_IRQHandler        ; I2C2 Error
                DCD     SPI1_IRQHandler           ; SPI1
                DCD     SPI2_IRQHandler           ; SPI2
                DCD     USART1_IRQHandler         ; USART1
                DCD     USART2_IRQHandler         ; USART2
                DCD     USART3_IRQHandler         ; USART3
                DCD     EXTI15_10_IRQHandler      ; EXTI Line 15..10
                DCD     RTCAlarm_IRQHandler       ; RTC Alarm through EXTI Line
                DCD     USBWakeUp_IRQHandler      ; USB Wakeup from suspend
                AREA    |.text|, CODE, READONLY
; Reset Handler
Reset_Handler  &nbspROC
                EXPORT  Reset_Handler             [WEAK]
                IMPORT  __main
                LDR     R0, =__main
                BX      R0
                ENDP
                ALIGN
; User Initial Stack & Heap
                IF   EF:__MICROLIB
                
                EXPORT  __initial_sp
                EXPORT  __heap_base
                EXPORT  __heap_limit
                
                ELSE
                
                IMPORT  __use_two_region_memory
                EXPORT  __user_initial_stackheap
__user_initial_stackheap
                LDR     R0, =  Heap_Mem
                LDR     R1, =(Stack_Mem + Stack_Size)
                LDR     R2, = (Heap_Mem +  Heap_Size)
                LDR     R3, = Stack_Mem
                BX      LR
                ALIGN
                ENDIF
 
                END
2020-8-5 06:54:34 评论

举报

你的清零当然是对特定数组了,你要整片清除干嘛?思路有问题!!!
2020-8-5 06:59:40 评论

举报

不干你事的RAM,你清除干啥呢?当然是用到的,有目的的清除了.
2020-8-5 07:17:15 评论

举报

主要是考虑到芯片异常复位,RAM区域的数据会出现异常。我被要求在芯片每次复位之后都先清空RAM区,然后再重新写值。暂时不要考虑思路有没有问题,这样做有没有必要,请问有没有办法实现这个功能?
2020-8-5 07:27:36 评论

举报

没这么搞过...
不知道行不行.内存非法访问,导致异常的可能性更大.
问题自己解决了,还是非常感谢大家。
2020-8-5 08:04:31 评论

举报

2020-8-5 08:21:26 评论

举报

只有小组成员才能发言,加入小组>>

83个成员聚集在这个小组

加入小组

创建小组步骤

关闭

站长推荐 上一条 /8 下一条

快速回复 返回顶部 返回列表