Skip to main content
Senior
February 7, 2024
Solved

can't make interrup callback work

  • February 7, 2024
  • 4 replies
  • 5203 views

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. 

Chao_0-1707314144251.png

Chao_1-1707314190727.png

the Priority is not configurable

 

Chao_2-1707314294871.png

Chao_3-1707314348468.png

again, the Priority is not configurable

 

Chao_4-1707314435175.png

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:

Chao_5-1707314857870.png

Actually, in system_stm32g0xx.c, it is defined as follow:

uint32_t SystemCoreClock = 16000000U;

 

Regards

Chao

This topic has been closed for replies.
Best answer by Karl Yamashita

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. 

4 replies

TDK
February 7, 2024

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.

"If you feel a post has answered your question, please click ""Accept as Solution""."
ChaoAuthor
Senior
February 7, 2024

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

 

ChaoAuthor
Senior
February 7, 2024

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?

Graduate II
February 7, 2024

Hello

Also, have you enable 'register callbacks' from your configuration ?

JTP1_0-1707321171708.png

Br JTP

mƎALLEm
ST Technical Moderator
February 7, 2024

He can use predefined __weak callbacks.

To give better visibility on the answered topics, please click "Best answer" on the reply which solved your issue or answered your question.
Pavel A.
February 7, 2024

@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.

 

ChaoAuthor
Senior
February 8, 2024

>Do you write your code "from scratch" or based on some examples

Yes, I do write my test code "from scratch" AND based on some examples,  trying to understand the "HAL" library functions and available example projects by reading the sources.

I have plenty of C++ code with hardware configuration to be ported from NXP platform to STM32CubeIDE, but CubeIDE and the sources in "HAL" library are less intuitive than NXP platform, I am basically a C++/Java programmer with basic understanding of C and hardware, but I managed to use the NXP drivers for UART within a couple of minutes without reading any sources or documentations. 

Are there any documentation on explaining the functionality of the HAL drivers, and the conditions of using them, and what I need to do in user codes when use them?

Pavel A.
February 8, 2024

Yes there's some documentation, but it er, leaves room for improvement. Help files in windows format (chm) are installed with the HAL library packages. These help files are mostly doxygen generated. For some MCUs they also converted it to PDF [example stm32f4]

There are also "getting started" PDFs . A good collection of documents for specific STM32 family is presented by CubeIDE on the start page or in CubeMX documentation  section. Many aspects are similar across all STM32 families, so information for other  STM32s can be useful too. But mainly, use your time, read the library sources.