STM32F103 5 serial ports used simultaneously

Publisher:painterLatest update time:2018-08-13 Source: eefocusKeywords:STM32F103 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Hardware platform: STM32F103 (with 5 serial ports)


5 serial ports work simultaneously without packet loss-_-


Related macro definitions


typedef enum

{

    UartPort1,

    UartPort2,

    UartPort3,

    UartPort4,

    UartPort5,

    UartPort_USB,

}UARTPORT;


#define RXTIMEOUT 10 // Receive timeout. If no data is received within 10mSec, it will be considered as a received packet.

#define UART_TXBUFFERLEN 0x100 //Send buffer size

#define UART_RXBUFFERLEN 0x400 //Receive buffer size


typedef struct

{

    //take over

    volatile Uint       rxHead;

    volatile Uint       rxTail;

    volatile Uchar      rxBuf[UART_RXBUFFERLEN];

    volatile Uchar      rxTimeOut_Base;

    volatile Uchar      rxTimeOut;


    //send

    volatile Uint       txHead;

    volatile Uint       txTail;

    volatile BOOLEAN    txEmpty;

    volatile Uchar baudrate;

    volatile Volatile txIdleTimeOut;

    volatile Uchar      txIdleTimeOut_Base;

    volatile Volatile txBuf[UART_TXBUFFERLEN];

}UART_STRUCT;


static UART_STRUCT uart1;

static UART_STRUCT uart2;

static UART_STRUCT uart3;

static UART_STRUCT uart4;

static UART_STRUCT uart5;


Serial port 1 initialization


    USART_InitTypeDef USART_InitStruct;

    GPIO_InitTypeDef GPIO_InitStruct;

    NVIC_InitTypeDef NVIC_InitStruct;


    RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1, ENABLE );


    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


    /* Configure USART1 Tx (PA9) as alternate function push-pull */

    GPIO_InitStruct.GPIO_Pin = PIN_USART1_TXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_USART1_TXD, &GPIO_InitStruct );


    /* Configure USART1 Rx (PA10) as input floating */

    GPIO_InitStruct.GPIO_Pin = PIN_USART1_RXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_USART1_RXD, &GPIO_InitStruct );


    USART_StructInit(&USART_InitStruct);

    USART_InitStruct.USART_BaudRate = 115200;

    USART_InitStruct.USART_WordLength = USART_WordLength_8b;

    USART_InitStruct.USART_StopBits = USART_StopBits_1;

    USART_InitStruct.USART_Parity = USART_Parity_No ;

    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART1, &USART_InitStruct);

    /* Enable the USARTx Interrupt */


#if 1

// USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

    USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);


    NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);

#endif

    USART_Cmd(USART1, ENABLE);


Serial port 2 initialization


    USART_InitTypeDef USART_InitStruct;

    GPIO_InitTypeDef GPIO_InitStruct;

    NVIC_InitTypeDef NVIC_InitStruct;


    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART2, ENABLE );


    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


    /* Configure USART2 CTS (PA0) as input floating */

#if UART2_HARDWARE_FLOW

    GPIO_InitStruct.GPIO_Pin = PIN_USART2_CTS;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_USART2_CTS, &GPIO_InitStruct );

#endif


    /* Configure USART2 Rx (PA3) as input floating */

    GPIO_InitStruct.GPIO_Pin = PIN_USART2_RXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_USART2_RXD, &GPIO_InitStruct );


    /* Configure USART2 RTS (PA1) as alternate function push-pull */

#if UART2_HARDWARE_FLOW

    GPIO_InitStruct.GPIO_Pin = PIN_USART2_RTS;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_USART2_RTS, &GPIO_InitStruct );

#endif


    /* Configure USART2 Tx (PA2) as alternate function push-pull */

    GPIO_InitStruct.GPIO_Pin = PIN_USART2_TXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_USART2_TXD, &GPIO_InitStruct );


    USART_StructInit(&USART_InitStruct);

    USART_InitStruct.USART_BaudRate = 115200;

    USART_InitStruct.USART_WordLength = USART_WordLength_8b;

    USART_InitStruct.USART_StopBits = USART_StopBits_1;

    USART_InitStruct.USART_Parity = USART_Parity_No ;

    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

#if UART2_HARDWARE_FLOW

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS;

#else

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

#endif


    USART_Init(USART2, &USART_InitStruct);

#if 1

// USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

    USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);


    NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);

#endif

    USART_Cmd(USART2, ENABLE);


Serial port 3 initialization


    USART_InitTypeDef USART_InitStruct;

    GPIO_InitTypeDef GPIO_InitStruct;

    NVIC_InitTypeDef NVIC_InitStruct;



    RCC_APB1PeriphClockCmd( RCC_APB1Periph_USART3, ENABLE );


    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


    /* Configure USART1 Tx (PC10) as alternate function push-pull */

    GPIO_InitStruct.GPIO_Pin = PIN_UART3_TXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_UART3_TXD, &GPIO_InitStruct );


    /* Configure USART1 Rx (PC11) as input floating */

    GPIO_InitStruct.GPIO_Pin = PIN_UART3_RXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_UART3_RXD, &GPIO_InitStruct );


    USART_StructInit(&USART_InitStruct);

    USART_InitStruct.USART_BaudRate = 115200;

    USART_InitStruct.USART_WordLength = USART_WordLength_8b;

    USART_InitStruct.USART_StopBits = USART_StopBits_1;

    USART_InitStruct.USART_Parity = USART_Parity_No ;

    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(USART3, &USART_InitStruct);


    /* Enable the USARTx Interrupt */

#if 1

    USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);


    NVIC_InitStruct.NVIC_IRQChannel = USART3_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);


#endif

    USART_Cmd(USART3, ENABLE);


Serial port 4 initialization


    USART_InitTypeDef USART_InitStruct;

    GPIO_InitTypeDef GPIO_InitStruct;

    NVIC_InitTypeDef NVIC_InitStruct;


    RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4, ENABLE );


    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;


#if 0

    GPIO_InitStruct.GPIO_Pin = PIN_UART4_CTS;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_UART4_CTS, &GPIO_InitStruct );

//    GPIO_ResetBits(PORT_UART4_CTS,PIN_UART4_CTS);

#endif


    /* Configure USART1 Tx (PC10) as alternate function push-pull */

    GPIO_InitStruct.GPIO_Pin = PIN_UART4_TXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_UART4_TXD, &GPIO_InitStruct );


    /* Configure USART1 Rx (PC11) as input floating */

    GPIO_InitStruct.GPIO_Pin = PIN_UART4_RXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_UART4_RXD, &GPIO_InitStruct );


    USART_StructInit(&USART_InitStruct);

    USART_InitStruct.USART_BaudRate = 115200;

    USART_InitStruct.USART_WordLength = USART_WordLength_8b;

    USART_InitStruct.USART_StopBits = USART_StopBits_1;

    USART_InitStruct.USART_Parity = USART_Parity_No ;

    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(UART4, &USART_InitStruct);


    /* Enable the USARTx Interrupt */

#if 1

// USART_ITConfig(USART2, USART_IT_TXE, ENABLE);

    USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);


    NVIC_InitStruct.NVIC_IRQChannel = UART4_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);


#endif

    USART_Cmd(UART4, ENABLE);


Serial port 5 initialization


    USART_InitTypeDef USART_InitStruct;

    GPIO_InitTypeDef GPIO_InitStruct;

    NVIC_InitTypeDef NVIC_InitStruct;


    RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5, ENABLE );


    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;



    /* Configure USART1 Tx (PC10) as alternate function push-pull */

    GPIO_InitStruct.GPIO_Pin = PIN_UART5_TXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;

    GPIO_Init( PORT_UART5_TXD, &GPIO_InitStruct );


    /* Configure USART1 Rx (PC11) as input floating */

    GPIO_InitStruct.GPIO_Pin = PIN_UART5_RXD;

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

    GPIO_Init( PORT_UART5_RXD, &GPIO_InitStruct );


    USART_StructInit(&USART_InitStruct);

    USART_InitStruct.USART_BaudRate = 115200;

    USART_InitStruct.USART_WordLength = USART_WordLength_8b;

    USART_InitStruct.USART_StopBits = USART_StopBits_1;

    USART_InitStruct.USART_Parity = USART_Parity_No ;

    USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

    USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;

    USART_Init(UART5, &USART_InitStruct);


    /* Enable the USARTx Interrupt */

#if 1

    USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);


    NVIC_InitStruct.NVIC_IRQChannel = UART5_IRQn;

    NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;

    NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;

    NVIC_Init(&NVIC_InitStruct);


#endif

    USART_Cmd(UART5, ENABLE);


Interrupt handling



void USART_Irq_Function(UART_STRUCT *uart,USART_TypeDef *uartdef)

{

    //Receive data USART_ GetITStatus

    while (USART_GetITStatus(uartdef, USART_IT_RXNE))

    {

        Flying tempo;


        USART_ClearFlag(uartdef, USART_FLAG_RXNE | USART_FLAG_ORE);

        temp = USART_ReceiveData(uartdef);

        if (((uart->rxHead + 1) % UART_RXBUFFERLEN) != uart->rxTail)

        {

            uart->rxBuf[uart->rxHead] = temp;

            uart->rxHead = (uart->rxHead + 1) % UART_RXBUFFERLEN;

            uart->rxTimeOut = uart->rxTimeOut_Base;

        }

    }

    //send data

    if (USART_GetITStatus(uartdef,USART_IT_TXE))

    {

        // USART_ClearFlag(uartdef, USART_FLAG_TXE);

        if (uart->txHead == uart->txTail)

        {

            uart->txEmpty = TRUE;

            USART_ITConfig(uartdef, USART_IT_TXE, DISABLE);

        }

        else

        {

            USART_SendData(uartdef, uart->txBuf[uart->txTail]);

            uart->txTail = (uart->txTail + 1) & (UART_TXBUFFERLEN - 1);

            uart->txIdleTimeOut = uart->txIdleTimeOut_Base;

        }

    }


}


extern void USART1_IRQHandler(void)

{

    USART_Irq_Function(&uart1,USART1);

}


extern void USART2_IRQHandler(void)

{

    USART_Irq_Function(&uart2,USART2);

}


extern void USART3_IRQHandler(void)

{

#ifdef INCLUDE_UARTPORT3

    USART_Irq_Function(&uart3,USART3);

#endif

}


extern void UART4_IRQHandler(void)

{

#ifdef INCLUDE_UARTPORT4


    USART_Irq_Function(&uart4,UART4);

#endif

}


extern void UART5_IRQHandler(void)

{

#ifdef INCLUDE_UARTPORT5

    USART_Irq_Function(&uart5,UART5);

#endif

}


Timeout Check


//Function: Check if a data packet has been received, called once every 1ms

//Parameters None

//Return: None

extern void checkUartRxTimeOut()

{

    if (uart1.rxTimeOut)

    {

        if (--uart1.rxTimeOut == 0)

        {

            pushEvent(evCOM1,uart1.rxHead);

        }

    }

    if (uart2.rxTimeOut)

    {

        if (--uart2.rxTimeOut == 0)

        {

            pushEvent(evCOM2,uart2.rxHead);

        }

    }


    if (uart3.rxTimeOut)

    {

        if (--uart3.rxTimeOut == 0)

        {

            pushEvent(evCOM3,uart3.rxHead);

        }

    }


    if (uart4.rxTimeOut)

    {

        if (--uart4.rxTimeOut == 0)

        {

            pushEvent(evCOM4,uart4.rxHead);

        }

    }


    if (uart5.rxTimeOut)

    {

        if (--uart5.rxTimeOut == 0)

        {

            pushEvent(evCOM5,uart5.rxHead);

        }

    }


    if (uart1.txIdleTimeOut) uart1.txIdleTimeOut--;

    if (uart2.txIdleTimeOut) uart2.txIdleTimeOut--;

#ifdef INCLUDE_UARTPORT3

    if (uart3.txIdleTimeOut) uart3.txIdleTimeOut--;

#endif

#ifdef INCLUDE_UARTPORT4

    if (uart4.txIdleTimeOut) uart4.txIdleTimeOut--;

#endif

#ifdef INCLUDE_UARTPORT5

    if (uart5.txIdleTimeOut) uart5.txIdleTimeOut--;

#endif

}


Keywords:STM32F103 Reference address:STM32F103 5 serial ports used simultaneously

Previous article:Basic use of STM32F407 serial port
Next article:STM32F4 USART [library function operation]

Recommended ReadingLatest update time:2024-11-16 18:09

Trying to use STM32f103c8t6 as USBCDC
I tinkered with stm32 when I was bored. There were too many resources in the lab to handle. I worked on dsp and fpga a few days ago, but found these chips a bit boring, so I came back to work on stm32. I worked on it for a day. In the afternoon, I made a FreeRTOS example. After reading some information, I found it was
[Microcontroller]
STM32F103 Slave I2C Configuration
I have just started to work on STM32 development. Due to project requirements, the Linux system needs to communicate with STM32 through I2C. There is limited suitable information on the Internet, and it took me a lot of time, so I record it here. Description: After Linux sends data, it polls stm32 every 10ms (it sho
[Microcontroller]
STM32f103 watchdog usage
//Watchdog timeout needs to be calculated // AGAIN 7.1 #ifndef __STM32_WDG_H__ #include "stm32f10x.h" #define LSI_FREQ   40000 void stm32_wdg_init(void); void stm32_wdg_enable(void); void stm32_wdg_feed(void); #endif #include "stm32_wdg.h" #include "stm32f10x_iwdg.h" void stm32_wdg_init(void) {     /*Enables write acc
[Microcontroller]
Design of short-range wireless data transmission system based on STM32F103 and nRF24L01
In recent years, with the development of wireless communication technology, the integration of wireless communication equipment has become increasingly high. This paper introduces a specific method of designing a short-distance wireless data transmission system using a high-performance, low-power 32-bit microprocess
[Microcontroller]
Design of short-range wireless data transmission system based on STM32F103 and nRF24L01
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号