Skip to main content
hospodar
Associate III
November 20, 2012
Question

UART example code for STM32F0

  • November 20, 2012
  • 60 replies
  • 11832 views
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
    This topic has been closed for replies.

    60 replies

    hospodar
    hospodarAuthor
    Associate III
    March 13, 2013
    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?
    Tesla DeLorean
    Guru
    March 14, 2013
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hospodar
    hospodarAuthor
    Associate III
    March 15, 2013
    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?

    Tesla DeLorean
    Guru
    March 15, 2013
    Posted on March 15, 2013 at 17:39

    0690X00000605RlQAI.png
    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hospodar
    hospodarAuthor
    Associate III
    March 16, 2013
    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 II
    March 17, 2013
    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
    hospodarAuthor
    Associate III
    June 5, 2013
    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?

    Tesla DeLorean
    Guru
    June 6, 2013
    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 (See Profile) Up vote any posts that you find helpful, it shows what's working..
    hospodar
    hospodarAuthor
    Associate III
    June 11, 2013
    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);
    }

    Tesla DeLorean
    Guru
    June 12, 2013
    Posted on June 12, 2013 at 02:05

    Yes, you'll want to use a ring/fifo buffer, and service that with an RXNE interrupt. You'll want to turn off the interrupt when there is not additional data to send.

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..