cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F0308 USART set up

Tom Power
Associate II
Posted on August 23, 2017 at 17:47

Hi,

I am having trouble getting some basic USART up and running on the STM32F0308-DISCO board. I have tried both sending and receiving and don't seem to see anything coming through on either end. My code is as follows:

Thanks in advance.

#include 'stm32f0xx.h'

#include 'string.h'

#define BSRR_VAL 0x0300

void usart_init( void );

/*-----------------------------------------------------------------------------*/

void usart_init( void )

{

USART_InitTypeDef USART_InitStructure;

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOB, ENABLE );

RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE );

/* Configure USART2 pins */

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;

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( GPIOB, &GPIO_InitStructure );

GPIO_PinAFConfig( GPIOB, GPIO_PinSource8, GPIO_AF_1 );

GPIO_PinAFConfig( GPIOB, GPIO_PinSource9, GPIO_AF_1 );

/* Config USART */

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( USART1, &USART_InitStructure );

USART_Cmd( USART1, ENABLE );

}

/*-----------------------------------------------------------------------------*/

void output_init( void )

{

RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOC, ENABLE );

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_StructInit( &GPIO_InitStructure );

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init( GPIOC, &GPIO_InitStructure );

}

/*-----------------------------------------------------------------------------*/

int main(void)

{

output_init();

usart_init();

while (1)

{

//usart_send_str( 'moving to 180' );

//while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);

//USART_SendData( USART1, (uint8_t)'X' );

while ( USART_GetFlagStatus( USART1, USART_FLAG_RXNE) == RESET );

char ch = (char)USART_ReceiveData( USART1 );

if ( ch == 'o' )

GPIOC->BSRR = BSRR_VAL;

else if ( ch == 'f' )

GPIOC->BRR = BSRR_VAL;

}

}

#stm32f0 #usart

Note: this post was migrated and contained many threaded conversations, some content may be missing.
15 REPLIES 15
Posted on August 23, 2017 at 19:44

PB8 and PB9 are USART pins?

http://www.st.com/content/ccc/resource/technical/document/datasheet/a4/5d/0b/0e/87/c4/4d/71/DM00088500.pdf/files/DM00088500.pdf/jcr:content/translations/en.DM00088500.pdf

 
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 23, 2017 at 19:48

I had been playing around with several pins, those must have been the most recent I tried. I also tried PA2 and PA3 with no success. 

Posted on August 23, 2017 at 19:55

0690X00000607zoQAA.png
Posted on August 23, 2017 at 22:15

PA2 and PA3 should at least have some connectivity to a USART peripheral, and a free for use on the board.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 24, 2017 at 13:09

OK, so since my device is an STM32F030R8 device, I should be using USART2 with PA2 and PA3, is this correct? 

I have just tried this and I am still not seeing anything. 

Posted on August 24, 2017 at 13:26

According to the table from the DS I posted above, there's no USART connectivity on PA2 and PA3 for STM32F030x8.

JW

Posted on August 24, 2017 at 15:49

Hi to everyone!

Checking the user's manual of the discovery board there shows that the pins that you are using to uart are free, pages 19 and 20:

http://www.st.com/content/ccc/resource/technical/document/user_manual/ad/36/2c/ad/32/6c/43/27/DM00092pdf/files/DM00092pdf/jcr:content/translations/en.DM00092pdf

0690X000006080YQAQ.png

0690X00000607ntQAA.png

By the way, the right pins are PA9 and PA10, there you havean issue.

I have a question, how are you testing that the uart is not working? are you plugging the board to the computer through a usb-to-serial converter and checking in a terminal if data is received? I made a test code for a nucleo board F030 using CubeMX and System Workbench, and as the nucleoSTLink chips works also as usb-to-serial brigde, for making the test is fast, here is the post:

https://community.st.com/thread/40685-stm32-cubemx-uart?commentID=156868#comment-156868

As the Discovery STlink chip doesn't works as usb-to-serial bridge you should use one external. I recommend to you to use CubeMX for initialize the peripherals, there you can be ''sure'' about the pins that are available in your board.

Posted on August 24, 2017 at 14:10

According to the document provided by

Turvey.Clive.002

, there should be ( see below ). Or am I misunderstanding?

I have also tried using USART1 and PA9 and PA10, which should work according to the table below, but I have had no luck.

0690X000006080DQAQ.png

Posted on August 24, 2017 at 14:40

I apparently have an older version of DS missing the footnote 3...

Then USART2 on PA2/3 should work.

As Clive said, make sure the pins are free on that board.  Check them as GPIO Input/Output.

What's written on the chip and what is its ID readout?

JW