2015-12-02 08:04 PM
Hi,
I am using STM32F407 UART port to read data from a device with baudrate 115200. However, I cannot obtained the desired data. The data I obtained is decoded as follow: 0x 94 86 b7 f3 94 86 b7 f3 0x 94 86 b7 f3 94 86 bc c2 0x 94 86 ed f5 94 86 bc c2 0x 94 86 b7 f3 94 86 bc c2 0x 94 86 81 bf 94 86 ad f5 0x 94 86 bc c2 94 86 86 aa 0x 94 86 da be 94 ... ... ... While the desired data should be decoded as follow (from linux picocom/cutecom with configuration of 115200, 8N1, no parity, no hardware control):00012750: 54 06 31 66 54 06 31 66
00012760: 54 06 2c 35 54 06 2c 35
00012770: 54 06 2c 35 54 06 2c 35
00012780: 54 06 22 1f 54 06 1d a2
00012790: 54 06 18 b9 54 06 2c 35
000127a0: 54 06 1d a2 54 06 31 66
000127b0: 54 06 36 73 54 06 22 1f
000127c0: 54 06 2c 35 54 06 2c 35
000127d0: 54 06 27 04 54 06 2c 35
000127e0: 54 06 2c 35 54 06 27 04
000127f0: 54 06 2c 35 54 06 2c 35
Actually I have found the wrong pattern as: 0x54 is the first byte transfered and is always decoded as 0x94 or 0xd4, the following bytes are also wrong due to some wrongly decoded bits.Below is my configuration code inside the STM32F407:
void trone_config(void) { valid_data = 0; /* Configures the nested vectored interrupt controller. */ NVIC_InitTypeDef NVIC_InitStructure; /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the USART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4, ENABLE); /* Enable GPIO clocks */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* Connect UART pins to AF7 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_UART4); GPIO_InitTypeDef GPIO_InitStructure_Serial2; GPIO_InitStructure_Serial2.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure_Serial2.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure_Serial2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure_Serial2.GPIO_PuPd = GPIO_PuPd_UP; /* USART RX pin configuration */ GPIO_InitStructure_Serial2.GPIO_Pin = GPIO_Pin_11; GPIO_Init(GPIOC, &GPIO_InitStructure_Serial2); USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx; /* Configure the UART4 */ USART_Init(UART4, &USART_InitStructure); /* Enable UART4 interrupt */ USART_ITConfig(UART4, USART_IT_RXNE, ENABLE); USART_Cmd(UART4, ENABLE); } void UART4_IRQHandler(void) { if (USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) { /* Read one byte from the receive data register */ uint8_t data = (USART_ReceiveData(UART4)); if(testflag<100) testbuff[testflag++] = data; else testflag = 0; } } I have debugged the board for quite a few days and still get the problem. I think I have configured the UART port in STM32F407 as indicated: baud rate 115200, word length 8 bits, 1 stop bit, no parity. Please help me out of this! Thank you very much!! Regards, Kun2015-12-03 03:45 AM
If you get data, i usually forget to change HSE definition inside stm32f4xx.h file. At least for me, it is used as timing information for UART. ( but not PLL )
What is your external clock signal ? if it's 25MHz, stock STM32F4x.h will work, if not, just modify HSE#if !defined (HSE_VALUE)
#define HSE_VALUE ((uint32_t)12000000) /*!< Value of the External oscillator in Hz */
#endif /* HSE_VALUE */
This is for my 12MHz TXCO ocillator
2015-12-03 03:57 AM
Do other USARTs work, and UART4 doesn't? Or is UART4 just the only one you tried.
If the issue impacts all U(S)ARTs then you might have a timing issue. Check clocks, PLLs, and what you're telling the code about the rate of the unknown external clock sources. Also be aware the the STM32 expects CMOS voltage levels, and is not compatible with RS232 ones, which need a converter, and are inverted.2015-12-03 07:03 PM
Hi Linas,
The external clock for me is actually 25 MHz clock, and the UART4 can be used for another device communication at baud rate 9600. For this case, the device needs to transfer data at 115200/8N1/HardwareNone mode. I changed the UART4 mode and found it cannot work correctly.2015-12-03 07:08 PM
Hi clive1,
Other UARTs work fine and the UART4 can be used for another device communication at baud rate 9600. For this case, the device needs to transfer data at 115200/8N1/HardwareNone mode. I changed the UART4 mode and found it cannot work correctly. I read the data from oscilloscope and found the data was correct but they can not be correctly read in. Each transaction contains 4 bytes, each byte has a low start bit and a high stop bit. The byte is transferred with LSB first. I am not sure if the configuration in my code is correct.2015-12-03 09:05 PM
Hi clive1,
The problem was solved that the baudrate is set to 123000 instead of 115200 to obtained the correct data. Thanks! Regards, LI Kun