cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L15x: UART Configuration and use problem

valeriogiampa
Associate II
Posted on May 14, 2012 at 23:54

hi everybody,

i have to cinfigure an uart interface of my stm32l152 on STM32l-discovery or STM32-sk evaluation board.

I have used to configure the uart peripheral interface the example in the stm32 library called HyperTerminal_Interrupt.

The uart peripheral configuration consist of these code lines:

USART_InitStructure.USART_BaudRate = 9600;

  USART_InitStructure.USART_WordLength = USART_WordLength_8b;

  USART_InitStructure.USART_StopBits = USART_StopBits_2;

  USART_InitStructure.USART_Parity = USART_Parity_Odd;

  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  STM_EVAL_COMInit(COMX, &USART_InitStructure);

The STM_EVAL_COMInit is a function defined in the stm32l152d_eval.h or stm32l152_eval.h file.

Than I have modified the main to transmit continuousily the 0xAA byte with a pooling of the TC flag. The source code that I have included in the main file after at the uart configuration consist of:

 while(1)

  {

    USART_SendData(USART3, 0xAA);

    while(USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET)

    {

      USART_GetFlagStatus(USART3, USART_FLAG_TXE);

    }

     

  }

I don't see any data transmission on the tx pin of the uart peripheral. Also, I don't find any change in the Uart Data Register (in the register tab of the IAR EWARM).

Can sameone help me to configure the uart on the stm32l152?...I can use the STM32l-discovery or the STM32-SK evaluation board to test it.

Best Regards

Valerio Giamp�

#usart1 #uart1 #stm32l152-uart-configuration
9 REPLIES 9
Posted on May 15, 2012 at 02:09

No doubt because the routine you are using, at least of V1.1.1, enables the clocks on the wrong APB.

Something like this should be workable.

// sourcer32@gmail.com - STM32L15x USART3 Demo

#include ''stm32l1xx.h''

main()

{

/* Enable GPIO clock */

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

/* Enable USART3 clock */

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

/* Configure USART Rx & Tx as alternate function */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOC, &GPIO_InitStructure);

/* Mux out USART3 Rx & Tx */

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);

USART_InitStructure.USART_BaudRate = 9600;

USART_InitStructure.USART_WordLength = USART_WordLength_9b;

USART_InitStructure.USART_StopBits = USART_StopBits_2;

USART_InitStructure.USART_Parity = USART_Parity_Odd;

USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

/* USART configuration */

USART_Init(USART3, &USART_InitStructure);

/* Enable USART */

USART_Cmd(USART3, ENABLE);

while(1)

{

while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET); /* Waitwhile TX full */

USART_SendData(USART3, 0xAA);

}

}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
yogee00
Associate II
Posted on May 02, 2013 at 19:44

thank you sir,

here i have used USART3 its working fine ...

but while USRT2 is not working , what changes I need to make ??

thank you for ur patience and great work .

hats off ...:)

Posted on May 02, 2013 at 20:10

Quick blind mod for USART2 PA2/PA3

// sourcer32@gmail.com - STM32L15x USART2 Demo 9600 8N1
#include ''stm32l1xx.h''
void main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Assumes CMSIS and SystemInit() set up clocks before calling main() */
/* Enable GPIO A clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable USART2 clock */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* Configure USART Rx & Tx as alternate function */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Mux out USART2 Rx & Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); // PA2 USART2_TX
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); // PA3 USART2_RX
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_None;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* USART configuration */
USART_Init(USART2, &USART_InitStructure);
/* Enable USART */
USART_Cmd(USART2, ENABLE);
while(1)
{
while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); /* Waitwhile TX full */
USART_SendData(USART2, 0xAA);
}
}

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
benjaminguillot.699
Associate II
Posted on February 25, 2014 at 15:38

Hi again clive^^

Iam asking u for the same think, but for usart1.

Im on the stm32L152.

I want to use usart1. Their is my code.

Nothing happened on PB6.

What i did :

    void USART_Configure_GPIO()    

{

  GPIO_InitTypeDef GPIO_InitStructure;

    USART_InitTypeDef* USART_InitStruct;

    

  /* Enable APB2 peripheral clock */

    

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    /* Enable GPIOB clock */

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    

/*    USART1 : TX->PB6,PA9                    USART2 : TX->PA2            USART3 : TX->PB10,PC10

                       RX->PB7,PA10                                     RX->PA3                             RX->PB11,PC11    */

    

  /* Configure GPIO port 6 pins TX USART1*/

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

    GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;  //NOPULL/DOWN/UP

    GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //PP/OD

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

    

    /* Connect the pin to the desired peripherals' Alternate Function*/

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_USART1) ; //TX

      /* USART_InitStruct members default value */

  USART_InitStruct->USART_BaudRate = 9600;

  USART_InitStruct->USART_WordLength = USART_WordLength_8b;

  USART_InitStruct->USART_StopBits = USART_StopBits_1;

  USART_InitStruct->USART_Parity = USART_Parity_No ;

  USART_InitStruct->USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

  USART_InitStruct->USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART1, USART_InitStruct);

    // finally this enables the complete USART1 peripheral

    USART_Cmd(USART1, ENABLE);

  /* Disable GPIOs clock */     

//  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, DISABLE);

//  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, DISABLE);

}

and in my main :

....

....

    /* Initializes the GPIO for  USART1*/        

    USART_Configure_GPIO();

    

  /* Disable SysTick IRQ and SysTick Timer */

  SysTick->CTRL  &= ~ ( SysTick_CTRL_TICKINT_Msk | SysTick_CTRL_ENABLE_Msk );

  /* Configure Wakeup from sleep using RTC event*/

  configureWakeup();

    LCD_GLASS_Clear();

    LCD_GLASS_DisplayString( (unsigned char *) ''ok'' );

    

  while(1){

    USART_SendData(USART1, 'T');

  }

benjaminguillot.699
Associate II
Posted on February 25, 2014 at 16:22

Hi Mr Clive,

I fixed the problem, but i didnt find what was wrong!!

If u can helpme to know what was the probleme, it will be very nice!

Because i made a copy/paste and just change the parameter for usart1.

But i would like to knwo what i made wrong in my first code!

Thank you for ure help!

Have a good afternoon.

My code:

     void USART1_Configure()    

{

    GPIO_InitTypeDef GPIO_InitStructure;

  USART_InitTypeDef USART_InitStructure;

    

    /* Enable GPIO B clock */

    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

    

    /* Enable USART1 clock */

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

    

 /* Configure USART Rx & Tx as alternate function */

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_40MHz;

  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

  GPIO_Init(GPIOB, &GPIO_InitStructure);

    

    /* Connect the pin to the desired peripherals' Alternate Function*/

    GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_USART1) ; //TX

      /* Mux out USART1 Rx (PB7) & Tx (PB6) */

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);

  GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

    

      /* USART_InitStruct members default value */

   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 configuration */

  USART_Init(USART1, &USART_InitStructure);

 

  /* Enable USART */

  USART_Cmd(USART1, ENABLE);

    

}

Posted on February 25, 2014 at 16:33

Jamming out data with USART_SendData() misses the point that TXE needs to be asserted to indicate it's ready to accept data.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
benjaminguillot.699
Associate II
Posted on February 25, 2014 at 17:26

Ok!! Thank you for youre fast response!

Have a good end day.

And thank you again for the time you waste with my stupid miss!

smartsudharshan77
Associate
Posted on March 01, 2016 at 10:41

The original post was too long to process during our migration. Please click on the provided URL to read the original post. https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I6hg&d=%2Fa%2F0X0000000bta%2FWDGu5qeGH_qaBcOzTTp7QAfyaD8ObnjD.f0YcSRdX0U&asPdf=false
Posted on March 01, 2016 at 11:40

The GPIO clocks are on APB2 and if you remap pins you must enable the AFIO clock.

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