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.
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.
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.
I am using GCC for ARM to compile the code:
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. When memset()
is present the code never reaches the main loop to start blinking the LED. When I comment memset()
then the LED blinks.
#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. After removing
-mcpu=cortex-m0
memset works again as expected! Thanks for pointing it out and helping to solve this!