2023-09-22 08:19 AM
I have set up a UART on a STM32L152C Discovery board on bare metal. No Embedded OS but using HAL.
I'm running a single thread. The UART is being configured as:
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
A simple function call in the while loop in main:
uint8_t myString[40];
int i=0;
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
i++;
sprintf((char*) myString, "Count %i\r\n", i);
HAL_UART_Transmit(&huart1, myString, strlen((char*) myString), HAL_MAX_DELAY);
while (HAL_UART_GetState(&huart1) != HAL_UART_STATE_READY) {
}
HAL_Delay(1);
/* USER CODE END WHILE */
}
yields the following output.
The first original buffer contents of the first HAL_UART_Transmit call just repeat:
Count 1
Count 1
Count 2
Count 3 .
.
.
Count 63
Count 1
Count 2
.
.
(repeats a cycle of 63)
IF HAL_Delay is changed to
HAL_Delay(5);
The output wraps at 23.
If HAL_Delay(10);
The output wraps at 13
If HAL_Delay is any value above 130,
The initial HAL_UART_Transmit call appears to just repeat:
Count 1
What am I missing here?
How is this supposed to be implemented to properly transmit an updated buffer on subsequent loops?
Comments appreciated.
Thanks
2023-09-22 08:56 AM
Hello @UCTst32
You should debug your code. That will show you what is happening every time and you will find the issue.
Best regards.
II
2023-09-22 10:35 AM
Thank you for the suggestion.
However, I have already traced through this using the debugger.
The index is incrementing as expected.
But it is unclear how the HAL_UART_Transmit function is running from what I see in the debugger.
Does the use of the HAL appear correct?
2023-09-22 01:47 PM
Is that REALLY your code (i.e. did you copy/paste into the forum post), or did you re-type it? There is no OBVIOUS reason for that behavior. If the snprintf() were overflowing your buffer the it might corrupt your loop variable, but that shouldn't happen (see below). Maybe a stack overflow? Set breakpoints and see what it going on.
Probably not the source of your issue, but:
2023-09-22 02:37 PM
I copied/pasted the snippets that seemed relevant.
Thanks for the suggestion.
Sounds like other than the need to use snprintf(), the code itself doesn't seem suspect
2023-09-22 02:59 PM
The code has to be working fine. You may consider use another product in your next project because this board is not recommended by ST for new projects.
Best regards.
II
2023-09-22 04:48 PM
Sounds like you have a watchdog enabled and the chip is getting reset at a given time after reset.
2023-09-25 07:40 AM - edited 2023-09-25 07:54 AM
/*#define HAL_IWDG_MODULE_ENABLED */
This is commented out in stm32l1xx_hal_conf.h
Don't see a WD enabled anywhere else.
Thanks for the suggestion though.
2023-09-25 08:27 AM
Use STM32CubeProgrammer to connect and check the option bytes for a hardware enabled IWDG.
2023-09-25 01:13 PM - edited 2023-09-25 05:05 PM
I'm using the STM32CubeProgrammer now to read the bytes.
I see that the software independent watchdog is active
What else am I looking for?
Thanks