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 24, 2017 at 16:42

Not a board I have to hand, here's a blind USART2 PA2/PA3 version

#include 'stm32f0xx.h'
#define BSRR_VAL 0x0300
/*-----------------------------------------------------------------------------*/
void usart_init(void)
{
 USART_InitTypeDef USART_InitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
 /* Configure USART2 pins */
 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);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, 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)
 {
 char ch;
 while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
 {
 if (USART_GetFlagStatus(USART2, USART_FLAG_TXE) != RESET)
 USART_SendData(USART2, (uint8_t)'X');
 }
 ch = (char)USART_ReceiveData(USART2);
 if (ch == 'o')
 GPIOC->BSRR = BSRR_VAL;
 else if (ch == 'f')
 GPIOC->BRR = BSRR_VAL;
 }
}
/*-----------------------------------------------------------------------------*/�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

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 15:28

I have verified that PA2/3 both work as GPIO Input and Output.

The following is on the chip:

STM32F030

R8T6

GH256 9U

CHN GH 407

ID readout:0x20006440

Posted on August 24, 2017 at 16:47

Then some USART1 examples, PA2/PA3 viability will depend on specific F0 part, the Data Sheet covers this.

// F0 where USART1 is on PA2/PA3
#include 'stm32f0xx.h'
#define BSRR_VAL 0x0300
/*-----------------------------------------------------------------------------*/
void usart_init(void)
{
 USART_InitTypeDef USART_InitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 /* Configure USART1 pins */
 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);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, 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)
 {
 char ch;
 while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
 {
 if (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET)
 USART_SendData(USART1, (uint8_t)'X');
 }
 ch = (char)USART_ReceiveData(USART1);
 if (ch == 'o')
 GPIOC->BSRR = BSRR_VAL;
 else if (ch == 'f')
 GPIOC->BRR = BSRR_VAL;
 }
}
/*-----------------------------------------------------------------------------*/
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
// F0 where USART1 is on PA9/PA10
#include 'stm32f0xx.h'
#define BSRR_VAL 0x0300
/*-----------------------------------------------------------------------------*/
void usart_init(void)
{
 USART_InitTypeDef USART_InitStructure;
 GPIO_InitTypeDef GPIO_InitStructure;
 RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
 /* Configure USART1 pins */
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
 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);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
 GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, 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)
 {
 char ch;
 while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET)
 {
 if (USART_GetFlagStatus(USART1, USART_FLAG_TXE) != RESET)
 USART_SendData(USART1, (uint8_t)'X');
 }
 ch = (char)USART_ReceiveData(USART1);
 if (ch == 'o')
 GPIOC->BSRR = BSRR_VAL;
 else if (ch == 'f')
 GPIOC->BRR = BSRR_VAL;
 }
}
/*-----------------------------------------------------------------------------*/
�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?

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 17:32

I have tried PA9/10 with no luck. I have been testing with usb-serial converter, as well as an oscilloscope to verify. I will take a look at CubeMX though, thank you. 

Posted on August 24, 2017 at 17:39

The SPL is a relatively thin abstraction, if you can't get the HW to function with that, then HAL/Cube isn't going to help

The examples posted should output a continuous stream of data. Don't connect anything but a scope probe, and validate the bit timing. Confirm the RX/TX sense is correct. The STM32 TX should go to the RX of another device.

The outputs are at CMOS levels, this is not compatible with RS232 levels. The signals should interface directly with other micro-controllers, or FTDI or SiLabs USB-to-CMOS Serial chips.

For DISCO and NUCLEO boards double check solder-bridge options where signals route to pin headers.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Tom Power
Associate II
Posted on August 25, 2017 at 11:01

Thank you

Waclawek.Jan

and

Turvey.Clive.002

, I managed to get this working with your help.