cancel
Showing results for 
Search instead for 
Did you mean: 

How-To use USART3

Posted on February 03, 2011 at 04:13

I'm trying to use USART3 with the regular mapping of Tx on PB10 and Rx on PB11.  To start with I was just trying to get a single character to transmit.  By the way, I'm using Atollic TrueSTUDIO. The following code does not seem to work.  Any ideas would be helpful. 

Thanks,

Jim

  /* Configure USART Tx as function push-pull */

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init( GPIOB, &GPIO_InitStructure );

  RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE);

  RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE );

  USART_ClockStructInit( &USART_ClockInitStruct );

  // The Struct Init function has USART_Clock disabled by default.

  //USART_ClockInitStruct.USART_Clock = USART_Clock_Enable;

  USART_ClockInit( USART3, &USART_ClockInitStruct  );

  USART_StructInit( &USART_InitStructure );

  USART_Init( USART3, &USART_InitStructure );

  USART_Cmd( USART3, ENABLE );

  USART_SendData( USART3, 'H' );

#usart #usart #discovery
5 REPLIES 5
Posted on February 04, 2011 at 13:07

Two quick observations. First you need to configure the GPIO outputs to be ''AF'' alternate function, ie the USART not the GPIO pin controller. Second, it is good practice to enable the APB clocks to the peripherals before programming register within those peripheral blocks, being synchronous, and all.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on February 06, 2011 at 01:48

Sweet.

This seems to do the trick!

Thanks,

Jim

  RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE );

  RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE );

  // Setup Tx pin.

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init( GPIOB, &GPIO_InitStructure );

  USART_ClockStructInit( &USART_ClockInitStruct );

  USART_ClockInit( USART3, &USART_ClockInitStruct  );

  USART_StructInit( &USART_InitStructure );

  USART_Init( USART3, &USART_InitStructure );

  USART_Cmd( USART3, ENABLE );

  char *s = ''Hello World!'';

  do {

      USART_SendData( USART3, *s );

      while( ! USART_GetFlagStatus( USART3, USART_FLAG_TXE ) );

  }

  while ( *(++s) );

Posted on February 06, 2011 at 21:35

Just for completeness, attached is scrap of code that fires up USART3 and flashes the LEDs on the Discovery board.

Thanks again,

Jim

gevides
Associate II
Posted on February 21, 2011 at 14:20

Hi,

It think that you forget to initialize USARTs registers (baud rate, word lenght, stop bits, etc). Am I right?

thanks

John F.
Senior
Posted on February 21, 2011 at 14:50

void USART_StructInit(USART_InitTypeDef *  USART_InitStruct) fills each USART_InitStruct member with its default value.