The role of [WEAK] in Keil-MDK

Publisher:CelestialMagicLatest update time:2017-02-18 Source: eefocusKeywords:Keil-MDK  WEAK Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The transplanted code is here:  https://code.csdn.net/KISSMonX/freertos_f3discovery_test

In the previous article, "The meaning of the "B ." statement in ARM assembly", I also introduced the role of [WEAK]. When
I was thinking about the transplantation problem yesterday (that is, when executing the first task, I jumped directly to B . in SVC_Handler), 
I thought of this problem, and then added a few macro definitions in the transplantation configuration file to solve the problem. The transplantation was successful. The details are introduced below. I 
will explain it again here. See if you really understand it. :)

The first step is obviously to post the code to show off. Extract the part to be introduced in the startup file. Then write it down in the form of notes. As follows:


  1. ; Reset handler This is the key point of the startup file. But I don't see any operation to set up stack space for C program??? Calling main directly is OK? MAN???  

  2. Reset_Handler    PROC  

  3.                  EXPORT  Reset_Handler             [WEAK]  

  4.         IMPORT  SystemInit  

  5.         IMPORT  __main  

  6.   

  7.                  LDR     R0, =SystemInit  

  8.                  BLX     R0  

  9.                  LDR     R0, =__main  

  10.                  BX      R0  

  11.                  ENDP  

  12.   

  13. ; Dummy Exception Handlers (infinite loops which can be modified)  

  14.   

  15. NMI_Handler     PROC  

  16.                 EXPORT  NMI_Handler                [WEAK]  

  17.                 B       .  

  18.                 ENDP  

  19. HardFault_Handler\  

  20.                 PROC  

  21.                 EXPORT  HardFault_Handler          [WEAK]  

  22.                 B       .  

  23.                 ENDP  

  24. MemManage_Handler\  

  25.                 PROC  

  26.                 EXPORT  MemManage_Handler          [WEAK]  

  27.                 B       .  

  28.                 ENDP  

  29. BusFault_Handler\  

  30.                 PROC  

  31.                 EXPORT  BusFault_Handler           [WEAK]  

  32.                 B       .  

  33.                 ENDP  

  34. UsageFault_Handler\  

  35.                 PROC  

  36.                 EXPORT  UsageFault_Handler         [WEAK]  

  37.                 B       .  

  38.                 ENDP  

  39. SVC_Handler     PROC  

  40.                 EXPORT  SVC_Handler                [WEAK]  

  41.                 B       .  

  42.                 ENDP  

  43. DebugMon_Handler\  

  44.                 PROC  

  45.                 EXPORT  DebugMon_Handler           [WEAK]  

  46.                 B       .  

  47.                 ENDP  

  48. PendSV_Handler  PROC  

  49.                 EXPORT  PendSV_Handler             [WEAK]  

  50.                 B       .  

  51.                 ENDP  

  52. SysTick_Handler PROC  

  53.                 EXPORT  SysTick_Handler            [WEAK]  

  54.                 B       .  

  55.                 ENDP  

  56.   

  57. Default_Handler PROC  

  58.   

  59.                 EXPORT  WWDG_IRQHandler                   [WEAK]                                          

  60.                 EXPORT  PVD_IRQHandler                    [WEAK]                        

  61.                 EXPORT  TAMPER_STAMP_IRQHandler           [WEAK]           

  62.                 EXPORT  RTC_WKUP_IRQHandler               [WEAK]                       

  63.                 EXPORT  FLASH_IRQHandler                  [WEAK]                                           

  64.                 EXPORT  RCC_IRQHandler                    [WEAK]                                              

  65.                 EXPORT  EXTI0_IRQHandler                  [WEAK]                                              

  66.                 EXPORT  EXTI1_IRQHandler                  [WEAK]                                               

  67.                 EXPORT  EXTI2_TS_IRQHandler               [WEAK]                                              

  68.                 EXPORT  EXTI3_IRQHandler                  [WEAK]                                             

  69.                 EXPORT  EXTI4_IRQHandler                  [WEAK]                                              

  70.                 EXPORT  DMA1_Channel1_IRQHandler          [WEAK]                                  

  71.                 EXPORT  DMA1_Channel2_IRQHandler          [WEAK]                                     

  72.                 EXPORT  DMA1_Channel3_IRQHandler          [WEAK]                                     

  73.                 EXPORT  DMA1_Channel4_IRQHandler          [WEAK]                                     

  74.                 EXPORT  DMA1_Channel5_IRQHandler          [WEAK]                                     

  75.                 EXPORT  DMA1_Channel6_IRQHandler          [WEAK]                                     

  76.                 EXPORT  DMA1_Channel7_IRQHandler          [WEAK]                                     

  77.                 EXPORT  ADC1_2_IRQHandler                 [WEAK]                           

  78.                 EXPORT  USB_HP_CAN1_TX_IRQHandler         [WEAK]                                                  

  79.                 EXPORT  USB_LP_CAN1_RX0_IRQHandler        [WEAK]                                                 

  80.                 EXPORT  CAN1_RX1_IRQHandler               [WEAK]                                                  

  81.                 EXPORT  CAN1_SCE_IRQHandler               [WEAK]                                                  

  82.                 EXPORT  EXTI9_5_IRQHandler                [WEAK]                                      

  83.                 EXPORT  TIM1_BRK_TIM15_IRQHandler         [WEAK]                    

  84.                 EXPORT  TIM1_UP_TIM16_IRQHandler          [WEAK]                  

  85.                 EXPORT  TIM1_TRG_COM_TIM17_IRQHandler     [WEAK]   

  86.                 EXPORT  TIM1_CC_IRQHandler                [WEAK]                                     

  87.                 EXPORT  TIM2_IRQHandler                   [WEAK]                                              

  88.                 EXPORT  TIM3_IRQHandler                   [WEAK]                                              

  89.                 EXPORT  TIM4_IRQHandler                   [WEAK]                                              

  90.                 EXPORT  I2C1_EV_IRQHandler                [WEAK]                                               

  91.                 EXPORT  I2C1_ER_IRQHandler                [WEAK]                                               

  92.                 EXPORT  I2C2_EV_IRQHandler                [WEAK]                                              

  93.                 EXPORT  I2C2_ER_IRQHandler                [WEAK]                                                 

  94.                 EXPORT  SPI1_IRQHandler                   [WEAK]                                             

  95.                 EXPORT  SPI2_IRQHandler                   [WEAK]                                              

  96.                 EXPORT  USART1_IRQHandler                 [WEAK]                                            

  97.                 EXPORT  USART2_IRQHandler                 [WEAK]                                            

  98.                 EXPORT  USART3_IRQHandler                 [WEAK]                                           

  99.                 EXPORT  EXTI15_10_IRQHandler              [WEAK]                                    

  100.                 EXPORT  RTC_Alarm_IRQHandler              [WEAK]                    

  101.                 EXPORT  USBWakeUp_IRQHandler              [WEAK]                          

  102.                 EXPORT  TIM8_BRK_IRQHandler               [WEAK]                   

  103.                 EXPORT  TIM8_UP_IRQHandler                [WEAK]                   

  104.                 EXPORT  TIM8_TRG_COM_IRQHandler           [WEAK]   

  105.                 EXPORT  TIM8_CC_IRQHandler                [WEAK]  

  106.                 EXPORT  ADC3_IRQHandler                   [WEAK]                                     

  107.                 EXPORT  SPI3_IRQHandler                   [WEAK]                                               

  108.                 EXPORT  UART4_IRQHandler                  [WEAK]                                              

  109.                 EXPORT  UART5_IRQHandler                  [WEAK]                                              

  110.                 EXPORT  TIM6_DAC_IRQHandler               [WEAK]                     

  111.                 EXPORT  TIM7_IRQHandler                   [WEAK]                      

  112.                 EXPORT  DMA2_Channel1_IRQHandler          [WEAK]                                    

  113.                 EXPORT  DMA2_Channel2_IRQHandler          [WEAK]                                     

  114.                 EXPORT  DMA2_Channel3_IRQHandler          [WEAK]                                      

  115.                 EXPORT  DMA2_Channel4_IRQHandler          [WEAK]                                      

  116.                 EXPORT  DMA2_Channel5_IRQHandler          [WEAK]  

  117.                 EXPORT  ADC4_IRQHandler                   [WEAK]                                   

  118.                 EXPORT  COMP1_2_3_IRQHandler              [WEAK]                                                 

  119.                 EXPORT  COMP4_5_6_IRQHandler              [WEAK]                                                 

  120.                 EXPORT  COMP7_IRQHandler                  [WEAK]                                                 

  121.                 EXPORT  USB_HP_IRQHandler                 [WEAK]                        

  122.                 EXPORT  USB_LP_IRQHandler                 [WEAK]                        

  123.                 EXPORT  USBWakeUp_RMP_IRQHandler          [WEAK]                          

  124.                 EXPORT  FPU_IRQHandler                    [WEAK]                  

Then I found this sentence in the keil help document: 


  1. /* WEAK : symbol is only imported into other sources if no other source exports an alternative symbol.  

  2. If [WEAK] is used without symbol, all exported symbols are weak. */  

Then there is this introduction:


This is for assembly language, don't worry if you are good at C language. If you are good at English, you can go directly to: About weak references and definitions,
which is much more detailed and accurate than what I introduced. 
For those who are not good at English, forget it. The general meaning is:


  1. // This means telling the linker:   

  2. // "I'm a little weak but I'm a gentleman. If you see the same symbol instance as mine elsewhere, just use it. Don't mind me, please ignore it!"   

So... knowing this, we can solve the problem of why FreeRTOS always jumps to SVC_Handler when executing the following code.

<乱入> SVC 作用:  SVCall  A supervisor call (SVC) is an exception that is triggered by the SVC instruction. In an OS environment, applications can use SVC instructions to 
access OS kernel functions and device drivers.


  1. __asm void prvStartFirstTask( void )  

  2. {  

  3.     PRESERVE8  

  4.   

  5.     /* Use the NVIC offset register to locate the stack. */  

  6.     ldr r0, =0xE000ED08  

  7.     ldr r0, [r0]  

  8.     ldr r0, [r0]  

  9.     /* Set the msp back to the start of the stack. */  

  10.     msr msp, r0  

  11.     /* Globally enable interrupts. */  

  12.     cpsie i  

  13.     etc.  

  14.     isb  

  15.     /* Call SVC to start the first task. */  

  16.     svc 0 // System call 0, for more information about SVC, please refer to Google or the ARM Cortex-M3 Authoritative Guide  

  17.     nop  

  18. }  

Because the various development tool manufacturers only do simple processing for the exceptions and interrupts of these startup files. Basically, they are all infinite loops (that is, "B.").
And they are specially modified with [WEAK]. In this way, users can rewrite their own processing functions according to their needs, and as long as the names are the same, it will be OK. What if the names are different? Then, I looked for the name SVC_Handler, but didn't find it, but I found the following function
in port.c :


  1. __asm void vPortSVCHandler( void )  

  2. {  

  3.     PRESERVE8  

  4.   

  5.     /* Get the location of the current TCB. */  

  6.     ldr r3, =pxCurrentTCB  

  7.     ldr r1, [r3]  

  8.     ldr r0, [r1]  

  9.     /* Pop the core registers. */  

  10.     ldmia r0!, {r4-r11, r14}  

  11.     msr psp, r0  

  12.     isb  

  13.     mov r0, #0  

  14.     msr basepri, r0  

  15.     bx r14  

  16. }  

Isn't this just SVC_Handler in disguise? Damn... Obviously I don't understand the content, so I won't explain it. I'll learn it later.
Of course, I'm not that smart. I found the files I ported before and found the following three macro definitions in portmacro.h:


  1. #define vPortSVCHandler      SVC_Handler  

  2. #define xPortSysTickHandler  SysTick_Handler  

  3. #define xPortPendSVHandler   PendSV_Handler  

Sweep. It turns out that the macro name is also stronger than the WEAK modified one. Recompile and link correctly. Burn and program, and all tasks run normally.


Keywords:Keil-MDK  WEAK Reference address:The role of [WEAK] in Keil-MDK

Previous article:MCU control and operation commands of SD card in SPI mode
Next article:ARM CPU register organization

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号