2024-02-07 06:11 AM
Hi,
I am a newbie to CubeIDE and STM32 Mcus, and have spent hours trying to make interrupt callback work on a Nucleo-STM32G070 board.
First, I tried HAL_UART_TxCpltCallback for USART2, this function is not called, then I downloaded an example project TIM_TimeBase which works, but when I added the timer 3 into my test project with the same configuration as in the example project, it does not work.
I must have missed something in configuration in my project.
the Priority is not configurable
again, the Priority is not configurable
As for the Timer 3, I am a bit confused by the calculation of the prescaler in the example project, could someone clarify it to me:
Actually, in system_stm32g0xx.c, it is defined as follow:
uint32_t SystemCoreClock = 16000000U;
Regards
Chao
Solved! Go to Solution.
2024-02-07 01:00 PM
Why are you printing one character at a time? Just use pass how many characters you want to send
char str[] = "Hello World\r\n";
HAL_UART_Transmit_IT(&huart2, (uint8_t*)str, strlen(str));
Also you should create a queue buffer to hold multiple messages. Then send the next message in the queue only after you call HAL_UART_Transmit_IT and get a HAL status == HAL_OK.
2024-02-07 07:38 AM
In order for HAL_UART_TxCpltCallback to be called, you need to start UART reception in IT or DMA mode. Are you doing so? Show the code. Are you sure data is coming in on those lines? If you use blocking function call (HAL_UART_Receive), do you get data?
Could be a misunderstanding of what is supposed to be happening here. There are CubeMX examples you can follow for UART.
2024-02-07 07:54 AM
Hello
Also, have you enable 'register callbacks' from your configuration ?
Br JTP
2024-02-07 08:03 AM
He can use predefined __weak callbacks.
2024-02-07 11:44 AM
Thanks a lot, I indeed did not use HAL_UART_Transmit_IT!
However, when I use HAL_UART_Transmit_IT, the callback is allright now, but the following code prints only the character 'H' every second (it prints both "Hello World" and "hello universe" when use HAL_UART_Transmit):
while (1)
{
HAL_Delay(1000);
printf("Hello World\n");
}
/* USER CODE BEGIN 4 */
int __io_putchar(int ch)
{
HAL_UART_Transmit_IT(&huart2, (uint8_t *)&ch, 1);
return ch;
}
void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart == &huart2)
{
HAL_GPIO_TogglePin(GPIOA, LED_GREEN_Pin);
}
}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
printf("hello universe\n");
}
/* USER CODE END 4 */
The TIM3 interrup did not work because CubeIDE did not generate the following code in main.c:
HAL_TIM_Base_Start_IT(&htim3)
Now, the HAL_TIM_PeriodElapsedCallback is called, and the HAL_UART_TxCpltCallback is called twice for the 2 printf calls, but the printf in while loop prints only 'H' and the printf in TIM3 calback seems to print the '\n'.
If delete the '\n' in printf("hello universe\n") to check whether it prints the last character 'e' or not, but the 2 printf only print 'h'
Regards
Chao
2024-02-07 11:55 AM
in this case, how many times the HAL_UART_TxCpltCallback should be called? Would it be called for each character in both printf statements or once for each printf statement?
2024-02-07 12:45 PM
> The TIM3 interrup did not work because CubeIDE did not generate the following code in main.c:
CubeMX initializes things, it does not write your program for you. Expect to contribute.
Calling HAL_UART_Transmit_IT many times in a row will not work. It needs time to complete.
2024-02-07 01:00 PM
Why are you printing one character at a time? Just use pass how many characters you want to send
char str[] = "Hello World\r\n";
HAL_UART_Transmit_IT(&huart2, (uint8_t*)str, strlen(str));
Also you should create a queue buffer to hold multiple messages. Then send the next message in the queue only after you call HAL_UART_Transmit_IT and get a HAL status == HAL_OK.
2024-02-07 01:06 PM
@Chao Do you write your code "from scratch" or based on some examples? Use of HAL_UART_Transmit_IT, for one, in the code you've shown is very wrong. Please take your time to understand the "HAL" library functions or available example projects. Just throwing together some code snippets and asking in the forum why it does not work (surprise!) is not a good tactic. Unfortunately these libraries are less intuitive than Arduino, so use your time, play with available examples, read the sources. This can take weeks rather than hours. If you don't have any time and must produce a working project urgently, find a consultant.
2024-02-08 01:59 AM
>CubeMX initializes things, it does not write your program for you.
Thank you for this reminding.