cancel
Showing results for 
Search instead for 
Did you mean: 

How to add my own code in usart_if.c file

pho3nix
Associate III

Hello,

I am trying to put the Lorawan keys (DEVEUI,APPEUI, APPKEY) from a serial terminal using uart communication for end node application. I am using uart interrupts for this. There is cubemx generated usart_if.c file which contains HAL_UART_RxCpltCallback() already. This is the code snippet:

 

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

/* USER CODE BEGIN HAL_UART_RxCpltCallback_1 */



/* USER CODE END HAL_UART_RxCpltCallback_1 */

if (huart->Instance == USART2)

{

if ((NULL != RxCpltCallback) && (HAL_UART_ERROR_NONE == huart->ErrorCode))

{

RxCpltCallback(&charRx, 1, 0);

}

HAL_UART_Receive_IT(huart, &charRx, 1);

}

/* USER CODE BEGIN HAL_UART_RxCpltCallback_2 */



/* USER CODE END HAL_UART_RxCpltCallback_2 */

}

 

 

How to modify this with my code so that I can receive a command from serial terminal and proceed accordingly. 

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Pavel A.
Evangelist III

This generated code already has a HAL_UART_RxCpltCallback defined, and it calls another function RxCpltCallback.

The characters are received into array charRx[] . RxCpltCallback looks like a function pointer defined somewhere, that initially is NULL. So look where you are expected to assign this pointer to your function. It will get characters received from the UART, one by one. (the function has one more parameter that can be used creatively).

Assemble these incoming characters to a buffer until you get end of line '\n' or '\r'. Then do something. Rinse, repeat.

 

 

void MyRx(const char *p, int cnt, int unused)
{
  for (; cnt; --cnt) {
     char c = *p++;
     if (c != '\n') {
          addToBuffer(c);
     } else {
         handleCommand();
     } 
   }
}

..........
RxCpltCallback = MyRx;

 

 

 

View solution in original post

3 REPLIES 3
Techn
Senior III

hal_uart.c has _weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart), but it has attribute as _weak

as weak, so you have to implement your version such as setting a flag or copy the data etc..

avoid time consuming activities since it is an interrupt.

 

 

_weak void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)

{

/* Prevent unused argument(s) compilation warning */

UNUSED(huart);

 

/* NOTE : This function should not be modified, when the callback is needed,

the HAL_UART_RxCpltCallback can be implemented in the user file.

*/

}

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

Putting user code within USER CODE blocks will have it persist through a code generation.

/* USER CODE BEGIN HAL_UART_RxCpltCallback_1 */

// your code here...

/* USER CODE END HAL_UART_RxCpltCallback_1 */

 

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

This generated code already has a HAL_UART_RxCpltCallback defined, and it calls another function RxCpltCallback.

The characters are received into array charRx[] . RxCpltCallback looks like a function pointer defined somewhere, that initially is NULL. So look where you are expected to assign this pointer to your function. It will get characters received from the UART, one by one. (the function has one more parameter that can be used creatively).

Assemble these incoming characters to a buffer until you get end of line '\n' or '\r'. Then do something. Rinse, repeat.

 

 

void MyRx(const char *p, int cnt, int unused)
{
  for (; cnt; --cnt) {
     char c = *p++;
     if (c != '\n') {
          addToBuffer(c);
     } else {
         handleCommand();
     } 
   }
}

..........
RxCpltCallback = MyRx;