[英]memset initialization of struct freeze
Some library I use in an embedded (STM32F103) project initializes it's structs with memset()
which for some reason causes the program to stop responding/hang.我在嵌入式(STM32F103)项目中使用的一些库使用
memset()
初始化它的结构,由于某种原因导致程序停止响应/挂起。
To test this I used:为了测试这个,我使用了:
typedef struct
{
uint8_t b;
uint8_t c;
} testStruct;
testStruct d = { .b = 0, .c = 0 };
memset(&d, 0, sizeof(d));
And sure enough this causes the same behavior.果然这会导致相同的行为。 Using
memset()
on an array appears to work fine.在数组上使用
memset()
似乎工作正常。
I have run out of ideas as to what the problem could be and even thought maybe its a memory alignment issue and tried using __attribute__((aligned(4),packed))
on the struct but this also did not help.我已经没有关于问题可能是什么的想法,甚至认为它可能是内存对齐问题并尝试在结构上使用
__attribute__((aligned(4),packed))
但这也没有帮助。
I am using GCC for ARM to compile the code:我正在使用 GCC for ARM 来编译代码:
arm-none-eabi-gcc -std=c11 --specs=nosys.specs -fno-exceptions -Wall -O0 -nostartfiles -mcpu=cortex-m0 -mthumb -mcpu=cortex-m3 -march=armv7-m -I %IncludeDir% -o %WorkingDir%bin\main.elf -T %WorkingDir%stm32f103c8t6.ld %WorkingDir%systick.c %WorkingDir%can.c %WorkingDir%uart.c %WorkingDir%fifo.c %WorkingDir%main.c %WorkingDir%libcanard\canard.c %LibDir%libopencm3_stm32f1.a
Any ideas as to what I am missing here?关于我在这里缺少什么的任何想法?
EDIT:编辑:
Below is the full test code.下面是完整的测试代码。 My test is simple, I am blinking a LED.
我的测试很简单,我正在闪烁一个 LED。 When
memset()
is present the code never reaches the main loop to start blinking the LED.当
memset()
存在时,代码永远不会到达主循环以开始闪烁 LED。 When I comment memset()
then the LED blinks.当我评论
memset()
,LED 会闪烁。
#define STM32F1
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/usart.h>
#include <string.h>
#include "systick.h"
void loopToggleLED(uint32_t tnow)
{
static uint32_t tPrevToggleLoop;
static bool LEDisOn;
if ((tnow - tPrevToggleLoop) < 1000000)
return;
tPrevToggleLoop = tnow;
if (!LEDisOn)
{
gpio_clear(GPIOC,GPIO13); // LED on
LEDisOn = true;
}
else
{
gpio_set(GPIOC,GPIO13); // LED off
LEDisOn = false;
}
}
int main()
{
rcc_clock_setup_in_hse_8mhz_out_72mhz();
// Enable GPIOC clock.
rcc_periph_clock_enable(RCC_GPIOC);
// Set GPIO8 (in GPIO port C) to 'output push-pull'.
gpio_set_mode(GPIOC,GPIO_MODE_OUTPUT_2_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,GPIO13);
systick_Init();
typedef struct
{
uint8_t b;
uint8_t c;
} testStruct;
testStruct d = { .b = 0, .c = 0 };
// Commenting memset() below allows the code to enter the loop below and blink the LEDs
memset(&d, 0, sizeof(d));
uint32_t tnow;
for (;;)
{
tnow = systick_Micros();
loopToggleLED(tnow);
}
return 0;
}
Solved!解决了! As KamilCuk pointed out, I mistakenly used mcpu flags for both cortex-m0 and cortex-m3.
正如 KamilCuk 指出的那样,我错误地将 mcpu 标志用于 cortex-m0 和 cortex-m3。 After removing
-mcpu=cortex-m0
memset works again as expected!删除后
-mcpu=cortex-m0
memset 再次按预期工作! Thanks for pointing it out and helping to solve this!感谢您指出并帮助解决这个问题!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.