2

i'm trying to get the usart to work on my stm32f0-discovery but now i found out that the documentation about this kinda " lacks" so is there anyone who has an example of any usart working for the stm32f050?

thanks.

Bart Teunissen

Bart Teunissen
  • 1,420
  • 5
  • 20
  • 43
  • 1
    STM normally have quite good code samples and accompanying PDF's. – leppie Apr 10 '13 at 11:53
  • Yes, thats true, but there is not much to be found about the combination stm32f0-discovery and the usart. There isn't even a example in the periph library (thats where you would expect it to be). – Bart Teunissen Apr 10 '13 at 13:46
  • That's a shame :( But any of those examples from f0-f4 should work (given it has support). – leppie Apr 10 '13 at 13:48
  • I tried that, but since I was starting out with the stm32f0, and that the stm32f0 is the most cheapest, simple and thus the most compatible for noobs i did not succeed. I could only find a ready to use example at the st forum, and even that example wasn't flawless. Therefor i went to set up the guide i have written, so other stm32f0 users can use it to own desires. Btw, the stm32f0 and stm32f4 examples and chips are not the same ;) – Bart Teunissen Apr 10 '13 at 13:50
  • What I meant is the API structure is the same :) Have fun hacking. I got the STM32F4 Discovery (not much more expensive than the F0 Discovery and very fast :p) and some other STM Cortex M3 devices. – leppie Apr 10 '13 at 13:53

1 Answers1

10

Okay after two days of searching around on the internet. I found this little piece of code, and i managed to get it working:

  USART_InitTypeDef USART_InitStructure;
  GPIO_InitTypeDef GPIO_InitStructure;

  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);

  //Configure USART2 pins:  Rx and Tx ----------------------------
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_2 | GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);

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

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

   USART_SendData(USART2, 'X');
  }
}

*note: this uart uses pin PA2 and PA3 (RX and TX).

it sends a X when the buffer of the sendbuffer of the uart is empty. Better still, this piece of code only uses the stm32f0xx.h file. So its stripped of any unneassesary parts.

hope someone can use this as well, because it cost me lots of efforts to find this simple code. Maybe i'll write a guide some day about uart programming with the stm32f0.

*Edit:

I have indeed written a tutorial about the usart. And it can be found here:

Tutorial usart stm32f0 Hope this helps loads of people.

Bart Teunissen
  • 1,420
  • 5
  • 20
  • 43