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 February 11, 2014 at 16:19

If you don't have any data to send you need to disable the TXE interrupt.

If you don't service a pending/enabled RXNE interrupt, the service routine will also continue to re-enter.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
hayettayechi
Associate
Posted on March 17, 2014 at 16:40

Hi ; 

Thank you so much , you have realy helped me.

but i steel have some problems :

i m working in a project where I will use the STM32f'4 board as  a signal wave genrator while the apmlitude and frequency value are controlled by a labview interface and  then sent through the rs232 communication .

so I need first to convert the string to an   16uint , and also i want to divise the string  that i receive into two parts , one will serve me as amplitude and the other one as frequency.

I will appreciate your help and i thank you in advance.

hayettayechi
Associate
Posted on March 17, 2014 at 16:41

Hi ; 

Thank you so much , you have realy helped me.

but i steel have some problems :

i m working in a project where I will use the STM32f'4 board as  a signal wave genrator while the apmlitude and frequency value are controlled by a labview interface and  then sent through the rs232 communication .

so I need first to convert the string to an   16uint , and also i want to divise the string  that i receive into two parts , one will serve me as amplitude and the other one as frequency.

I will appreciate your help and i thank you in advance.

Posted on March 17, 2014 at 17:41

This might have been better posted as a new thread.

You should probably accumulate the string, and separate the numbers with a comma or space. If you have a string you could use sscanf() or atoi() to do the numeric conversion.
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 25, 2014 at 20:36

My little piece of code where I putting characters to tx_buffer and then I sending it to USART_Tx, is working, but in result it doesn´t do what I want. I need incorporate circle fifo buffer with tail and head, what is more complicated. For example, I found 

http://coactionos.com/embedded%20design%20tips/2013/10/02/Tips-A-FIFO-Buffer-Implementation/

 

There are pointers, and I don´t understand such code in general form.

Posted on March 26, 2014 at 00:59

The thread here seems oddly popular, here is a quick blind build using the fifo code you pointed to.

// STM32 USART IRQ TX/RX FIFO ECHO (USART1 Tx PA.9, Rx PA.10) STM32F0-Discovery sourcer32@gmail.com
#include ''stm32f0xx.h''
#include ''stm32f0_discovery.h''
//**************************************************************************************
// FIFO implementation from http://coactionos.com/embedded%20design%20tips/2013/10/02/Tips-A-FIFO-Buffer-Implementation/
// Ivan's Thread [DEAD LINK /public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=/public/STe2ecommunities/mcu/Lists/STM32Discovery/UART%20example%20code%20for%20STM32F0&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/AllItems.aspx&currentviews=10407]https://my.st.com/public/STe2ecommunities/mcu/Lists/STM32Discovery/Flat.aspx?RootFolder=%2fpublic%2fSTe2ecommunities%2fmcu%2fLists%2fSTM32Discovery%2fUART%20example%20code%20for%20STM32F0&FolderCTID=0x01200200770978C69A1141439FE559EB459D75800084C20D8867EAD444A5987D47BE638E0F&TopicsView=https%3A%2F%2Fmy.st.com%2Fpublic%2FSTe2ecommunities%2Fmcu%2FLists%2FSTM32Discovery%2FAllItems.aspx¤tviews=10407
typedef struct {
char * buf;
int head;
int tail;
int size;
} fifo_t;
//This initializes the FIFO structure with the given buffer and size
void fifo_init(fifo_t * f, char * buf, int size){
f->head = 0;
f->tail = 0;
f->size = size;
f->buf = buf;
}
//This reads nbytes bytes from the FIFO
//The number of bytes read is returned
int fifo_read(fifo_t * f, void * buf, int nbytes){
int i;
char * p;
p = buf;
for(i=0; i < 
nbytes
; i++){
if( f->tail != f->head ){ //see if any data is available
*p++ = f->buf[f->tail]; //grab a byte from the buffer
f->tail++; //increment the tail
if( f->tail == f->size ){ //check for wrap-around
f->tail = 0;
}
} else {
return i; //number of bytes read
}
}
return nbytes;
}
//This writes up to nbytes bytes to the FIFO
//If the head runs in to the tail, not all bytes are written
//The number of bytes written is returned
int fifo_write(fifo_t * f, const void * buf, int nbytes){
int i;
const char * p;
p = buf;
for(i=0; i < 
nbytes
; i++){
//first check to see if there is space in the buffer
if( (f->head + 1 == f->tail) ||
( (f->head + 1 == f->size) && (f->tail == 0) )){
return i; //no more room
} else {
f->buf[f->head] = *p++;
f->head++; //increment the head
if( f->head == f->size ){ //check for wrap-around
f->head = 0;
}
}
}
return nbytes;
}
//**************************************************************************************
#define RXSIZE 1024
uint8_t RxBuffer[RXSIZE];
fifo_t RxFifo[1];
#define TXSIZE 512
uint8_t TxBuffer[TXSIZE];
fifo_t TxFifo[1];
//**************************************************************************************
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET) // Received characters added to fifo
{
uint16_t rx;
rx = USART_ReceiveData(USART1); // Receive the character
fifo_write(RxFifo, &rx, 1); // Place in reception fifo
}
if (USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
uint8_t tx;
if (fifo_read(TxFifo, &tx, 1) == 1) // Anything to send?
USART_SendData(USART1, (uint16_t)tx); // Transmit the character
else
USART_ITConfig(USART1, USART_IT_TXE, DISABLE); // Suppress interrupt when empty
}
}
//**************************************************************************************
int main(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// Initialize early
fifo_init(RxFifo, (void *)RxBuffer, sizeof(RxBuffer));
fifo_init(TxFifo, (void *)TxBuffer, sizeof(TxBuffer));
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_RXNE, ENABLE); // Enable always
// Fifo based echo example
while(1) // Don't want to exit
{
uint8_t fwd;
if (fifo_read(RxFifo, &fwd, 1) == 1) // Anything available in the reception buffer?
{
fifo_write(TxFifo, &fwd, 1); // Echo it back to be sent
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); // Enable when something to send
}
__WFI(); // Keil - Loop spins when interrupts occur, not grinding constantly
}
}
//**************************************************************************************
#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 31, 2014 at 11:15

Thanks. Can I do this in order to writing characters (ch) from printf to fifo in fputc function?

int
fputc
(
int
ch, 
FILE
*f)
{
while
(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
{
fifo_write(TxFifo, &ch, 1); 
// Echo it back to be sent
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); 

 // Enable whensomething to send 
__WFI(); 
// Keil - Loop spins when interrupts occur,
 not grinding constantly
}
return
(ch);
}

And then sending these characters (ch) to USART in interrupt handler...?

if
(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
uint8_t ch;
if
(fifo_read(TxFifo, &ch, 1) == 1) 
// Anything to send?
USART_SendData(USART1, (uint16_t)ch); 
// Transmit the character
else
USART_ITConfig(USART1, USART_IT_TXE, DISABLE); 
// Suppress interrupt when empty
}

Posted on March 31, 2014 at 14:27

You should leave the IRQ alone, it manages the TX and RX FIFO as it is. Changing it to use the variable ''ch'' serves no purpose I can understand.

The output routine puts the character in the FIFO buffer, the current state of the TXE flag is irrelevant. The TXE interrupt flag is enabled to ensure the output lights off in the case where the buffer was previously exhausted, and the TXE interrupt was disabled.

int fputc(int ch, FILE *f)
{
while(fifo_write(TxFifo, &ch, 1) != 1) // Try to insert ch to Tx Fifo if space
__WFI(); // Wait if not
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
return(ch);
}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
joseph
Associate II
Posted on April 11, 2014 at 17:41

joseph
Associate II
Posted on April 11, 2014 at 17:43