cancel
Showing results for 
Search instead for 
Did you mean: 

UART in STM32F407

Lyubomyr D
Associate II
Posted on May 02, 2018 at 17:24

Hello

I run into a stupid problem. I have a data that is sent by a device with bitrate115200. I need to read it with STM2F4. The data is five HEX numbers.

I was trying to do this in different ways but I failed. 

I am reading from UART4 and sending logs to UART2. All the setting is dome with CubeMX. Interrupts are on.

I am using a Discovery board.

I am using the Logic analyzer and I see that the device is sending correct data. But I receive not what I am seeing in Logic analyzer.

Please, help me.

Version 1:

struct_type data1;

struct_type

*data = &data1;

sprintf(uart2Data,'Waiting for data \r\n');

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

HAL_UART_Receive_IT(&huart4, (uint8_t *)data, sizeof(struct_type)); // reading data

sprintf(uart2Data,'

struct_elem1

= %d \r\n',

data->

struct_elem1

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem2

 = %d \r\n',

data->

struct_elem2

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem3

 = %lu \r\n',

data->

struct_elem3

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'struct_elem4= %lu \r\n',

data->

struct_elem4

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem5

 = %d \r\n',

data->

struct_elem5

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

Version 2

struct_type

data;

uint8_t buffer[sizeof(

struct_type

)];

sprintf(uart2Data,'Waiting for data \r\n');

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

HAL_UART_Receive_IT(&huart4, (uint8_t *)&buffer, sizeof(

struct_type

)); // reading data

memcpy( &data, &buffer, sizeof(struct_type));

sprintf(uart2Data,'

struct_elem1

= %d \r\n',

data.

struct_elem1

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem2

 = %d \r\n',

data.

struct_elem2

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem3

 = %lu \r\n',

data.

struct_elem3

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'struct_elem4= %lu \r\n',

data.

struct_elem4

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

sprintf(uart2Data,'

struct_elem5

 = %d \r\n',

data.

struct_elem5

);

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

Version 3

struct_type

data1;

uint8_t buffer[sizeof(

struct_type

)];

uint8_t = 0;

while ( i<sizeof(

struct_type

))

{

    HAL_UART_Receive_IT(&huart4, (uint8_t *)buffer[i], sizeof(uint8_t));

    i++;

}

for (i=0; i<sizeof(

struct_type

); i++)

{

    sprintf(uart2Data,'buffer[%d] = %x \r\n', i, buffer[i]);

    HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

}

#stm32f-uart
7 REPLIES 7
T J
Lead
Posted on May 03, 2018 at 01:46

Are you using the Cube ?

I had to enable the DMA in a circular buffer

then I read the buffer DMA counter, to check if a byte(s) has arrived.

Pavel A.
Evangelist III
Posted on May 03, 2018 at 02:18

What exactly does not work? Do you receive less than 5 bytes? Nothing at all?

-- pa

Posted on May 03, 2018 at 07:46

No, I am not using DMA. I tried but I was receiving only the first data value all the time.

Posted on May 03, 2018 at 07:48

I am receiving not the data that I expect to receive. I see the correct data on the Logis Analyzer, but when I log to UART2 I see something different.

Posted on May 03, 2018 at 08:14

I am receiving not the data that I expect to receive.

Then what do you expect, and what do you see ?

People here solely rely on the information you provide ...

... but when I log to UART2 I see something different.

Your debug output is not really a criteria. Better check what UART4 actually receives.

Lyubomyr D
Associate II
Posted on May 07, 2018 at 19:12

Thank You very much for Your time and hints.

I found the problem. Apparently it was in two following lines

sprintf(uart2Data,'Waiting for data \r\n');

HAL_UART_Transmit(&huart2, (uint8_t *)&uart2Data,strlen(uart2Data), 0xFFFF);

The dta transfer is so fast that some data was lost during logging intermediate values to uart2. I removed that amd everything worked.

I appreciate Your help a lot.

Posted on May 07, 2018 at 19:25

You might want to consider a larger buffer, or pool of buffers which you release/manage in the callback function.

Also note that sprintf() returns the length of the string it creates.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..