6 years, 10 months ago.

UART - Nucleo STM32F411

Hi Guys i have problem with Nucleo using Serial or BufferSerial (similar test done with arduino it work without problem but in Nucleo Stm32F411Ret6 not)

i connected one (Nucleo1) STM32F411RET6 in PC another one (Nucleo2) STM32F411RET6 connected to the first ones via 5V / GND and D2/D8 (UART TX,UART RX) see picture for description

/media/uploads/Valoni/uar2nucleo.png

Code used to Nucleo1 is

include the mbed library with this snippet

**/* Nucelo 1 - code */**
#include "mbed.h"
#include "BufferSerial.h"

Serial PC(USBTX, USBRX);
BufferSerial Device(PA_9, PA_10);

int main() 
{
	PC.baud(57600);
	Device.baud(57600);
	
	while(true)
	{
		if (Device.readable())
		{
			while (Device.readable())
			{
				uint8_t MyValues = Device.getc();
				
				if (MyValues == 13) 
				{
					PC.printf("\n\n");
					PC.printf("%02x,", MyValues);
				}
				else
				{
					PC.printf("%02x,", MyValues);
				}
					
			}
		}
		wait_ms(50);
	}
}

Code used to Nucleo 2 is

include the mbed library with this snippet

/* Nucleo 2 - code */
#include "mbed.h"
#include "BufferSerial.h"

BufferSerial Device(PA_9, PA_10);

int main() 
{
	Device.baud(57600);
	
	uint8_t cmd1[] = { 0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x27 };
	uint8_t cmd2[] = { 0x0d, 0xaa, 0x01, 0x02 };
	uint8_t cmd3[] = { 0x0d, 0xaa, 0x01, 0x00,  0x27 };
	uint8_t cmd4[] = { 0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27 };
	
	
	while(true)
	{
		Device.write(cmd1, sizeof(cmd1), 0, 0);
		wait_ms(50);
		
		Device.write(cmd2, sizeof(cmd2), 0, 0);
		wait_ms(50);
		
		Device.write(cmd3, sizeof(cmd3), 0, 0);
		wait_ms(50);
		
		Device.write(cmd4, sizeof(cmd4), 0, 0);
		wait_ms(50);
	}
}

But it give me wrong results (or i missing something) like below : ???

instead to receive in Serial Monitor results

0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x27,

0x0d, 0xaa, 0x01, 0x02,

0x0d, 0xaa, 0x01, 0x00, 0x27,

0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27,

I receive something like :

0x0d, 0x00, 0x00, 0x00, 0x01, 0x1f,0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27, 0x27, 0x27, 0x27, 0x27, 0x27 0x27, 0x27, 0x27 0x27, 0x27, 0x27 0x27 0x27,0xaa,0xaa .... (one long row until i get another 0x0d which start with new line)

Could you try a faster baudrate (115200) and also increasing the value of wait_ms between the .write. Note: For the Nucleo2 you don't need BufferedSerial work only with Serial, you are only sending.

Best regards

posted by Felícito Manzano 10 Jul 2017

I need to use that speed of communication with other devices but i face problem that i tried to simulate with nucleo 2

(similiar test on arduino work as well it could read from nucleo2 info without problem)

posted by VALON HOTI 11 Jul 2017

Problem resolved in non usually way with change of hardware clock *for uart port only so the code belove work without problem as READER.

/* 
   reader is resolved in this way
*/
#include "mbed.h"
Serial pc(SERIAL_TX, SERIAL_RX);
Serial myDevice(PA_9, PA_10);
int i = 0;
uint8_t MyInfo;
bool myCondiyion = false;
uint8_t buffer[14];
void interrupt();
int main()
{
    pc.baud(57600);
    pc.printf("Hello World !\r\n");
    pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
    pc.printf("PCLK1 is %d Hz\r\n", HAL_RCC_GetPCLK1Freq());
    pc.printf("PCLK2 is %d Hz\r\n", HAL_RCC_GetPCLK2Freq());
    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR);
    RCC->CFGR &= ~0x0000E000;  // PPRE2, Bit 15-13    
    RCC->CFGR |=  0x00008000;  // PPRE2: PCLK2 = AHB / 2 
    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR);
    pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
    pc.printf("PCLK1 is %d Hz\r\n", HAL_RCC_GetPCLK1Freq());
    pc.printf("PCLK2 is %d Hz\r\n", HAL_RCC_GetPCLK2Freq());
    myDevice.baud(57600);
    myDevice.attach(&interrupt, Serial::RxIrq);
 
    while (1) 
    {
        if (myCondiyion == true) 
        {
            for (int j = 0; j <14; j++)
            {
                /* trego gjendjen e bufferit */
                pc.printf("0x%02x,", buffer[j]);
            }
            memset(buffer, 0, sizeof(buffer) / sizeof(buffer[0]));
            myCondiyion = false;
             i = 0;
             pc.printf("\n");
        }
    }
}
void interrupt() 
{
    MyInfo = myDevice.getc();
    if (MyInfo == 13)
    {
        myCondiyion = true;
        i=0;
        buffer[i] = MyInfo;
    }
    else
    {
        i++;
        buffer[i] = MyInfo;
    }
}
posted by VALON HOTI 26 Jul 2017

SENDER now is defined in this way too

/* sender is resolved in this way */
#include "mbed.h"

Serial pc(SERIAL_TX, SERIAL_RX);
Serial myDevice(PA_9, PA_10);
 
int main()
{
    pc.baud(57600);
 
    pc.printf("Hello World !\r\n");
 
   //Trego gjendjen e kllokun systemor para ndryshimit 
    pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
    pc.printf("PCLK1 is %d Hz\r\n", HAL_RCC_GetPCLK1Freq());
    pc.printf("PCLK2 is %d Hz\r\n", HAL_RCC_GetPCLK2Freq());
 
    //Beni ndryshimet
    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR);
    RCC->CFGR &= ~0x0000E000;  // PPRE2, Bit 15-13    
    RCC->CFGR |=  0x00008000;  // PPRE2: PCLK2 = AHB / 2 
    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR);
 
    //Trego gjendjen e kllokun systemor pas ndryshimet 
    pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
    pc.printf("PCLK1 is %d Hz\r\n", HAL_RCC_GetPCLK1Freq());
    pc.printf("PCLK2 is %d Hz\r\n", HAL_RCC_GetPCLK2Freq());
 
    /* percakto nje levizje */
    myDevice.baud(57600);
    
    uint8_t cmd1[] = { 0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x27 };
    uint8_t cmd2[] = { 0x0d, 0xaa, 0x01, 0x02 };
    uint8_t cmd3[] = { 0x0d, 0xaa, 0x01, 0x00,  0x27 };
    uint8_t cmd4[] = { 0x0d, 0xaa, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x27 };
    
    
    while(true)
    {
        myDevice.write(cmd1, sizeof(cmd1), 0, 0);
        wait_ms(50);
        
        myDevice.write(cmd2, sizeof(cmd2), 0, 0);
        wait_ms(50);
        
        myDevice.write(cmd3, sizeof(cmd3), 0, 0);
        wait_ms(50);
        
        myDevice.write(cmd4, sizeof(cmd4), 0, 0);
        wait_ms(50);
    }
}
posted by VALON HOTI 26 Jul 2017

what is strange is this part of code for changing clock of uart communication droping it on half this one helped me to have good communication between devices

    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR);

   /* part which resolved problem */
    RCC->CFGR &= ~0x0000E000;  // PPRE2, Bit 15-13    
    RCC->CFGR |=  0x00008000;  // PPRE2: PCLK2 = AHB / 2 

    pc.printf("RCC_CFGR is 0x%08X\r\n", RCC->CFGR); 
    pc.printf("CPU SystemCoreClock is %d Hz\r\n", SystemCoreClock);
    pc.printf("PCLK1 is %d Hz\r\n", HAL_RCC_GetPCLK1Freq());
    pc.printf("PCLK2 is %d Hz\r\n", HAL_RCC_GetPCLK2Freq());

this part was been resolved into :

https://developer.mbed.org/questions/54029/USART-Problem-with-NucleoF401RE/

posted by VALON HOTI 26 Jul 2017
Be the first to answer this question.

Assigned to VALON HOTI 6 years, 8 months ago.

This means that the question has been accepted and is being worked on.