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

    Tesla DeLorean
    Guru
    November 21, 2012
    Posted on November 21, 2012 at 05:35

    Something like this will probably work

    /* USART2 PA.2 Tx, PA.3 Rx STM32F0-Discovery sourcer32@gmail.com */
    #include ''stm32f0xx.h''
    #include ''stm32f0_discovery.h''
    int main(void)
    {
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
    GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
    /* Configure USART2 pins: Rx and Tx ----------------------------*/
    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);
    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(USART2, &USART_InitStructure);
    USART_Cmd(USART2,ENABLE);
    while(1)
    {
    while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET)
    USART_SendData(USART2, 'X');
    }
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    frankmeyer9
    Associate III
    November 21, 2012
    Posted on November 21, 2012 at 09:30

    And if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry.

    http://www.acmesystems.it/106

    Andrew Neil
    Super User
    November 21, 2012
    Posted on November 21, 2012 at 10:18

    ''if you really intend to connect the discovery board to the PC, there you find some easy to replicate level shifter circuitry''

    Or use something like this for the connection to the PC:

    http://www.ftdichip.com/Products/Cables/USBTTLSerial.htm

    It connects direct to the microcontroller's

    UART pins - be sure to choose the correct voltage version!

    Or this:

    0690X00000603FwQAI.jpg

    http://www.ftdichip.com/Products/Cables/RPi.htm

    Although it's described as a ''Raspberry Pi'' cable, it neither knows nor cares what you actually connect it to!

    This one just provides Rx, Tx, and Ground. 3.3V only.

    A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
    Tesla DeLorean
    Guru
    February 1, 2013
    Posted on February 01, 2013 at 01:14

    And for USART1, this should be workable for polling. Edit: fixed bug

    /* USART1 PA.9 Tx, PA.10 Rx STM32F0-Discovery sourcer32@gmail.com */
    #include ''stm32f0xx.h''
    #include ''stm32f0_discovery.h''
    int main(void)
    {
    USART_InitTypeDef USART_InitStructure;
    GPIO_InitTypeDef GPIO_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);
    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);
    while(1)
    {
    while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
    USART_SendData(USART1, 'X');
    }
    }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    gururaj
    Visitor II
    February 16, 2013
    Posted on February 16, 2013 at 14:27

    Tesla DeLorean
    Guru
    February 16, 2013
    Posted on February 16, 2013 at 15:41

    Looks like I missed a semi-colon in my wait of TXE loop.

    You need to enable the interrupt on the USART

      USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    shammi88r
    Visitor II
    March 8, 2013
    Posted on March 08, 2013 at 12:50

    Hello,

    I have written the program for the Usart2 and i want to see the result on putty terminal but i am not able to see any kind of data on the putty terminal 

    here is my source code 

    int main (void)

    {

       USART_InitTypeDef USART_InitStructure;

       RCC_Configurartion();

       GPIO_Configurartion();

       USART_InitStructure.USART_BaudRate = 115200;/*set the baud rate to 115200 */

       USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*set the data bit 8 */

       USART_InitStructure.USART_StopBits = USART_StopBits_1;/*set the stop bit 1 */

       USART_InitStructure.USART_Parity = USART_Parity_No;/*no parity */

       USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;/*No hardware flow control */

       USART_InitStructure.USART_Mode = USART_Mode_Rx |USART_Mode_Tx;/*send and receive */

       /*Configure the serial port */

       USART_Init (USART1, &USART_InitStructure);

       /*Configure the serial port */

       USART_Init (USART2, &USART_InitStructure);

       /*Enable serial port 1 */

       USART_Cmd (USART1, ENABLE);

       /*Enable the serial port */

       USART_Cmd (USART2, ENABLE);

       /*Wait until end of transmission from USART1 to USART2 */

     while (1)

      {

     USART_SendData (USART2, '1');

     while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET);

     USART_SendData (USART2, '5');

     while (USART_GetFlagStatus (USART2, USART_FLAG_TC)== RESET);

    // if (USART_GetFlagStatus (USART1, USART_FLAG_RXNE)== SET)

       USART_SendData (USART2, USART_ReceiveData (USART1));

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

       Delay (0XFFFFF);

      }

    }

    void GPIO_Configurartion(void)

    {

       GPIO_InitTypeDef GPIO_InitStructure;

    //////Configure USART1 Rx as input Floating

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

       GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

       GPIO_Init(GPIOA, &GPIO_InitStructure);

    //////Configure USART1 Tx as alternet function push pull

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

       GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

       GPIO_Init(GPIOA, &GPIO_InitStructure);

    //////Configure USART2 Rx as input Floating

       GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;

    GPIO_InitStructure.GPIO_Speed  = GPIO_Speed_50MHz;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    //////Configure USART2 Tx as alternet function push pull

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init(GPIOD, &GPIO_InitStructure);

    }

    void RCC_Configurartion(void)

    {

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);

    }

    Tesla DeLorean
    Guru
    March 8, 2013
    Posted on March 08, 2013 at 15:28

    Tags are great and all, but perhaps you should specify what part you're using, and start a new thread if it doesn't relate to this thread's topic??

    RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // << APB2 SURELY?
    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 13, 2013
    Posted on March 13, 2013 at 17:49

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

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