简体   繁体   English

STM32:自己的 function 不工作。 我该如何解决?

[英]STM32: An own function does not work. How can I fix it?

Recently I have been working with STM32 programming.最近一直在做STM32编程。 I tried to write my own function and insert it into the generated code, but strangely the function does not work.我尝试编写自己的 function 并将其插入到生成的代码中,但奇怪的是 function 不起作用。 When debugging, the controller hangs at the point of the function call.调试时,controller 在 function 调用处挂起。 Can anyone help me with this?谁能帮我这个? I would be grateful for any help.如果有任何帮助,我将不胜感激。 The code is attached.附上代码。 (Not relevant parts are cut out) [iOut_ref is not used in DMA routine] (不相关部分被剪掉)[iOut_ref 不用于 DMA 例程]

/* USER CODE BEGIN PD */
#define MAX_MEASURED_VALUE 3.3
#define MAX_VALUE 4095
/* USER CODE END PD */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint16_t iOut_ref = 0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
void ChangeRefValue(uint16_t*, float);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void ChangeRefValue(uint16_t* valueRef, float fValueRef)
{
    *valueRef = (int)(MAX_VALUE * (fValueRef / MAX_MEASURED_VALUE));
}
/* USER CODE END 0 */
int main(void)
{
  /* USER CODE BEGIN 1 */
  /* USER CODE END 1 */

  /* MCU Configuration--------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    ChangeRefValue(&iOut_ref, 3.3);

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}
... ```

It is very likely that the compiler has optimized the function and your variable completely as it is not used anywhere.编译器很可能已经完全优化了 function 和您的变量,因为它没有在任何地方使用。

So most likely your loop compiles to the single branch instruction to itself.因此,您的循环很可能会编译为自身的单个分支指令。

https://godbolt.org/z/cYoTWfY1a https://godbolt.org/z/cYoTWfY1a

ChangeRefValue:
        push    {r4, lr}
        mov     r4, r0
        mov     r0, r1
        bl      __aeabi_f2d
        adr     r3, .L2
        ldrd    r2, [r3]
        bl      __aeabi_ddiv
        adr     r3, .L2+8
        ldrd    r2, [r3]
        bl      __aeabi_dmul
        bl      __aeabi_d2iz
        strh    r0, [r4]        @ movhi
        pop     {r4, pc}
.L2:
        .word   1717986918
        .word   1074423398
        .word   0
        .word   1085275648
main:
        push    {r3, lr}
        bl      HAL_Init
        bl      SystemClock_Config
.L5:
        b       .L5
iOut_ref:

or with FPU enabled:或启用 FPU:

ChangeRefValue:
        vldr.32 s15, .L2
        vmul.f32        s0, s0, s15
        vcvt.s32.f32    s0, s0
        vmov    r3, s0  @ int
        strh    r3, [r0]        @ movhi
        bx      lr
.L2:
        .word   1151016216
main:
        push    {r3, lr}
        bl      HAL_Init
        bl      SystemClock_Config
.L5:
        b       .L5
iOut_ref:```

You simply need to write a bit more complicated program and use the result of the function somewhere.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM