cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F072 discovery board USART to hyperterminal communication problem

Nickname4855_O
Associate II
Posted on November 11, 2015 at 20:08

Hi,

I don't know why this code doesn't work. I have following simple code to check USART and the code is running on STM32F072 discovery board. But when I check PA2 pin with scope, it shows nothing. I also connect the board to PC serial port and check with hyperterminal and no luck. 

 *******************************************

static void USART_Config(void);

static void GPIO_Config(void);

static void RCC_Config(void);

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

/**

  * @brief  Main program.

  * @param  None

  * @retval None

  */

int main(void)

{

 

 

 /* RCC configuration */

 RCC_Config();

 

 /* GPIO configuration */

 GPIO_Config();

 

  /* USART configuration */

  USART_Config();

 

  while(1)

  {

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

  

  USART_SendData(USART2, 10); 

  }

  

}

/**

  * @brief Configure the USART1 Device

  * @param  None

  * @retval None

  */

static void USART_Config(void)

{

  USART_InitTypeDef USART_InitStructure;

   

/* USART1 configured as follow:

  - BaudRate = 9600 baud 

  - Word Length = 8 Bits

  - Two Stop Bit

  - Odd parity

  - Hardware flow control disabled (RTS and CTS signals)

  - Receive and transmit enabled

  */

  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_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

 

 USART_Init(USART2, &USART_InitStructure);

 

  USART_Cmd(USART2, ENABLE);

  

}

/**

  * @brief Configure the GPIO Device

  * @param  None

  * @retval None

  */

static void GPIO_Config(void)

{

 GPIO_InitTypeDef GPIO_InitStructure;

 /* Connect PXx to USART2_Tx */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);

  /* Connect PXx to USART2_Rx */

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

 

  /* Configure USART2 Tx/Rx as alternate function push-pull */

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

//  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOA, &GPIO_InitStructure);

      

}

/**

  * @brief Configure the RCC

  * @param  None

  * @retval None

  */

static void RCC_Config(void)

{

 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);   //enable GPIOA clock

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //enable USART2 clock

}

#rtfm
14 REPLIES 14
Posted on November 11, 2015 at 21:25

Try this, at 115200 8N1

static void USART_Config(void);
static void GPIO_Config(void);
static void RCC_Config(void);
/* Private functions ---------------------------------------------------------*/
/**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
/* RCC configuration */
RCC_Config();
/* GPIO configuration */
GPIO_Config();
/* USART configuration */
USART_Config();
while(1)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); 
USART_SendData(USART2, 65); 
} 
}
/**
* @brief Configure the USART1 Device
* @param None
* @retval None
*/
static void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
/* USART1 configured as follow:
- BaudRate = 115200 baud 
- Word Length = 8 Bits
- Two Stop Bit
- Odd parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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 | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
/**
* @brief Configure the GPIO Device
* @param None
* @retval None
*/
static void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART2 Tx/Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure); 
/* Connect PXx to USART2_Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
/* Connect PXx to USART2_Rx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
}
/**
* @brief Configure the RCC
* @param None
* @retval None
*/
static void RCC_Config(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); //enable GPIOA clock
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); //enable USART2 clock
}

Make sure there are no solder-bridges to be made.
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname4855_O
Associate II
Posted on November 11, 2015 at 22:19

Thanks for the quick response.

I have tried your code, but it still doesn't work.

The board is on default configuration, no modification has been made.  

Nickname4855_O
Associate II
Posted on November 12, 2015 at 21:14

Today, I try the code on another new STM32F072 board. Again it doesn't work.

I am totally lost here, can anyone help on this?

Posted on November 12, 2015 at 22:59

What debugger are you using? And have you stepped through the code in the debugger? This code looks reasonable enough.

Make sure to use a USB-to-CMOS Serial to connect to the signals as they are not RS232 compatible levels, for that you'd need a MAX3232 or something similar.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname4855_O
Associate II
Posted on November 13, 2015 at 00:23

I am using MDK-ARM. I step through the code, and probing on PA2 pin, but don't see any signal on scope, Pin PA2 stays at low when stepping through

USART_SendData(USART2, 65);

Nickname4855_O
Associate II
Posted on November 13, 2015 at 00:40

TX:

Transmit data Output. When the transmitter is disabled, the output pin returns to its I/O port configuration. When the transmitter is enabled and nothing is to be transmitted, the TX pin is at high level. In single-wire and smartcard modes, this I/O is used to transmit and receive the data.

 

This is stated from STM32F072 reference manual. It seems to me that PA2 pin should stay at high level if USART2 is enabled, but I don't see that. ??? 

Posted on November 13, 2015 at 00:56

And you're using the line

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Nickname4855_O
Associate II
Posted on November 13, 2015 at 01:08

Yes, the code sets it to NO_PULL. When I set it to PULLUP, I still see PA2 at low level.   

Posted on November 13, 2015 at 02:41

You need to read the manual and follow the directions to check the solder bridges. SB27 and SB28 need to be made for PA2 and PA3 to get to the pins.

http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00099401.pdf

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..