cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F105 USART2's DR register not working

maxime
Associate II
Posted on July 18, 2014 at 13:48

Hi all,

I'm trying to use the USART2 on the STM32F105CL to communicate with an hyper-terminal but unfortunatly, the hyper-terminal is not receiving anything. Even with a very simple code.

/* Includes ------------------------------------------------------------------*/

#include <stdio.h>

#include <string.h>

#include ''stm32f10x.h''

#include ''stm32f10x_gpio.h''

#include ''stm32f10x_rcc.h''

#include ''stm32f10x_usart.h''

#include ''leds.h''

/* Private typedef -----------------------------------------------------------*/

/* Private define ------------------------------------------------------------*/

/* Private macro -------------------------------------------------------------*/

/* Private variables ---------------------------------------------------------*/

/* Private function prototypes -----------------------------------------------*/

/* Private functions ---------------------------------------------------------*/

int main(void)

{

LED_Init();

/* ************************************************************/

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

/* Enable GPIO clock */

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);

/* Enable USART clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);

//Configure USART2 pins:  Rx and Tx ----------------------------

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

GPIO_Init(GPIOA, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA, &GPIO_InitStructure);

USART_InitStructure.USART_BaudRate = 57600;

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 | USART_Mode_Tx;

USART_Init(USART2, &USART_InitStructure);

USART_Cmd(USART2,ENABLE);

/* ********************************************/

while (1)

{

for(int i=0;i<100000;i++)

{

LED_On();

}

for(int i=0;i<100000;i++)

{

LED_Off();

}

while(USART_GetFlagStatus(USART2,USART_FLAG_TC) == RESET){};

USART_SendData(USART2,'a');

while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){};

}

return 0;

}

Sombody has any clue ?

Thanks,
15 REPLIES 15
Posted on July 18, 2014 at 14:39

Get a scope, confirm if there is a signal, or not.

Make sure HSE_VALUE reflects the external crystal speed.

Don't front test TC, you only need to test TXE to know if the holding buffer is empty or not.

while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){};

USART_SendData(USART2,'a');

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
maxime
Associate II
Posted on July 18, 2014 at 15:12

hi, thanks for the answer !

The fact that the led is going on and off the right way doesn't mean that the HSE_Value reflects the external crystal speed?

maxime
Associate II
Posted on July 18, 2014 at 15:16

And I can't test with a score, because after 2 turns in the while(1), i'm in an infinite loop waiting for USART_FLAG_TXE to be SET...

Posted on July 18, 2014 at 15:31

The HSE_VALUE is used to compute the correct baud rate, and play no role in flashing your LEDs

If the USART2 registers always contain zero, it would suggest the peripheral is not clocked properly.

Suggest you don't use a debugger, or park a peripheral view over the USART. Beyond that you'd need to review the tool chain you are using, and the project construction. Review the templates provided in the standard peripheral library.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
maxime
Associate II
Posted on July 18, 2014 at 15:33

I use a debugger. That's why I can say that the register DR is never full. Using breakpoint, when I'm at the line USART_SendData(...), the flag register change to 0 and DR stay at 0.

maxime
Associate II
Posted on July 18, 2014 at 15:37

After Initialization of the USART, in USART2, i have SR = 192 and CR1 = 8204.

 Don't know if it's help...

Posted on July 18, 2014 at 15:53

Look the debugger is invasive. And you can't read the value you wrote in DR, it's a peripheral register, not a memory, the read/write go to different internal registers.

If you remove the breakpoints, and watchs, and just let the code run does the LED flash?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
maxime
Associate II
Posted on July 18, 2014 at 15:56

Oh ok...

Hum, no, the led flashs only once, after i'm in a infinite loop in the 

''while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){};''

Posted on July 18, 2014 at 16:58

The code itself doesn't look problematic, I don't like the C++ syntax used, but probably not the issue here. The way you've used the #include's suggests you haven't used one of the project templates.

What tool chain are you using?

What version of the library?

What is the value of RESET?
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..