Skip to main content
JGreg.2
Associate III
June 2, 2020
Solved

Rx lane not receiving

  • June 2, 2020
  • 6 replies
  • 1227 views

Hello,

I try to make an UART communication between Nucleo STM32F030RE and a STM32F030K6 mounted on a custom pcb.

  • sends from NUCLEO to STM32F030K6 is OK
  • Receive from STM32F030K6 is ok
  • sends from STM32F030K6 to NUCLEO is OK (i can check it and decode thanks to oscilloscop)
  • Receive from NUCLEO does not work (i used PA10 RX pin of UART1 of NUCLEO)

Here is the code i use for NUCLEO:

int data1 = 0x21 ;
int data2 = 0x22 ;
char *datachar1 = (char*)&data1 ;
char *datachar2 = (char*)&data2;
char *data = (char*)strcat(datachar1, datachar2) ;
char *msg = "KO \n\r";
char *Rxbuffer = NULL ;
int status ;
 
 
while (1)
{
HAL_UART_Transmit(&huart1, (uint8_t*)data, strlen(data), 0xFFFF);
status = HAL_UART_Receive(&huart1, (uint8_t*)Rxbuffer, 2, 65000) ;
if ( status == HAL_OK )
 {
 HAL_UART_Transmit(&huart2, (uint8_t*)Rxbuffer, strlen(Rxbuffer), 0xFFFF); //send to monitor the reception of uart1
 } else {
 HAL_UART_Transmit(&huart2, (uint8_t*)msg, strlen(msg), 0xFFFF);
 } 
 HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5) ; //to visualiy check on the nucleo that the main loop is still running
 HAL_Delay(5000) ; 
}

Basically, NUCLEO UART1 sends 2 hexadecimal commands to STM32F030K6.

STM32F030K6 reads, interprets it and replies in function of the command received.

STM32F030K6 replie is 6 bytes sent to NUCLEO on PA_10 (UART1 Rx).

To check what NUCLEO has received from STM32F030K6, i sent what it has received directly to UART2 which is transmit via usb to a computer to visually read.

If i got a wrong status from the UART1 receive function, i send a readable "KO" status to the computer... which happens all the time.

Any tips ?

Note: whatever the timeout value i put as argument of the HAL_UART_Receive function, the delay time i see thanks to the data monitored is the same. Is it normal ?

This topic has been closed for replies.
Best answer by TDK
char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ; 

ptRxBuffer now points to the byte after the end of RxBuffer. Not what you want. You want ptRxBuffer = RxBuffer. No need to create another variable.

 int data[2] = {0x81,0x17} ;
 int *ptdata = &data[2] ;

ptdata points to the byte at data[2], which is the byte after 0x17, past the end of your array. Again, what you want is ptdata = data, so no need for another variable.

6 replies

TDK
June 2, 2020

> char *data = (char*)strcat(datachar1, datachar2) ;

This isn't safe. datachar1 is a character, not a null-terminated string.

> char *Rxbuffer = NULL ;

You shouldn't be writing to this. It's a null pointer, not a space for data. Maybe you want char Rxbuffer[32];

> Any tips ?

Can you see the transmission on the line? Check that the PA10 pin is set to the correct mode. Could also be that the message is sent before the board is in HAL_UART_Receive.

"If you feel a post has answered your question, please click ""Accept as Solution""."
JGreg.2
JGreg.2Author
Associate III
June 2, 2020

First of all, thank you for your reply and sorry for my beginner questions..

> char *data = (char*)strcat(datachar1, datachar2) ;

 This isn't safe. datachar1 is a character, not a null-terminated string.

> Indeed, i just want to send hexadecimal values throught UART using HAL_UART_Transmit and this is only way i found for it. Maybe you would have a better solution?

> char *Rxbuffer = NULL ;

You shouldn't be writing to this. It's a null pointer, not a space for data. Maybe you want char Rxbuffer[32];

I thought this was the way to initialize pointer to a string with no specific value in it.

I guess that the following declaration/initialisation is more appropriate:

char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ; 

.. because it now works !

Thank you TDK

TDK
TDKBest answer
June 3, 2020
char RxBuffer[6] ;
char *ptRxBuffer = &RxBuffer[6] ; 

ptRxBuffer now points to the byte after the end of RxBuffer. Not what you want. You want ptRxBuffer = RxBuffer. No need to create another variable.

 int data[2] = {0x81,0x17} ;
 int *ptdata = &data[2] ;

ptdata points to the byte at data[2], which is the byte after 0x17, past the end of your array. Again, what you want is ptdata = data, so no need for another variable.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Piranha
Principal III
June 3, 2020

And again... This has nothing to do with HAL, STM32 or ST. It is the lack of basic understanding of C and programming in general. This is why one has to first learn the basics of programming and C language and only then advance to more complex projects.

JGreg.2
JGreg.2Author
Associate III
June 3, 2020

@Piranha​  yes, i totally agree but i have now understood my mistakes.

So thank you for your time @TDK​