简体   繁体   中英

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

Recently I have been working with STM32 programming. I tried to write my own function and insert it into the generated code, but strangely the function does not work. When debugging, the controller hangs at the point of the function call. 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]

/* 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.

So most likely your loop compiles to the single branch instruction to itself.

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:

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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