STM32F 发现 - 对 arm_sin_f32 的未定义引用

STM32F Discovery - Undefined reference to arm_sin_f32

我是 STM32F 探索板编程的新手。我按照说明 here 并设法使闪烁的 LED 灯正常工作。

但现在我正在尝试播放我从 here 那里借用代码的音频。在我的 Makefile 中,我包含了 CFLAGS += -lm,据我所知,arm_sin_f32 已定义。

这是 main.c 的代码:

#define USE_STDPERIPH_DRIVER
#include "stm32f4xx.h"
#define ARM_MATH_CM4
#include <arm_math.h> 
#include <math.h>
#include "speaker.h"

//Quick hack, approximately 1ms delay
void ms_delay(int ms)
{
    while (ms-- > 0) {
      volatile int x=5971;
      while (x-- > 0)
        __asm("nop");
   }
}

volatile uint32_t msTicks = 0;

// SysTick Handler (every time the interrupt occurs, this is called)
void SysTick_Handler(void){ msTicks++; }

// initialize the system tick 
void InitSystick(void){
   SystemCoreClockUpdate();
    // division occurs in terms of seconds... divide by 1000 to get ms, for example
   if (SysTick_Config(SystemCoreClock / 10000)) { while (1); } // 
update every 0.0001 s, aka 10kHz
}


//Flash orange LED at about 1hz
int main(void)
{
    SystemInit();
    InitSystick();
    init_speaker();
    int16_t audio_sample;
    int loudness = 250;
    float audio_freq = 440;
    audio_sample = (int16_t) (loudness * arm_sin_f32(audio_freq*msTicks/10000));
    send_to_speaker(audio_sample);
}

但是在尝试 运行 make 时出现以下错误:

main.c:42: undefined reference to `arm_sin_f32'

通过使用 -lm,您将 link 访问 libc 的数学库,它为您提供

的浮点数

https://www.gnu.org/software/libc/manual/html_node/Trig-Functions.html

Function: double sin (double x)
Function: float sinf (float x)
Function: long double sinl (long double x)
Function: _FloatN sinfN (_FloatN x)
Function: _FloatNx sinfNx (_FloatNx x)
Preliminary: | MT-Safe | AS-Safe | AC-Safe | See POSIX Safety Concepts.

These functions return the sine of x, where x is given in radians. The return value is in the range -1 to 1.

您需要像使用浮点数一样使用 sinf


如果您想使用 arm_sin_f32,那么您应该 link 到 CMSIS 的 dsp 库。 https://www.keil.com/pack/doc/CMSIS/DSP/html/group__sin.html

float32_t   arm_sin_f32 (float32_t x)
    Fast approximation to the trigonometric sine function for floating-point 
    data.

您应该 link 到适当的预编译库,详见:CMSIS DSP 软件库


此时最新版本的 CMSIS 可在以下位置获得: https://github.com/ARM-software/CMSIS_5 我认为您不应该简单地复制 c-files,因为它会 'pollute' 您自己的项目并且更新会很困难。

只需克隆存储库并在您的 makefile 中添加:

CMSISPATH = "C:/path/to/cmsis/top/directory"
CFLAGS += -I$(CMSISPATH)/CMSIS/DSP/Include
LDFLAGS += -L$(CMSISPATH)/CMSIS/Lib/GCC/ -larm_cortexM4lf_math

首先arm_sin_32不存在。 arm_sin_f32 例如是的。还有更多不同的。您需要从 CMSIS 添加适当的 c 文件到您的项目中,例如:CMSIS/DSP/Source/FastMathFunctions/arm_sin_f32.c

我建议不要使用 keil 中的那个,因为它可能已过时 - 只需从 github 下载最新版本的 CMSIS。

arm_.... 函数不是 m 库的一部分。

不要使用 nop-s 作为延迟,因为它们会在不执行的情况下立即从管道中冲出。它们仅用于填充