cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F4-Discovery - USART problem

bkdi
Associate II
Posted on October 09, 2012 at 23:15

Hi,

I'm trying to connect together two USARTs. First is set as a transmitter and second as a receiver. I connect together two pins on the board PA9 - USART1_TX and PC7 USART6_RX. I have problem with receving data by USART6 from USART1. My program won't exit the loop - 

while

(

!

(

USART6

->

SR

&

USART_SR_RXNE

)

).

 

My

code looks like this:

int main(void)

 

{

 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN | RCC_AHB1ENR_GPIODEN | RCC_AHB1ENR_GPIOCEN;

 

 RCC->APB2ENR |= RCC_APB2ENR_USART1EN | RCC_APB2ENR_USART6EN; //USART clock enable

 GPIO_Port_Init (GPIOA, USART1_TX, GPIO_Mode_AF, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);

 

 GPIO_Port_Init (GPIOC, USART6_RX, GPIO_Mode_AF, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);

 

 GPIO_Port_Init (GPIOD, LED1_Green | LED2_Orange, GPIO_Mode_OUT, GPIO_OType_PP, GPIO_Speed_25MHz, GPIO_PuPd_NOPULL);

 

 

 

GPIO_Port_AF_Init(GPIOA, USART1_TX, GPIO_AF_USART1);

 

 GPIO_Port_AF_Init(GPIOC, USART6_RX, GPIO_AF_USART6);

 USART1->CR1 = 0x0000;

 

 USART1->CR2 = 0x0000;

 

 USART6->CR1 = 0x0000;

 

 USART6->CR2 = 0x0000;

 USART1->CR1 |= USART_CR1_UE; //USART enable

 

 USART6->CR1 |= USART_CR1_UE; //USART enable

 USART1->BRR = 84000000/9600;

 

 USART6->BRR = 84000000/9600;

 USART1->CR1 |= USART_CR1_TE; //transmitter enable

 

 USART6->CR1 |= USART_CR1_RE; //receiver enable

 while( !( USART1->SR & USART_SR_TXE )); //wait until DR register will be empty

 

 

USART1->DR = 'o'; //send data

 

 

 

while(!(USART1->SR & USART_SR_TC)); //wait for end of transmission

 while(!(USART6->SR & USART_SR_RXNE)); //wait for data in DR register 

 

 

uint16_t a = USART6->DR;

 

 

 

if (a=='o')

 

 {

 

    LED1_Green_on;

 

 }

 while (1)

 

 {

 }

 

}

 

 

Thanks in advance!
7 REPLIES 7
Posted on October 10, 2012 at 03:33

What library are you using, and where can the documentation be found?

What are the defines behind the values used?

Be aware that the ST libraries AF Init equivalent takes GPIO_PinSource# values not GPIO_Pin_# ones.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist III
Posted on October 10, 2012 at 09:08

''My program won't exit the loop - 

while

(

!

(

USART6

->

SR

&

USART_SR_RXNE

)

)''

 

So think about what could cause that to happen.

You code comment says it:

while( !(USART6->SR & USART_SR_RXNE) ); //wait for data in DR register 

If it doesn't exit that loop, it's not receiving anything - is it?!

So work back through the chain to find where it's broken...

eg, is anything actually being transmitted?
bkdi
Associate II
Posted on October 10, 2012 at 16:46

I'm not using any library, functions in code above are my. They work good, because I checked value of registers and GPIOA->AFRH was equal to  0x70 and GPIOC->AFRL  0x80000000.

I think that it is problem with receiver. When I'm measuring voltage with low-cost multimeter between PA9 and GND in transmission time, it is about 2V and before or after the transmision PA9 is at high level 3V.

bkdi
Associate II
Posted on October 10, 2012 at 16:46

Posted on October 10, 2012 at 20:18

Well with no visibility into your code, I can't offer any input on that.

Your problem is mostly likely C49, get an oscilliscope it should be evident.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
bkdi
Associate II
Posted on October 11, 2012 at 23:59

I have changed the role of USART1 and USART6 and the communication is working good. I think that problem was a capacitor which is connected to PA9 pin (USART1 transmitter).

Posted on October 12, 2012 at 10:58

I think that problem was a capacitor which is connected to PA9 pin (USART1 transmitter).

 

Without doubt, sending a continuous stream at 9600 baud looks profoundly bad on a scope, 2400 might be passable.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..