cancel
Showing results for 
Search instead for 
Did you mean: 

UART example code for STM32F0

hospodar
Associate III
Posted on November 20, 2012 at 23:34

Hello,

I looking for some example code for communication between my ST-DISCOVERY F0 board and PC (UART). Can someone help me with it? I´m novice.

Thank you
60 REPLIES 60
Posted on March 13, 2013 at 18:42

Have someone working program for demonstrating USART with interrupts for F0?

This should work for an interrupt

// STM32 USART IRQ TX/RX Loop (USART1 Tx PA.9, Rx PA.10) STM32F0-Discovery sourcer32@gmail.com
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
volatile char StringLoop[] = ''The quick brown fox jumps over the lazy dog

'';
//**************************************************************************************
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx ----------------------------*/
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);
/* Enable USART1 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_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(USART1, &USART_InitStructure);
USART_Cmd(USART1,ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
while(1); // Don't want to exit
}
//**************************************************************************************
void USART1_IRQHandler(void)
{
static int tx_index = 0;
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET) // Transmit the string in a loop
{
USART_SendData(USART1, StringLoop[tx_index++]);
if (tx_index >= (sizeof(StringLoop) - 1))
tx_index = 0;
}
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters modify string
{
StringLoop[rx_index++] = USART_ReceiveData(USART1);
if (rx_index >= (sizeof(StringLoop) - 1))
rx_index = 0;
}
}
//**************************************************************************************
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf(''Wrong parameters value: file %s on line %d

'', file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on March 13, 2013 at 23:52

I have error: ''main.c(33): error:  #20: identifier ''NVIC_InitTypeDef'' is undefined''. 

In which file it is defined?
Posted on March 14, 2013 at 01:28

stm32f0xx_misc.h via stm32F0xx_conf.h via stm32f0xx.h, when USE_STDPERIPH_DRIVER is defined in project

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on March 15, 2013 at 17:10

Probably I don´t have defined ''USE_STDPERIPH_DRIVER'' in project.

Please, how and where I can define it? You think preprocessor symbol?

Posted on March 15, 2013 at 17:39

0690X00000605RlQAI.png
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on March 16, 2013 at 19:22

Ok, thank you. Have you some readme for this code? I want understand how it working.

dusan2
Associate III
Posted on March 17, 2013 at 15:21

Dear Ivan,

You might find chapter 14 at

http://www.fmf.uni-lj.si/~ponikvar/STM32f407.htm

 useful.

hospodar
Associate III
Posted on June 06, 2013 at 01:05

Hello, I need program where the procesor will be executing some code in main program, while receiving of data from terminal will be performed in interrupt. If receiving will be finished, it will continue in performing of main program.  

I don´t know how I can modify (upper) showed program exactly for this purpose.

Can you help me?

Posted on June 06, 2013 at 03:33

// STM32 USART IRQ RX Line Buffer (USART1 Tx PA.9, Rx PA.10) STM32F0-Discovery sourcer32@gmail.com
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
#include <
string.h
> // memcpy()
//**************************************************************************************
#define LINEMAX 100 // Maximal allowed/expected line length
volatile char line_buffer[LINEMAX + 1]; // Holding buffer with space for terminating NUL
volatile int line_valid = 0;
//**************************************************************************************
void USART1_IRQHandler(void)
{
static char rx_buffer[LINEMAX]; // Local holding buffer to build line
static int rx_index = 0;
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received character?
{
char rx = USART_ReceiveData(USART1);
if ((rx == '
') || (rx == '
')) // Is this an end-of-line condition, either will suffice?
{
if (rx_index != 0) // Line has some content?
{
memcpy((void *)line_buffer, rx_buffer, rx_index); // Copy to static line buffer from dynamic receive buffer
line_buffer[rx_index] = 0; // Add terminating NUL
line_valid = 1; // flag new line valid for processing
rx_index = 0; // Reset content pointer
}
}
else
{
if (rx_index == LINEMAX) // If overflows pull back to start
rx_index = 0;
rx_buffer[rx_index++] = rx; // Copy to buffer and increment
}
}
}
//**************************************************************************************
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); // Enable GPIO A bank clock
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); // Enable USART1 clock
/* Configure Alternate Function pin muxing fabric to escape USART1 Rx and Tx */
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx */
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);
/* Enable USART1 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/* Configure USART1 settings */
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); // Enable USART1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); // Enable USART1 Receive Interrupt
while(1) // Don't want to exit
{
__WFI(); // Wait for an interrupt rather than grind endlessly
if (line_valid) // A new line has arrived
{
// ProcessLine(line_buffer); // Do something with the line
line_valid = 0; // clear pending flag
}
}
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hospodar
Associate III
Posted on June 12, 2013 at 00:30

Thank you Clive.

And one more question... How I can retarget printf function to software buffer in order to send data in interrupt handler? I suppose, that it will need some changes in this function. Is my suppose right?

int
fputc
(
int
ch, 
FILE
*f)
{
while
(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, ch);
return
(ch);
}