cancel
Showing results for 
Search instead for 
Did you mean: 

can't make interrup callback work

Chao
Senior

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

1 ACCEPTED SOLUTION

Accepted Solutions

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. 

If you find my answers useful, click the accept button so that way others can see the solution.

View solution in original post

12 REPLIES 12
TDK
Guru

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".
JTP1
Lead

Hello

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

JTP1_0-1707321171708.png

Br JTP

He can use predefined __weak callbacks.

To give better visibility on the answered topics, please click on "Accept as Solution" on the reply which solved your issue or answered your question.

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

 

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?

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

If you feel a post has answered your question, please click "Accept as Solution".

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. 

If you find my answers useful, click the accept button so that way others can see the solution.
Pavel A.
Evangelist III

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

 

>CubeMX initializes things, it does not write your program for you.

Thank you for this reminding.