Loading...
 

System Workbench for STM32


Calling a function break execution, but not in debug

Hi
I have a running setup of SW4STM32 (latest software on a Ubuntu 16.04 machine) and my project, imported from CubeMX, works and debug without problem.
Only thing I can’t solve is to proper program the chip and let it start the execution of the firmware at power on.
Afret a power cycle the main program doesn’t start until I program again. I tried to progrram the generated bin with ST-Link Utility, but the result doesn’t change.
I read about similar problems, but none of the proposed solution worked for me.
Can someone help me in finding the correct configuration for a “Release” version?
Thanks.

EDIT:
Sorry for editing the thread title, but I figure out that the problem is not related to the process of importing a CubeMX project.
I started from scratch with a new project without re-add the logic function (only a blinking led is left).
In this state the board correctly start at power on and the led blinks.
When I add my code again, I found that calling a LCD init function broke the execution. Commenting it out solve the problem. Nothing particular happens in this function:

void TM_HD44780_Init(uint8_t cols, uint8_t rows) {
/* Initialize delay */
//TM_DELAY_Init();

/* Init pinout */
TM_HD44780_InitPins();

/* At least 40ms */
HD44780_Delay(45000);

/* Set LCD width and height */
HD44780_Opts.Rows = rows;
HD44780_Opts.Cols = cols;

/* Set cursor pointer to beginning for LCD */
HD44780_Opts.currentX = 0;
HD44780_Opts.currentY = 0;

HD44780_Opts.DisplayFunction = HD44780_4BITMODE | HD44780_5x8DOTS | HD44780_1LINE;
if (rows > 1) {
HD44780_Opts.DisplayFunction |= HD44780_2LINE;
}

/* Try to set 4bit mode */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);

/* Second try */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);

/* Third goo! */
TM_HD44780_Cmd4bit(0x03);
HD44780_Delay(4500);

/* Set 4-bit interface */
TM_HD44780_Cmd4bit(0x02);
HD44780_Delay(100);

/* Set # lines, font size, etc. */
TM_HD44780_Cmd(HD44780_FUNCTIONSET | HD44780_Opts.DisplayFunction);

/* Turn the display on with no cursor or blinking default */
HD44780_Opts.DisplayControl = HD44780_DISPLAYON;
TM_HD44780_DisplayOn();

/* Clear lcd */
TM_HD44780_Clear();

/* Default font directions */
HD44780_Opts.DisplayMode = HD44780_ENTRYLEFT | HD44780_ENTRYSHIFTDECREMENT;
TM_HD44780_Cmd(HD44780_ENTRYMODESET | HD44780_Opts.DisplayMode);

/* Delay */
HD44780_Delay(4500);
}

I only commented the TM_DELAY_Init() call ancd changed the delay code to reflect my needs.
Can someone explain me why this happens?
Thanks

Some debugging lead me to the answer:
I’m using a delay function that use DWT, but a too simple initialization of it generate the problem during normal execution (I didn’t investigate on the difference from debug to regular execution).
So, at least for me, the problem is not on importing the project from CubeMX, but on the code added to the project after the import.

For reference, this is the simpler init (found on internet, no credit, sorry):
DWT->CTRL |= 1 ; // enable the counter
DWT->CYCCNT = 0; // reset the counter

And this is the correct one (with credit):
// @author Tilen Majerle
// @email tilen at majerle.eu
// @website http://stm32f4-discovery.netQuestion */

uint32_t c;

/* Enable TRC */
CoreDebug->DEMCR &= ~0x01000000;
CoreDebug->DEMCR |= 0x01000000;

/* Enable counter */
DWT->CTRL &= ~0x00000001;
DWT->CTRL |= 0x00000001;

/* Reset counter */
DWT->CYCCNT = 0;

/* Check if DWT has started */
c = DWT->CYCCNT;

/* 2 dummys */
__ASM volatile (“NOP”);
__ASM volatile (“NOP”);

/* Return difference, if result is zero, DWT has not started */
return (DWT->CYCCNT - c);

Hope this can help someone.
Bye