cancel
Showing results for 
Search instead for 
Did you mean: 

serial com. USART6 not working

axelsamik
Associate II
Posted on September 30, 2015 at 09:15

To start with, I know usart problems are common and I've read lots of threads and other sources trying to find whats wrong but I don't see it. Anyone here see anything to correct?

/*Enable USART6 clock*/ 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); 
/*GPIO stuff*/
RCC_AHB1PeriphClockCmd(RCC_GPIOC, Enable);
GPIO_InitTypeDef GPIO_InitStructure; //setup GPIO_InitStructure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_AF_USART6;
GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(USART6,
&GPIO_InitStructure);
/*Connect pins to alternate function, ie USART*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
/*Initialization of USART */
USART_InitTypeDef USART_InitStructure; //setup UART_InitStructure
USART_ClockInitTypeDef USART_ClockInitStructure; //setup UART_ClockInitStructure
USART_ClockStructInit(&USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART6, &USART_InitStructure);
USART_ClockInit(USART6, &USART_ClockInitStructure);
USART_Cmd(USART6, ENABLE);

After this initialization we write with:

while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET);
USARTx->DR = value & (uint16_t)0xFF;

and read with:

while (USART_GetFlagStatus(USARTx, USART_FLAG_RXNE) == RESET);
return (uint16_t)(USARTx->DR & (uint16_t)0xFF);

But we don't get any data to our serial port, and yes the usart settings on the PC have been checked, Any idea? /Thanks
10 REPLIES 10
qwer.asdf
Senior
Posted on September 30, 2015 at 09:43

Replace this:

GPIO_InitStructure.GPIO_Mode = GPIO_AF_USART6;

with this:

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

Also, are you sure you want the

USART_ClockInit stuff? Are you really using

USART in synchronous mode? 

AvaTar
Lead
Posted on September 30, 2015 at 11:29

In addition to the previous poster I'd like to highlight the following points.

First, I never used pullup/pulldown for UARTs.

 GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

The internal pullup resistors are weak, so it probably does no harm. Second, you did not tell about the hardware you use. Check that nothing else is routed to those pins what interferes with UART operation. Some Discoveryboards are infamous for using up most of the useful pins. Third, you can use a debugger to step into the initialization, and check that all config registers are properly set. You can also short the TX and RX pin externally, and check that you receive every byte you send. And fourth, a scope is always useful to follow the signal through the hardware, and locate the issue.
Posted on September 30, 2015 at 11:55

You can't just mix up random constants and interfaces and hope they work.

/*Enable USART6 clock*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); /*GPIO stuff*/ RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; //setup GPIO_InitStructure GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init( GPIOC, &GPIO_InitStructure); /*Connect pins to alternate function, ie USART*/ GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6); /*Initialization of USART */ USART_InitTypeDef USART_InitStructure; //setup UART_InitStructure USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART6, &USART_InitStructure); USART_Cmd(USART6, ENABLE);


Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
axelsamik
Associate II
Posted on September 30, 2015 at 12:53

Thanks for the feedback! Will correct what you pointed out.

axelsamik
Associate II
Posted on September 30, 2015 at 15:05

The current code, a few of the previous errors were copy paste mistakes.

/*Enable USART6 clock*/ 
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); 
/*GPIO stuff*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure; //setup GPIO_InitStructure
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Fast_Speed;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOC,
&GPIO_InitStructure);
/*Connect pins to alternate function, ie USART*/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);
/*Initialization of USART */
USART_InitTypeDef USART_InitStructure; //setup UART_InitStructure
USART_ClockInitTypeDef USART_ClockInitStructure; //setup UART_ClockInitStructure
USART_ClockStructInit(&USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART6, &USART_InitStructure);
USART_ClockInit(USART6, &USART_ClockInitStructure);
USART_Cmd(USART6, ENABLE);

I'm running

STM32F4 Discovery with the expansion board STM32F4DIS-BB.

I use a RS232 cable connected at the base boards serial port and connect to the computer through an RS323 to usb adapter. The code doesn't crash or hang when writing. When I'm reading it stays at:

while
(USART_GetFlagStatus(USART6, USART_FLAG_RXNE) == RESET);

Any idea? A note, I've tried running the code snippet you clive1 uploaded in this post:

/public/STe2ecommunities/mcu/Lists/STM32Discovery/DispForm.aspx?ID=10469&RootFolder=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/%5bSTM32F4-Discovery%5d%20USART6%20Problem&Source=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder%3Dhttps%253a%252f%252fmy%252est%252ecom%252fpublic%252fSTe2ecommunities%252fmcu%252fLists%252fSTM32Discovery%252f%255bSTM32F4%252dDiscovery%255d%2520USART6%2520Problem%26FolderCTID%3D0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F%26currentviews%3D1006

whit the same result.
Posted on September 30, 2015 at 15:43

Make sure HSE_VALUE and the PLL settings in system_stm32f4xx.c reflect the 8 MHz clock source used on the DISCO, make sure it's not set for the 25 MHz of the EVAL

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
axelsamik
Associate II
Posted on September 30, 2015 at 16:18

The HSE_VALUE is 8 MHz.

As of PLL I'm not sure how to interpret it:

#define PLL_M 8 
#define PLL_N 336
/* SYSCLK = PLL_VCO / PLL_P */
#define PLL_P 2
/* USB OTG FS, SDIO and RNG Clock = PLL_VCO / PLLQ */
#define PLL_Q

I'll appreciate your help.
Posted on September 30, 2015 at 16:31

The ClockInit stuff is extraneous, you can lose it.

Check the jumpers on the STM32F4-DIS-BB to ensure PC6/7 get to the RS232 connector.

Double check the cable if the USB-RS232 Dongle is not directly attached.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
axelsamik
Associate II
Posted on September 30, 2015 at 17:07

Yes the jumpers JP1 and JP2 are set.

I'm starting to suspect that the cause may be on the PC side.

I'll have the virtual com driver installed though and use a serial viewer with the correct USART settings.