我是 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
,您将链接到 libc 的数学库,它为您提供了 float
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.
您需要像使用 float 一样使用 sinf
。
如果您想使用 arm_sin_f32
,那么您应该链接到 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.
您应该链接到适当的预编译库,详见:CMSIS DSP Software Library
此时最新版本的 CMSIS 可在以下位置获得: https://github.com/ARM-software/CMSIS_5我认为您不应该简单地复制 c 文件,因为它会“污染”您自己的项目并且更新会很困难。
只需下载最新版本并在您的 makefile 中添加:
CMSISPATH = "C:/path/to/cmsis/top/directory"
CFLAGS += -I$(CMSISPATH)/CMSIS/DSP/Include
LDFLAGS += -L$(CMSISPATH)/CMSIS/Lib/GCC/ -larm_cortexM4lf_math
我是一名优秀的程序员,十分优秀!